From dd4447d7b8955e627cba88909ebc59fffd19c441 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste <bleme@pm.me>
Date: Tue, 19 Feb 2019 15:38:27 +0100
Subject: [PATCH] update: add failing tests for urls resolver feature

---
 djangoldp/models.py               | 13 +++++++++++++
 djangoldp/resolver.py             | 17 +++++++++++++++++
 djangoldp/tests/models.py         | 10 ++++++++++
 djangoldp/tests/runner.py         |  1 +
 djangoldp/tests/tests_resolver.py | 25 +++++++++++++++++++++++++
 djangoldp/tests/tests_save.py     |  2 +-
 djangoldp/tests/tests_update.py   |  2 +-
 djangoldp/tests/urls.py           |  3 ++-
 8 files changed, 70 insertions(+), 3 deletions(-)
 create mode 100644 djangoldp/resolver.py
 create mode 100644 djangoldp/tests/tests_resolver.py

diff --git a/djangoldp/models.py b/djangoldp/models.py
index 50b31c6c..bcb62f61 100644
--- a/djangoldp/models.py
+++ b/djangoldp/models.py
@@ -1,6 +1,19 @@
 from django.conf import settings
 from django.db import models
 
+
+class LDPModel(models.Model):
+    ldp_path = None
+
+    def full_path(self):
+        return "{}/{}".format(self.container_path(), self.pk)
+
+    def container_path(self):
+        return self.ldp_path
+
+    class Meta:
+        abstract = True
+
 class LDPSource(models.Model):
     container = models.URLField()
     federation = models.CharField(max_length=255)
diff --git a/djangoldp/resolver.py b/djangoldp/resolver.py
new file mode 100644
index 00000000..949ae3f1
--- /dev/null
+++ b/djangoldp/resolver.py
@@ -0,0 +1,17 @@
+class LDPResolver:
+
+    @classmethod
+    def resource_url(cls, dummy):
+        pass
+
+    @classmethod
+    def container_url(cls, dummy):
+        pass
+
+    @classmethod
+    def resource_path(cls, dummy):
+        pass
+
+    @classmethod
+    def container_path(cls, dummy):
+        pass
diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py
index 13a0aedf..29b0a521 100644
--- a/djangoldp/tests/models.py
+++ b/djangoldp/tests/models.py
@@ -1,6 +1,8 @@
 from django.conf import settings
 from django.db import models
 
+from djangoldp.models import LDPModel
+
 
 class Skill(models.Model):
     title = models.CharField(max_length=255, blank=True, null=True)
@@ -23,3 +25,11 @@ class Message(models.Model):
     author_user = models.ForeignKey(settings.AUTH_USER_MODEL)
 
 
+class Dummy(models.Model):
+    some = models.CharField(max_length=255, blank=True, null=True)
+
+
+class LDPDummy(LDPModel):
+    some = models.CharField(max_length=255, blank=True, null=True)
+    ldp_path = "ldp-dummys"
+
diff --git a/djangoldp/tests/runner.py b/djangoldp/tests/runner.py
index 1cf8604d..17419667 100644
--- a/djangoldp/tests/runner.py
+++ b/djangoldp/tests/runner.py
@@ -25,6 +25,7 @@ from django.test.runner import DiscoverRunner
 test_runner = DiscoverRunner(verbosity=1)
 
 failures = test_runner.run_tests([
+    'djangoldp.tests.tests_resolver',
     'djangoldp.tests.tests_save',
     'djangoldp.tests.tests_update'])
 if failures:
diff --git a/djangoldp/tests/tests_resolver.py b/djangoldp/tests/tests_resolver.py
new file mode 100644
index 00000000..55818f6d
--- /dev/null
+++ b/djangoldp/tests/tests_resolver.py
@@ -0,0 +1,25 @@
+from django.test import TestCase
+
+from djangoldp.resolver import LDPResolver
+from djangoldp.tests.models import Dummy, LDPDummy
+
+
+class UrlUtils(TestCase):
+
+    def test_class_not_inheriting_ldp_model(self):
+        dummy = Dummy.objects.create(some="text")
+        self.assertEquals("http://localhost/dummys/{}".format(dummy.pk), LDPResolver.resource_url(dummy))
+        self.assertEquals("/dummys/{}".format(dummy.pk), LDPResolver.resource_path(dummy))
+        self.assertEquals("http://localhost/dummys", LDPResolver.container_url(dummy))
+        self.assertEquals("/dummys", LDPResolver.container_path(dummy))
+
+    def test_class_inheriting_ldp_model(self):
+        dummy = LDPDummy.objects.create(some="text")
+        self.assertEquals("http://localhost/ldp-dummys/{}".format(dummy.pk), LDPResolver.resource_url(dummy))
+        self.assertEquals("/ldp-dummys/{}".format(dummy.pk), LDPResolver.resource_path(dummy))
+        self.assertEquals("http://localhost/ldp-dummys/{}".format(dummy.pk), dummy.resource_url())
+        self.assertEquals("/ldp-dummys/{}".format(dummy.pk), dummy.resource_path())
+        self.assertEquals("http://localhost/ldp-dummys", LDPResolver.container_url(dummy))
+        self.assertEquals("/ldp-dummys", LDPResolver.container_path(dummy))
+        self.assertEquals("http://localhost/dummys/", dummy.container_url())
+        self.assertEquals("/ldp-dummys", dummy.container_path())
diff --git a/djangoldp/tests/tests_save.py b/djangoldp/tests/tests_save.py
index 1117c928..7e05496a 100644
--- a/djangoldp/tests/tests_save.py
+++ b/djangoldp/tests/tests_save.py
@@ -4,7 +4,7 @@ from djangoldp.serializers import LDPSerializer
 from djangoldp.tests.models import Skill, JobOffer
 
 
-class Serializer(TestCase):
+class Save(TestCase):
 
     def test_save_m2m(self):
         skill1 = Skill.objects.create(title="skill1", obligatoire="obligatoire")
diff --git a/djangoldp/tests/tests_update.py b/djangoldp/tests/tests_update.py
index 7b232b44..622b55ef 100644
--- a/djangoldp/tests/tests_update.py
+++ b/djangoldp/tests/tests_update.py
@@ -5,7 +5,7 @@ from djangoldp.serializers import LDPSerializer
 from djangoldp.tests.models import Skill, JobOffer, Thread, Message
 
 
-class Serializer(TestCase):
+class Update(TestCase):
 
     def test_update(self):
         skill = Skill.objects.create(title="to drop", obligatoire="obligatoire")
diff --git a/djangoldp/tests/urls.py b/djangoldp/tests/urls.py
index 0066129b..8e1cbc86 100644
--- a/djangoldp/tests/urls.py
+++ b/djangoldp/tests/urls.py
@@ -1,6 +1,6 @@
 from django.conf import settings
 
-from djangoldp.tests.models import Skill, JobOffer, Message, Thread
+from djangoldp.tests.models import Skill, JobOffer, Message, Thread, Dummy
 from djangoldp.views import LDPViewSet
 from django.conf.urls import url
 
@@ -11,4 +11,5 @@ urlpatterns = [
     url(r'^messages/', LDPViewSet.urls(model=Message, permission_classes=[], fields=["@id", "text"], nested_fields=[])),
     url(r'^threads/', LDPViewSet.urls(model=Thread, nested_fields=["message_set"], permission_classes=())),
     url(r'^users/', LDPViewSet.urls(model=settings.AUTH_USER_MODEL, permission_classes=[])),
+    url(r'^dummys/', LDPViewSet.urls(model=Dummy, permission_classes=[])),
 ]
\ No newline at end of file
-- 
GitLab