Skip to content

Commit

Permalink
Add support for non-parent_link one-to-one fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
charettes committed Jan 13, 2018
1 parent d3c9d84 commit 69fe503
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions seal/related.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def get_object(self, instance):
# the query from being performed.
if any(field in fields for field in deferred):
raise SealedObject('Cannot fetch related field %s on a sealed object.' % self.field.name)
else:
raise SealedObject('Cannot fetch related field %s on a sealed object.' % self.field.name)
return super(SealableForwardOneToOneDescriptor, self).get_object(instance)


Expand Down
4 changes: 4 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ class SeaLion(SealableModel):
class GreatSeaLion(SeaLion):
# TODO: add support for auto-generated o2os parent_link and non-parent link o2o.
sealion_ptr = models.OneToOneField(SeaLion, models.CASCADE, parent_link=True, primary_key=True)


class Gull(SealableModel):
sealion = models.OneToOneField(SeaLion, models.CASCADE, related_name='gull')
16 changes: 15 additions & 1 deletion tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.test import TestCase
from seal.exceptions import SealedObject

from .models import GreatSeaLion, Location, SeaLion
from .models import GreatSeaLion, Gull, Location, SeaLion


class SealableQuerySetTests(TestCase):
Expand All @@ -12,6 +12,7 @@ def setUpTestData(cls):
cls.great_sealion = GreatSeaLion.objects.create(height=1, weight=100, location=cls.location)
cls.sealion = cls.great_sealion.sealion_ptr
cls.sealion.previous_locations.add(cls.location)
cls.gull = Gull.objects.create(sealion=cls.sealion)

def test_state_sealed_assigned(self):
instance = SeaLion.objects.seal().get()
Expand Down Expand Up @@ -39,6 +40,19 @@ def test_sealed_select_related_foreign_key(self):
instance = SeaLion.objects.select_related('location').seal().get()
self.assertEqual(instance.location, self.location)

def test_sealed_one_to_one(self):
instance = Gull.objects.seal().get()
with self.assertRaisesMessage(SealedObject, 'Cannot fetch related field sealion on a sealed object.'):
instance.sealion

def test_not_sealed_one_to_one(self):
instance = Gull.objects.get()
self.assertEqual(instance.sealion, self.sealion)

def test_sealed_select_related_one_to_one(self):
instance = Gull.objects.select_related('sealion').seal().get()
self.assertEqual(instance.sealion, self.sealion)

def test_sealed_many_to_many(self):
instance = SeaLion.objects.seal().get()
message = 'Cannot fetch many-to-many field previous_locations on a sealed object.'
Expand Down
16 changes: 15 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.test import SimpleTestCase
from seal.exceptions import SealedObject

from .models import GreatSeaLion, Location, SeaLion
from .models import GreatSeaLion, Gull, Location, SeaLion


class SealableModelTests(SimpleTestCase):
Expand All @@ -26,6 +26,20 @@ def test_sealed_instance_reverse_foreign_key_access(self):
with self.assertRaisesMessage(SealedObject, message):
instance.visitors.all()

def test_sealed_instance_one_to_one_access(self):
instance = Gull.from_db('default', ['id', 'sealion_id'], [1, 1])
instance._state.sealed = True
message = "Cannot fetch related field sealion on a sealed object."
with self.assertRaisesMessage(SealedObject, message):
instance.sealion

def test_sealed_instance_reverse_one_to_one_access(self):
instance = SeaLion.from_db('default', ['id'], [1])
instance._state.sealed = True
message = "Cannot fetch related field gull on a sealed object."
with self.assertRaisesMessage(SealedObject, message):
instance.gull

def test_sealed_instance_parent_link_access(self):
instance = SeaLion.from_db('default', ['id'], [1])
instance._state.sealed = True
Expand Down

0 comments on commit 69fe503

Please sign in to comment.