<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> from django . test import TestCase <EOL> from seed . lib . superperms . orgs . models import ( <EOL> Organization as SuperOrganization , <EOL> ) <EOL> from seed . models import ( <EOL> BuildingSnapshot , <EOL> StatusLabel as Label , <EOL> CanonicalBuilding , <EOL> ) <EOL> from seed . serializers . labels import ( <EOL> LabelSerializer , <EOL> UpdateBuildingLabelsSerializer , <EOL> ) <EOL> from seed . factory import SEEDFactory <EOL> class TestLabelSerializer ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_initialization_requires_organization_as_argument ( self ) : <EOL> with self . assertRaises ( KeyError ) : <EOL> LabelSerializer ( building_snapshots = BuildingSnapshot . objects . none ( ) ) <EOL> organization = SuperOrganization . objects . create ( name = '<STR_LIT>' ) <EOL> LabelSerializer ( <EOL> super_organization = organization , <EOL> building_snapshots = BuildingSnapshot . objects . none ( ) , <EOL> ) <EOL> def test_initialization_requires_building_snapshots_as_argument ( self ) : <EOL> organization = SuperOrganization . objects . create ( name = '<STR_LIT>' ) <EOL> with self . assertRaises ( KeyError ) : <EOL> LabelSerializer ( super_organization = organization ) <EOL> LabelSerializer ( <EOL> super_organization = organization , <EOL> building_snapshots = BuildingSnapshot . objects . none ( ) , <EOL> ) <EOL> def test_uses_provided_organization_over_post_data ( self ) : <EOL> """<STR_LIT>""" <EOL> organization_a = SuperOrganization . objects . create ( name = '<STR_LIT>' ) <EOL> organization_b = SuperOrganization . objects . create ( name = '<STR_LIT>' ) <EOL> data = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : organization_a . pk , <EOL> } <EOL> serializer = LabelSerializer ( <EOL> data = data , <EOL> super_organization = organization_b , <EOL> building_snapshots = BuildingSnapshot . objects . none ( ) , <EOL> ) <EOL> self . assertTrue ( serializer . is_valid ( ) , serializer . errors ) <EOL> self . assertEqual ( <EOL> serializer . validated_data [ '<STR_LIT>' ] , organization_b <EOL> ) <EOL> def test_computed_property_is_applied ( self ) : <EOL> organization = SuperOrganization . objects . create ( name = '<STR_LIT>' ) <EOL> label_a = Label . objects . create ( <EOL> color = "<STR_LIT>" , name = "<STR_LIT>" , super_organization = organization , <EOL> ) <EOL> label_b = Label . objects . create ( <EOL> color = "<STR_LIT>" , name = "<STR_LIT>" , super_organization = organization , <EOL> ) <EOL> bs = SEEDFactory . building_snapshot ( <EOL> canonical_building = CanonicalBuilding . objects . create ( ) , <EOL> ) <EOL> bs . canonical_building . labels . add ( label_a ) <EOL> qs = BuildingSnapshot . objects . all ( ) <EOL> serializer = LabelSerializer ( <EOL> label_a , <EOL> super_organization = organization , <EOL> building_snapshots = qs , <EOL> ) <EOL> self . assertTrue ( serializer . data [ '<STR_LIT>' ] ) <EOL> serializer = LabelSerializer ( <EOL> label_b , <EOL> super_organization = organization , <EOL> building_snapshots = qs , <EOL> ) <EOL> self . assertFalse ( serializer . data [ '<STR_LIT>' ] ) <EOL> class TestUpdateBuildingLabelsSerializer ( TestCase ) : <EOL> def test_initialization_requires_organization_as_argument ( self ) : <EOL> with self . assertRaises ( KeyError ) : <EOL> UpdateBuildingLabelsSerializer ( <EOL> queryset = CanonicalBuilding . objects . none ( ) , <EOL> ) <EOL> organization = SuperOrganization . objects . create ( name = '<STR_LIT>' ) <EOL> UpdateBuildingLabelsSerializer ( <EOL> queryset = CanonicalBuilding . objects . none ( ) , <EOL> super_organization = organization , <EOL> ) <EOL> def test_initialization_requires_queryset_as_argument ( self ) : <EOL> organization = SuperOrganization . objects . create ( name = '<STR_LIT>' ) <EOL> with self . assertRaises ( KeyError ) : <EOL> UpdateBuildingLabelsSerializer ( <EOL> super_organization = organization , <EOL> ) <EOL> UpdateBuildingLabelsSerializer ( <EOL> super_organization = organization , <EOL> queryset = CanonicalBuilding . objects . none ( ) , <EOL> ) <EOL> def test_labels_are_applied ( self ) : <EOL> organization = SuperOrganization . objects . create ( name = '<STR_LIT>' ) <EOL> label_a = Label . objects . create ( <EOL> color = "<STR_LIT>" , <EOL> name = "<STR_LIT>" , <EOL> super_organization = organization , <EOL> ) <EOL> label_b = Label . objects . create ( <EOL> color = "<STR_LIT>" , <EOL> name = "<STR_LIT>" , <EOL> super_organization = organization , <EOL> ) <EOL> bs_1 = SEEDFactory . building_snapshot ( <EOL> canonical_building = CanonicalBuilding . objects . create ( ) , <EOL> ) <EOL> bs_2 = SEEDFactory . building_snapshot ( <EOL> canonical_building = CanonicalBuilding . objects . create ( ) , <EOL> ) <EOL> self . assertFalse ( bs_1 . canonical_building . labels . filter ( pk = label_a . pk ) . exists ( ) ) <EOL> self . assertFalse ( bs_1 . canonical_building . labels . filter ( pk = label_b . pk ) . exists ( ) ) <EOL> self . assertFalse ( bs_2 . canonical_building . labels . filter ( pk = label_a . pk ) . exists ( ) ) <EOL> self . assertFalse ( bs_2 . canonical_building . labels . filter ( pk = label_b . pk ) . exists ( ) ) <EOL> qs = CanonicalBuilding . objects . all ( ) <EOL> self . assertEqual ( qs . count ( ) , <NUM_LIT:2> ) <EOL> data = { <EOL> '<STR_LIT>' : [ label_a . pk , label_b . pk ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> serializer = UpdateBuildingLabelsSerializer ( <EOL> data = data , <EOL> super_organization = organization , <EOL> queryset = qs , <EOL> ) <EOL> self . assertTrue ( serializer . is_valid ( ) , serializer . errors ) <EOL> serializer . save ( ) <EOL> self . assertEqual ( qs . count ( ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( bs_1 . canonical_building . labels . filter ( pk = label_a . pk ) . exists ( ) ) <EOL> self . assertTrue ( bs_1 . canonical_building . labels . filter ( pk = label_b . pk ) . exists ( ) ) <EOL> self . assertTrue ( bs_2 . canonical_building . labels . filter ( pk = label_a . pk ) . exists ( ) ) <EOL> self . assertTrue ( bs_2 . canonical_building . labels . filter ( pk = label_b . pk ) . exists ( ) ) <EOL> def test_labels_are_removed ( self ) : <EOL> organization = SuperOrganization . objects . create ( name = '<STR_LIT>' ) <EOL> label_a = Label . objects . create ( <EOL> color = "<STR_LIT>" , <EOL> name = "<STR_LIT>" , <EOL> super_organization = organization , <EOL> ) <EOL> label_b = Label . objects . create ( <EOL> color = "<STR_LIT>" , <EOL> name = "<STR_LIT>" , <EOL> super_organization = organization , <EOL> ) <EOL> bs_1 = SEEDFactory . building_snapshot ( <EOL> canonical_building = CanonicalBuilding . objects . create ( ) , <EOL> ) <EOL> bs_2 = SEEDFactory . building_snapshot ( <EOL> canonical_building = CanonicalBuilding . objects . create ( ) , <EOL> ) <EOL> bs_1 . canonical_building . labels . add ( label_a , label_b ) <EOL> bs_2 . canonical_building . labels . add ( label_b ) <EOL> self . assertTrue ( bs_1 . canonical_building . labels . filter ( pk = label_a . pk ) . exists ( ) ) <EOL> self . assertTrue ( bs_1 . canonical_building . labels . filter ( pk = label_b . pk ) . exists ( ) ) <EOL> self . assertFalse ( bs_2 . canonical_building . labels . filter ( pk = label_a . pk ) . exists ( ) ) <EOL> self . assertTrue ( bs_2 . canonical_building . labels . filter ( pk = label_b . pk ) . exists ( ) ) <EOL> qs = CanonicalBuilding . objects . all ( ) <EOL> self . assertEqual ( qs . count ( ) , <NUM_LIT:2> ) <EOL> data = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ label_a . pk , label_b . pk ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> serializer = UpdateBuildingLabelsSerializer ( <EOL> data = data , <EOL> super_organization = organization , <EOL> queryset = qs , <EOL> ) <EOL> self . assertTrue ( serializer . is_valid ( ) , serializer . errors ) <EOL> serializer . save ( ) <EOL> self . assertEqual ( qs . count ( ) , <NUM_LIT:2> ) <EOL> self . assertFalse ( bs_1 . canonical_building . labels . filter ( pk = label_a . pk ) . exists ( ) ) <EOL> self . assertFalse ( bs_1 . canonical_building . labels . filter ( pk = label_b . pk ) . exists ( ) ) <EOL> self . assertFalse ( bs_2 . canonical_building . labels . filter ( pk = label_a . pk ) . exists ( ) ) <EOL> self . assertFalse ( bs_2 . canonical_building . labels . filter ( pk = label_b . pk ) . exists ( ) ) </s>
