From 2e68f063e2ba829c5470595920bca89a53d96c9b Mon Sep 17 00:00:00 2001 From: Shubham Date: Tue, 7 Jun 2016 22:03:28 +0530 Subject: [PATCH 1/3] get_or_create function added to querySetJob --- cacheback/queryset.py | 10 ++++++++++ tests/queryset_tests.py | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cacheback/queryset.py b/cacheback/queryset.py index dde0495..e0ab28d 100644 --- a/cacheback/queryset.py +++ b/cacheback/queryset.py @@ -38,6 +38,16 @@ def fetch(self, *args, **kwargs): return self.model.objects.get(**kwargs) +class QuerySetGetOrCreateJob(QuerySetJob): + """ + for ORM reads that use ``get_or_create`` method. + """ + + def fetch(self, *args, **kwargs): + model_object, created = self.model.objects.get_or_create(**kwargs) + return model_object + + class QuerySetFilterJob(QuerySetJob): """ For ORM reads that use the ``filter`` method. diff --git a/tests/queryset_tests.py b/tests/queryset_tests.py index f266bfc..c02ba27 100644 --- a/tests/queryset_tests.py +++ b/tests/queryset_tests.py @@ -2,7 +2,7 @@ from django.test import TestCase from cacheback.base import Job -from cacheback.queryset import QuerySetFilterJob, QuerySetGetJob +from cacheback.queryset import QuerySetFilterJob, QuerySetGetJob, QuerySetGetOrCreateJob from tests.dummyapp import models @@ -65,6 +65,21 @@ def test_returns_result_on_first_call(self): self.assertEqual('Alan', result.name) +class TestGetOrCreateQuerySetJob(TestCase): + + def setUp(self): + self.job = QuerySetGetOrCreateJob(models.DummyModel) + models.DummyModel.objects.create(name="Barry") + + def tearDown(self): + models.DummyModel.objects.all().delete() + cache.clear() + + def test_returns_result_on_first_call(self): + result = self.job.get(name='Alan') + self.assertEqual('Alan', result.name) + + class EchoJob(Job): def fetch(self, *args, **kwargs): return (args, kwargs) From 7c2ac4878e132911553719c5e408e1b8d8b3f562 Mon Sep 17 00:00:00 2001 From: Shubham Date: Tue, 7 Jun 2016 22:57:42 +0530 Subject: [PATCH 2/3] Import order --- tests/queryset_tests.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/queryset_tests.py b/tests/queryset_tests.py index c02ba27..368a91b 100644 --- a/tests/queryset_tests.py +++ b/tests/queryset_tests.py @@ -2,18 +2,16 @@ from django.test import TestCase from cacheback.base import Job -from cacheback.queryset import QuerySetFilterJob, QuerySetGetJob, QuerySetGetOrCreateJob +from cacheback.queryset import QuerySetFilterJob, QuerySetGetOrCreateJob, QuerySetGetJob from tests.dummyapp import models class ManualQuerySetJob(Job): - def fetch(self, name): return models.DummyModel.objects.filter(name=name) class TestManualQuerySetJob(TestCase): - def setUp(self): self.job = ManualQuerySetJob() models.DummyModel.objects.create(name="Alan") @@ -34,7 +32,6 @@ def test_makes_only_one_database_query(self): class TestFilterQuerySetJob(TestCase): - def setUp(self): self.job = QuerySetFilterJob(models.DummyModel) models.DummyModel.objects.create(name="Alan") @@ -50,7 +47,6 @@ def test_returns_result_on_first_call(self): class TestGetQuerySetJob(TestCase): - def setUp(self): self.job = QuerySetGetJob(models.DummyModel) models.DummyModel.objects.create(name="Alan") @@ -66,7 +62,6 @@ def test_returns_result_on_first_call(self): class TestGetOrCreateQuerySetJob(TestCase): - def setUp(self): self.job = QuerySetGetOrCreateJob(models.DummyModel) models.DummyModel.objects.create(name="Barry") @@ -86,7 +81,6 @@ def fetch(self, *args, **kwargs): class TestEdgeCases(TestCase): - def setUp(self): self.job = EchoJob() From c4b3f5e1cb642fcea14d55f9186285ce97ac3778 Mon Sep 17 00:00:00 2001 From: Shubham Date: Tue, 7 Jun 2016 23:05:27 +0530 Subject: [PATCH 3/3] Import order --- tests/queryset_tests.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/queryset_tests.py b/tests/queryset_tests.py index 368a91b..e584863 100644 --- a/tests/queryset_tests.py +++ b/tests/queryset_tests.py @@ -2,7 +2,8 @@ from django.test import TestCase from cacheback.base import Job -from cacheback.queryset import QuerySetFilterJob, QuerySetGetOrCreateJob, QuerySetGetJob +from cacheback.queryset import ( + QuerySetFilterJob, QuerySetGetJob, QuerySetGetOrCreateJob) from tests.dummyapp import models