def test_clean_invalid_asymmetric(self): """For a symmetric relationship, source and destination properties must match if specified.""" o2os = Relationship( name="Site to Site", slug="site-to-site", source_type=self.site_ct, source_label="Site A", source_hidden=True, source_filter={"name": ["site-a"]}, destination_type=self.rack_ct, destination_label="Site B", destination_hidden=False, destination_filter={"name": ["site-b"]}, type=RelationshipTypeChoices.TYPE_ONE_TO_ONE_SYMMETRIC, ) with self.assertRaises(ValidationError) as handler: o2os.clean() expected_errors = { "destination_type": ["Must match source_type for a symmetric relationship"], "destination_label": ["Must match source_label for a symmetric relationship"], "destination_hidden": ["Must match source_hidden for a symmetric relationship"], "destination_filter": ["Must match source_filter for a symmetric relationship"], } self.assertEqual(handler.exception.message_dict, expected_errors)
def test_clean_same_object(self): m2m = Relationship( name="Another Vlan to Rack", slug="vlan-rack-2", source_type=self.rack_ct, destination_type=self.rack_ct, type=RelationshipTypeChoices.TYPE_MANY_TO_MANY, ) with self.assertRaises(ValidationError): m2m.clean()
def test_clean_filter_not_valid(self): m2m = Relationship( name="Another Vlan to Rack", slug="vlan-rack-2", source_type=self.site_ct, source_filter={"notvalid": "not a region"}, destination_type=self.rack_ct, type=RelationshipTypeChoices.TYPE_MANY_TO_MANY, ) with self.assertRaises(ValidationError): m2m.clean()
def test_clean_valid(self): m2m = Relationship( name="Another Vlan to Rack", slug="vlan-rack-2", source_type=self.site_ct, source_filter={"name": ["site-b"]}, destination_type=self.rack_ct, destination_filter={"site": ["site-a"]}, type=RelationshipTypeChoices.TYPE_MANY_TO_MANY, ) m2m.clean()
def test_clean_valid(self): m2m = Relationship( name="Another Vlan to Rack", slug="vlan-rack-2", source_type=self.site_ct, source_filter={"region": "myregion"}, destination_type=self.rack_ct, destination_filter={"site": "mysite"}, type=RelationshipTypeChoices.TYPE_MANY_TO_MANY, ) m2m.clean() self.assertTrue(True)
def test_clean_filter_not_dict(self): m2m = Relationship( name="Another Vlan to Rack", slug="vlan-rack-2", source_type=self.site_ct, source_filter=["a list not a dict"], destination_type=self.rack_ct, type=RelationshipTypeChoices.TYPE_MANY_TO_MANY, ) with self.assertRaises(ValidationError) as handler: m2m.clean() expected_errors = { "source_filter": ["Filter for dcim.Site must be a dictionary"] } self.assertEqual(handler.exception.message_dict, expected_errors)
def test_clean_valid_symmetric_implicit(self): """For a symmetric relationship, omitted relevant properties are autofilled on clean.""" o2os = Relationship( name="Site to Site", slug="site-to-site", source_type=self.site_ct, destination_type=self.site_ct, source_label="Site", destination_filter={"name": ["site-b"]}, type=RelationshipTypeChoices.TYPE_ONE_TO_ONE_SYMMETRIC, ) o2os.clean() self.assertEqual(o2os.destination_label, "Site") self.assertEqual(o2os.source_filter, {"name": ["site-b"]}) self.assertEqual(o2os.source_type, o2os.destination_type) self.assertEqual(o2os.source_label, o2os.destination_label) self.assertEqual(o2os.source_hidden, o2os.destination_hidden) self.assertEqual(o2os.source_filter, o2os.destination_filter)
def test_clean_filter_not_valid(self): m2m = Relationship( name="Another Vlan to Rack", slug="vlan-rack-2", source_type=self.site_ct, source_filter={"notvalid": "not a region"}, destination_type=self.rack_ct, type=RelationshipTypeChoices.TYPE_MANY_TO_MANY, ) with self.assertRaises(ValidationError) as handler: m2m.clean() expected_errors = { "source_filter": [ "'notvalid' is not a valid filter parameter for dcim.Site object" ] } self.assertEqual(handler.exception.message_dict, expected_errors) m2m = Relationship( name="Another Vlan to Rack", slug="vlan-rack-2", source_type=self.site_ct, source_filter={"region": "not a list"}, destination_type=self.rack_ct, type=RelationshipTypeChoices.TYPE_MANY_TO_MANY, ) with self.assertRaises(ValidationError) as handler: m2m.clean() expected_errors = { "source_filter": ["'region': Enter a list of values."] } self.assertEqual(handler.exception.message_dict, expected_errors) m2m = Relationship( name="Another Vlan to Rack", slug="vlan-rack-2", source_type=self.site_ct, source_filter={"region": ["not a valid region"]}, destination_type=self.rack_ct, type=RelationshipTypeChoices.TYPE_MANY_TO_MANY, ) with self.assertRaises(ValidationError) as handler: m2m.clean() expected_errors = { "source_filter": [ "'region': Select a valid choice. not a valid region is not one of the available choices." ] } self.assertEqual(handler.exception.message_dict, expected_errors)