def test_labels_are_removed(self):
        organization = SuperOrganization.objects.create(name='test-org')

        label_a = Label.objects.create(
            color="red",
            name="label-a",
            super_organization=organization,
        )
        label_b = Label.objects.create(
            color="red",
            name="label-b",
            super_organization=organization,
        )

        bs_1 = SEEDFactory.building_snapshot(
            canonical_building=CanonicalBuilding.objects.create(), )
        bs_2 = SEEDFactory.building_snapshot(
            canonical_building=CanonicalBuilding.objects.create(), )

        bs_1.canonical_building.labels.add(label_a, label_b)
        bs_2.canonical_building.labels.add(label_b)

        # Sanity check
        self.assertTrue(
            bs_1.canonical_building.labels.filter(pk=label_a.pk).exists())
        self.assertTrue(
            bs_1.canonical_building.labels.filter(pk=label_b.pk).exists())
        self.assertFalse(
            bs_2.canonical_building.labels.filter(pk=label_a.pk).exists())
        self.assertTrue(
            bs_2.canonical_building.labels.filter(pk=label_b.pk).exists())

        qs = CanonicalBuilding.objects.all()
        self.assertEqual(qs.count(), 2)

        data = {
            'add_label_ids': [],
            'remove_label_ids': [label_a.pk, label_b.pk],
            'selected_buildings': [],
            'select_all_checkbox': True,
        }

        serializer = UpdateBuildingLabelsSerializer(
            data=data,
            super_organization=organization,
            queryset=qs,
        )
        self.assertTrue(serializer.is_valid(), serializer.errors)

        serializer.save()
        self.assertEqual(qs.count(), 2)

        self.assertFalse(
            bs_1.canonical_building.labels.filter(pk=label_a.pk).exists())
        self.assertFalse(
            bs_1.canonical_building.labels.filter(pk=label_b.pk).exists())
        self.assertFalse(
            bs_2.canonical_building.labels.filter(pk=label_a.pk).exists())
        self.assertFalse(
            bs_2.canonical_building.labels.filter(pk=label_b.pk).exists())
    def test_initialization_requires_queryset_as_argument(self):
        organization = SuperOrganization.objects.create(name='test-org')

        with self.assertRaises(KeyError):
            UpdateBuildingLabelsSerializer(super_organization=organization, )

        UpdateBuildingLabelsSerializer(
            super_organization=organization,
            queryset=CanonicalBuilding.objects.none(),
        )
Ejemplo n.º 3
0
    def test_labels_are_removed(self):
        organization = SuperOrganization.objects.create(name='test-org')

        label_a = Label.objects.create(
            color="red",
            name="label-a",
            super_organization=organization,
        )
        label_b = Label.objects.create(
            color="red",
            name="label-b",
            super_organization=organization,
        )

        bs_1 = SEEDFactory.building_snapshot(
            canonical_building=CanonicalBuilding.objects.create(),
        )
        bs_2 = SEEDFactory.building_snapshot(
            canonical_building=CanonicalBuilding.objects.create(),
        )

        bs_1.canonical_building.labels.add(label_a, label_b)
        bs_2.canonical_building.labels.add(label_b)

        # Sanity check
        self.assertTrue(bs_1.canonical_building.labels.filter(pk=label_a.pk).exists())
        self.assertTrue(bs_1.canonical_building.labels.filter(pk=label_b.pk).exists())
        self.assertFalse(bs_2.canonical_building.labels.filter(pk=label_a.pk).exists())
        self.assertTrue(bs_2.canonical_building.labels.filter(pk=label_b.pk).exists())

        qs = CanonicalBuilding.objects.all()
        self.assertEqual(qs.count(), 2)

        data = {
            'add_label_ids': [],
            'remove_label_ids': [label_a.pk, label_b.pk],
            'selected_buildings': [],
            'select_all_checkbox': True,
        }

        serializer = UpdateBuildingLabelsSerializer(
            data=data,
            super_organization=organization,
            queryset=qs,
        )
        self.assertTrue(serializer.is_valid(), serializer.errors)

        serializer.save()
        self.assertEqual(qs.count(), 2)

        self.assertFalse(bs_1.canonical_building.labels.filter(pk=label_a.pk).exists())
        self.assertFalse(bs_1.canonical_building.labels.filter(pk=label_b.pk).exists())
        self.assertFalse(bs_2.canonical_building.labels.filter(pk=label_a.pk).exists())
        self.assertFalse(bs_2.canonical_building.labels.filter(pk=label_b.pk).exists())