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 6a1c8d5 commit e89ccc5
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 @@ -25,3 +25,7 @@ class Koala(models.Model):
weight = models.PositiveIntegerField()

objects = SealableQuerySet.as_manager()


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, Koala, Location, SeaLion
from .models import GreatSeaLion, Gull, Koala, Location, SeaLion


class SealableQuerySetTests(TestCase):
Expand All @@ -13,6 +13,7 @@ def setUpTestData(cls):
cls.koala = Koala.objects.create(height=1, weight=10)
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 @@ -40,6 +41,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.seal()
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.seal()
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.seal()
Expand Down

0 comments on commit e89ccc5

Please sign in to comment.