-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtest_search.py
178 lines (145 loc) · 5.39 KB
/
test_search.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from datetime import datetime
import pytest
from sqlalchemy.orm import Session
from app.config import PSQL_ENVIRONMENT
from app.database.models import Event, User
from app.internal.search import _get_stripped_keywords, get_results_by_keywords
class TestSearch:
"""Search feature tests.
Works with PostgreSQL.
"""
SEARCH = '/search'
GOOD_KEYWORDS = [
({'keywords': 'lov'}, b'test'),
({'keywords': 'very emotional'}, b'second event'),
({'keywords': 'event'}, b'My second event'),
({'keywords': 'event'}, b'My first event'),
({'keywords': 'jam'}, b'is fun'),
({'keywords': ' jam '}, b'is fun'),
]
BAD_KEYWORDS = [
({'keywords': ''}, b'Invalid'),
({'keywords': 'ev!@&'}, b'No matching'),
({'keywords': '[]'}, b'No matching'),
({'keywords': 'firsttttt'}, b'No matching'),
]
NOT_PSQL_ENV_KEYWORDS = [
({'keywords': 'lov'}, b'No matching'),
({'keywords': 'very emotional'}, b'No matching'),
({'keywords': 'event'}, b'No matching'),
({'keywords': 'jam'}, b'No matching'),
({'keywords': ' jam '}, b'No matching'),
({'keywords': ''}, b'Invalid'),
]
KEYWORDS_FOR_FUNC = [
'lov',
'very emotional',
'event',
'jam',
' jam ',
]
STRIPPED_KEYWORDS = [
(" love string ", "love:* & string:*"),
("test ", "test:*"),
("i am awesome", "i:* & am:* & awesome:*"),
("a lot of spaces", "a:* & lot:* & of:* & spaces:*"),
]
@staticmethod
def add_event(session: Session, title: str, content: str, owner_id: int):
"""Inserts an Event into the database.
Args:
session: The database fixture.
title: The Event's title.
content: The Event's content.
owner_id: The User's ID.
"""
event = Event(
title=title,
content=content,
start=datetime.today(),
end=datetime.today(),
owner_id=owner_id,
)
session.add(event)
session.commit()
@staticmethod
def create_event_data(session: Session, user: User):
"""Creates and adds Events to the database.
Args:
session: The database fixture.
user: The User fixture.
"""
events = [
{
'title': "My first event",
'content': 'I am so excited',
},
{
'title': "My second event",
'content': 'I am very emotional',
},
{
'title': "Pick up my nephews",
'content': 'Very important',
},
{
'title': "Solve this ticket",
'content': 'I can do this',
},
{
'title': "Jam with my friends",
'content': "Jamming is fun",
},
{
'title': 'test',
'content': 'love string',
}
]
for event in events:
TestSearch.add_event(session,
title=event['title'],
content=event['content'],
owner_id=user.id,
)
@staticmethod
def test_search_page_exists(client, session):
response = client.get(TestSearch.SEARCH)
assert response.ok
assert b'Search event by keyword' in response.content
@pytest.mark.skipif(not PSQL_ENVIRONMENT, reason="Not PSQL environment")
@pytest.mark.parametrize('data, string', TestSearch.GOOD_KEYWORDS)
def test_search_good_keywords(data, string, client, session, user):
TestSearch.create_event_data(session, user)
resp = client.post(TestSearch.SEARCH, data=data)
assert string in resp.content
@pytest.mark.skipif(not PSQL_ENVIRONMENT, reason="Not PSQL environment")
@pytest.mark.parametrize('data, string', TestSearch.BAD_KEYWORDS)
def test_search_bad_keywords(data, string, client, session, user):
TestSearch.create_event_data(session, user)
resp = client.post(TestSearch.SEARCH, data=data)
assert string in resp.content
@pytest.mark.skipif(PSQL_ENVIRONMENT, reason="PSQL environment")
@pytest.mark.parametrize('data, string', TestSearch.NOT_PSQL_ENV_KEYWORDS)
def test_search_not_psql_env_keywords(data, string, client, session, user):
TestSearch.create_event_data(session, user)
response = client.post(TestSearch.SEARCH, data=data)
assert string in response.content
@pytest.mark.skipif(PSQL_ENVIRONMENT, reason="Not PSQL environment")
@pytest.mark.parametrize('input_string', TestSearch.KEYWORDS_FOR_FUNC)
def test_get_results_by_keywords_func(input_string, client, session, user):
TestSearch.create_event_data(session, user)
assert not get_results_by_keywords(session, input_string, 1)
@pytest.mark.parametrize('input_string, output_string',
TestSearch.STRIPPED_KEYWORDS)
def test_search_stripped_keywords(input_string, output_string):
assert _get_stripped_keywords(input_string) == output_string
def test_events_tsv_column_exists():
column_created = True
try:
Event.events_tsv
except AttributeError:
column_created = False
if PSQL_ENVIRONMENT:
assert column_created
else:
assert not column_created