示例#1
0
    def test_custom(self):
        mixer = Mixer()
        mixer.register(Rabbit, {
            'title': lambda: 'Mr. Rabbit'
        })

        rabbit = mixer.blend(Rabbit)
        self.assertEqual(rabbit.title, 'Mr. Rabbit')

        from mixer.backend.django import GenFactory

        def getter(*args, **kwargs):
            return "Always same"

        class MyFactory(GenFactory):
            generators = {models.CharField: getter}

        gen = MyFactory.gen_maker(models.CharField)
        self.assertEqual(gen(), "Always same")

        mixer = Mixer(factory=MyFactory, fake=False)
        self.assertEqual(mixer._Mixer__factory, MyFactory)

        test = mixer.blend(Rabbit)
        self.assertEqual(test.title, "Always same")
示例#2
0
    def test_register(self):
        mixer = Mixer()
        mixer.register(Rabbit, {
            'title': lambda: 'Mr. Rabbit'
        })

        rabbit = mixer.blend(Rabbit)
        self.assertEqual(rabbit.title, 'Mr. Rabbit')
示例#3
0
class PromoteRegulationGroupsTestCase(TestCase):
    """
    Test promote_regulation_groups
    """
    def setUp(self):
        self.tree = create_nomenclature_tree("UK")

        self.model_classes = [Chapter, Section, Heading, SubHeading, Commodity]

        self.mixer = Mixer()

        for model_class in self.model_classes:
            self.mixer.register(model_class, nomenclature_tree=self.tree)

    def test_models_without_regulations(self):

        for model_class in self.model_classes:
            obj = self.mixer.blend(model_class)
            self.assertFalse(obj.regulationgroup_set.exists())
            promote_regulation_groups(obj)
            self.assertFalse(obj.regulationgroup_set.exists())
            obj.delete()

    def test_models_with_regulations(self):
        model_classes = [
            (Chapter, "chapters"),
            (Section, "sections"),
            (Heading, "headings"),
            (SubHeading, "subheadings"),
            (Commodity, "commodities"),
        ]

        for model_class, relation_attr in model_classes:
            obj = self.mixer.blend(model_class)
            regulation = self.mixer.blend(RegulationGroup,
                                          **{relation_attr: obj})

            promote_regulation_groups(obj)
            self.assertEqual(obj.regulationgroup_set.count(), 1)
            self.assertEqual(obj.regulationgroup_set.first(), regulation)

            regulation.delete()
            obj.delete()

    def test_models_in_one_level_hierarchy_gets_promoted(self):
        """
        Test simple hierarchy

        Before:
        Heading    - No regulation
           |
        Commodity  - <RegulationGroup: A>

        After:
        Heading    - <RegulationGroup: A>
           |
        Commodity  - No regulation
        """
        heading = self.mixer.blend(Heading)
        commodity = self.mixer.blend(Commodity, heading=heading)
        regulation = self.mixer.blend(RegulationGroup, commodities=commodity)

        self.assertFalse(heading.regulationgroup_set.exists())
        self.assertEqual(commodity.regulationgroup_set.count(), 1)

        promote_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), regulation)
        self.assertFalse(commodity.regulationgroup_set.exists())

    def test_models_duplicated_in_one_level_hierarchy_gets_merged(self):
        """
        Test simple hierarchy

        Before:
        Heading    - <RegulationGroup: A>
           |
        Commodity  - <RegulationGroup: A>

        After:
        Heading    - <RegulationGroup: A>
           |
        Commodity  - No regulation
        """
        heading = self.mixer.blend(Heading)
        commodity = self.mixer.blend(Commodity, heading=heading)
        regulation = self.mixer.blend(RegulationGroup,
                                      headings=heading,
                                      commodities=commodity)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(commodity.regulationgroup_set.count(), 1)

        promote_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), regulation)
        self.assertFalse(commodity.regulationgroup_set.exists())

    def test_models_multi_children_in_one_level_hierarchy_gets_promoted(self):
        """
        Test simple hierarchy with multiple children

        Before:
                       Heading - No regulation
                                 |
                   ______________________________
                  |                              |
        Commodity - <RegulationGroup: A>    Commodity - <RegulationGroup: A>

        After:
                       Heading - <RegulationGroup: A>
                                 |
                   ______________________________
                  |                              |
        Commodity - No regulation    Commodity - No regulation
        """
        heading = self.mixer.blend(Heading)
        a_commodity = self.mixer.blend(Commodity, heading=heading)
        b_commodity = self.mixer.blend(Commodity, heading=heading)
        regulation = self.mixer.blend(RegulationGroup,
                                      commodities=[a_commodity, b_commodity])

        self.assertFalse(heading.regulationgroup_set.exists())
        self.assertEqual(a_commodity.regulationgroup_set.count(), 1)
        self.assertEqual(b_commodity.regulationgroup_set.count(), 1)

        promote_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), regulation)
        self.assertFalse(a_commodity.regulationgroup_set.exists())
        self.assertFalse(b_commodity.regulationgroup_set.exists())

    def test_models_duplicated_multi_children_in_one_level_hierarchy_gets_merged(
            self):
        """
        Test simple hierarchy with multiple children

        Before:
                       Heading - <RegulationGroup: A>
                                 |
                   ______________________________
                  |                              |
        Commodity - <RegulationGroup: A>    Commodity - <RegulationGroup: A>

        After:
                       Heading - <RegulationGroup: A>
                                 |
                   ______________________________
                  |                              |
        Commodity - No regulation    Commodity - No regulation
        """
        heading = self.mixer.blend(Heading)
        a_commodity = self.mixer.blend(Commodity, heading=heading)
        b_commodity = self.mixer.blend(Commodity, heading=heading)
        regulation = self.mixer.blend(RegulationGroup,
                                      headings=heading,
                                      commodities=[a_commodity, b_commodity])

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(a_commodity.regulationgroup_set.count(), 1)
        self.assertEqual(b_commodity.regulationgroup_set.count(), 1)

        promote_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), regulation)
        self.assertFalse(a_commodity.regulationgroup_set.exists())
        self.assertFalse(b_commodity.regulationgroup_set.exists())

    def test_models_multi_children_multi_regulations_in_one_level_hierarchy_gets_promoted(
        self, ):
        """
        Test simple hierarchy with multiple children multiple regulations

        Before:
                       Heading - No regulation
                                 |
                   ------------------------------
                  |                              |
        Commodity - <RegulationGroup: A>    Commodity - <RegulationGroup: A>
                    <RegulationGroup: B>

        After:
                       Heading - <RegulationGroup: A>
                                 |
                   ------------------------------
                  |                              |
        Commodity - <RegulationGroup: B>    Commodity - No regulation
        """
        heading = self.mixer.blend(Heading)
        a_commodity = self.mixer.blend(Commodity, heading=heading)
        b_commodity = self.mixer.blend(Commodity, heading=heading)
        a_regulation = self.mixer.blend(RegulationGroup,
                                        commodities=[a_commodity, b_commodity])
        b_regulation = self.mixer.blend(RegulationGroup,
                                        commodities=[a_commodity])

        self.assertFalse(heading.regulationgroup_set.exists())
        self.assertEqual(a_commodity.regulationgroup_set.count(), 2)
        self.assertEqual(b_commodity.regulationgroup_set.count(), 1)

        promote_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), a_regulation)
        self.assertTrue(a_commodity.regulationgroup_set.count(), 1)
        self.assertEqual(a_commodity.regulationgroup_set.first(), b_regulation)
        self.assertFalse(b_commodity.regulationgroup_set.exists())

    def test_models_in_multi_level_hierarchy_gets_promoted(self):
        """
        Test multi level hierarchy

        Before:
        Chapter      - No RegulationGroup
           |
        Section      - No RegulationGroup
           |
        Heading      - No RegulationGroup
           |
        SubHeading   - No RegulationGroup
           |
        Commodity    - <RegulationGroup: A>

        After:
        Chapter      - <RegulationGroup: A>
           |
        Section      - No RegulationGroup
           |
        Heading      - No RegulationGroup
           |
        SubHeading   - No RegulationGroup
           |
        Commodity    - No RegulationGroup
        """
        section = self.mixer.blend(Section)
        chapter = self.mixer.blend(Chapter, section=section)
        heading = self.mixer.blend(Heading, chapter=chapter)
        sub_heading = self.mixer.blend(SubHeading, heading=heading)
        commodity = self.mixer.blend(Commodity, parent_subheading=sub_heading)
        regulation = self.mixer.blend(RegulationGroup, commodities=commodity)

        self.assertFalse(section.regulationgroup_set.exists())
        self.assertFalse(chapter.regulationgroup_set.exists())
        self.assertFalse(heading.regulationgroup_set.exists())
        self.assertFalse(sub_heading.regulationgroup_set.exists())
        self.assertEqual(commodity.regulationgroup_set.count(), 1)

        promote_regulation_groups(section)

        self.assertEqual(section.regulationgroup_set.count(), 1)
        self.assertEqual(section.regulationgroup_set.first(), regulation)
        self.assertFalse(chapter.regulationgroup_set.exists())
        self.assertFalse(heading.regulationgroup_set.exists())
        self.assertFalse(sub_heading.regulationgroup_set.exists())
        self.assertFalse(commodity.regulationgroup_set.exists())

    def test_models_multi_children_in_multi_level_hierarchy_gets_promoted(
            self):
        """
        Test multi children level hierarchy
        """
        section = self.mixer.blend(Section)

        a_chapter = self.mixer.blend(Chapter, section=section)
        b_chapter = self.mixer.blend(Chapter, section=section)
        chapters = [a_chapter, b_chapter]

        a_a_heading = self.mixer.blend(Heading, chapter=a_chapter)
        a_b_heading = self.mixer.blend(Heading, chapter=a_chapter)
        b_a_heading = self.mixer.blend(Heading, chapter=b_chapter)
        b_b_heading = self.mixer.blend(Heading, chapter=b_chapter)
        headings = [a_a_heading, a_b_heading, b_a_heading, b_b_heading]

        a_a_a_sub_heading = self.mixer.blend(SubHeading, heading=a_a_heading)
        a_a_b_sub_heading = self.mixer.blend(SubHeading, heading=a_a_heading)
        a_b_a_sub_heading = self.mixer.blend(SubHeading, heading=a_b_heading)
        a_b_b_sub_heading = self.mixer.blend(SubHeading, heading=a_b_heading)
        b_a_a_sub_heading = self.mixer.blend(SubHeading, heading=b_a_heading)
        b_a_b_sub_heading = self.mixer.blend(SubHeading, heading=b_a_heading)
        b_b_a_sub_heading = self.mixer.blend(SubHeading, heading=b_b_heading)
        b_b_b_sub_heading = self.mixer.blend(SubHeading, heading=b_b_heading)
        sub_headings = [
            a_a_a_sub_heading,
            a_a_b_sub_heading,
            a_b_a_sub_heading,
            a_b_b_sub_heading,
            b_a_a_sub_heading,
            b_a_b_sub_heading,
            b_b_a_sub_heading,
            b_b_b_sub_heading,
        ]

        a_a_a_a_commodity = self.mixer.blend(
            Commodity, parent_subheading=a_a_a_sub_heading)
        a_a_a_b_commodity = self.mixer.blend(
            Commodity, parent_subheading=a_a_a_sub_heading)
        a_a_b_a_commodity = self.mixer.blend(
            Commodity, parent_subheading=a_a_b_sub_heading)
        a_a_b_b_commodity = self.mixer.blend(
            Commodity, parent_subheading=a_a_b_sub_heading)
        a_b_a_a_commodity = self.mixer.blend(
            Commodity, parent_subheading=a_b_a_sub_heading)
        a_b_a_b_commodity = self.mixer.blend(
            Commodity, parent_subheading=a_b_a_sub_heading)
        a_b_b_a_commodity = self.mixer.blend(
            Commodity, parent_subheading=a_b_b_sub_heading)
        a_b_b_b_commodity = self.mixer.blend(
            Commodity, parent_subheading=a_b_b_sub_heading)
        b_a_a_a_commodity = self.mixer.blend(
            Commodity, parent_subheading=b_a_a_sub_heading)
        b_a_a_b_commodity = self.mixer.blend(
            Commodity, parent_subheading=b_a_a_sub_heading)
        b_a_b_a_commodity = self.mixer.blend(
            Commodity, parent_subheading=b_a_b_sub_heading)
        b_a_b_b_commodity = self.mixer.blend(
            Commodity, parent_subheading=b_a_b_sub_heading)
        b_b_a_a_commodity = self.mixer.blend(
            Commodity, parent_subheading=b_b_a_sub_heading)
        b_b_a_b_commodity = self.mixer.blend(
            Commodity, parent_subheading=b_b_a_sub_heading)
        b_b_b_a_commodity = self.mixer.blend(
            Commodity, parent_subheading=b_b_b_sub_heading)
        b_b_b_b_commodity = self.mixer.blend(
            Commodity, parent_subheading=b_b_b_sub_heading)
        commodities = [
            a_a_a_a_commodity,
            a_a_a_b_commodity,
            a_a_b_a_commodity,
            a_a_b_b_commodity,
            a_b_a_a_commodity,
            a_b_a_b_commodity,
            a_b_b_a_commodity,
            a_b_b_b_commodity,
            b_a_a_a_commodity,
            b_a_a_b_commodity,
            b_a_b_a_commodity,
            b_a_b_b_commodity,
            b_b_a_a_commodity,
            b_b_a_b_commodity,
            b_b_b_a_commodity,
            b_b_b_b_commodity,
        ]

        regulation = self.mixer.blend(RegulationGroup,
                                      commodities=[c.pk for c in commodities])

        self.assertFalse(section.regulationgroup_set.exists())
        for chapter in chapters:
            self.assertFalse(chapter.regulationgroup_set.exists())
        for heading in headings:
            self.assertFalse(heading.regulationgroup_set.exists())
        for sub_heading in sub_headings:
            self.assertFalse(sub_heading.regulationgroup_set.exists())
        for commodity in commodities:
            self.assertEqual(commodity.regulationgroup_set.count(), 1)

        promote_regulation_groups(section)

        self.assertEqual(section.regulationgroup_set.count(), 1)
        self.assertEqual(section.regulationgroup_set.first(), regulation)
        for chapter in chapters:
            self.assertFalse(chapter.regulationgroup_set.exists())
        for heading in headings:
            self.assertFalse(heading.regulationgroup_set.exists())
        for sub_heading in sub_headings:
            self.assertFalse(sub_heading.regulationgroup_set.exists())
        for commodity in commodities:
            self.assertFalse(commodity.regulationgroup_set.exists())

    def test_models_multi_regulations_multi_children_in_multi_level_hierarchy_gets_promoted(
        self, ):
        """
        Test multi regulations and multi children level hierarchy
        """
        section = self.mixer.blend(Section)

        a_chapter = self.mixer.blend(Chapter,
                                     chapter_code="a",
                                     section=section)
        b_chapter = self.mixer.blend(Chapter,
                                     chapter_code="b",
                                     section=section)

        a_a_heading = self.mixer.blend(Heading,
                                       heading_code="a_a",
                                       chapter=a_chapter)
        a_b_heading = self.mixer.blend(Heading,
                                       heading_code="a_b",
                                       chapter=a_chapter)
        b_a_heading = self.mixer.blend(Heading,
                                       heading_code="b_a",
                                       chapter=b_chapter)
        b_b_heading = self.mixer.blend(Heading,
                                       heading_code="b_b",
                                       chapter=b_chapter)
        headings = [a_a_heading, a_b_heading, b_a_heading, b_b_heading]

        a_a_a_sub_heading = self.mixer.blend(SubHeading,
                                             commodity_code="a_a_a",
                                             heading=a_a_heading)
        a_a_b_sub_heading = self.mixer.blend(SubHeading,
                                             commodity_code="a_a_b",
                                             heading=a_a_heading)
        a_b_a_sub_heading = self.mixer.blend(SubHeading,
                                             commodity_code="a_b_a",
                                             heading=a_b_heading)
        a_b_b_sub_heading = self.mixer.blend(SubHeading,
                                             commodity_code="a_b_b",
                                             heading=a_b_heading)
        b_a_a_sub_heading = self.mixer.blend(SubHeading,
                                             commodity_code="b_a_a",
                                             heading=b_a_heading)
        b_a_b_sub_heading = self.mixer.blend(SubHeading,
                                             commodity_code="b_a_b",
                                             heading=b_a_heading)
        b_b_a_sub_heading = self.mixer.blend(SubHeading,
                                             commodity_code="b_b_a",
                                             heading=b_b_heading)
        b_b_b_sub_heading = self.mixer.blend(SubHeading,
                                             commodity_code="b_b_b",
                                             heading=b_b_heading)
        sub_headings = [
            a_a_a_sub_heading,
            a_a_b_sub_heading,
            a_b_a_sub_heading,
            a_b_b_sub_heading,
            b_a_a_sub_heading,
            b_a_b_sub_heading,
            b_b_a_sub_heading,
            b_b_b_sub_heading,
        ]

        a_a_a_a_commodity = self.mixer.blend(
            Commodity,
            commodity_code="a_a_a_a",
            parent_subheading=a_a_a_sub_heading)
        a_a_a_b_commodity = self.mixer.blend(
            Commodity,
            commodity_code="a_a_a_b",
            parent_subheading=a_a_a_sub_heading)
        a_a_b_a_commodity = self.mixer.blend(
            Commodity,
            commodity_code="a_a_b_a",
            parent_subheading=a_a_b_sub_heading)
        a_a_b_b_commodity = self.mixer.blend(
            Commodity,
            commodity_code="a_a_b_b",
            parent_subheading=a_a_b_sub_heading)
        a_b_a_a_commodity = self.mixer.blend(
            Commodity,
            commodity_code="a_b_a_a",
            parent_subheading=a_b_a_sub_heading)
        a_b_a_b_commodity = self.mixer.blend(
            Commodity,
            commodity_code="a_b_a_b",
            parent_subheading=a_b_a_sub_heading)
        a_b_b_a_commodity = self.mixer.blend(
            Commodity,
            commodity_code="a_b_b_a",
            parent_subheading=a_b_b_sub_heading)
        a_b_b_b_commodity = self.mixer.blend(
            Commodity,
            commodity_code="a_b_b_b",
            parent_subheading=a_b_b_sub_heading)
        b_a_a_a_commodity = self.mixer.blend(
            Commodity,
            commodity_code="b_a_a_a",
            parent_subheading=b_a_a_sub_heading)
        b_a_a_b_commodity = self.mixer.blend(
            Commodity,
            commodity_code="b_a_a_b",
            parent_subheading=b_a_a_sub_heading)
        b_a_b_a_commodity = self.mixer.blend(
            Commodity,
            commodity_code="b_a_b_a",
            parent_subheading=b_a_b_sub_heading)
        b_a_b_b_commodity = self.mixer.blend(
            Commodity,
            commodity_code="b_a_b_b",
            parent_subheading=b_a_b_sub_heading)
        b_b_a_a_commodity = self.mixer.blend(
            Commodity,
            commodity_code="b_b_a_a",
            parent_subheading=b_b_a_sub_heading)
        b_b_a_b_commodity = self.mixer.blend(
            Commodity,
            commodity_code="b_b_a_b",
            parent_subheading=b_b_a_sub_heading)
        b_b_b_a_commodity = self.mixer.blend(
            Commodity,
            commodity_code="b_b_b_a",
            parent_subheading=b_b_b_sub_heading)
        b_b_b_b_commodity = self.mixer.blend(
            Commodity,
            commodity_code="b_b_b_b",
            parent_subheading=b_b_b_sub_heading)
        commodities = [
            a_a_a_a_commodity,
            a_a_a_b_commodity,
            a_a_b_a_commodity,
            a_a_b_b_commodity,
            a_b_a_a_commodity,
            a_b_a_b_commodity,
            a_b_b_a_commodity,
            a_b_b_b_commodity,
            b_a_a_a_commodity,
            b_a_a_b_commodity,
            b_a_b_a_commodity,
            b_a_b_b_commodity,
            b_b_a_a_commodity,
            b_b_a_b_commodity,
            b_b_b_a_commodity,
            b_b_b_b_commodity,
        ]

        a_regulation_commodities = commodities
        a_regulation = self.mixer.blend(
            RegulationGroup,
            title="a_regulation",
            commodities=[c.pk for c in a_regulation_commodities],
        )
        b_regulation_commodities = commodities[:8]
        b_regulation = self.mixer.blend(
            RegulationGroup,
            title="b_regulation",
            commodities=[c.pk for c in b_regulation_commodities],
        )
        c_regulation_commodities = commodities[:4]
        c_regulation = self.mixer.blend(
            RegulationGroup,
            title="c_regulation",
            commodities=[c.pk for c in c_regulation_commodities],
        )
        d_regulation_commodities = commodities[:2]
        d_regulation = self.mixer.blend(
            RegulationGroup,
            title="d_regulation",
            commodities=[c.pk for c in d_regulation_commodities],
        )
        e_regulation_commodities = commodities[:1]
        e_regulation = self.mixer.blend(
            RegulationGroup,
            title="e_regulation",
            commodities=[c.pk for c in e_regulation_commodities],
        )

        promote_regulation_groups(section)

        self.assertEqual(section.regulationgroup_set.count(), 1)
        self.assertEqual(section.regulationgroup_set.first(), a_regulation)

        self.assertEqual(a_chapter.regulationgroup_set.count(), 1)
        self.assertEqual(a_chapter.regulationgroup_set.first(), b_regulation)
        self.assertFalse(b_chapter.regulationgroup_set.exists())

        self.assertEqual(a_a_heading.regulationgroup_set.count(), 1)
        self.assertEqual(a_a_heading.regulationgroup_set.first(), c_regulation)
        for heading in headings[1:]:
            self.assertFalse(heading.regulationgroup_set.exists())

        self.assertEqual(a_a_a_sub_heading.regulationgroup_set.count(), 1)
        self.assertEqual(a_a_a_sub_heading.regulationgroup_set.first(),
                         d_regulation)
        for sub_heading in sub_headings[1:]:
            self.assertFalse(sub_heading.regulationgroup_set.exists())

        self.assertEqual(a_a_a_a_commodity.regulationgroup_set.count(), 1)
        self.assertEqual(a_a_a_a_commodity.regulationgroup_set.first(),
                         e_regulation)
        for commodity in commodities[1:]:
            self.assertFalse(commodity.regulationgroup_set.exists())

    def test_models_in_simple_sub_heading_hierarchy_gets_promoted(self):
        """
        Test simple sub heading hierarchy

        Before:
        Sub Heading    - No regulation
           |
        Sub Heading    - No regulation
           |
        Commodity      - <RegulationGroup: A>

        After:
        Sub Heading    - <RegulationGroup: A>
           |
        Sub Heading    - No regulation
           |
        Commodity  - No regulation
        """
        a_sub_heading = self.mixer.blend(SubHeading)
        b_sub_heading = self.mixer.blend(SubHeading,
                                         parent_subheading=a_sub_heading)
        commodity = self.mixer.blend(Commodity,
                                     parent_subheading=b_sub_heading)
        regulation = self.mixer.blend(RegulationGroup, commodities=commodity)

        self.assertFalse(a_sub_heading.regulationgroup_set.exists())
        self.assertFalse(b_sub_heading.regulationgroup_set.exists())
        self.assertEqual(commodity.regulationgroup_set.count(), 1)

        promote_regulation_groups(a_sub_heading)

        self.assertEqual(a_sub_heading.regulationgroup_set.count(), 1)
        self.assertEqual(a_sub_heading.regulationgroup_set.first(), regulation)
        self.assertFalse(b_sub_heading.regulationgroup_set.exists())
        self.assertFalse(commodity.regulationgroup_set.exists())

    def test_models_in_simple_sub_heading_and_heading_hierarchy_gets_promoted(
            self):
        """
        Test simple sub heading hierarchy

        Before:
        Heading        - No regulation
           |
        Sub Heading    - No regulation
           |
        Sub Heading    - No regulation
           |
        Commodity      - <RegulationGroup: A>

        After:
        Heading        - <RegulationGroup: A>
           |
        Sub Heading    - No regulation
           |
        Sub Heading    - No regulation
           |
        Commodity  - No regulation
        """
        heading = self.mixer.blend(Heading)
        a_sub_heading = self.mixer.blend(SubHeading, heading=heading)
        b_sub_heading = self.mixer.blend(SubHeading,
                                         parent_subheading=a_sub_heading)
        commodity = self.mixer.blend(Commodity,
                                     parent_subheading=b_sub_heading)
        regulation = self.mixer.blend(RegulationGroup, commodities=commodity)

        self.assertFalse(a_sub_heading.regulationgroup_set.exists())
        self.assertFalse(b_sub_heading.regulationgroup_set.exists())
        self.assertEqual(commodity.regulationgroup_set.count(), 1)

        promote_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), regulation)
        self.assertFalse(a_sub_heading.regulationgroup_set.exists())
        self.assertFalse(b_sub_heading.regulationgroup_set.exists())
        self.assertFalse(commodity.regulationgroup_set.exists())
