Ejemplo n.º 1
0
 def setUp(self):
     from mockups.management.commands.mockups import Command
     self.command = Command()
     self.options = {
         'no_follow_fk': None,
         'no_follow_m2m': None,
         'generate_fk': None,
         'follow_m2m': None,
         'generate_m2m': None,
         'verbosity': '0',
         'use': '',
     }
     self.original_registry = mockups.helpers._registry
     mockups.helpers._registry = {}
Ejemplo n.º 2
0
    def setUp(self):
        from mockups.management.commands.mockups import Command

        self.command = Command()
        self.options = {
            "no_follow_fk": None,
            "no_follow_m2m": None,
            "generate_fk": None,
            "follow_m2m": None,
            "generate_m2m": None,
            "verbosity": "0",
            "use": "",
        }
        self.original_registry = mockups.helpers._registry
        mockups.helpers._registry = {}
Ejemplo n.º 3
0
class TestManagementCommand(TestCase):
    def setUp(self):
        from mockups.management.commands.mockups import Command

        self.command = Command()
        self.options = {
            "no_follow_fk": None,
            "no_follow_m2m": None,
            "generate_fk": None,
            "follow_m2m": None,
            "generate_m2m": None,
            "verbosity": "0",
            "use": "",
        }
        self.original_registry = mockups.helpers._registry
        mockups.helpers._registry = {}

    def tearDown(self):
        mockups.helpers._registry = self.original_registry

    def test_basic(self):
        models = ()
        # empty attributes are allowed
        self.command.handle(*models, **self.options)
        self.assertEqual(SimpleModel.objects.count(), 0)

        models = ("mockups_test.SimpleModel:1",)
        self.command.handle(*models, **self.options)
        self.assertEqual(SimpleModel.objects.count(), 1)

        models = ("mockups_test.SimpleModel:5",)
        self.command.handle(*models, **self.options)
        self.assertEqual(SimpleModel.objects.count(), 6)

    def test_generate_fk(self):
        models = ("mockups_test.DeepLinkModel2:1",)
        self.options["generate_fk"] = "related,related__related"
        self.command.handle(*models, **self.options)
        obj = DeepLinkModel2.objects.get()
        self.assertTrue(obj.related)
        self.assertTrue(obj.related.related)
        self.assertEqual(obj.related.related2, obj.related.related)

    def test_generate_fk_with_no_follow(self):
        models = ("mockups_test.DeepLinkModel2:1",)
        self.options["generate_fk"] = "related,related__related"
        self.options["no_follow_fk"] = True
        self.command.handle(*models, **self.options)
        obj = DeepLinkModel2.objects.get()
        self.assertTrue(obj.related)
        self.assertTrue(obj.related.related)
        self.assertEqual(obj.related.related2, None)

    def test_generate_fk_with_ALL(self):
        models = ("mockups_test.DeepLinkModel2:1",)
        self.options["generate_fk"] = "ALL"
        self.command.handle(*models, **self.options)
        obj = DeepLinkModel2.objects.get()
        self.assertTrue(obj.related)
        self.assertTrue(obj.related.related)
        self.assertTrue(obj.related.related2)
        self.assertTrue(obj.related.related != obj.related.related2)

    def test_no_follow_m2m(self):
        Mockup(SimpleModel).create(1)

        models = ("mockups_test.NullableFKModel:1",)
        self.options["no_follow_m2m"] = True
        self.command.handle(*models, **self.options)
        obj = NullableFKModel.objects.get()
        self.assertEqual(obj.m2m.count(), 0)

    def test_follow_m2m(self):
        Mockup(SimpleModel).create(10)
        Mockup(OtherSimpleModel).create(10)

        models = ("mockups_test.M2MModel:25",)
        self.options["follow_m2m"] = "m2m:3:3,secondm2m:0:10"
        self.command.handle(*models, **self.options)

        for obj in M2MModel.objects.all():
            self.assertEqual(obj.m2m.count(), 3)
            self.assertTrue(0 <= obj.secondm2m.count() <= 10)

    def test_generate_m2m(self):
        models = ("mockups_test.M2MModel:10",)
        self.options["generate_m2m"] = "m2m:1:1,secondm2m:2:5"
        self.command.handle(*models, **self.options)

        all_m2m, all_secondm2m = set(), set()
        for obj in M2MModel.objects.all():
            self.assertEqual(obj.m2m.count(), 1)
            self.assertTrue(2 <= obj.secondm2m.count() <= 5 or obj.secondm2m.count() == 0)
            all_m2m.update(obj.m2m.all())
            all_secondm2m.update(obj.secondm2m.all())
        self.assertEqual(all_m2m, set(SimpleModel.objects.all()))
        self.assertEqual(all_secondm2m, set(OtherSimpleModel.objects.all()))

    def test_using_registry(self):
        mockups.register(SimpleModel, SimpleMockup)
        models = ("mockups_test.SimpleModel:10",)
        self.command.handle(*models, **self.options)
        for obj in SimpleModel.objects.all():
            self.assertEqual(obj.name, "foo")
