-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
52 lines (45 loc) · 1.85 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os, sys
sys.path.append('.')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.conf import settings
from django.test.client import Client
from django.test.utils import setup_test_environment, teardown_test_environment
from django.core.management import call_command
from django.core import mail
from django.contrib.auth.models import User
from bounty.models import Project
def pytest_funcarg__django_client(request):
old_name = settings.DATABASE_NAME
def setup():
setup_test_environment()
settings.DEBUG = False
from django.db import connection
connection.creation.create_test_db(1, True)
return Client()
def teardown(client):
teardown_test_environment()
from django.db import connection
connection.creation.destroy_test_db(old_name, 1)
return request.cached_setup(setup, teardown, "session")
def pytest_funcarg__client(request):
def setup():
return request.getfuncargvalue('django_client')
def teardown(client):
call_command('flush', verbosity=0, interactive=False)
mail.outbox = []
return request.cached_setup(setup, teardown, "function")
# I make test usernames and passwords identical for easy login
def pytest_funcarg__user(request):
'''Create a user with no special permissions.'''
user = User.objects.create_user(username="user", password="user", email="user@example.com")
return user
def pytest_funcarg__admin(request):
'''Create an admin user with all permissions.'''
admin = User.objects.create_user(username="admin", password="admin", email="admin@example.com")
admin.is_superuser = True
admin.save()
return admin
def pytest_funcarg__project(request):
user = request.getfuncargvalue('user')
return Project.objects.create(name="Test Project",
description="This project is a test", creator=user)