Skip to content

Commit f96ee73

Browse files
authored
Merge pull request #45 from caravancoop/feature/unit-tests
Add unit tests for models #2
2 parents 30aba20 + 76d2031 commit f96ee73

17 files changed

+240
-64
lines changed

.circleci/config.yml

+33-34
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,29 @@ workflows:
66
jobs:
77
- test-py37
88
- test-py36
9-
- test-py27
109
- check
1110

1211
jobs:
1312

1413
test-py37:
1514
docker:
1615
- image: circleci/python:3.7
16+
- image: circleci/postgres:9.6-alpine
17+
environment:
18+
POSTGRES_DB: circle_test
19+
POSTGRES_USER: testapp
20+
POSTGRES_PASSWORD: ""
1721
working_directory: ~/rest-auth-toolkit
22+
environment:
23+
DATABASE_URL: postgres://testapp@localhost/circle_test
1824
steps:
1925
- checkout
2026
- restore_cache:
21-
key: rest-auth-toolkit-py37-v2
27+
keys:
28+
- rest-auth-toolkit-py37-v5-{{ arch }}-{{ checksum "requirements-test.txt" }}-{{ checksum "demo/requirements.txt" }}
29+
- rest-auth-toolkit-py37-v5-{{ arch }}-{{ checksum "requirements-test.txt" }}-{{ checksum "demo/requirements.txt" }}
30+
- rest-auth-toolkit-py37-v5-{{ arch }}-{{ checksum "requirements-test.txt" }}
31+
- rest-auth-toolkit-py37-v5-{{ arch }}-
2232
- run:
2333
name: Install CI tools
2434
command: |
@@ -27,20 +37,34 @@ jobs:
2737
- run:
2838
name: Test with Python 3.7
2939
command: venv/bin/tox -e py37
40+
- run:
41+
name: Check coverage with Python 3.7
42+
command: venv/bin/tox -e coverage
3043
- save_cache:
31-
key: rest-auth-toolkit-py37-v2
44+
key: rest-auth-toolkit-py37-v5-{{ arch }}-{{ checksum "requirements-test.txt" }}-{{ checksum "demo/requirements.txt" }}
3245
paths:
3346
- venv
3447
- .tox
3548

3649
test-py36:
3750
docker:
3851
- image: circleci/python:3.6
52+
- image: circleci/postgres:9.6-alpine
53+
environment:
54+
POSTGRES_DB: circle_test
55+
POSTGRES_USER: testapp
56+
POSTGRES_PASSWORD: ""
3957
working_directory: ~/rest-auth-toolkit
58+
environment:
59+
DATABASE_URL: postgres://testapp@localhost/circle_test
4060
steps:
4161
- checkout
4262
- restore_cache:
43-
key: rest-auth-toolkit-py36-v2
63+
keys:
64+
- rest-auth-toolkit-py36-v3-{{ arch }}-{{ checksum "requirements-test.txt" }}-{{ checksum "demo/requirements.txt" }}
65+
- rest-auth-toolkit-py36-v2-{{ arch }}-{{ checksum "requirements-test.txt" }}-{{ checksum "demo/requirements.txt" }}
66+
- rest-auth-toolkit-py36-v3-{{ arch }}-{{ checksum "requirements-test.txt" }}
67+
- rest-auth-toolkit-py36-v3-{{ arch }}-
4468
- run:
4569
name: Install CI tools
4670
command: |
@@ -50,54 +74,29 @@ jobs:
5074
name: Test with Python 3.6
5175
command: venv/bin/tox -e py36
5276
- save_cache:
53-
key: rest-auth-toolkit-py36-v2
54-
paths:
55-
- venv
56-
- .tox
57-
58-
test-py27:
59-
docker:
60-
# This image contains Python 3.6 (to install flit) and 2.7 (to run tests)
61-
- image: circleci/python:3.6
62-
working_directory: ~/rest-auth-toolkit
63-
steps:
64-
- checkout
65-
- restore_cache:
66-
key: rest-auth-toolkit-py27-v2
67-
- run:
68-
name: Install CI tools
69-
command: |
70-
python3.6 -m venv venv
71-
venv/bin/pip install tox
72-
- run:
73-
name: Test with Python 2.7
74-
command: |
75-
venv/bin/tox --sdistonly
76-
venv/bin/tox -e py27
77-
- save_cache:
78-
key: rest-auth-toolkit-py27-v2
77+
key: rest-auth-toolkit-py36-v3-{{ arch }}-{{ checksum "requirements-test.txt" }}-{{ checksum "demo/requirements.txt" }}
7978
paths:
8079
- venv
8180
- .tox
8281

8382
check:
8483
docker:
85-
- image: circleci/python:3.6
84+
- image: circleci/python:3.7
8685
working_directory: ~/rest-auth-toolkit
8786
steps:
8887
- checkout
8988
- restore_cache:
90-
key: rest-auth-toolkit-check-v3
89+
key: rest-auth-toolkit-check-v5
9190
- run:
9291
name: Install CI tools
9392
command: |
94-
python3.6 -m venv venv
93+
python3.7 -m venv venv
9594
venv/bin/pip install tox
9695
- run:
9796
name: Check packaging and dependencies
9897
command: venv/bin/tox -e pkg
9998
- save_cache:
100-
key: rest-auth-toolkit-check-v3
99+
key: rest-auth-toolkit-check-v5
101100
paths:
102101
- venv
103102
- .tox

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ __pycache__
44
/demo/.env
55

66
# Test artifacts
7-
/.cache/
7+
/.pytest_cache/
8+
/.coverage
89
/.tox/
910
/venv/
1011

