Skip to content

Commit

Permalink
Added to demonstrate that messages with a nullable profile foreign ke…
Browse files Browse the repository at this point in the history
…y, i.e. s, are not deleted when they are removed from a profile.
  • Loading branch information
gibsonbailey committed Jan 1, 2020
1 parent b655fda commit 4cc08d8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
14 changes: 12 additions & 2 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ class CustomPK(models.Model):
)


class Message(models.Model):
class AbstractMessage(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
message = models.CharField(max_length=100)

class Meta:
abstract = True


class Message(AbstractMessage):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)


class PersistentMessage(AbstractMessage):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)


class AnotherProfile(models.Model):
sites = models.ManyToManyField(Site)
Expand Down
21 changes: 21 additions & 0 deletions tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Meta:
fields = ('pk', 'message',)


class PersistentMessageSerializer(serializers.ModelSerializer):

class Meta:
model = models.PersistentMessage
fields = ('pk', 'message',)


class SiteSerializer(serializers.ModelSerializer):
url = serializers.CharField()

Expand Down Expand Up @@ -54,6 +61,13 @@ class Meta:
fields = ('pk', 'sites', 'avatars', 'access_key', 'message_set',)


class PersistentProfileSerializer(ProfileSerializer):
message_set = PersistentMessageSerializer(many=True)

class Meta(ProfileSerializer.Meta):
pass


class UserSerializer(WritableNestedModelSerializer):
# Reverse OneToOne relation
profile = ProfileSerializer(required=False, allow_null=True)
Expand All @@ -64,6 +78,13 @@ class Meta:
fields = ('pk', 'profile', 'username', 'user_avatar')


class PersistentUserSerializer(WritableNestedModelSerializer):
profile = PersistentProfileSerializer(required=False, allow_null=True)

class Meta(UserSerializer.Meta):
pass


class CustomSerializer(UserSerializer):
# Simulate having non-modelfield information on the serializer
custom_field = serializers.CharField()
Expand Down
43 changes: 43 additions & 0 deletions tests/test_writable_nested_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,49 @@ def test_update(self):
# Sites shouldn't be deleted either as it is M2M
self.assertEqual(models.Site.objects.count(), 3)

def test_update_no_delete_if_null_is_false(self):
serializer = serializers.UserSerializer(data=self.get_initial_data())
serializer.is_valid(raise_exception=True)
user = serializer.save()

# Check instances count
self.assertEqual(models.User.objects.count(), 1)
self.assertEqual(models.Message.objects.count(), 3)

# Update
user_pk = user.pk
profile_pk = user.profile.pk

serializer = serializers.PersistentUserSerializer(
data={
'pk': user_pk,
'username': 'new',
'profile': {
'pk': profile_pk,
'access_key': None,
'sites': [
{
'url': 'http://new-site.com',
},
],
'avatars': [
],
'message_set': [
],
},
},
instance=user,
)

serializer.is_valid(raise_exception=True)
user = serializer.save()
user.refresh_from_db()

# Check instances count
self.assertEqual(models.User.objects.count(), 1)
self.assertEqual(models.Avatar.objects.count(), 0)
self.assertEqual(models.Message.objects.count(), 3)

def test_update_reverse_one_to_one_without_pk(self):
serializer = serializers.UserSerializer(data=self.get_initial_data())
serializer.is_valid(raise_exception=True)
Expand Down

0 comments on commit 4cc08d8

Please sign in to comment.