Exemplo n.º 1
0
class ModelTrackerTests(ModelTrackerTestCase, ModelTrackerCommonTests):
    def setUp(self):
        self.instance = Tracked()
        self.tracker = self.instance.tracker

    def test_descriptor(self):
        self.assertTrue(isinstance(Tracked.tracker, ModelTracker))

    def test_post_save_has_changed(self):
        self.update_instance(name='retro', number=4)
        self.assertHasChanged(name=False, number=False)
        self.instance.name = 'new age'
        self.assertHasChanged(name=True, number=False)
        self.instance.number = 8
        self.assertHasChanged(name=True, number=True)
        self.instance.name = 'retro'
        self.assertHasChanged(name=False, number=True)

    def test_post_save_previous(self):
        self.update_instance(name='retro', number=4)
        self.instance.name = 'new age'
        self.assertPrevious(name='retro', number=4)

    def test_post_save_changed(self):
        self.update_instance(name='retro', number=4)
        self.assertChanged()
        self.instance.name = 'new age'
        self.assertChanged(name='retro')
        self.instance.number = 8
        self.assertChanged(name='retro', number=4)
        self.instance.name = 'retro'
        self.assertChanged(number=4)

    def test_current(self):
        self.assertCurrent(id=None, name='', number=None)
        self.instance.name = 'new age'
        self.assertCurrent(id=None, name='new age', number=None)
        self.instance.number = 8
        self.assertCurrent(id=None, name='new age', number=8)
        self.instance.save()
        self.assertCurrent(id=self.instance.id, name='new age', number=8)
Exemplo n.º 2
0
 def setUp(self):
     self.instance = Tracked()
     self.tracker = self.instance.tracker
Exemplo n.º 3
0
class ModelTrackerTests(ModelTrackerTestCase, ModelTrackerCommonTests):
    def setUp(self):
        self.instance = Tracked()
        self.tracker = self.instance.tracker

    def test_descriptor(self):
        self.assertTrue(isinstance(Tracked.tracker, ModelTracker))

    def test_first_save(self):
        self.assertHasChanged(name=True, number=True)
        self.assertPrevious(name=None, number=None)
        self.assertCurrent(name='', number=None, id=None)
        self.assertChanged()
        self.instance.name = 'retro'
        self.instance.number = 4
        self.assertHasChanged(name=True, number=True)
        self.assertPrevious(name=None, number=None)
        self.assertCurrent(name='retro', number=4, id=None)
        self.assertChanged()
        # Django 1.4 doesn't have update_fields
        if django.VERSION >= (1, 5, 0):
            self.instance.save(update_fields=[])
            self.assertHasChanged(name=True, number=True)
            self.assertPrevious(name=None, number=None)
            self.assertCurrent(name='retro', number=4, id=None)
            self.assertChanged()
            self.assertRaises(ValueError,
                              self.instance.save,
                              update_fields=['number'])

    def test_post_save_has_changed(self):
        self.update_instance(name='retro', number=4)
        self.assertHasChanged(name=False, number=False)
        self.instance.name = 'new age'
        self.assertHasChanged(name=True, number=False)
        self.instance.number = 8
        self.assertHasChanged(name=True, number=True)
        self.instance.name = 'retro'
        self.assertHasChanged(name=False, number=True)

    def test_post_save_previous(self):
        self.update_instance(name='retro', number=4)
        self.instance.name = 'new age'
        self.assertPrevious(name='retro', number=4)

    def test_post_save_changed(self):
        self.update_instance(name='retro', number=4)
        self.assertChanged()
        self.instance.name = 'new age'
        self.assertChanged(name='retro')
        self.instance.number = 8
        self.assertChanged(name='retro', number=4)
        self.instance.name = 'retro'
        self.assertChanged(number=4)

    def test_current(self):
        self.assertCurrent(id=None, name='', number=None)
        self.instance.name = 'new age'
        self.assertCurrent(id=None, name='new age', number=None)
        self.instance.number = 8
        self.assertCurrent(id=None, name='new age', number=8)
        self.instance.save()
        self.assertCurrent(id=self.instance.id, name='new age', number=8)

    def test_update_fields(self):
        # Django 1.4 doesn't have update_fields
        if django.VERSION >= (1, 5, 0):
            self.update_instance(name='retro', number=4)
            self.assertChanged()
            self.instance.name = 'new age'
            self.instance.number = 8
            self.assertChanged(name='retro', number=4)
            self.instance.save(update_fields=[])
            self.assertChanged(name='retro', number=4)
            self.instance.save(update_fields=['name'])
            in_db = Tracked.objects.get(id=self.instance.id)
            self.assertEqual(in_db.name, self.instance.name)
            self.assertNotEqual(in_db.number, self.instance.number)
            self.assertChanged(number=4)
            self.instance.save(update_fields=['number'])
            self.assertChanged()
            in_db = Tracked.objects.get(id=self.instance.id)
            self.assertEqual(in_db.name, self.instance.name)
            self.assertEqual(in_db.number, self.instance.number)
