From 32a34057c412a65a7fa4faf53969c27c67d7e4d2 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste <bleme@pm.me> Date: Wed, 6 Mar 2019 13:07:34 +0100 Subject: [PATCH 1/2] update: add test reproducing the #94 error --- djangoldp/tests/models.py | 17 ++++++++++++++ djangoldp/tests/tests_save.py | 42 +++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py index a68a803c..0b880a1a 100644 --- a/djangoldp/tests/models.py +++ b/djangoldp/tests/models.py @@ -32,3 +32,20 @@ class Dummy(models.Model): class LDPDummy(Model): some = models.CharField(max_length=255, blank=True, null=True) + +class Invoice(Model): + title = models.CharField(max_length=255, blank=True, null=True) + + +class Batch(Model): + invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, related_name='batches') + title = models.CharField(max_length=255, blank=True, null=True) + + +class Task(models.Model): + batch = models.ForeignKey(Batch, on_delete=models.CASCADE, related_name='tasks') + title = models.CharField(max_length=255) + + +Batch._meta.serializer_fields = ['@id', 'title', 'invoice', 'tasks'] +Task._meta.serializer_fields = ['@id', 'title', 'batch'] diff --git a/djangoldp/tests/tests_save.py b/djangoldp/tests/tests_save.py index 36f724bb..d3424d1f 100644 --- a/djangoldp/tests/tests_save.py +++ b/djangoldp/tests/tests_save.py @@ -1,11 +1,45 @@ from django.test import TestCase from djangoldp.serializers import LDPSerializer -from djangoldp.tests.models import Skill, JobOffer +from djangoldp.tests.models import Skill, JobOffer, Invoice class Save(TestCase): + def test_save_m2m_graph_with_many_nested(self): + invoice = { + "@graph": [ + { + "@id": "./", + "batches": {"@id": "_:b381"}, + "title": "Nouvelle facture", + }, + { + "@id": "_:b381", + "tasks": {"@id": "_:b382"}, + "title": "Batch 1" + }, + { + "@id": "_:b382", + "title": "Tache 1" + } + ] + } + + meta_args = {'model': Invoice, 'depth': 1, 'fields': ("@id", "title", "batches")} + + meta_class = type('Meta', (), meta_args) + serializer_class = type(LDPSerializer)('InvoiceSerializer', (LDPSerializer,), {'Meta': meta_class}) + serializer = serializer_class(data=invoice) + serializer.is_valid() + result = serializer.save() + + self.assertEquals(result.title, "Nouvelle facture") + self.assertIs(result.batches.count(), 1) + self.assertEquals(result.batches.all()[0].title, "Batch 1") + self.assertIs(result.batches.all()[0].tasks.count(), 1) + #self.assertEquals(result.batches.all()[0].tasks.all()[0].title, "Tache 1") + def test_save_m2m(self): skill1 = Skill.objects.create(title="skill1", obligatoire="obligatoire") skill2 = Skill.objects.create(title="skill2", obligatoire="obligatoire") @@ -35,9 +69,9 @@ class Save(TestCase): def test_save_m2m_graph_simple(self): job = {"@graph": [ - {"title": "job test", - }, - ]} + {"title": "job test", + }, + ]} meta_args = {'model': JobOffer, 'depth': 1, 'fields': ("@id", "title", "skills")} -- GitLab From db8b2ea427e89776aa8f8a340017c13dcbf89e28 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste <bleme@pm.me> Date: Tue, 12 Mar 2019 11:06:08 +0100 Subject: [PATCH 2/2] fix test by increasing depth --- djangoldp/serializers.py | 6 +++--- djangoldp/tests/tests_save.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index 7107a004..f8caee7a 100644 --- a/djangoldp/serializers.py +++ b/djangoldp/serializers.py @@ -17,9 +17,9 @@ from rest_framework.utils import model_meta from rest_framework.utils.field_mapping import get_nested_relation_kwargs from rest_framework.utils.serializer_helpers import ReturnDict +from djangoldp import permissions from djangoldp.fields import LDPUrlField, IdURLField from djangoldp.models import Model -from djangoldp import permissions class LDListMixin: @@ -210,11 +210,11 @@ class LDPSerializer(HyperlinkedModelSerializer): if hasattr(obj._meta, 'auto_author'): data['permissions'] += permissions.AnonymousReadOnly.author_perms else: - data['permissions'] += permissions.AnonymousReadOnly.authenticated_perms + data['permissions'] += permissions.AnonymousReadOnly.authenticated_perms if hasattr(obj._meta, 'rdf_context'): data['@context'] = obj._meta.rdf_context - + return data def build_standard_field(self, field_name, model_field): diff --git a/djangoldp/tests/tests_save.py b/djangoldp/tests/tests_save.py index d3424d1f..e560f019 100644 --- a/djangoldp/tests/tests_save.py +++ b/djangoldp/tests/tests_save.py @@ -26,7 +26,7 @@ class Save(TestCase): ] } - meta_args = {'model': Invoice, 'depth': 1, 'fields': ("@id", "title", "batches")} + meta_args = {'model': Invoice, 'depth': 2, 'fields': ("@id", "title", "batches")} meta_class = type('Meta', (), meta_args) serializer_class = type(LDPSerializer)('InvoiceSerializer', (LDPSerializer,), {'Meta': meta_class}) -- GitLab