Browse Source

Add a method to find Services with same name

Jocelyn Delalande 7 years ago
parent
commit
548a41a32a
2 changed files with 15 additions and 0 deletions
  1. 2 0
      costs/models.py
  2. 13 0
      costs/tests/test_models.py

+ 2 - 0
costs/models.py

@@ -314,6 +314,8 @@ class ServiceQuerySet(models.QuerySet):
     def subscribables(self):
         return self.exclude(internal=True)
 
+    def similar_to(self, service):
+        return self.filter(name=service.name).exclude(pk=service.pk)
 
 class Service(AbstractResource):
     """ A service we sell

+ 13 - 0
costs/tests/test_models.py

@@ -115,6 +115,19 @@ class ServiceTests(TestCase):
             'subscriptions_count': -1,
         })
 
+    def test_similar(self):
+        other_doc = Document.objects.create(name='other budget')
+        a = Service.objects.create(
+            name='Foo', document=self.doc, subscriptions_count=3)
+        b = Service.objects.create(
+            name='Foo', document=other_doc, subscriptions_count=3)
+        c = Service.objects.create(
+            name='Bar', document=other_doc, subscriptions_count=3)
+
+        similars = Service.objects.similar_to(a)
+        self.assertEqual(similars.count(), 1)
+        self.assertEqual(similars.first(), b)
+
 
 class AbstractUseTests(TestCase):
     """ Testing AbstractUseTests through CostUse