From e8a1b3a543ab7d8b303f54e3ce48681f6c1589e7 Mon Sep 17 00:00:00 2001 From: Hugo Rodger-Brown Date: Wed, 25 Nov 2015 12:35:15 +0000 Subject: Fix for failing test_django tests I've added a save_and_refresh function to allow objects to be saved and then updated from the database, replicating the refresh_from_db method that exists in Django 1.8, and wrapping in the save method. --- tests/test_django.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'tests') diff --git a/tests/test_django.py b/tests/test_django.py index 8646764..959b19c 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -31,9 +31,11 @@ if django_loaded and django.VERSION < (1, 7): # pragma: no cover pass # the refresh_from_db method only came in with 1.8, so in order to make this -# work will all supported versions we have our own function -def refresh_from_db(obj): - return obj.__class__.objects.get(id=obj.id) +# work will all supported versions we have our own function. +def save_and_refresh(obj): + """Saves an object, and refreshes from the database.""" + obj.save() + obj = obj.__class__.objects.get(id=obj.id) @unittest.skipIf(not django_loaded, "Django not installed") @@ -59,18 +61,14 @@ class DjangoFieldTestCase(unittest.TestCase): obj = models.PartialVersionModel() self.assertIsNone(obj.id) self.assertIsNone(obj.optional) - obj.save() - - # now retrieve from db - obj = refresh_from_db(obj) + save_and_refresh(obj) self.assertIsNotNone(obj.id) self.assertIsNone(obj.optional_spec) # now set to something that is not null spec = semantic_version.Spec('==0,!=0.2') obj.optional_spec = spec - obj.save() - obj = refresh_from_db(obj) + save_and_refresh(obj) self.assertEqual(obj.optional_spec, spec) def test_spec_save(self): @@ -79,18 +77,14 @@ class DjangoFieldTestCase(unittest.TestCase): obj = models.PartialVersionModel() self.assertIsNone(obj.id) self.assertIsNone(obj.optional_spec) - obj.save() - - # now retrieve from db - obj.refresh_from_db() + save_and_refresh(obj) self.assertIsNotNone(obj.id) self.assertIsNone(obj.optional_spec) # now set to something that is not null spec = semantic_version.Spec('==0,!=0.2') obj.optional_spec = spec - obj.save() - obj.refresh_from_db() + save_and_refresh(obj) self.assertEqual(obj.optional_spec, spec) def test_partial_spec(self): -- cgit v1.2.1