コード例 #1
0
 def test_optional(self):
     contract = Contract({
         'key': Template(t.Int()),
         'value': Template(t.String()),
         'optional': Template(t.String(), optional=True)
     })
     data = {
         'key': 1,
         'value': 'test',
         'optional': 'test',
     }
     result = contract(data)
     self.assertEqual(result, data)
     del data['optional']
     result = contract(data)
     self.assertEqual(result, data)
コード例 #2
0
    def test_default(self):
        trafaret = t.String()
        template = Template(trafaret, 'null')
        self.assertEqual(template.default, 'null')
        template.default = 'new'
        self.assertEqual(template.default, 'new')

        with self.assertRaises(ValueError):
            template.default = 42

        del template.template
        with self.assertRaises(ValueError):
            template.default = 42

        del template.default
        self.assertIsNone(template.default)
コード例 #3
0
    def test_simple_value(self):
        template = Template(t.Int())
        contract = Contract(template)
        result = contract.__call__(42)
        self.assertEqual(result, 42)

        with self.assertRaises(ValueError):
            result = contract('error')
コード例 #4
0
    def test_simple_value_with_default(self):
        template = Template(t.Int(), default=7)
        contract = Contract(template)
        result = contract(42)
        self.assertEqual(result, 42)

        result = contract('error')
        self.assertEqual(result, 7)
コード例 #5
0
    def test_dict_value(self):
        contract = Contract({
            'id': Template(t.Int()),
            'value': Template(t.String()),
        })
        result = contract({'id': 42, 'value': 'test'})
        self.assertEqual(result, {'id': 42, 'value': 'test'})
        result = contract({'id': 42, 'value': 'test', 'key': 666})
        self.assertEqual(result, {'id': 42, 'value': 'test'})

        with self.assertRaises(ValueError):
            result = contract({'id': 'error', 'value': 'test'})
        with self.assertRaises(ValueError):
            result = contract({'id': 42, 'value': 666})
        with self.assertRaises(ValueError):
            result = contract({'id': 1})
        with self.assertRaises(ValueError):
            result = contract(666)
コード例 #6
0
    def test_list_single_value_with_default(self):
        template = Template(t.Int(), 42)
        contract = Contract([template])

        result = contract([8, 800, 555, 35, 35])
        self.assertEqual(result, [8, 800, 555, 35, 35])

        result = contract([8, 800, 555, 'error', 35])
        self.assertEqual(result, [8, 800, 555, 42, 35])
コード例 #7
0
    def test_list_many_values(self):
        contract = Contract(
            [Template(t.Int()),
             Template(t.String()),
             Template(t.Float())])
        result = contract([1, 'Test', 12.5])
        self.assertEqual(result, [1, 'Test', 12.5])

        with self.assertRaises(ValueError):
            result = contract(['error', 'Test', 12.5])
        with self.assertRaises(ValueError):
            result = contract([1, 666, 12.5])
        with self.assertRaises(ValueError):
            result = contract([1, 'Test', 'error'])
        with self.assertRaises(ValueError):
            result = contract([1, 'Test'])
        with self.assertRaises(ValueError):
            result = contract([1, 'Test', 12.5, 'error'])
        with self.assertRaises(ValueError):
            result = contract(42)
コード例 #8
0
    def test_simple_validator(self):
        # Create contract
        trafaret = t.Trafaret()
        template = Template(trafaret)
        contract = Contract(template)
        self.assertEqual(contract.template, template)
        new_template = Template(trafaret)
        contract.template = new_template
        self.assertEqual(contract.template, new_template)

        # Int
        int_t = Template(t.Int())
        contract = Contract(int_t)

        # String
        string_t = Template(t.String())
        contract = Contract(string_t)

        # Dict
        dict_t = Template(t.Dict({'id': t.Int, 'email': t.Email}))
        contract.template = dict_t

        # List
        list_t = Template(t.List(t.Int))
        contract.template = list_t
コード例 #9
0
    def test_dict_validator(self):
        dict_t = {
            'id': Template(t.Int(gt=0)),
            'val': Template(t.String()),
        }
        contract = Contract(dict_t)

        dict_t = {
            'id': Template(t.Int(gt=0)),
            'val': 'error',
        }
        with self.assertRaises(ValueError):
            contract.template = dict_t
        with self.assertRaises(ValueError):
            contract = Contract(dict_t)

        dict_t = {
            12: Template(t.Int(gt=0)),
            'val': 'error',
        }
        with self.assertRaises(TypeError):
            contract = Contract(dict_t)
コード例 #10
0
    def test_list_single_value(self):
        template = Template(t.Int())
        contract = Contract([template])
        result = contract([42])
        self.assertEqual(result, [42])

        result = contract([8, 800, 555, 35, 35])
        self.assertEqual(result, [8, 800, 555, 35, 35])

        with self.assertRaises(ValueError):
            result = contract([8, 800, 555, 'error', 35])
        with self.assertRaises(ValueError):
            result = contract(42)