Exemplo n.º 4
0
 def setUp(self):
     self.instance = Tracked()
     self.tracker = self.instance.tracker
Exemplo n.º 5
0
class ModelTrackerTests(ModelTrackerTestCase, ModelTrackerCommonTests):
    def setUp(self):
        self.instance = Tracked()
        self.tracker = self.instance.tracker

    def test_descriptor(self):
        self.assertTrue(isinstance(Tracked.tracker, ModelTracker))

    def test_first_save(self):
        self.assertHasChanged(name=True, number=True)
        self.assertPrevious(name=None, number=None)
        self.assertCurrent(name="", number=None, id=None)
        self.assertChanged()
        self.instance.name = "retro"
        self.instance.number = 4
        self.assertHasChanged(name=True, number=True)
        self.assertPrevious(name=None, number=None)
        self.assertCurrent(name="retro", number=4, id=None)
        self.assertChanged()
        # Django 1.4 doesn't have update_fields
        if django.VERSION >= (1, 5, 0):
            self.instance.save(update_fields=[])
            self.assertHasChanged(name=True, number=True)
            self.assertPrevious(name=None, number=None)
            self.assertCurrent(name="retro", number=4, id=None)
            self.assertChanged()
            self.assertRaises(ValueError, self.instance.save, update_fields=["number"])

    def test_post_save_has_changed(self):
        self.update_instance(name="retro", number=4)
        self.assertHasChanged(name=False, number=False)
        self.instance.name = "new age"
        self.assertHasChanged(name=True, number=False)
        self.instance.number = 8
        self.assertHasChanged(name=True, number=True)
        self.instance.name = "retro"
        self.assertHasChanged(name=False, number=True)

    def test_post_save_previous(self):
        self.update_instance(name="retro", number=4)
        self.instance.name = "new age"
        self.assertPrevious(name="retro", number=4)

    def test_post_save_changed(self):
        self.update_instance(name="retro", number=4)
        self.assertChanged()
        self.instance.name = "new age"
        self.assertChanged(name="retro")
        self.instance.number = 8
        self.assertChanged(name="retro", number=4)
        self.instance.name = "retro"
        self.assertChanged(number=4)

    def test_current(self):
        self.assertCurrent(id=None, name="", number=None)
        self.instance.name = "new age"
        self.assertCurrent(id=None, name="new age", number=None)
        self.instance.number = 8
        self.assertCurrent(id=None, name="new age", number=8)
        self.instance.save()
        self.assertCurrent(id=self.instance.id, name="new age", number=8)

    def test_update_fields(self):
        # Django 1.4 doesn't have update_fields
        if django.VERSION >= (1, 5, 0):
            self.update_instance(name="retro", number=4)
            self.assertChanged()
            self.instance.name = "new age"
            self.instance.number = 8
            self.assertChanged(name="retro", number=4)
            self.instance.save(update_fields=[])
            self.assertChanged(name="retro", number=4)
            self.instance.save(update_fields=["name"])
            in_db = Tracked.objects.get(id=self.instance.id)
            self.assertEqual(in_db.name, self.instance.name)
            self.assertNotEqual(in_db.number, self.instance.number)
            self.assertChanged(number=4)
            self.instance.save(update_fields=["number"])
            self.assertChanged()
            in_db = Tracked.objects.get(id=self.instance.id)
            self.assertEqual(in_db.name, self.instance.name)
            self.assertEqual(in_db.number, self.instance.number)