Skip to content

Commit

Permalink
Merge pull request #74 from stphivos/refactor/remove_duplication
Browse files Browse the repository at this point in the history
Refactor more maintainability issues on duplication
  • Loading branch information
stphivos authored Mar 8, 2018
2 parents 1c5ffc2 + ce723b5 commit 4ecd6b3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
41 changes: 14 additions & 27 deletions django_mock_queries/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,24 @@ def compiler(queryset, connection, using, **kwargs):
Model.refresh_from_db = Mock() # Make this into a noop.


class MockOneToManyMap(object):
class MockMap(object):
def __init__(self, original):
""" Wrap a mock mapping around the original one-to-many relation. """
self.map = {}
self.original = original

def __set__(self, instance, value):
""" Set a related object for an instance. """

self.map[id(instance)] = (weakref.ref(instance), value)

def __getattr__(self, name):
""" Delegate all other calls to the original. """

return getattr(self.original, name)


class MockOneToManyMap(MockMap):
def __get__(self, instance, owner):
""" Look in the map to see if there is a related set.
Expand All @@ -129,23 +141,8 @@ def __get__(self, instance, owner):

return related_objects

def __set__(self, instance, value):
""" Set a related object for an instance. """

self.map[id(instance)] = (weakref.ref(instance), value)

def __getattr__(self, name):
""" Delegate all other calls to the original. """

return getattr(self.original, name)


class MockOneToOneMap(object):
def __init__(self, original):
""" Wrap a mock mapping around the original one-to-one relation. """
self.map = {}
self.original = original

class MockOneToOneMap(MockMap):
def __get__(self, instance, owner):
""" Look in the map to see if there is a related object.
Expand All @@ -170,16 +167,6 @@ def __get__(self, instance, owner):
)
return related_object

def __set__(self, instance, value):
""" Set a related object for an instance. """

self.map[id(instance)] = (weakref.ref(instance), value)

def __getattr__(self, name):
""" Delegate all other calls to the original. """

return getattr(self.original, name)


def find_all_models(models):
""" Yield all models and their parents. """
Expand Down
3 changes: 3 additions & 0 deletions examples/users/analytics/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ def create_user(self, **attrs):
def today_visitors_count(self):
result = User.objects.filter(last_login__gte=date.today()).aggregate(Count('last_login'))
return result['last_login__count']

def staff_usernames(self):
return User.objects.filter(is_staff=True).values_list('username', flat=True)
14 changes: 9 additions & 5 deletions examples/users/analytics/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@

@skipIfDBFeature('is_mocked')
class TestApi(TestCase):
def test_api_create_user(self):
start_count = User.objects.count()
def setUp(self):
self.api = AnalyticsApi()

User.objects.create(username='bob')
final_count = User.objects.count()
def test_api_create_user(self):
_ = User.objects.create(username='plain1')
_ = User.objects.create(username='plain2')
staff1 = User.objects.create(username='staff1', is_staff=True)
staff2 = User.objects.create(username='staff2', is_staff=True)

self.assertEqual(start_count + 1, final_count)
usernames = [str(x) for x in self.api.staff_usernames()]
self.assertEqual(usernames, [staff1.username, staff2.username])


@mocked_relations(User)
Expand Down

0 comments on commit 4ecd6b3

Please sign in to comment.