Speed up development with full-stack environments for every branch.

Learn More

Fighting complexity through functional composition [Python, validation, REST and web services]

Forked from Basic Python.

136 Runs 132 Views 77 Copies

Code for: http://danbunea.blogspot.ro/2015/02/fighting-complexity-through-functional.html

Saved

Saved

danbunea 1

danbunea
published 2 years ago

#encoding:utf8

__author__ = 'danbunea'

from imperative import validate_simplest_json
from imperative_to_functional import validate_simplest_json_imperative_linear
from functional_composition import validate_simplest_functional_composition
import unittest



class TestValidations(unittest.TestCase):
    def validate_pair(self,json, is_valid, number_of_errors):
        # errors = validate_simplest_json_imperative_linear(json)
        errors = self.validation_method(json)
        print "",is_valid, errors, json
        self.assertEquals(len(errors)==0,is_valid)
        self.assertEquals(len(errors),number_of_errors)

    def validate(self, validation_function):
        print ""
        print validation_function.__name__
        
        self.validation_method = validation_function
        self.validate_pair({},False,1)
        self.validate_pair({"measurement":None},False,1)
        self.validate_pair({"measurement":-1},False,1)
        self.validate_pair({"measurement":{}},False,1)
        self.validate_pair({"measurement":False},False,1)
        self.validate_pair({"measurement":"abc"},True,0)
        self.validate_pair({"measurement":u"Citroën"},True,0)
        self.validate_pair({"measurement":""},False,1)
        self.validate_pair({"measurement":"a"},False,1)
        self.validate_pair({"measurement":"abcdefghijklmnefghij"},False,1)
        self.validate_pair({"measurement":"password"},False,1)
        self.validate_pair({"measurement":"archived"},False,1)
        self.validate_pair({"measurement":"arCHived"},False,1)


    def test_validate_simplest_json_imperative(self):
        self.validate(validate_simplest_json)

    def test_validate_simplest_json_imperative_linear(self):
        self.validate(validate_simplest_json_imperative_linear)

    def test_json_validation_functional_composition(self):
        self.validate(validate_simplest_functional_composition)




if __name__ == '__main__':
    unittest.main()
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).