Ejemplo n.º 4
0
class TestManagementCommand(TestCase):
    def setUp(self):
        from mockups.management.commands.mockups import Command
        self.command = Command()
        self.options = {
            'no_follow_fk': None,
            'no_follow_m2m': None,
            'generate_fk': None,
            'follow_m2m': None,
            'generate_m2m': None,
            'verbosity': '0',
            'use': '',
        }
        self.original_registry = mockups.helpers._registry
        mockups.helpers._registry = {}

    def tearDown(self):
        mockups.helpers._registry = self.original_registry

    def test_basic(self):
        models = ()
        # empty attributes are allowed
        self.command.handle(*models, **self.options)
        self.assertEqual(SimpleModel.objects.count(), 0)

        models = ('mockups_test.SimpleModel:1',)
        self.command.handle(*models, **self.options)
        self.assertEqual(SimpleModel.objects.count(), 1)

        models = ('mockups_test.SimpleModel:5',)
        self.command.handle(*models, **self.options)
        self.assertEqual(SimpleModel.objects.count(), 6)

    def test_generate_fk(self):
        models = ('mockups_test.DeepLinkModel2:1',)
        self.options['generate_fk'] = 'related,related__related'
        self.command.handle(*models, **self.options)
        obj = DeepLinkModel2.objects.get()
        self.assertTrue(obj.related)
        self.assertTrue(obj.related.related)
        self.assertEqual(obj.related.related2, obj.related.related)

    def test_generate_fk_with_no_follow(self):
        models = ('mockups_test.DeepLinkModel2:1',)
        self.options['generate_fk'] = 'related,related__related'
        self.options['no_follow_fk'] = True
        self.command.handle(*models, **self.options)
        obj = DeepLinkModel2.objects.get()
        self.assertTrue(obj.related)
        self.assertTrue(obj.related.related)
        self.assertEqual(obj.related.related2, None)

    def test_generate_fk_with_ALL(self):
        models = ('mockups_test.DeepLinkModel2:1',)
        self.options['generate_fk'] = 'ALL'
        self.command.handle(*models, **self.options)
        obj = DeepLinkModel2.objects.get()
        self.assertTrue(obj.related)
        self.assertTrue(obj.related.related)
        self.assertTrue(obj.related.related2)
        self.assertTrue(obj.related.related != obj.related.related2)

    def test_no_follow_m2m(self):
        Mockup(SimpleModel).create(1)

        models = ('mockups_test.NullableFKModel:1',)
        self.options['no_follow_m2m'] = True
        self.command.handle(*models, **self.options)
        obj = NullableFKModel.objects.get()
        self.assertEqual(obj.m2m.count(), 0)

    def test_follow_m2m(self):
        Mockup(SimpleModel).create(10)
        Mockup(OtherSimpleModel).create(10)

        models = ('mockups_test.M2MModel:25',)
        self.options['follow_m2m'] = 'm2m:3:3,secondm2m:0:10'
        self.command.handle(*models, **self.options)

        for obj in M2MModel.objects.all():
            self.assertEqual(obj.m2m.count(), 3)
            self.assertTrue(0 <= obj.secondm2m.count() <= 10)

    def test_generate_m2m(self):
        models = ('mockups_test.M2MModel:10',)
        self.options['generate_m2m'] = 'm2m:1:1,secondm2m:2:5'
        self.command.handle(*models, **self.options)

        all_m2m, all_secondm2m = set(), set()
        for obj in M2MModel.objects.all():
            self.assertEqual(obj.m2m.count(), 1)
            self.assertTrue(
                2 <= obj.secondm2m.count() <= 5 or
                obj.secondm2m.count() == 0)
            all_m2m.update(obj.m2m.all())
            all_secondm2m.update(obj.secondm2m.all())
        self.assertEqual(all_m2m, set(SimpleModel.objects.all()))
        self.assertEqual(all_secondm2m, set(OtherSimpleModel.objects.all()))

    def test_using_registry(self):
        mockups.register(SimpleModel, SimpleMockup)
        models = ('mockups_test.SimpleModel:10',)
        self.command.handle(*models, **self.options)
        for obj in SimpleModel.objects.all():
            self.assertEqual(obj.name, 'foo')