Skip to content

Commit

Permalink
Merge pull request #140 from uclouvain/feature/139
Browse files Browse the repository at this point in the history
#139 unit tests 'uuid' field in SerializableModel
  • Loading branch information
glamarca committed Feb 15, 2017
2 parents 16046cc + e795465 commit 8b3e7c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
9 changes: 6 additions & 3 deletions models/serializable_model.py
Expand Up @@ -162,8 +162,11 @@ def serialize(obj, last_syncs=None):
for f in obj.__class__._meta.fields:
attribute = getattr(obj, f.name)
if f.is_relation:
if isinstance(attribute, SerializableModel):
dict[f.name] = serialize(attribute, last_syncs=last_syncs)
try:
if attribute and getattr(attribute, 'uuid'):
dict[f.name] = serialize(attribute, last_syncs=last_syncs)
except AttributeError:
pass
else:
try:
json.dumps(attribute)
Expand Down Expand Up @@ -222,7 +225,7 @@ def persist(structure):
else:
del kwargs['id']
obj = model_class(**kwargs)
super(SerializableModel, obj).save()
super(SerializableModel, obj).save(force_insert=True)
obj_id = obj.id
if obj_id:
return obj_id
Expand Down
7 changes: 6 additions & 1 deletion tests/models/test_serializable_model.py
Expand Up @@ -28,7 +28,7 @@
from osis_common.models.exception import MultipleModelsSerializationException
from osis_common.models.serializable_model import serialize_objects, format_data_for_migration
from osis_common.tests.models_for_tests.serializable_tests_models import ModelWithoutUser, \
ModelWithUser
ModelWithUser, ModelNotSerializable


class TestSerializeObject(TestCase):
Expand Down Expand Up @@ -65,6 +65,11 @@ def test_serialization_without_user(self):
self.assertEqual('Without User', serialized_fields.get('name'))
self.assertEqual('tests.modelwithoutuser', serialized_model)

def test_contains_uuid_field(self):
self.assertTrue(getattr(self.model_with_user, 'uuid'))
obj = ModelNotSerializable()
self.assertRaises(AttributeError, getattr, obj, 'uuid')


class TestFormatDataForMigration(TestCase):

Expand Down
7 changes: 6 additions & 1 deletion tests/models_for_tests/serializable_tests_models.py
Expand Up @@ -26,6 +26,7 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.fields import CharField
from osis_common.models.serializable_model import SerializableModel
from django.db import models


class ModelWithUser(SerializableModel):
Expand Down Expand Up @@ -68,4 +69,8 @@ def find_by_id(cls, id):
try:
return ModelWithoutUser.objects.get(id=id)
except ObjectDoesNotExist:
return None
return None


class ModelNotSerializable(models.Model):
pass

0 comments on commit 8b3e7c5

Please sign in to comment.