class TransformCatalyst(Catalyst): a = IntegerField() coordinate = NestedField(coordinate_catalyst) transform = TransformNested('coordinate')
class ReversedTransformCatalyst(TransformCatalyst): coordinate = NestedField(coordinate_catalyst, dump_required=False) transform = TransformNested('coordinate', dump_method='flat_to_nested', load_method='nested_to_flat')
def test_transform_nested(self): coordinate_catalyst = Catalyst({ 'x': IntegerField(), 'y': IntegerField() }) class TransformCatalyst(Catalyst): a = IntegerField() coordinate = NestedField(coordinate_catalyst) transform = TransformNested('coordinate') catalyst = TransformCatalyst() # test wrong fields with self.assertRaises(ValueError): catalyst.transform.set_fields(fields={}) with self.assertRaises(TypeError) as cm: catalyst.transform.set_fields( fields={'coordinate': IntegerField()}) self.assertIn('NestedField', str(cm.exception)) with self.assertRaises(ValueError) as cm: catalyst.transform.set_fields( fields={ 'coordinate': NestedField(coordinate_catalyst, many=True) }) self.assertIn('many=True', str(cm.exception)) # test valid loading_data = {'a': 0, 'x': 1, 'y': -1} dumping_data = {'a': 0, 'coordinate': {'x': 1, 'y': -1}} result = catalyst.load(loading_data) self.assertTrue(result.is_valid) self.assertDictEqual(result.valid_data, dumping_data) result = catalyst.dump(dumping_data) self.assertTrue(result.is_valid) self.assertDictEqual(result.valid_data, loading_data) # test invalid invalid_loading_data = {'a': 0, 'x': 'x', 'y': -1} result = catalyst.load(invalid_loading_data) self.assertFalse(result.is_valid) self.assertSetEqual(set(result.errors), {'x'}) self.assertDictEqual(result.invalid_data, {'x': 'x'}) invalid_dumping_data = {'a': 0, 'coordinate': {'x': 'x', 'y': -1}} result = catalyst.dump(invalid_dumping_data) self.assertFalse(result.is_valid) self.assertSetEqual(set(result.errors), {'coordinate'}) self.assertDictEqual(result.invalid_data, {'coordinate': {'x': 'x'}}) # test change process methods fields = {'x': catalyst.coordinate} with self.assertRaises(AttributeError): TransformNested('x', dump_method='wrong').set_fields(fields) with self.assertRaises(AttributeError): TransformNested('x', load_method='wrong').set_fields(fields) class ReversedTransformCatalyst(TransformCatalyst): coordinate = NestedField(coordinate_catalyst, dump_required=False) transform = TransformNested('coordinate', dump_method='flat_to_nested', load_method='nested_to_flat') reversed_catalyst = ReversedTransformCatalyst() dumping_data = {'a': 0, 'x': 1, 'y': -1} loading_data = {'a': 0, 'coordinate': {'x': 1, 'y': -1}} result = reversed_catalyst.dump(dumping_data) self.assertTrue(result.is_valid) self.assertDictEqual(result.valid_data, loading_data) result = reversed_catalyst.load(loading_data) self.assertTrue(result.is_valid) self.assertDictEqual(result.valid_data, dumping_data)
class A(Catalyst): a = IntegerField() b = IntegerField() args = ListField(IntegerField()) kwargs = NestedField(Kwargs())
class Article(Catalyst): title = StringField() content = StringField() author = NestedField(user_catalyst)
def test_nest_field(self): with self.assertRaises(TypeError): NestedField() with self.assertRaises(TypeError): field = NestedField(Catalyst) fields = {'name': StringField(max_length=3)} field = NestedField(Catalyst(fields), name='a', key='a') self.assertEqual(field.dump({'name': '1'}), {'name': '1'}) self.assertEqual(field.dump({'name': '1234'}), {'name': '1234'}) with self.assertRaises(ValidationError): field.dump({'n': 'm'}) with self.assertRaises(ValidationError): field.dump(1) self.assertEqual(field.load({'name': '1'}), {'name': '1'}) self.assertDictEqual(field.load({'n': 'm'}), {}) with self.assertRaises(ValidationError): field.load({'name': '1234'}) with self.assertRaises(ValidationError): field.load(1) # list with dict items field = NestedField(Catalyst({'x': IntegerField()}), many=True) data = [{'x': 1}, {'x': 2}] self.assertListEqual(data, field.load(data)) data = [{'x': 1}, {'x': 'x'}] with self.assertRaises(ValidationError) as cm: field.load(data) result = cm.exception.detail self.assertIsInstance(result.errors[1]['x'], ValueError) data = [{'x': 1}, None] with self.assertRaises(ValidationError) as cm: field.load(data) result = cm.exception.detail self.assertIsInstance(result.errors[1]['load'], TypeError)
def test_list_field(self): with self.assertRaises(TypeError): ListField() with self.assertRaises(TypeError): field = ListField(FloatField) field = ListField(item_field=FloatField()) # dump self.assertListEqual(field.dump([1.0, 2.0, 3.0]), [1.0, 2.0, 3.0]) self.assertListEqual(field.dump([]), []) self.assertEqual(field.dump(None), None) with self.assertRaises(TypeError): field.dump(1) # load self.assertListEqual(field.load([1, 2, 3]), [1.0, 2.0, 3.0]) self.assertListEqual(field.load([]), []) with self.assertRaises(ValidationError) as cm: field.load([1, 'a', 3]) result = cm.exception.detail self.assertIsInstance(result.errors[1], ValueError) self.assertEqual(result.invalid_data[1], 'a') self.assertEqual(result.valid_data, [1.0, 3.0]) with self.assertRaises(TypeError): field.load(1) with self.assertRaises(ValidationError): field.load(None) field = ListField(item_field=FloatField(), all_errors=False) with self.assertRaises(ValidationError) as cm: field.load([1, 'a', 'b']) result = cm.exception.detail self.assertEqual(set(result.errors), {1}) self.assertEqual(result.invalid_data[1], 'a') self.assertEqual(result.valid_data, [1.0]) # two-dimensional array field = ListField(ListField(IntegerField())) data = [[1], [2]] self.assertListEqual(data, field.load(data)) data = [[1], ['x']] with self.assertRaises(ValidationError) as cm: field.load(data) result = cm.exception.detail self.assertIsInstance(result.errors[1][0], ValueError) data = [[1], None] with self.assertRaises(ValidationError) as cm: field.load(data) result = cm.exception.detail self.assertIsInstance(result.errors[1], ValidationError) # list with dict items field = ListField(NestedField(Catalyst({'x': IntegerField()}))) data = [{'x': 1}, {'x': 2}] self.assertListEqual(data, field.load(data)) data = [{'x': 1}, {'x': 'x'}] with self.assertRaises(ValidationError) as cm: field.load(data) result = cm.exception.detail self.assertIsInstance(result.errors[1]['x'], ValueError) data = [{'x': 1}, None] with self.assertRaises(ValidationError) as cm: field.load(data) result = cm.exception.detail self.assertIsInstance(result.errors[1], ValidationError) field = ListField(IntegerField(), 2, 3) self.assertListEqual(field.load(['1', '2']), [1, 2]) with self.assertRaises(ValidationError): field.load(['1']) with self.assertRaises(ValidationError): field.load(['1', '2', '3', '4'])