demo/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
This is a minimal Django project that shows how to use
44
the package and helps checking if changes break usage.
55

6+
This app is also used by the automated tests.
7+
8+
69
## How to install
710

811
Run this command once from the repository root:
@@ -26,7 +29,7 @@ Define the environment variables needed by the app:
2629

2730
```
2831
export DEMO_FACEBOOK_APP_ID="..."
29-
export DEMO_FACEBOOK_SECRET_KEY="..."
32+
export DEMO_FACEBOOK_APP_SECRET_KEY="..."
3033
```
3134

3235
(using a [virtualenvwrapper hook](https://virtualenvwrapper.readthedocs.io/en/latest/scripts.html#postactivate)

demo/demo/accounts/admin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class EmailConfirmationAdmin(admin.ModelAdmin):
1818

1919

2020
class APITokenAdmin(admin.ModelAdmin):
21-
list_display = ('key', 'user', 'created')
21+
list_display = ('__str__', 'created')
2222
ordering = ('user', '-created')
2323
date_hierarchy = 'created'
2424
readonly_fields = ('key', 'created')

demo/demo/settings.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@
22

33
import os
44

5+
from django.core.management.utils import get_random_secret_key
6+
7+
import dj_database_url
8+
59
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
610

711
# Quick-start development settings - unsuitable for production
812
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
913

14+
if os.getenv('RAT_TESTING'):
15+
os.environ['DEMO_SECRET_KEY'] = get_random_secret_key()
16+
os.environ['DEMO_FACEBOOK_APP_ID'] = 'fb app id'
17+
os.environ['DEMO_FACEBOOK_APP_SECRET_KEY'] = 'fb app key'
18+
19+
1020
SECRET_KEY = os.getenv('DEMO_SECRET_KEY',
1121
'1(9v73@)*ws6i(v_i5*_4ty^+ji0@7u($6onk7pt-_ncxkqs@@')
22+
1223
DEBUG = True
1324

1425
FACEBOOK_APP_ID = os.environ['DEMO_FACEBOOK_APP_ID']
15-
FACEBOOK_APP_SECRET_KEY = os.environ['DEMO_FACEBOOK_SECRET_KEY']
26+
FACEBOOK_APP_SECRET_KEY = os.environ['DEMO_FACEBOOK_APP_SECRET_KEY']
27+
28+
DATABASES = {
29+
'default': dj_database_url.parse(os.environ['DATABASE_URL']),
30+
}
1631

1732
SITE_ID = 1
1833
ROOT_URLCONF = 'demo.urls'
@@ -49,14 +64,6 @@
4964
]
5065

5166

52-
DATABASES = {
53-
'default': {
54-
'ENGINE': 'django.db.backends.sqlite3',
55-
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
56-
}
57-
}
58-
59-
6067
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
6168

6269

demo/requirements.in

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ pip-tools
33
django == 2.0.*
44
djangorestframework
55
django-debug-toolbar
6+
dj-database-url
7+
psycopg2
68

79
django-model-utils
810
django-shortuuidfield

demo/requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
# This file is autogenerated by pip-compile
33
# To update, run:
44
#
5-
# pip-compile --output-file requirements.txt requirements.in
5+
# pip-compile requirements.in
66
#
77
certifi==2018.8.24 # via requests
88
chardet==3.0.4 # via requests
99
click==6.7 # via pip-tools
10+
dj-database-url==0.5.0
1011
django-debug-toolbar==1.10.1
1112
django-model-utils==3.1.2
1213
django-shortuuidfield==0.1.3
@@ -17,6 +18,7 @@ first==2.0.1 # via pip-tools
1718
idna==2.7 # via requests
1819
markdown==2.6.11
1920
pip-tools==2.0.2
21+
psycopg2==2.7.5
2022
pygments==2.2.0
2123
pytz==2018.5 # via django
2224
requests==2.19.1 # via facepy

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ classifiers = [
1313
"Development Status :: 3 - Alpha",
1414
"Intended Audience :: Developers",
1515
"License :: OSI Approved :: MIT License",
16-
"Programming Language :: Python :: 2.7",
1716
"Programming Language :: Python :: 3.5",
1817
"Programming Language :: Python :: 3.6",
1918
"Programming Language :: Python :: 3.7",

requirements-test.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
flake8
2+
3+
pytest
4+
pytest-env
5+
pytest-django
6+
pytest-pythonpath
7+
8+
coverage

rest_auth_toolkit/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Meta:
103103
abstract = True
104104

105105
def __str__(self):
106-
return self.key
106+
return 'API token for {user}'.format(user=self.user)
107107

108108
def save(self, *args, **kwargs):
109109
if not self.key:

tests/__init__.py

Whitespace-only changes.

tests/functional/__init__.py

Whitespace-only changes.

tests/functional/conftest.py

Whitespace-only changes.

tests/unit/__init__.py

Whitespace-only changes.

tests/unit/conftest.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
3+
from demo.accounts.models import User, APIToken
4+
5+
6+
@pytest.fixture
7+
def user0(db):
8+
return User.objects.create_user(
9+
10+
is_active=True,
11+
)
12+
13+
14+
@pytest.fixture
15+
def user1(db):
16+
return User.objects.create_user(
17+
18+
password='unitpass123',
19+
is_active=True,
20+
)
21+
22+
23+
@pytest.fixture
24+
def token0(user0):
25+
return APIToken.objects.create_token(
26+
user=user0,
27+
)

0 commit comments

Comments
 (0)