示例#4
0
class InheritedRegulationGroupsTestCase(TestCase):
    """
    Test regulation groups manager inherited
    """

    def setUp(self):
        self.tree = create_nomenclature_tree("UK")

        self.model_classes = [Chapter, Section, Heading, SubHeading, Commodity]

        self.mixer = Mixer()

        for model_class in self.model_classes:
            self.mixer.register(model_class, nomenclature_tree=self.tree)

    def test_models_without_regulation_groups(self):

        for model_class in self.model_classes:
            obj = self.mixer.blend(model_class)
            self.assertFalse(obj.regulationgroup_set.exists())
            regulation_groups = RegulationGroup.objects.inherited(obj)
            self.assertEqual(set(regulation_groups), set([]))
            obj.delete()

    def test_model_with_regulation(self):
        model_classes = [
            (Chapter, "chapters"),
            (Section, "sections"),
            (Heading, "headings"),
            (SubHeading, "subheadings"),
            (Commodity, "commodities"),
        ]

        for model_class, relation_attr in model_classes:
            obj = self.mixer.blend(model_class)

            a_regulation = self.mixer.blend(RegulationGroup, **{relation_attr: obj})
            b_regulation = self.mixer.blend(RegulationGroup, **{relation_attr: obj})

            self.assertEqual(obj.regulationgroup_set.count(), 2)

            regulation_groups = RegulationGroup.objects.inherited(obj)
            self.assertEqual(set(regulation_groups), set([a_regulation, b_regulation]))

            a_regulation.delete()
            b_regulation.delete()
            obj.delete()

    def test_models_in_one_level_hierarchy_single_regulation(self):
        heading = self.mixer.blend(Heading)
        regulation = self.mixer.blend(RegulationGroup, headings=heading)
        commodity = self.mixer.blend(Commodity, heading=heading)

        commodity_regulation_groups = RegulationGroup.objects.inherited(commodity)
        self.assertEqual(set(commodity_regulation_groups), set([regulation]))

        heading_regulation_groups = RegulationGroup.objects.inherited(heading)
        self.assertEqual(set(heading_regulation_groups), set([regulation]))

    def test_models_multi_regulation_groups_one_level_hierarchy(self):
        heading = self.mixer.blend(Heading)
        commodity = self.mixer.blend(Commodity, heading=heading)

        a_regulation = self.mixer.blend(RegulationGroup, headings=heading)
        b_regulation = self.mixer.blend(RegulationGroup, commodities=commodity)

        commodity_regulation_groups = RegulationGroup.objects.inherited(commodity)
        self.assertEqual(
            set(commodity_regulation_groups), set([a_regulation, b_regulation])
        )

        heading_regulation_groups = RegulationGroup.objects.inherited(heading)
        self.assertEqual(set(heading_regulation_groups), set([a_regulation]))

    def test_models_same_regulation_multiple_times_one_level_hierarchy(self):
        heading = self.mixer.blend(Heading)
        commodity = self.mixer.blend(Commodity, heading=heading)
        regulation = self.mixer.blend(
            RegulationGroup, headings=heading, commodities=commodity
        )

        commodity_regulation_groups = RegulationGroup.objects.inherited(commodity)
        self.assertEqual(set(commodity_regulation_groups), set([regulation]))

        heading_regulation_groups = RegulationGroup.objects.inherited(heading)
        self.assertEqual(set(heading_regulation_groups), set([regulation]))

    def test_model_multi_level_hierarchy_one_regulation(self):
        section = self.mixer.blend(Section)
        regulation = self.mixer.blend(RegulationGroup, sections=section)
        chapter = self.mixer.blend(Chapter, section=section)
        heading = self.mixer.blend(Heading, chapter=chapter)
        sub_heading = self.mixer.blend(SubHeading, heading=heading)
        commodity = self.mixer.blend(Commodity, parent_subheading=sub_heading)

        commodity_regulation_groups = RegulationGroup.objects.inherited(commodity)
        self.assertEqual(set(commodity_regulation_groups), set([regulation]))

        sub_heading_regulation_groups = RegulationGroup.objects.inherited(sub_heading)
        self.assertEqual(set(sub_heading_regulation_groups), set([regulation]))

        heading_regulation_groups = RegulationGroup.objects.inherited(heading)
        self.assertEqual(set(heading_regulation_groups), set([regulation]))

        chapter_regulation_groups = RegulationGroup.objects.inherited(chapter)
        self.assertEqual(set(chapter_regulation_groups), set([regulation]))

        section_regulation_groups = RegulationGroup.objects.inherited(section)
        self.assertEqual(set(section_regulation_groups), set([regulation]))

    def test_model_multi_level_hierarchy_single_regulation(self):
        section = self.mixer.blend(Section)
        chapter = self.mixer.blend(Chapter, section=section)
        heading = self.mixer.blend(Heading, chapter=chapter)
        sub_heading = self.mixer.blend(SubHeading, heading=heading)
        commodity = self.mixer.blend(Commodity, parent_subheading=sub_heading)
        regulation = self.mixer.blend(
            RegulationGroup,
            sections=section,
            chapters=chapter,
            headings=heading,
            sub_headings=sub_heading,
            commodities=commodity,
        )

        commodity_regulation_groups = RegulationGroup.objects.inherited(commodity)
        self.assertEqual(set(commodity_regulation_groups), set([regulation]))

        sub_heading_regulation_groups = RegulationGroup.objects.inherited(sub_heading)
        self.assertEqual(set(sub_heading_regulation_groups), set([regulation]))

        heading_regulation_groups = RegulationGroup.objects.inherited(heading)
        self.assertEqual(set(heading_regulation_groups), set([regulation]))

        chapter_regulation_groups = RegulationGroup.objects.inherited(chapter)
        self.assertEqual(set(chapter_regulation_groups), set([regulation]))

        section_regulation_groups = RegulationGroup.objects.inherited(section)
        self.assertEqual(set(section_regulation_groups), set([regulation]))

    def get_model_multi_level_hierarchy_multiple_regulation_groups(self):
        section = self.mixer.blend(Section)
        section_regulation = self.mixer.blend(RegulationGroup, sections=section)

        chapter = self.mixer.blend(Chapter, section=section)
        chapter_regulation = self.mixer.blend(RegulationGroup, chapters=chapter)

        heading = self.mixer.blend(Heading, chapter=chapter)
        heading_regulation = self.mixer.blend(RegulationGroup, headings=heading)

        sub_heading = self.mixer.blend(SubHeading, heading=heading)
        sub_heading_regulation = self.mixer.blend(
            RegulationGroup, sub_headings=sub_heading
        )

        commodity = self.mixer.blend(Commodity, parent_subheading=sub_heading)
        commodity_regulation = self.mixer.blend(RegulationGroup, commodities=commodity)

        commodity_regulation_groups = RegulationGroup.objects.inherited(commodity)
        self.assertEqual(
            set(commodity_regulation_groups),
            set(
                [
                    section_regulation,
                    chapter_regulation,
                    heading_regulation,
                    sub_heading_regulation,
                    commodity_regulation,
                ]
            ),
        )

        sub_heading_regulation_groups = RegulationGroup.objects.inherited(sub_heading)
        self.assertEqual(
            set(sub_heading_regulation_groups),
            set(
                [
                    section_regulation,
                    chapter_regulation,
                    heading_regulation,
                    sub_heading_regulation,
                ]
            ),
        )

        heading_regulation_groups = RegulationGroup.objects.inherited(heading)
        self.assertEqual(
            set(heading_regulation_groups),
            set([section_regulation, chapter_regulation, heading_regulation]),
        )

        chapter_regulation_groups = RegulationGroup.objects.inherited(chapter)
        self.assertEqual(
            set(chapter_regulation_groups),
            set([section_regulation, chapter_regulation]),
        )

        section_regulation_groups = RegulationGroup.objects.inherited(section)
        self.assertEqual(set(section_regulation_groups), set([section_regulation]))

    def test_regulation_groups_in_multiple_hierarchies(self):
        a_commodity = self.mixer.blend(Commodity)
        a_regulation = self.mixer.blend(RegulationGroup, commodities=a_commodity)

        b_commodity = self.mixer.blend(Commodity)
        b_regulation = self.mixer.blend(RegulationGroup, commodities=b_commodity)

        a_commodity_regulation_groups = RegulationGroup.objects.inherited(a_commodity)
        self.assertEqual(set(a_commodity_regulation_groups), set([a_regulation]))

        b_commodity_regulation_groups = RegulationGroup.objects.inherited(b_commodity)
        self.assertEqual(set(b_commodity_regulation_groups), set([b_regulation]))
