Skip to content

Commit

Permalink
refactoring the way tests are stored
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanNoelk committed Apr 5, 2018
1 parent e0d6f1a commit ad52398
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 36 deletions.
2 changes: 2 additions & 0 deletions v1/accounts/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# encoding: utf-8
11 changes: 6 additions & 5 deletions v1/accounts/tests/tests.py → v1/accounts/tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from django.test import TestCase
from rest_framework_jwt.settings import api_settings

jwt_decode_handler = api_settings.JWT_DECODE_HANDLER


class AccountTests(TestCase):
fixtures=['test/users.json']
fixtures = ['test/users.json']

def setUp(self):
self.jwt_decode_handler = api_settings.JWT_DECODE_HANDLER

def test_obtain_authtoken_success(self):
resp = self.client.post(
Expand All @@ -21,7 +22,7 @@ def test_obtain_authtoken_success(self):

self.assertEqual(resp.status_code, 200)
self.assertTrue(resp.json()['id'] == 1)
decoded_token = jwt_decode_handler(resp.json()['token'])
decoded_token = self.jwt_decode_handler(resp.json()['token'])
self.assertTrue(decoded_token.get('user_id') == 1)
self.assertTrue(decoded_token.get('username') == 'testuser1')

Expand All @@ -34,4 +35,4 @@ def test_obtain_authtoken_wrong_password(self):
}
)

self.assertEqual(resp.status_code, 400)
self.assertEqual(resp.status_code, 400)
2 changes: 2 additions & 0 deletions v1/common/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# encoding: utf-8
32 changes: 1 addition & 31 deletions v1/common/tests/tests.py → v1/common/tests/test_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,9 @@

from django.contrib.auth.models import AnonymousUser, User
from django.test import TestCase, RequestFactory
from v1.recipe.models import Recipe
from v1.common.recipe_search import get_search_results
from v1.common.permissions import IsAdminOrReadOnly, IsOwnerOrReadOnly


class GetSearchResultsTests(TestCase):
fixtures = [
'test/users.json',
'course_data.json',
'cuisine_data.json',
'ing_data.json',
'recipe_data.json'
]

def test_get_search_results(self):
query = get_search_results(
['title', 'ingredient_groups__ingredients__title', 'tags__title'],
Recipe.objects,
'chili'
).distinct()

self.assertTrue(len(query.all()) > 0)

def test_get_search_no_results(self):
query = get_search_results(
['title', 'ingredient_groups__ingredients__title', 'tags__title'],
Recipe.objects,
'blue berry'
).distinct()

self.assertTrue(len(query.all()) == 0)


class PermissionTest(TestCase):
def setUp(self):
# Every test needs access to the request factory.
Expand Down Expand Up @@ -71,7 +41,7 @@ def test_is_owner_admin(self):
IsOwnerOrReadOnly().has_permission(request, None)
)

def test_is_admin_admin(self):
def test_is_admin_or_read_only(self):
# Recall that middleware are not supported. You can simulate a
# logged-in user by setting request.user manually.
request = self.factory.get('/admin')
Expand Down
34 changes: 34 additions & 0 deletions v1/common/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
# encoding: utf-8

from django.test import TestCase
from v1.recipe.models import Recipe
from v1.common.recipe_search import get_search_results


class GetSearchResultsTests(TestCase):
fixtures = [
'test/users.json',
'course_data.json',
'cuisine_data.json',
'ing_data.json',
'recipe_data.json'
]

def test_get_search_results(self):
query = get_search_results(
['title', 'ingredient_groups__ingredients__title', 'tags__title'],
Recipe.objects,
'chili'
).distinct()

self.assertTrue(len(query.all()) > 0)

def test_get_search_no_results(self):
query = get_search_results(
['title', 'ingredient_groups__ingredients__title', 'tags__title'],
Recipe.objects,
'blue berry'
).distinct()

self.assertTrue(len(query.all()) == 0)
2 changes: 2 additions & 0 deletions v1/recipe/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# encoding: utf-8
2 changes: 2 additions & 0 deletions v1/recipe_groups/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# encoding: utf-8

0 comments on commit ad52398

Please sign in to comment.