Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_or_create function added to querySetJob #61

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cacheback/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 16 additions & 6 deletions tests/queryset_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
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


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")
Expand All @@ -34,7 +33,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")
Expand All @@ -50,7 +48,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")
Expand All @@ -65,13 +62,26 @@ 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)


class TestEdgeCases(TestCase):

def setUp(self):
self.job = EchoJob()

Expand Down