Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge

  • Loading branch information...
commit 28db63ff6de8b66b550b8ebee3e771ff83a1e17a 2 parents e305130 + bce2f2a
@jonashaag jonashaag authored
Showing with 34 additions and 8 deletions.
  1. +3 −1 djangotoolbox/fields.py
  2. +31 −7 djangotoolbox/tests.py
View
4 djangotoolbox/fields.py
@@ -275,6 +275,8 @@ def get_db_prep_value(self, (embedded_instance, value_list), **kwargs):
if self.embedded_model is None:
values.update({'_module' : embedded_instance.__class__.__module__,
'_model' : embedded_instance.__class__.__name__})
+ # This instance will exist in the db very soon.
+ embedded_instance._entity_exists = True
return values
# TODO/XXX: Remove this once we have a cleaner solution
@@ -301,4 +303,4 @@ def to_python(self, values):
data[str(field.attname)] = values[field.column]
except KeyError:
pass
- return model(**data)
+ return model(__entity_exists=True, **data)
View
38 djangotoolbox/tests.py
@@ -234,6 +234,9 @@ def assertEqualDatetime(self, d1, d2):
""" Compares d1 and d2, ignoring microseconds """
self.assertEqual(d1.replace(microsecond=0), d2.replace(microsecond=0))
+ def assertNotEqualDatetime(self, d1, d2):
+ self.assertNotEqual(d1.replace(microsecond=0), d2.replace(microsecond=0))
+
def _simple_instance(self):
EmbeddedModelFieldModel.objects.create(simple=EmbeddedModel(someint='5'))
return EmbeddedModelFieldModel.objects.get()
@@ -251,22 +254,41 @@ def test_simple(self):
instance = EmbeddedModelFieldModel.objects.get()
self.assertEqual(instance.simple.id, instance.id)
- def test_pre_save(self, field='simple'):
+ def _test_pre_save(self, instance, get_field):
# Make sure field.pre_save is called for embedded objects
from time import sleep
- instance = EmbeddedModelFieldModel.objects.create(**{field: EmbeddedModel()})
- auto_now = getattr(instance, field).auto_now
- auto_now_add = getattr(instance, field).auto_now_add
+ instance.save()
+ auto_now = get_field(instance).auto_now
+ auto_now_add = get_field(instance).auto_now_add
self.assertNotEqual(auto_now, None)
self.assertNotEqual(auto_now_add, None)
+
+ sleep(1) # FIXME
instance.save()
+ self.assertNotEqualDatetime(get_field(instance).auto_now,
+ get_field(instance).auto_now_add)
+
instance = EmbeddedModelFieldModel.objects.get()
+ instance.save()
# auto_now_add shouldn't have changed now, but auto_now should.
- self.assertEqualDatetime(getattr(instance, field).auto_now_add, auto_now_add)
- self.assertGreater(getattr(instance, field).auto_now, auto_now)
+ self.assertEqualDatetime(get_field(instance).auto_now_add, auto_now_add)
+ self.assertGreater(get_field(instance).auto_now, auto_now)
+
+ def test_pre_save(self):
+ obj = EmbeddedModelFieldModel(simple=EmbeddedModel())
+ self._test_pre_save(obj, lambda instance: instance.simple)
def test_pre_save_untyped(self):
- self.test_pre_save(field='simple_untyped')
+ obj = EmbeddedModelFieldModel(simple_untyped=EmbeddedModel())
+ self._test_pre_save(obj, lambda instance: instance.simple_untyped)
+
+ def test_pre_save_in_list(self):
+ obj = EmbeddedModelFieldModel(untyped_list=[EmbeddedModel()])
+ self._test_pre_save(obj, lambda instance: instance.untyped_list[0])
+
+ def test_pre_save_in_dict(self):
+ obj = EmbeddedModelFieldModel(untyped_dict={'a': EmbeddedModel()})
+ self._test_pre_save(obj, lambda instance: instance.untyped_dict['a'])
def test_pre_save_list(self):
# Also make sure auto_now{,add} works for embedded object *lists*.
@@ -337,10 +359,12 @@ def test_foreignkey_in_embedded_object(self):
self.assertNotIn('some_relation', simple.__dict__)
self.assertIsInstance(simple.__dict__['some_relation_id'], type(obj.id))
self.assertIsInstance(simple.some_relation, DictModel)
+
EmbeddedModelFieldTest = unittest.skipIf(
not supports_dicts, "Backend doesn't support dicts")(
EmbeddedModelFieldTest)
+
class SignalTest(TestCase):
def test_post_save(self):
created = []
Please sign in to comment.
Something went wrong with that request. Please try again.