def setUp(self): self.field = CSVDataField(from_form=IPAddressCSVForm)
class CSVDataFieldTest(TestCase): def setUp(self): self.field = CSVDataField(from_form=IPAddressCSVForm) def test_clean(self): input = """ address,status,vrf 192.0.2.1/32,Active,Test VRF """ output = ( { "address": None, "status": None, "vrf": None }, [{ "address": "192.0.2.1/32", "status": "Active", "vrf": "Test VRF" }], ) self.assertEqual(self.field.clean(input), output) def test_clean_invalid_header(self): input = """ address,status,vrf,xxx 192.0.2.1/32,Active,Test VRF,123 """ with self.assertRaises(forms.ValidationError): self.field.clean(input) def test_clean_missing_required_header(self): input = """ status,vrf Active,Test VRF """ with self.assertRaises(forms.ValidationError): self.field.clean(input) def test_clean_default_to_field(self): input = """ address,status,vrf.name 192.0.2.1/32,Active,Test VRF """ output = ( { "address": None, "status": None, "vrf": "name" }, [{ "address": "192.0.2.1/32", "status": "Active", "vrf": "Test VRF" }], ) self.assertEqual(self.field.clean(input), output) def test_clean_pk_to_field(self): input = """ address,status,vrf.pk 192.0.2.1/32,Active,123 """ output = ( { "address": None, "status": None, "vrf": "pk" }, [{ "address": "192.0.2.1/32", "status": "Active", "vrf": "123" }], ) self.assertEqual(self.field.clean(input), output) def test_clean_custom_to_field(self): input = """ address,status,vrf.rd 192.0.2.1/32,Active,123:456 """ output = ( { "address": None, "status": None, "vrf": "rd" }, [{ "address": "192.0.2.1/32", "status": "Active", "vrf": "123:456" }], ) self.assertEqual(self.field.clean(input), output) def test_clean_invalid_to_field(self): input = """ address,status,vrf.xxx 192.0.2.1/32,Active,123:456 """ with self.assertRaises(forms.ValidationError): self.field.clean(input) def test_clean_to_field_on_non_object(self): input = """ address,status.foo,vrf 192.0.2.1/32,Bar,Test VRF """ with self.assertRaises(forms.ValidationError): self.field.clean(input)