コード例 #11
0
    def test_list_validator(self):
        list_t = [
            Template(t.Int()),
            Template(t.List(t.Int)),
            Template(t.Dict({
                'id': t.Int,
                'val': t.String
            })),
        ]
        contract = Contract(list_t)

        list_t = [
            Template(t.Int()),
            'error',
            Template(t.Dict({
                'id': t.Int,
                'val': t.String
            })),
        ]
        with self.assertRaises(ValueError):
            contract.template = list_t
        with self.assertRaises(ValueError):
            contract = Contract(list_t)
コード例 #12
0
    def test_binary_templates(self):
        tempalte_1 = Template(t.Int())
        tempalte_2 = Template(t.String())
        contract = Contract(tempalte_1 | tempalte_2)
        result = contract(42)
        self.assertEqual(result, 42)
        result = contract('test')
        self.assertEqual(result, 'test')
        with self.assertRaises(ValueError):
            result = contract([123])

        tempalte_1 = Template(t.Int(), default=42)
        tempalte_2 = Template(t.String(), default='Test')
        with self.assertRaises(TemplateError):
            tempalte = tempalte_1 | tempalte_2

        tempalte_1 = Template(t.Int(), default=42)
        tempalte_2 = Template(t.String())
        tempalte = tempalte_1 | tempalte_2  # noqa

        tempalte_1 = Template(t.Int())
        tempalte_2 = Template(t.String(), default='Test')
        tempalte = tempalte_1 | tempalte_2  # noqa
コード例 #13
0
    def test_complex_dict_value(self):
        contract = Contract(
            {
                'key':
                Template(t.Int()),
                'value':
                Template(t.String()),
                'optional':
                Template(t.String(), optional=True),
                'property': [Template(t.String())],
                'optional_property': [Template(t.String(), optional=True)],
                'objects': [{
                    'id': Template(t.Int()),
                    'val': Template(t.String())
                }],
            },
            optional_keys=['optional_property'])
        data = {
            'key': 1,
            'value': 'test',
            'optional': 'test',
            'property': ['A', 'B', 'C'],
            'optional_property': ['A', 'B', 'C'],
            'objects': [{
                'id': 1,
                'val': 'test'
            }, {
                'id': 2,
                'val': 'test'
            }],
        }
        result = contract(data)
        self.assertEqual(result, data)
        del data['optional_property']
        del data['optional']
        result = contract(data)
        self.assertEqual(result, data)

        del data['objects']
        with self.assertRaises(ValueError):
            result = contract(data)
コード例 #14
0
    def test_template(self):
        with self.assertRaises(ValueError):
            template = Template('error')

        trafaret = t.String()
        template = Template(trafaret)
        self.assertEqual(template.template, [trafaret])
        template.check('test')
        with self.assertRaises(ValueError):
            template.check(42)

        template.template = t.Int()
        template.check(42)

        del template.template
        with self.assertRaises(ValueError):
            template.check('test')
コード例 #15
0
    def test_binary_template(self):
        # Simple values
        template_1 = Template(t.Int())
        template_2 = Template(t.String())
        contract = Contract(template_1 | template_2)
        result = contract(42)
        self.assertEqual(result, 42)
        result = contract('test')
        self.assertEqual(result, 'test')
        with self.assertRaises(ValueError):
            result = contract(['list'])

        # Simple list values
        template_1 = Template(t.Int())
        template_2 = Template(t.String())
        contract = Contract([template_1 | template_2])
        result = contract([1, 2, 3])
        self.assertEqual(result, [1, 2, 3])
        result = contract(['test_1', 'test_2'])
        self.assertEqual(result, ['test_1', 'test_2'])
        result = contract([1, 'test', 3])
        self.assertEqual(result, [1, 'test', 3])

        # Combinated list values
        template_1 = Template(t.Int())
        template_2 = Template(t.String())
        contract = Contract([template_1 | template_2, template_1, template_2])
        result = contract([1, 1, 'test'])
        self.assertEqual(result, [1, 1, 'test'])
        result = contract(['test', 1, 'test'])
        self.assertEqual(result, ['test', 1, 'test'])

        # Dict values
        template = {
            'key': Template(t.String()) | Template(t.Int()),
            'value': Template(t.String()),
        }
        contract = Contract(template)
        result = contract({'key': 'key', 'value': 'test'})
        self.assertEqual(result, {'key': 'key', 'value': 'test'})
        result = contract({'key': 12, 'value': 'test'})
        self.assertEqual(result, {'key': 12, 'value': 'test'})

        template_1 = {
            'key': Template(t.String()),
            'value': Template(t.String()),
        }
        template_2 = {
            'id': Template(t.Int()),
            'value': Template(t.String()),
        }
        contract_1 = Contract(template_1)
        contract_2 = Contract(template_2)
        contract = contract_1 | contract_2
        result = contract({'key': 'key', 'value': 'test'})
        self.assertEqual(result, {'key': 'key', 'value': 'test'})
        result = contract({'id': 42, 'value': 'test'})
        self.assertEqual(result, {'id': 42, 'value': 'test'})