class ReplicateRegulationGroupsTestCase(TestCase):
    """
    Test promote_regulation_groups
    """
    def setUp(self):
        self.tree = create_nomenclature_tree("UK")

        self.model_classes = [Chapter, Section, Heading, SubHeading, Commodity]

        self.mixer = Mixer()

        for model_class in self.model_classes:
            self.mixer.register(model_class, nomenclature_tree=self.tree)

    def test_models_without_regulations(self):
        for model_class in self.model_classes:
            obj = self.mixer.blend(model_class)
            self.assertFalse(obj.regulationgroup_set.exists())
            replicate_regulation_groups(obj)
            self.assertFalse(obj.regulationgroup_set.exists())
            obj.delete()

    def test_models_with_regulations(self):
        model_classes = [
            (Chapter, "chapters"),
            (Section, "sections"),
            (Heading, "headings"),
            (SubHeading, "subheadings"),
            (Commodity, "commodities"),
        ]

        for model_class, relation_attr in model_classes:
            obj = self.mixer.blend(model_class)
            regulation = self.mixer.blend(RegulationGroup,
                                          **{relation_attr: obj})

            replicate_regulation_groups(obj)
            self.assertEqual(obj.regulationgroup_set.count(), 1)
            self.assertEqual(obj.regulationgroup_set.first(), regulation)

            regulation.delete()
            obj.delete()

    def test_models_in_one_level_hierarchy_gets_replicated(self):
        """
        Test simple hierarchy

        Before:
        Heading    - <RegulationGroup: A>
           |
        Commodity  - No regulation

        After:
        Heading    - <RegulationGroup: A>
           |
        Commodity  - <RegulationGroup: A>
        """
        heading = self.mixer.blend(Heading)
        commodity = self.mixer.blend(Commodity, heading=heading)
        regulation = self.mixer.blend(RegulationGroup, headings=heading)

        self.assertTrue(heading.regulationgroup_set.exists())
        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertFalse(commodity.regulationgroup_set.exists())
        self.assertEqual(commodity.regulationgroup_set.count(), 0)

        replicate_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), regulation)
        self.assertEqual(commodity.regulationgroup_set.count(), 1)
        self.assertEqual(commodity.regulationgroup_set.first(), regulation)

    def test_models_duplicated_in_one_level_hierarchy_gets_merged(self):
        """
        Test simple hierarchy

        Before:
        Heading    - <RegulationGroup: A>
           |
        Commodity  - <RegulationGroup: A>

        After:
        Heading    - <RegulationGroup: A>
           |
        Commodity  - <RegulationGroup: A>
        """
        heading = self.mixer.blend(Heading)
        commodity = self.mixer.blend(Commodity, heading=heading)
        regulation = self.mixer.blend(RegulationGroup,
                                      headings=heading,
                                      commodities=commodity)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(commodity.regulationgroup_set.count(), 1)

        replicate_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), regulation)
        self.assertEqual(commodity.regulationgroup_set.count(), 1)
        self.assertEqual(commodity.regulationgroup_set.first(), regulation)

    def test_models_multi_children_in_one_level_hierarchy_gets_replicated(
            self):
        """
        Test simple hierarchy with multiple children

        Before:
                       Heading - <RegulationGroup: A>
                                 |
                   ______________________________
                  |                              |
        Commodity - No regulation    Commodity - No regulation

        After:
                       Heading - <RegulationGroup: A>
                                 |
                   ______________________________
                  |                              |
        Commodity - <RegulationGroup: A>    Commodity - <RegulationGroup: A>
        """
        heading = self.mixer.blend(Heading)
        a_commodity = self.mixer.blend(Commodity, heading=heading)
        b_commodity = self.mixer.blend(Commodity, heading=heading)
        regulation = self.mixer.blend(
            RegulationGroup,
            headings=heading,
        )

        self.assertEquals(heading.regulationgroup_set.count(), 1)
        self.assertFalse(a_commodity.regulationgroup_set.exists())
        self.assertFalse(b_commodity.regulationgroup_set.exists())

        replicate_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), regulation)
        self.assertEqual(a_commodity.regulationgroup_set.count(), 1)
        self.assertEqual(a_commodity.regulationgroup_set.first(), regulation)
        self.assertEqual(b_commodity.regulationgroup_set.count(), 1)
        self.assertEqual(b_commodity.regulationgroup_set.first(), regulation)

    def test_models_multi_children_multi_regulations_in_one_level_hierarchy_gets_replicated(
        self, ):
        """
        Test simple hierarchy with multiple children multiple regulations

        Before:
                       Heading - <RegulationGroup: A>
                                 <RegulationGroup: B>
                                 |
                   ------------------------------
                  |                              |
        Commodity - No regulation    Commodity - No regulation

        After:
                       Heading - <RegulationGroup: A>
                                 <RegulationGroup: B>
                                 |
                   ------------------------------
                  |                              |
        Commodity - <RegulationGroup: A>    Commodity - <RegulationGroup: A>
                    <RegulationGroup: B>                <RegulationGroup: B>
        """
        heading = self.mixer.blend(Heading)
        a_commodity = self.mixer.blend(Commodity, heading=heading)
        b_commodity = self.mixer.blend(Commodity, heading=heading)
        a_regulation = self.mixer.blend(RegulationGroup, headings=heading)
        b_regulation = self.mixer.blend(RegulationGroup, headings=heading)

        self.assertEqual(heading.regulationgroup_set.count(), 2)
        self.assertFalse(a_commodity.regulationgroup_set.exists())
        self.assertFalse(b_commodity.regulationgroup_set.exists())

        replicate_regulation_groups(heading)

        self.assertEqual(heading.regulationgroup_set.count(), 2)
        self.assertIn(a_regulation, heading.regulationgroup_set.all())
        self.assertIn(b_regulation, heading.regulationgroup_set.all())
        self.assertEqual(a_commodity.regulationgroup_set.count(), 2)
        self.assertIn(a_regulation, a_commodity.regulationgroup_set.all())
        self.assertIn(b_regulation, a_commodity.regulationgroup_set.all())
        self.assertEqual(b_commodity.regulationgroup_set.count(), 2)
        self.assertIn(a_regulation, b_commodity.regulationgroup_set.all())
        self.assertIn(b_regulation, b_commodity.regulationgroup_set.all())

    def test_models_in_multi_level_hierarchy_gets_promoted(self):
        """
        Test multi level hierarchy

        Before:
        Chapter      - <RegulationGroup: A>
           |
        Section      - No RegulationGroup
           |
        Heading      - No RegulationGroup
           |
        SubHeading   - No RegulationGroup
           |
        Commodity    - No RegulationGroup

        After:
        Chapter      - <RegulationGroup: A>
           |
        Section      - <RegulationGroup: A>
           |
        Heading      - <RegulationGroup: A>
           |
        SubHeading   - <RegulationGroup: A>
           |
        Commodity    - <RegulationGroup: A>
        """
        section = self.mixer.blend(Section)
        chapter = self.mixer.blend(Chapter, section=section)
        heading = self.mixer.blend(Heading, chapter=chapter)
        sub_heading = self.mixer.blend(SubHeading, heading=heading)
        commodity = self.mixer.blend(Commodity, parent_subheading=sub_heading)
        regulation = self.mixer.blend(RegulationGroup, sections=section)

        self.assertEqual(section.regulationgroup_set.count(), 1)
        self.assertFalse(chapter.regulationgroup_set.exists())
        self.assertFalse(heading.regulationgroup_set.exists())
        self.assertFalse(sub_heading.regulationgroup_set.exists())
        self.assertFalse(commodity.regulationgroup_set.exists())

        replicate_regulation_groups(section)

        self.assertEqual(section.regulationgroup_set.count(), 1)
        self.assertEqual(section.regulationgroup_set.first(), regulation)
        self.assertEqual(chapter.regulationgroup_set.count(), 1)
        self.assertEqual(chapter.regulationgroup_set.first(), regulation)
        self.assertEqual(heading.regulationgroup_set.count(), 1)
        self.assertEqual(heading.regulationgroup_set.first(), regulation)
        self.assertEqual(sub_heading.regulationgroup_set.count(), 1)
        self.assertEqual(sub_heading.regulationgroup_set.first(), regulation)
        self.assertEqual(commodity.regulationgroup_set.count(), 1)
        self.assertEqual(commodity.regulationgroup_set.first(), regulation)