Beispiel #1
0
 def test_bare_use(self):
     form = Form(Use(int))
     valid = form.validate('4')
     self.assertTrue(valid)
     self.assertEqual(4, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #2
0
 def test_bare_AND(self):
     form = Form(And(int, lambda x: x == 4))
     valid = form.validate(4)
     self.assertTrue(valid)
     self.assertEqual(4, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #3
0
 def test_bare_value(self):
     form = Form(4)
     valid = form.validate(4)
     self.assertTrue(valid)
     self.assertEqual(4, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #4
0
 def test_bare_use(self):
     form = Form(Use(int))
     valid = form.validate("4")
     self.assertTrue(valid)
     self.assertEqual(4, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #5
0
 def test_bare_value(self):
     form = Form(4)
     valid = form.validate(4)
     self.assertTrue(valid)
     self.assertEqual(4, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #6
0
 def test_bare_value_fail(self):
     form = Form(4)
     valid = form.validate(3)
     self.assertFalse(valid)
     self.assertEqual(None, form.cleaned)
     self.assertEqual(len(form.errors.section_errors), 1)
     self.assertFalse(form.errors)
Beispiel #7
0
 def test_extra_key(self):
     schema = {"one": 1, "three": {"four": 4}}
     data = {"one": 1, "two": 2, "three": {"four": 4, "five": 5}}
     form = Form(schema)
     self.assertFalse(form.validate(data))
     self.assertEqual(len(form.errors.section_errors), 1)
     self.assertEqual(len(form.errors["three"].section_errors), 1)
Beispiel #8
0
 def test_dependant_key(self):
     schema = {
         'key': 'value',
         Optional(2): 'two',
         Or: {
             'opt1': 1,
             'opt2': 2,
             'opt3': 3,
         },
         If([[2]], "conditional"): "exists",
         If([['opt1']], "opt1_condition"): True,
         If([['opt2'], [2]], "compound_if"): True
     }
     form = Form(schema)
     for data in [{
             'key': 'value',
             'opt2': 2
     }, {
             'key': 'value',
             2: 'two',
             'conditional': "exists",
             'opt3': 3
     }, {
             'key': 'value',
             2: 'two',
             'conditional': "exists",
             'opt2': 2,
             'compound_if': True
     }]:
         valid = form.validate(data)
         self.assertTrue(valid)
         self.assertEqual(data, form.cleaned)
         self.assertFalse(form.errors)
         self.assertFalse(len(form.errors.section_errors))
Beispiel #9
0
 def test_nested_map(self):
     schema = {"one": "string", "two": 2, "three": {"nested_one": "string", "nested_two": 22}}
     form = Form(schema)
     valid = form.validate(schema)
     self.assertTrue(valid)
     self.assertEqual(schema, form.cleaned)
     self.assertFalse(form.errors)
Beispiel #10
0
 def test_bare_AND(self):
     form = Form(And(int, lambda x: x == 4))
     valid = form.validate(4)
     self.assertTrue(valid)
     self.assertEqual(4, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #11
0
 def test_wrong_value_inside_container(self):
     schema = {
         Optional('one'): 'one',
         If([['one']], 2): 'two',
         Or: {
             'three': 3,
             3: 'three'
         },
         XOr: {
             'four': 4,
             4: 'four'
         }
     }
     data = {
         'one': 1,
         2: 2,
         3: 3,
         4: 4,
     }
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertTrue(form.errors)
     for key in ['one', 2, 3, 4]:
         self.assertTrue(form.errors[key])
Beispiel #12
0
 def test_missing_xor(self):
     schema = {"pre": "a", XOr: {"one": 1, "two": 2}}
     data = {"pre": "a"}
     form = Form(schema)
     form.validate(data)
     self.assertEqual(len(form.errors.section_errors), 1)
     self.assertTrue("missing" in form.errors.section_errors[0].lower())
Beispiel #13
0
 def test_conditional_key(self):
     schema = {
         'tip': 'top',
         Or: {
             1: 1,
             2: 2,
             3: 3
         },
         XOr: {
             '1': 1,
             '2': 2,
             '3': 3
         }
     }
     form = Form(schema)
     for data in [
         {
             'tip': 'top',
             1: 1,
             3: 3,
             '2': 2
         },
         {
             'tip': 'top',
             2: 2,
             '1': 1
         },
     ]:
         valid = form.validate(data)
         self.assertTrue(valid)
         self.assertEqual(data, form.cleaned)
         self.assertFalse(form.errors)
Beispiel #14
0
 def test_bare_value_fail(self):
     form = Form(4)
     valid = form.validate(3)
     self.assertFalse(valid)
     self.assertEqual(None, form.cleaned)
     self.assertEqual(len(form.errors.section_errors), 1)
     self.assertFalse(form.errors)
Beispiel #15
0
 def test_missing_in_if(self):
     schema = {Optional("one"): 1, If([["one"]], "two"): 2}
     data = {"one": 1}
     form = Form(schema)
     form.validate(data)
     self.assertEqual(len(form.errors.section_errors), 1)
     self.assertTrue("missing" in form.errors.section_errors[0].lower())
Beispiel #16
0
 def test_missing_in_if(self):
     schema = {Optional('one'): 1, If([['one']], 'two'): 2}
     data = {'one': 1}
     form = Form(schema)
     form.validate(data)
     self.assertEqual(len(form.errors.section_errors), 1)
     self.assertTrue('missing' in form.errors.section_errors[0].lower())
Beispiel #17
0
 def test_bare_OR(self):
     form = Form(Or(str, lambda x: x == 4))
     for v in ["asdf", 4]:
         valid = form.validate(v)
         self.assertTrue(valid)
         self.assertEqual(v, form.cleaned)
         self.assertFalse(form.errors)
         self.assertFalse(form.errors.section_errors)
Beispiel #18
0
 def test_simple_map(self):
     schema = {'one': 'string', 'two': 2, 3: 'three', 4: True}
     data = {'one': 'string', 'two': 2, 3: u'three', 4: True}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertEqual(data, form.cleaned)
     self.assertFalse(form.errors)
Beispiel #19
0
 def test_nested_lists(self):
     data = [[str(i), 2 * i] for i in range(5)]
     schema = [And([Use(int)], lambda x: len(x) == 2)]
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #20
0
 def test_wrong_value(self):
     schema = [1]
     form = Form(schema)
     for data in [['1'], ['1' for i in range(10)], [2, 3, 4], [1, 1, 1, 2]]:
         valid = form.validate(data)
         self.assertFalse(valid)
         self.assertTrue(form.errors)
         self.assertFalse(form.errors.section_errors)
Beispiel #21
0
 def test_multiple_val_sequence(self):
     schema = [1, '1']
     form = Form(schema)
     for data in [[1], [1, 1, 1], ['1'], [1, '1'], [1, 1, '1', '1', 1]]:
         valid = form.validate(data)
         self.assertTrue(valid)
         self.assertFalse(form.errors)
         self.assertFalse(form.errors.section_errors)
Beispiel #22
0
 def test_Or_fail(self):
     schema = {'n': Or('a', 1, 3)}
     data = {'n': 4}
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertTrue(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #23
0
 def test_blank_nested_list(self):
     schema = {'items': [Use(int)]}
     data = {'items': []}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertEqual(data, form.cleaned)
     self.assertFalse(form.errors)
Beispiel #24
0
 def test_missing_key(self):
     schema = {'one': 1, 'two': 2, 'three': {'four': 4, 'five': 5}}
     data = {'two': 2}
     form = Form(schema)
     self.assertFalse(form.validate(data))
     self.assertEqual(len(form.errors.section_errors), 2)
     for sectionerr in form.errors.section_errors:
         self.assertTrue("missing" in sectionerr.lower())
Beispiel #25
0
 def test_if_noexist(self):
     schema = {Optional('one'): 'value', If([['one']], 'two'): 2}
     data = {}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #26
0
 def test_wrong_value(self):
     schema = [1]
     form = Form(schema)
     for data in [["1"], ["1" for i in range(10)], [2, 3, 4], [1, 1, 1, 2]]:
         valid = form.validate(data)
         self.assertFalse(valid)
         self.assertTrue(form.errors)
         self.assertFalse(form.errors.section_errors)
Beispiel #27
0
 def test_bare_OR(self):
     form = Form(Or(str, lambda x: x == 4))
     for v in ['asdf', 4]:
         valid = form.validate(v)
         self.assertTrue(valid)
         self.assertEqual(v, form.cleaned)
         self.assertFalse(form.errors)
         self.assertFalse(form.errors.section_errors)
Beispiel #28
0
 def test_blank_nested_list(self):
     schema = {"items": [Use(int)]}
     data = {"items": []}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertEqual(data, form.cleaned)
     self.assertFalse(form.errors)
Beispiel #29
0
 def test_nested_lists(self):
     data = [[str(i), 2 * i] for i in range(5)]
     schema = [And([Use(int)], lambda x: len(x) == 2)]
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #30
0
 def test_conditional_key(self):
     schema = {"tip": "top", Or: {1: 1, 2: 2, 3: 3}, XOr: {"1": 1, "2": 2, "3": 3}}
     form = Form(schema)
     for data in [{"tip": "top", 1: 1, 3: 3, "2": 2}, {"tip": "top", 2: 2, "1": 1}]:
         valid = form.validate(data)
         self.assertTrue(valid)
         self.assertEqual(data, form.cleaned)
         self.assertFalse(form.errors)
Beispiel #31
0
 def test_And_fail(self):
     schema = {"a": And(str, lambda x: len(x) == 3), "b": And(int, 4)}
     data = {"a": "12", "b": "4"}
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertEqual(len(form.errors), 2)
     self.assertFalse(form.errors.section_errors)
Beispiel #32
0
 def test_function_fail(self):
     schema = {"two": str, "three": int, "4": lambda x: x.type in ["one", "two"]}
     data = {"two": 5, "three": "0", "4": 10}
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertEqual(len(form.errors), 3)
     self.assertFalse(form.errors.section_errors)
Beispiel #33
0
 def test_if_noexist(self):
     schema = {Optional("one"): "value", If([["one"]], "two"): 2}
     data = {}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #34
0
 def test_Or_fail(self):
     schema = {"n": Or("a", 1, 3)}
     data = {"n": 4}
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertTrue(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #35
0
 def test_multiple_val_sequence(self):
     schema = [1, "1"]
     form = Form(schema)
     for data in [[1], [1, 1, 1], ["1"], [1, "1"], [1, 1, "1", "1", 1]]:
         valid = form.validate(data)
         self.assertTrue(valid)
         self.assertFalse(form.errors)
         self.assertFalse(form.errors.section_errors)
Beispiel #36
0
 def test_missing_key(self):
     schema = {"one": 1, "two": 2, "three": {"four": 4, "five": 5}}
     data = {"two": 2}
     form = Form(schema)
     self.assertFalse(form.validate(data))
     self.assertEqual(len(form.errors.section_errors), 2)
     for sectionerr in form.errors.section_errors:
         self.assertTrue("missing" in sectionerr.lower())
Beispiel #37
0
 def test_simple_map(self):
     schema = {"one": "string", "two": 2, 3: "three", 4: True}
     data = {"one": "string", "two": 2, 3: u"three", 4: True}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertEqual(data, form.cleaned)
     self.assertFalse(form.errors)
Beispiel #38
0
 def test_Or_pass(self):
     schema = {"n": Or("a", 1, 3)}
     data = {"n": 3}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
     self.assertEqual(data, form.cleaned)
Beispiel #39
0
 def test_use_fail(self):
     schema = [Use(lambda x: x + 10)]
     data = [1, 2, '3']
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertTrue(len(form.errors) == 1)
     self.assertTrue(form.errors[2])
     self.assertFalse(form.errors.section_errors)
Beispiel #40
0
 def test_use_with_and(self):
     schema = {"u": And(Use(int), lambda x: x % 2 == 0)}
     data = {"u": "666"}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertEqual({"u": 666}, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #41
0
 def test_And_pass(self):
     schema = {'a': And(len, str), 'b': And(str, len, 'bee')}
     data = {'a': 'eh', 'b': 'bee'}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
     self.assertEqual(data, form.cleaned)
Beispiel #42
0
 def test_Or_pass(self):
     schema = {'n': Or('a', 1, 3)}
     data = {'n': 3}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
     self.assertEqual(data, form.cleaned)
Beispiel #43
0
 def test_simple_sequence(self):
     schema = [1]
     form = Form(schema)
     for data in [[1], [1, 1, 1], [1 for i in range(10)]]:
         valid = form.validate(data)
         self.assertTrue(valid)
         self.assertEqual(form.cleaned, data)
         self.assertFalse(form.errors)
         self.assertFalse(form.errors.section_errors)
Beispiel #44
0
 def test_and_key(self):
     schema = {And(int, lambda x: x % 2 == 0): 35}
     data = {12: 35, 8: 35, 10: 35}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertEqual(data, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #45
0
 def test_use_int(self):
     schema = {'use': Use(int)}
     data = {'use': '666'}
     form = Form(schema)
     valid = form.validate(data)
     self.assertEqual({'use': 666}, form.cleaned)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #46
0
 def test_simple_sequence(self):
     schema = [1]
     form = Form(schema)
     for data in [[1], [1, 1, 1], [1 for i in range(10)]]:
         valid = form.validate(data)
         self.assertTrue(valid)
         self.assertEqual(form.cleaned, data)
         self.assertFalse(form.errors)
         self.assertFalse(form.errors.section_errors)
Beispiel #47
0
 def test_use_sequence(self):
     schema = [Use(int)]
     data = [1, "2", "34"]
     form = Form(schema)
     valid = form.validate(data)
     self.assertEqual([1, 2, 34], form.cleaned)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #48
0
 def test_function_pass(self):
     schema = {'one': len, 'two': str, 'three': int}
     data = {'one': [0, 1], 'two': 'asdf', 'three': 0}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
     self.assertEqual(data, form.cleaned)
Beispiel #49
0
 def test_use_with_and(self):
     schema = {'u': And(Use(int), lambda x: x % 2 == 0)}
     data = {'u': '666'}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertEqual({'u': 666}, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #50
0
 def test_use_int(self):
     schema = {"use": Use(int)}
     data = {"use": "666"}
     form = Form(schema)
     valid = form.validate(data)
     self.assertEqual({"use": 666}, form.cleaned)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #51
0
 def test_and_key_fail(self):
     schema = {And(int, lambda x: x % 2 == 0): 35}
     data = {'6': 35, 3: 34, 2: 34}
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertEqual(form.cleaned, {})
     self.assertEqual(len(form.errors[3]), 1)
     self.assertEqual(len(form.errors.section_errors), 2)
Beispiel #52
0
 def test_use_fail(self):
     schema = [Use(lambda x: x + 10)]
     data = [1, 2, "3"]
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertTrue(len(form.errors) == 1)
     self.assertTrue(form.errors[2])
     self.assertFalse(form.errors.section_errors)
Beispiel #53
0
 def test_optional_strictness(self):
     schema = {Optional('one'): And(str, lambda x: len(x) == 6)}
     data = {'one': '12345'}
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertEqual({}, form.cleaned)
     self.assertFalse(form.errors.section_errors)
     self.assertEqual(len(form.errors), 1)
Beispiel #54
0
 def test_msg_val(self):
     msg = "not b"
     data = {"a": "c"}
     for validator in ["b", int, lambda x: x.upper() == x, Msg("b", "what")]:
         schema = {"a": Msg(validator, msg)}
         form = Form(schema)
         self.assertFalse(form.validate(data))
         self.assertEqual(form.errors["a"][0], msg)
         self.assertTrue(len(form.errors) == 1)
Beispiel #55
0
 def test_function_pass(self):
     schema = {"one": len, "two": str, "three": int}
     data = {"one": [0, 1], "two": "asdf", "three": 0}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
     self.assertEqual(data, form.cleaned)
Beispiel #56
0
 def test_And_pass(self):
     schema = {"a": And(len, str), "b": And(str, len, "bee")}
     data = {"a": "eh", "b": "bee"}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
     self.assertEqual(data, form.cleaned)
Beispiel #57
0
 def test_use_sequence(self):
     schema = [Use(int)]
     data = [1, '2', '34']
     form = Form(schema)
     valid = form.validate(data)
     self.assertEqual([1, 2, 34], form.cleaned)
     self.assertTrue(valid)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #58
0
 def test_and_key(self):
     schema = {And(int, lambda x: x % 2 == 0): 35}
     data = {12: 35, 8: 35, 10: 35}
     form = Form(schema)
     valid = form.validate(data)
     self.assertTrue(valid)
     self.assertEqual(data, form.cleaned)
     self.assertFalse(form.errors)
     self.assertFalse(form.errors.section_errors)
Beispiel #59
0
 def test_key_wrap_or(self):
     msg = 'noooooor'
     schema = {Msg(Or, msg): {'a': 0, 'b': 2}}
     data = {}
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertEqual(form.errors.section_errors[0], msg)
     del form.errors['__section_errors__']
     self.assertFalse(form.errors)
Beispiel #60
0
 def test_And_fail(self):
     schema = {
         'a': And(str, lambda x: len(x) == 3),
         'b': And(int, 4),
     }
     data = {'a': '12', 'b': '4'}
     form = Form(schema)
     valid = form.validate(data)
     self.assertFalse(valid)
     self.assertEqual(len(form.errors), 2)
     self.assertFalse(form.errors.section_errors)