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")
def test_many_to_many_through(self): mixer = Mixer() pointa = mixer.blend('django_app.pointa') self.assertTrue(pointa.other.all()) pointb = mixer.blend('pointb') pointa = mixer.blend('pointa', other=pointb) self.assertEqual(list(pointa.other.all()), [pointb])
def test_register(self): mixer = Mixer() mixer.register(Rabbit, { 'title': lambda: 'Mr. Rabbit' }) rabbit = mixer.blend(Rabbit) self.assertEqual(rabbit.title, 'Mr. Rabbit')
def setUp(self): mixer = Mixer() self.country = mixer.blend( Country, name="Test Country", country_code="XT", trade_agreement_title="The White-Gold Concordat", ) self.gsp_country = mixer.blend( Country, name="Test Country 2", country_code="BX", scenario="TEST_TA", ) self.commodity = mixer.blend(Commodity, commodity_code="0100000000")
def test_random_fields(self): mixer = Mixer(fake=False) rabbit = mixer.blend('django_app.rabbit') self.assertTrue(isinstance(rabbit, Rabbit)) self.assertTrue(rabbit.id) self.assertTrue(rabbit.pk) self.assertEqual(rabbit.pk, 1) self.assertEqual(len(rabbit.title), 16) self.assertTrue(isinstance(rabbit.active, bool)) self.assertTrue(isinstance(rabbit.created_at, datetime.date)) self.assertTrue(isinstance(rabbit.updated_at, datetime.datetime)) self.assertTrue(isinstance(rabbit.opened_at, datetime.time)) self.assertTrue('@' in rabbit.email) self.assertTrue(rabbit.description) self.assertTrue(rabbit.some_field) self.assertTrue(rabbit.money) hat = mixer.blend('django_app.hat', color=mixer.random) self.assertTrue(hat.color in ('RD', 'GRN', 'BL'))
def test_fields(self): mixer = Mixer() rabbit = mixer.blend('django_app.rabbit') self.assertTrue(isinstance(rabbit, Rabbit)) self.assertTrue(rabbit.id) self.assertTrue(rabbit.pk) self.assertEqual(rabbit.pk, 1) self.assertEqual(len(rabbit.title), 16) self.assertTrue(isinstance(rabbit.active, bool)) self.assertTrue(isinstance(rabbit.created_at, datetime.date)) self.assertTrue(isinstance(rabbit.updated_at, datetime.datetime)) self.assertTrue(isinstance(rabbit.opened_at, datetime.time)) self.assertTrue('@' in rabbit.email) self.assertTrue(rabbit.speed) self.assertTrue(rabbit.description) self.assertEqual(rabbit.picture.read(), b'pylama\n') rabbit = mixer.blend('rabbit') self.assertTrue(rabbit)
def test_random_fields(self): from .django_app.models import Rabbit mixer = Mixer(fake=False) rabbit = mixer.blend('django_app.rabbit') self.assertTrue(isinstance(rabbit, Rabbit)) self.assertTrue(rabbit.id) self.assertTrue(rabbit.pk) self.assertEqual(rabbit.pk, 1) self.assertEqual(len(rabbit.title), 16) self.assertTrue(isinstance(rabbit.active, bool)) self.assertTrue(isinstance(rabbit.created_at, datetime.date)) self.assertTrue(isinstance(rabbit.updated_at, datetime.datetime)) self.assertTrue(isinstance(rabbit.opened_at, datetime.time)) self.assertTrue('@' in rabbit.email) self.assertTrue(rabbit.description) self.assertTrue(rabbit.some_field) self.assertTrue(rabbit.money) hat = mixer.blend('django_app.hat', color=mixer.random) self.assertTrue(hat.color in ('RD', 'GRN', 'BL'))
def test_fields(self): from .django_app.models import Rabbit mixer = Mixer() rabbit = mixer.blend('django_app.rabbit') self.assertTrue(isinstance(rabbit, Rabbit)) self.assertTrue(rabbit.id) self.assertTrue(rabbit.pk) self.assertEqual(rabbit.pk, 1) self.assertEqual(len(rabbit.title), 16) self.assertTrue(isinstance(rabbit.active, bool)) self.assertTrue(isinstance(rabbit.created_at, datetime.date)) self.assertTrue(isinstance(rabbit.updated_at, datetime.datetime)) self.assertTrue(isinstance(rabbit.opened_at, datetime.time)) self.assertTrue('@' in rabbit.email) self.assertTrue(rabbit.speed) self.assertTrue(rabbit.description) self.assertEqual(rabbit.picture.read(), b'pylama\n') rabbit = mixer.blend('rabbit') self.assertTrue(rabbit)
def test_check_countries_consistency_via_command(self): mixer = Mixer() country_missing_roo = mixer.blend( Country, name="Test Country 2", country_code="BX", scenario="TEST_TA", ) country_missing_roo.save() with self.assertLogs("rules_of_origin.ingest.importer", level="ERROR") as warning_log: call_command("check_rules_of_origin") # Assert 'BX - Test Country 2' is in the error message to sentry self.assertIn("BX - Test Country 2", str(warning_log.output))
def setUp(self): Country.objects.all().delete() mixer = Mixer() self.country = mixer.blend( Country, name="Test Country", country_code="XT", has_eu_trade_agreement=False, scenario="STICKER_TRADES", content_url="gotgotgotneed.com", trade_agreement_title="The Very Agreeable Agreement", trade_agreement_type="Football Sticker Swap", roo_guidance_url="igetfabianbartezineverypackforgodssake.com", ) self.country.save()
def test_fields(self): from .django_app.models import Rabbit mixer = Mixer() rabbit = mixer.blend('django_app.rabbit') self.assertTrue(isinstance(rabbit, Rabbit)) self.assertTrue(rabbit.id) self.assertTrue(rabbit.pk) self.assertEqual(rabbit.pk, 1) self.assertEqual(len(rabbit.title), 16) self.assertTrue(isinstance(rabbit.active, bool)) self.assertTrue(isinstance(rabbit.created_at, datetime.date)) self.assertTrue(isinstance(rabbit.updated_at, datetime.datetime)) self.assertTrue(isinstance(rabbit.opened_at, datetime.time)) self.assertTrue('@' in rabbit.email) self.assertTrue(rabbit.speed) self.assertTrue(rabbit.description)
def create_object(model, *, data=None, commit=True): if data is None: data = {} mixer = Mixer(commit=commit) return mixer.blend(model, **data)
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())
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)
class BaseAdminTestCase: fixtures = ['bcm.json', 'groups.json'] """ We have to inherit from "object" here cause TestCase is something like singleton and if TestCase will be specified here, base class will be tested too with errors """ url_prefix = None # 'mo_admin' / 'go_admin' group_name = None # 'MO Admins' / 'GO Admins' mo_admin_instance = None main_user_credentials = None group = None request_factory = None mixer = None def setUp(self): self.mo_admin_instance = MemberOrganisationOwnerAdmin( CompanyOrganisationOwner, AdminSite() ) self.main_user_credentials = { 'username': '******', 'password': '******', } self.group = Group.objects.get(name=self.group_name) self.request_factory = RequestFactory() self.mixer = Mixer(locale='en') def create_required_instances(self): user11 = self.create_django_user(self.main_user_credentials) user12 = self.create_django_user() user21 = self.create_django_user() mo1 = self.mixer.blend( MemberOrganisation, name='GS1 France', country=Country.objects.get(name='France') ) mo2 = self.mixer.blend( MemberOrganisation, name='GS1 Belgium', country=Country.objects.get(name='Belgium') ) mo1_user1 = self.mixer.blend( MemberOrganisationUser, organization=mo1, user=user11, is_admin=True ) mo1_user2 = self.mixer.blend( MemberOrganisationUser, organization=mo1, user=user12, is_admin=True ) mo2_user1 = self.mixer.blend(MemberOrganisationUser, organization=mo2, user=user21) mo_owner1 = self.mixer.blend( MemberOrganisationOwner, organization_user=mo1_user1, organization=mo1 ) mo_owner2 = self.mixer.blend( MemberOrganisationOwner, organization_user=mo2_user1, organization=mo2 ) co_fields = self.get_force_random_fields_for_mixer( CompanyOrganisation, company='CO1 Test', member_organisation=mo1, country=mo1.country, active=True, excluded_fields=['created'] ) co1 = self.mixer.blend(CompanyOrganisation, **co_fields) co_fields = self.get_force_random_fields_for_mixer( CompanyOrganisation, company='CO2 Test', member_organisation=mo2, country=mo2.country, active=True, excluded_fields=['created'] ) co2 = self.mixer.blend(CompanyOrganisation, **co_fields) co1_user1 = self.mixer.blend( CompanyOrganisationUser, user=user11, organization=co1, is_admin=True, ) co2_user1 = self.mixer.blend( CompanyOrganisationUser, user=user21, organization=co2, is_admin=True, ) co1_owner = self.mixer.blend( CompanyOrganisationOwner, organization_user=co1_user1, organization=co1 ) co2_owner = self.mixer.blend( CompanyOrganisationOwner, organization_user=co2_user1, organization=co2 ) co1_prefix1 = self.mixer.blend(Prefix, company_organisation=co1, member_organisation=mo1) co2_prefix1 = self.mixer.blend(Prefix, company_organisation=co2, member_organisation=mo2) co1_log1 = self.mixer.blend(Log) co2_log1 = self.mixer.blend(Log) return { variable_name: instance for variable_name, instance in locals().items() if isinstance(instance, models.Model) } def create_django_user(self, user_credentials=None, add_to_group=True): if user_credentials: user = User(**user_credentials) user.set_password(self.main_user_credentials['password']) user.save() else: user = self.mixer.blend(User) if add_to_group: user.groups.add(self.group) return user def get_force_random_fields_for_mixer(self, model_class, excluded_fields=None, **kwargs): """ Mixer sets default values from model, but it raises errors when blank=False and default='' at the same time, so we have to force fields to be set by random values :param model_class: :param excluded_fields: ('id', ) # prevent to randomize some fields :param kwargs: field_name=instance or value # predefined field values :return: dict of field_names: random/predefined values """ excluded_fields = {'id'} | set(excluded_fields or []) co_fields = { field.name: self.mixer.RANDOM for field in model_class._meta.fields if field.name not in excluded_fields } for field_name, field_value in kwargs.items(): co_fields[field_name] = field_value return co_fields @classmethod def model_instance_to_post_data(cls, instance): data = serializers.serialize('json', [instance, ]) post_data = json.loads(data) post_data = post_data[0]['fields'] for field in instance._meta.fields: # splitting date and time for admin forms if isinstance(field, DateTimeField): field_datetime = parse_datetime(post_data[field.name]) post_data[f'{field.name}_0'] = str(field_datetime.date()) post_data[f'{field.name}_1'] = str(field_datetime.time()) del post_data[field.name] if not field.serialize and isinstance(field, models.ForeignKey): post_data[field.name] = getattr(instance, field.name).pk return post_data def get_urls_by_types(self, url_types, excluded_apps=None): """ Filters url list by types like: "changelist", "add", "change", "delete" """ excluded_apps = excluded_apps if excluded_apps else [] url_names = list() for url in self.mo_admin_instance.get_urls(): if not url.name: continue if not url.name.startswith(self.url_prefix): continue denied_app_urls = [ app_name for app_name in excluded_apps if url.name.startswith(f'{self.url_prefix}_{app_name}') ] if denied_app_urls: continue if any(url_type in url.name for url_type in url_types): url_names.append(reverse(f'admin:{url.name}')) return url_names def get_url_for_model(self, model_class, action, pk=None): app_label = model_class._meta.app_label model_name = model_class._meta.model_name return ( reverse( f'admin:{self.url_prefix}_{app_label}_{model_name}_{action}', args=(pk,) if pk else None)) def test_changelist_add_urls_non_authorized_user(self): """ Non authorized users must receive 302 http response to login page """ url_names = self.get_urls_by_types(['changelist', 'add'], excluded_apps=['auth']) for url_name in url_names: response = self.client.get(url_name) self.assertEqual( response.status_code, 302, f'URL "{url_name}" should be denied for non authorized users' ) def test_changelist_add_urls_authorized_user(self): """ Authorized and authenticated users must receive 200 http response """ login_result = self.client.login(**self.main_user_credentials) self.assertTrue(login_result, 'Can\'t login to with test user credentials') url_names = self.get_urls_by_types( ['changelist', 'add'], excluded_apps=['auth','member_organisations',] ) for url_name in url_names: response = self.client.get(url_name) self.assertEqual( response.status_code, 200, f'URL "{url_name}" should be allowed for authorized/authenticated users' ) def perform_adding_tests(self, models_config): for model_class, model_conf in models_config.items(): if not model_conf: continue predefined_fields = model_conf.get('predefined_fields', {}) model_fields = self.get_force_random_fields_for_mixer( model_class, excluded_fields=['created'], **predefined_fields ) model_instance = self.mixer.blend(model_class, **model_fields) model_instance.delete() # it seems mixer doesn't care about his commit=False post_data = self.model_instance_to_post_data(model_instance) self.assertFalse( model_class.objects.filter(**predefined_fields).exists(), f'the {model_class} instance with predefined data: ' f'"{predefined_fields}" mustn\'t be in the test database before submitting' ) # instance creating is here model_add_url = self.get_url_for_model(model_class, 'add') response = self.client.post(model_add_url, data=post_data) self.assertEqual( response.status_code, 302, f'Should be a redirect after an instance submitting, model: "{model_class}"' ) self.assertEqual( response.url, self.get_url_for_model(model_class, 'changelist'), f'Wrong redirect url after an instace adding, model: "{model_class}"' ) self.assertTrue( model_class.objects.filter(**predefined_fields).exists(), f'CompanyOrganisation "{model_instance}" must be ' f'in the test database after submitting' ) def perform_deleting_tests(self, models_config): for model_class, model_conf in models_config.items(): if 'model_instance' not in model_conf: continue model_instance = model_conf['model_instance'] post_data = {'post': 'yes'} co_url = self.get_url_for_model(model_class, 'delete', model_instance.pk) # instance creating is here response = self.client.post(co_url, data=post_data) self.assertEqual( response.status_code, 302, f'Should be a redirect after an instance submitting, model: "{model_class}"' ) self.assertEqual( response.url, self.get_url_for_model(model_class, 'changelist'), f'Wrong redirect url after an instace removing for {model_class}' ) self.assertFalse( model_class.objects.filter(pk=model_instance.pk).exists(), f'"{model_instance}" must be removed here already' )
def test_relation(self): mixer = Mixer() hole = mixer.blend('django_app.hole', title='hole4') self.assertEqual(hole.owner.pk, 1) self.assertEqual(hole.title, 'hole4') hat = mixer.blend('django_app.hat') self.assertFalse(hat.owner) self.assertEqual(hat.brend, 'wood') self.assertTrue(hat.color in ('RD', 'GRN', 'BL')) hat = mixer.blend('django_app.hat', owner=mixer.select) self.assertTrue(hat.owner) silk = mixer.blend('django_app.silk') self.assertFalse(silk.hat.owner) silk = mixer.blend('django_app.silk', hat__owner__title='booble') self.assertTrue(silk.hat.owner) self.assertEqual(silk.hat.owner.title, 'booble') door = mixer.blend('django_app.door', hole__title='flash', hole__size=244) self.assertTrue(door.hole.owner) self.assertEqual(door.hole.title, 'flash') self.assertEqual(door.hole.size, 244) door = mixer.blend('django_app.door') self.assertNotEqual(door.hole.title, 'flash') num = mixer.blend('django_app.number', doors=[door]) self.assertEqual(num.doors.get(), door) num = mixer.blend('django_app.number') self.assertEqual(num.doors.count(), 1) num = mixer.blend('django_app.number', doors__size=42) self.assertEqual(num.doors.all()[0].size, 42) tag = mixer.blend('django_app.tag', customer=mixer.random) self.assertTrue(tag.customer)
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]))
from rest_framework.views import APIView from faker import Faker from good_spot.places.models import Place, PlaceType fake = Faker() mixer = Mixer(commit=False) mocked_objects_count = 10 place_rating_choice = list(range(1, 5)) place_types = [] place_type_choice = ["bar", "restaurant", "night club"] pl_i = 1 for pl_type in place_type_choice: place_type = mixer.blend(PlaceType) place_type.id = pl_i place_type.name = pl_type place_types.append(place_type) pl_i += 1 def make_place(place_id): place = mixer.blend(Place) place.id = place_id place.name = fake.name() place.google_rating = fake.random_element(place_rating_choice) random_point = Point( random.uniform(50.4, 50.5), random.uniform(30.44, 30.62) )
class TestEventSourceSafety(GoogleCalendarTestCase): def setUp(self): super().setUp() self.src.events = [] self.safe_mixer = Mixer(commit=False) def test_is_safe_by_default(self): """ By default all should be safe """ self.assertTrue(self.src._ExternalEventSource__is_safe()) def test_is_safe_with_10_events(self): """ Try to replace 10 events by 8 events """ for i in range(0, 10): mixer.blend(ExternalEvent, teacher=self.teacher, src=self.src) for i in range(0, 8): # create 8 non-saved events self.src.events.append( self.safe_mixer.blend(ExternalEvent, teacher=self.teacher, src=self.src)) self.assertTrue(self.src._ExternalEventSource__is_safe()) def test_is_safe_to_delete_10_recurring_event(self): """ Try to replace 12 events (10 of them recurring) with 2 events """ mixer.blend(ExternalEvent, teacher=self.teacher, src=self.src) # some event parent_event = mixer.blend( ExternalEvent, teacher=self.teacher, src=self.src) # this event will be parent to 10 others for i in range(0, 10): mixer.blend(ExternalEvent, teacher=self.teacher, src=self.src, parent=parent_event) for i in range(0, 2): # create 2 non-saved events self.src.events.append( self.safe_mixer.blend(ExternalEvent, teacher=self.teacher, src=self.src)) self.assertTrue(self.src._ExternalEventSource__is_safe()) def test_unsafe_with_zero_events(self): """ Try to replace 10 events by 0 events """ for i in range(0, 10): mixer.blend(ExternalEvent, teacher=self.teacher, src=self.src) self.assertFalse(self.src._ExternalEventSource__is_safe()) def test_unsafe_with_more_then_two_times_difference(self): """ Try to replace 10 events by 3 events """ for i in range(0, 10): mixer.blend(ExternalEvent, teacher=self.teacher, src=self.src) for i in range(0, 3): # create 3 non-saved events self.src.events.append( self.safe_mixer.blend(ExternalEvent, teacher=self.teacher, src=self.src)) self.assertFalse(self.src._ExternalEventSource__is_safe())