2020-04-16 02:41:53 +03:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from app.filter import Filter
|
2020-06-02 21:54:47 +03:00
|
|
|
from app.utils.misc import generate_user_keys
|
2020-04-16 02:41:53 +03:00
|
|
|
from datetime import datetime
|
|
|
|
from dateutil.parser import *
|
|
|
|
|
|
|
|
|
|
|
|
def get_search_results(data):
|
2020-06-02 21:54:47 +03:00
|
|
|
secret_key = generate_user_keys()
|
|
|
|
soup = Filter(user_keys=secret_key).clean(BeautifulSoup(data, 'html.parser'))
|
2020-04-16 02:41:53 +03: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 03:59:33 +03:00
|
|
|
def test_get_results(client):
|
2020-04-16 02:41:53 +03:00
|
|
|
rv = client.get('/search?q=test')
|
|
|
|
assert rv._status_code == 200
|
|
|
|
|
2020-04-16 02:54:38 +03: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-16 02:41:53 +03:00
|
|
|
|
|
|
|
|
2020-04-29 03:59:33 +03: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-16 02:41:53 +03:00
|
|
|
def test_recent_results(client):
|
|
|
|
times = {
|
2020-04-29 03:59:33 +03:00
|
|
|
'past year': 365,
|
|
|
|
'past month': 31,
|
|
|
|
'past week': 7
|
2020-04-16 02:41:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for time, num_days in times.items():
|
2020-04-29 03:59:33 +03:00
|
|
|
rv = client.post('/search', data=dict(q='test :' + time))
|
2020-04-16 02:41:53 +03: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 03:11:02 +03:00
|
|
|
if not date_span or len(date_span) > 15 or len(date_span) < 7:
|
2020-04-16 02:41:53 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
date = parse(date_span)
|
2020-05-20 20:07:01 +03:00
|
|
|
assert (current_date - date).days <= (num_days + 5) # Date can have a little bit of wiggle room
|
2020-04-16 02:41:53 +03:00
|
|
|
except ParserError:
|
2020-05-23 23:27:23 +03:00
|
|
|
pass
|