2020-04-15 23:41:53 +00:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from app.filter import Filter
|
2020-06-02 18:54:47 +00:00
|
|
|
from app.utils.misc import generate_user_keys
|
2020-04-15 23:41:53 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from dateutil.parser import *
|
|
|
|
|
|
|
|
|
|
|
|
def get_search_results(data):
|
2020-06-02 18:54:47 +00:00
|
|
|
secret_key = generate_user_keys()
|
|
|
|
soup = Filter(user_keys=secret_key).clean(BeautifulSoup(data, 'html.parser'))
|
2020-04-15 23:41:53 +00:00
|
|
|
|
|
|
|
main_divs = soup.find('div', {'id': 'main'})
|
|
|
|
assert len(main_divs) > 1
|
|
|
|
|
|
|
|
result_divs = []
|
|
|
|
for div in main_divs:
|
|
|
|
# Result divs should only have 1 inner div
|
|
|
|
if len(list(div.children)) != 1 or not div.findChild() or 'div' not in div.findChild().name:
|
|
|
|
continue
|
|
|
|
|
|
|
|
result_divs.append(div)
|
|
|
|
|
|
|
|
return result_divs
|
|
|
|
|
|
|
|
|
2020-04-29 00:59:33 +00:00
|
|
|
def test_get_results(client):
|
2020-04-15 23:41:53 +00:00
|
|
|
rv = client.get('/search?q=test')
|
|
|
|
assert rv._status_code == 200
|
|
|
|
|
2020-04-15 23:54:38 +00:00
|
|
|
# Depending on the search, there can be more
|
|
|
|
# than 10 result divs
|
|
|
|
assert len(get_search_results(rv.data)) >= 10
|
|
|
|
assert len(get_search_results(rv.data)) <= 15
|
2020-04-15 23:41:53 +00:00
|
|
|
|
|
|
|
|
2020-04-29 00:59:33 +00:00
|
|
|
def test_post_results(client):
|
|
|
|
rv = client.post('/search', data=dict(q='test'))
|
|
|
|
assert rv._status_code == 200
|
|
|
|
|
|
|
|
# Depending on the search, there can be more
|
|
|
|
# than 10 result divs
|
|
|
|
assert len(get_search_results(rv.data)) >= 10
|
|
|
|
assert len(get_search_results(rv.data)) <= 15
|
|
|
|
|
|
|
|
|
2020-04-15 23:41:53 +00:00
|
|
|
def test_recent_results(client):
|
|
|
|
times = {
|
2020-04-29 00:59:33 +00:00
|
|
|
'past year': 365,
|
|
|
|
'past month': 31,
|
|
|
|
'past week': 7
|
2020-04-15 23:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for time, num_days in times.items():
|
2020-04-29 00:59:33 +00:00
|
|
|
rv = client.post('/search', data=dict(q='test :' + time))
|
2020-04-15 23:41:53 +00:00
|
|
|
result_divs = get_search_results(rv.data)
|
|
|
|
|
|
|
|
current_date = datetime.now()
|
|
|
|
for div in result_divs:
|
|
|
|
date_span = div.find('span').decode_contents()
|
2020-04-27 00:11:02 +00:00
|
|
|
if not date_span or len(date_span) > 15 or len(date_span) < 7:
|
2020-04-15 23:41:53 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
date = parse(date_span)
|
2020-05-20 17:07:01 +00:00
|
|
|
assert (current_date - date).days <= (num_days + 5) # Date can have a little bit of wiggle room
|
2020-04-15 23:41:53 +00:00
|
|
|
except ParserError:
|
2020-05-23 20:27:23 +00:00
|
|
|
pass
|