def test_intersection_production(self): name_type = c.struct({'name': c.String}) age_type = c.struct({'age': c.Int}) my_type = c.intersection(name_type, age_type) base_dict = {'name': 'mirko', 'age': '36'} self.assertEqual(base_dict, my_type(base_dict, ctx=context.create(production_mode=True)))
def test_intersection_custom_error(self): name_type = c.struct({'name': c.String}) age_type = c.struct({'age': c.Int}) my_type = c.intersection(name_type, age_type) observer = Mock() ctx = context.create(validation_error_observer=observer) my_type({'name': 'mirko', 'age': '36'}, ctx=ctx) observer.on_error.assert_called_once_with(_ANY_CONTEXT, 'Struct{name: String} or Struct{age: Int}', dict)
def test_named_intersection(self): name_type = c.struct({'name': c.String}) age_type = c.struct({'age': c.Int}) my_type = c.intersection(name_type, age_type, name='MyType') d = my_type({'name': 'mirko', 'age': 36}) self.assertEqual('mirko', d.name) with self.assertRaises(exceptions.PyCombValidationError) as e: my_type({'name': 'mirko', 'age': '36'}) e = e.exception self.assertEqual( 'Error on MyType: ' 'expected Struct{name: String} or Struct{age: Int} but was dict', e.args[0])
def test_intersection_dispatcher(self): name_type = c.struct({'name': c.String}) age_type = c.struct({'age': c.Int}) my_type = c.intersection(name_type, age_type, dispatcher=lambda x: age_type) d = my_type({'name': 'mirko', 'age': 36}) self.assertEqual(36, d.age) with self.assertRaises(exceptions.PyCombValidationError) as e: my_type({'name': 'mirko', 'age': '36'}) e = e.exception self.assertEqual( 'Error on Intersection(Struct{name: String}, Struct{age: Int}): ' 'expected Struct{name: String} or Struct{age: Int} but was dict', e.args[0])