-
Notifications
You must be signed in to change notification settings - Fork 7
/
conftest.py
132 lines (111 loc) · 3.64 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
import sys
import pytest
import haystack
from rest_framework.test import APIClient
from django.core.management import call_command
from unittest.mock import patch, Mock
from readme.models import User, Item
from django.conf import settings
def pytest_configure():
# workaround to avoid django pipeline issue
# refers to
settings.STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
QUEEN = 'queen with spaces änd umlauts'
EXAMPLE_COM = 'http://www.example.com/'
@pytest.fixture
def user(db):
try:
user = User.objects.get(username='admin')
except User.DoesNotExist:
user = User.objects.create_user('admin', 'admin@example.com',
'password')
user.is_staff = True
user.is_superuser = True
user.save()
return user
@pytest.fixture
def other_user(db):
try:
user = User.objects.get(username='other_user')
except User.DoesNotExist:
user = User.objects.create_user('other_user', 'other_user@example.com',
'password')
user.is_staff = True
user.is_superuser = True
user.save()
return user
@pytest.fixture
def api_user(db):
try:
user = User.objects.get(username='dev')
except User.DoesNotExist:
user = User.objects.create_user('dev', 'dev@example.com',
'dev')
user.is_staff = True
user.is_superuser = True
user.save()
return user
@pytest.fixture
def user_client(client, user):
client.login(username='admin', password='password')
return client
@pytest.fixture
def api_client(api_user):
client = APIClient()
client.login(username='dev', password='dev')
return client
def add_example_item(user, tags=None):
item = Item.objects.create(url=EXAMPLE_COM, title='nothing', owner=user, readable_article="")
if tags is not None:
item.tags.add(*tags)
item.save()
return item
@pytest.yield_fixture(scope='module')
def tagged_items(db, user):
items = [
add_example_item(user, ('fish', 'boxing')),
add_example_item(user, ('fish', QUEEN)),
add_example_item(user, (QUEEN, 'bartender')),
add_example_item(user, (QUEEN, 'pypo')),
add_example_item(user, tuple())]
yield items
for item in items:
item.delete()
@pytest.fixture
def clear_index():
call_command('clear_index', interactive=False, verbosity=0)
@pytest.fixture
def get_mock(request, clear_index):
patcher = patch('requests.get')
get_mock = patcher.start()
return_mock = Mock(headers={'content-type': 'text/html',
'content-length': 500},
encoding='utf-8')
return_mock.iter_content.return_value = iter([b"example.com"])
get_mock.return_value = return_mock
def fin():
patcher.stop()
request.addfinalizer(fin)
return get_mock
TEST_INDEX = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index_test'),
},
}
@pytest.fixture
def test_index(settings):
settings.HAYSTACK_CONNECTIONS = TEST_INDEX
call_command('clear_index', interactive=False, verbosity=0)
haystack.connections.reload('default')
@pytest.yield_fixture(scope='module')
def simple_items(db, user):
items = {
'item_fish': add_example_item(user, [QUEEN, 'fish', 'cookie']),
'item_box': add_example_item(user, [QUEEN, 'box']),
'filter': Item.objects.filter(owner_id=user.id),
}
yield items
items['item_fish'].delete()
items['item_box'].delete()