Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robdox committed Jul 3, 2018
1 parent 38e2d0a commit 7440907
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 72 deletions.
11 changes: 5 additions & 6 deletions easy_scoping/ScopingMixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@ def scoped_query(*args, **kwargs):
return scoped_query
raise AttributeError('Queryset for %s has no attribute %s' %(self.model, name))

def a(self):
def a(self, *args, **kwargs):
return self.all()

def f(self, *args, **kwargs):
return self.filter(*args, **kwargs)

def e(self, *args, **kwargs):
return self.exclude(*args, **kwargs)

class ScopingMixin(object):

@classmethod
def a(self):
return self.objects.all()

@classmethod
def f(self, *args, **kwargs):
return self.objects.filter(*args, **kwargs)

@classmethod
def scopes(cls):
if not getattr(cls, '__scopes__', None):
Expand All @@ -33,7 +32,7 @@ def scopes(cls):
def scope(cls, name, func):
from types import MethodType
if name in cls.scopes():
name = '%s_%s'%(cls.label, name)
name = '_%s'%(name)

cls.__scopes__[name] = func

Expand Down
62 changes: 0 additions & 62 deletions easy_scoping/tests/test_filtering.py

This file was deleted.

150 changes: 150 additions & 0 deletions easy_scoping/tests/test_scoping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from django.test import TestCase
from widgets.models import Widget
from django.core import management as man


class ScopingTests(TestCase):

def setUp(self):
man.call_command('loaddata',
'tests/fixtures/filter_test_data.json')

def test_db_data_loaded(self):
obj = Widget.objects.all()
self.assertEqual(obj.count(), 336)

obj = obj.a()
self.assertEqual(obj.count(), 336)

def test_no_scope_registered(self):
with self.assertRaises(AttributeError):
Widget.a().not_a_scope()

def test_redundant_chain(self):
obj1 = Widget.a().basic_query_widget()
obj2 = obj1.basic_query_widget()

self.assertQuerysetEqual(obj1,
obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(obj1.count(), obj2.count())
self.assertEqual(obj1.get(), obj2.get())

not_obj1 = Widget.a().not_basic_query_widget()
not_obj2 = not_obj1.not_basic_query_widget()

self.assertQuerysetEqual(not_obj1,
not_obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(not_obj1.count(), not_obj2.count())

def test_query_widget(self):
obj1 = Widget.objects.filter(color='blue',
size='small',
shape='circle')
obj2 = Widget.a().basic_query_widget()

self.assertQuerysetEqual(obj1,
obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(obj1.count(), obj2.count())
self.assertEqual(obj1.get(), obj2.get())

not_obj1 = Widget.objects.exclude(color='blue',
size='small',
shape='circle')
not_obj2 = Widget.a().not_basic_query_widget()

self.assertQuerysetEqual(not_obj1,
not_obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(not_obj1.count(), not_obj2.count())

def test_query_chaining(self):
obj1 = Widget.objects.filter(color='blue') \
.filter(size='small') \
.filter(shape='circle')
obj2 = Widget.a().blue().small().circle()

self.assertQuerysetEqual(obj1,
obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(obj1.count(), obj2.count())
self.assertEqual(obj1.get(), obj2.get())

not_obj1 = Widget.objects.exclude(color='blue') \
.exclude(size='small') \
.exclude(shape='circle')
not_obj2 = Widget.a().not_blue().not_small().not_circle()

self.assertQuerysetEqual(not_obj1,
not_obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(not_obj1.count(), not_obj2.count())

def test_query_blue(self):
obj1 = Widget.objects.filter(color='blue')
obj2 = Widget.a().blue()

self.assertEqual(obj1.count(), obj2.count())
self.assertQuerysetEqual(obj1,
obj2,
transform=lambda x: x,
ordered=False)

not_obj1 = Widget.objects.exclude(color='blue')
not_obj2 = Widget.a().not_blue()

self.assertEqual(not_obj1.count(), not_obj2.count())
self.assertQuerysetEqual(not_obj1,
not_obj2,
transform=lambda x: x,
ordered=False)

def test_before_y2k(self):
import datetime

obj1 = Widget.objects.filter(used_on__lte=datetime.date(2000, 1, 1))
obj2 = Widget.a().before_y2k()

self.assertQuerysetEqual(obj1,
obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(obj1.count(), obj2.count())

not_obj1 = Widget.objects.exclude(used_on__lte=datetime.date(2000, 1, 1))
not_obj2 = Widget.a().not_before_y2k()

self.assertQuerysetEqual(not_obj1,
not_obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(not_obj1.count(), not_obj2.count())

def test_after_y2k(self):
import datetime

obj1 = Widget.objects.filter(used_on__gte=datetime.date(2000, 1, 1))
obj2 = Widget.a().after_y2k()

self.assertQuerysetEqual(obj1,
obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(obj1.count(), obj2.count())

not_obj1 = Widget.objects.exclude(used_on__gte=datetime.date(2000, 1, 1))
not_obj2 = Widget.a().not_after_y2k()

self.assertQuerysetEqual(not_obj1,
not_obj2,
transform=lambda x: x,
ordered=False)
self.assertEqual(not_obj1.count(), not_obj2.count())
3 changes: 1 addition & 2 deletions easy_scoping/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ setenv =
PYTHONWARNINGS=once::DeprecationWarning
commands =
coverage run ./runtests.py
coverage report --omit=./.tox/*
coverage report -m --omit=./.tox/*
{[cleanup]commands}
deps =
coverage
mock
django111: Django>=1.11,<2.0
django20: Django>=2.0

Expand Down
15 changes: 13 additions & 2 deletions easy_scoping/widgets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@ def get_shape(self):
def get_used_on(self):
return self.used_on


# Basic scopes for testing filtering
Widget.scope('basic_query_widget', lambda qs: qs.f(color='blue',
size='small',
shape='circle'))

Widget.scope('blue', lambda qs: qs.f(color='blue'))
Widget.scope('small', lambda qs: qs.f(size='small'))
Widget.scope('circle', lambda qs: qs.f(shape='circle'))
Widget.scope('before_y2k', lambda qs: qs.f(used_on__lte=datetime.date(2000,1,1)))
Widget.scope('after_y2k', lambda qs: qs.f(used_on__gte=datetime.date(2000,1,1)))

# Basic scopes for testing excluding
Widget.scope('not_basic_query_widget', lambda qs: qs.e(color='blue',
size='small',
shape='circle'))
Widget.scope('not_blue', lambda qs: qs.e(color='blue'))
Widget.scope('not_small', lambda qs: qs.e(size='small'))
Widget.scope('not_circle', lambda qs: qs.e(shape='circle'))
Widget.scope('not_before_y2k', lambda qs: qs.e(used_on__lte=datetime.date(2000,1,1)))
Widget.scope('not_after_y2k', lambda qs: qs.e(used_on__gte=datetime.date(2000,1,1)))

0 comments on commit 7440907

Please sign in to comment.