mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2025-12-30 21:49:50 -05:00
basics of pytest
This commit is contained in:
6
cookbook/tests/pytest/__init__.py
Normal file
6
cookbook/tests/pytest/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.test import utils
|
||||
from django_scopes import scopes_disabled
|
||||
|
||||
# disables scoping error in all queries used inside the test functions
|
||||
# fixtures need to have their own scopes_disabled
|
||||
utils.setup_databases = scopes_disabled()(utils.setup_databases)
|
||||
58
cookbook/tests/pytest/conftest.py
Normal file
58
cookbook/tests/pytest/conftest.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import inspect
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from django.contrib.auth.models import User, Group
|
||||
from django_scopes import scopes_disabled
|
||||
|
||||
from cookbook.models import Space
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_password():
|
||||
return 'strong-test-pass'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def create_user(db, django_user_model, test_password):
|
||||
def make_user(**kwargs):
|
||||
kwargs['password'] = test_password
|
||||
if 'username' not in kwargs:
|
||||
kwargs['username'] = str(uuid.uuid4())
|
||||
return django_user_model.objects.create_user(**kwargs)
|
||||
|
||||
return make_user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def space_1(client):
|
||||
with scopes_disabled():
|
||||
return Space.objects.get_or_create(name='space_1')[0]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def space_2(client):
|
||||
with scopes_disabled():
|
||||
return Space.objects.get_or_create(name='space_2')[0]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_1(client, space_1):
|
||||
with scopes_disabled():
|
||||
user = User.objects.create(username='user_1')
|
||||
user.groups.add(Group.objects.get(name='user'))
|
||||
user.userpreference.space = space_1
|
||||
user.userpreference.save()
|
||||
client.force_login(user)
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_2(client, space_2):
|
||||
with scopes_disabled():
|
||||
user = User.objects.create(username='user_2')
|
||||
user.groups.add(Group.objects.get(name='user'))
|
||||
user.userpreference.space = space_2
|
||||
user.userpreference.save()
|
||||
client.force_login(user)
|
||||
return client
|
||||
20
cookbook/tests/pytest/test_test.py
Normal file
20
cookbook/tests/pytest/test_test.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.urls import reverse
|
||||
from django_scopes import scopes_disabled
|
||||
|
||||
from cookbook.models import Keyword, Space
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_user_create(user_1, user_2):
|
||||
r = user_1.post(reverse('api:keyword-list'), {'name': 'test', 'space': 1}, content_type='application/json')
|
||||
response = json.loads(r.content)
|
||||
assert r.status_code == 201
|
||||
assert response['name'] == 'test'
|
||||
|
||||
r = user_2.get(reverse('api:keyword-detail', args={response['id']}), content_type='application/json')
|
||||
assert r.status_code == 404
|
||||
3
pytest.ini
Normal file
3
pytest.ini
Normal file
@@ -0,0 +1,3 @@
|
||||
[pytest]
|
||||
DJANGO_SETTINGS_MODULE = recipes.settings
|
||||
python_files = tests.py test_*.py *_tests.py
|
||||
Reference in New Issue
Block a user