Changeset View
Changeset View
Standalone View
Standalone View
swh/search/tests/test_search.py
# Copyright (C) 2019-2021 The Software Heritage developers | # Copyright (C) 2019-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from datetime import datetime, timedelta, timezone | |||||
from hypothesis import given, settings, strategies | from hypothesis import given, settings, strategies | ||||
import pytest | |||||
from swh.core.api.classes import stream_results | from swh.core.api.classes import stream_results | ||||
class CommonSearchTest: | class CommonSearchTest: | ||||
def test_origin_url_unique_word_prefix(self): | def test_origin_url_unique_word_prefix(self): | ||||
origin_foobar_baz = {"url": "http://foobar.baz"} | origin_foobar_baz = {"url": "http://foobar.baz"} | ||||
origin_barbaz_qux = {"url": "http://barbaz.qux"} | origin_barbaz_qux = {"url": "http://barbaz.qux"} | ||||
▲ Show 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | def test_origin_visit_types_update_search(self): | ||||
["hg"], | ["hg"], | ||||
["svn", "git"], | ["svn", "git"], | ||||
["hg", "git"], | ["hg", "git"], | ||||
["hg", "svn"], | ["hg", "svn"], | ||||
["git", "hg", "svn"], | ["git", "hg", "svn"], | ||||
] | ] | ||||
) | ) | ||||
def test_origin_nb_visits_update_search(self): | |||||
origin_url = "http://foobar.baz" | |||||
self.search.origin_update([{"url": origin_url}]) | |||||
self.search.flush() | |||||
def _update_nb_visits(nb_visits): | |||||
self.search.origin_update([{"url": origin_url, "nb_visits": nb_visits}]) | |||||
self.search.flush() | |||||
def _check_min_nb_visits(min_nb_visits): | |||||
actual_page = self.search.origin_search( | |||||
url_pattern=origin_url, min_nb_visits=min_nb_visits, | |||||
) | |||||
assert actual_page.next_page_token is None | |||||
results = [r["url"] for r in actual_page.results] | |||||
expected_results = [origin_url] | |||||
assert sorted(results) == sorted(expected_results) | |||||
_update_nb_visits(2) | |||||
_check_min_nb_visits(2) # Works for = 2 | |||||
_check_min_nb_visits(1) # Works for < 2 | |||||
with pytest.raises(AssertionError): | |||||
_check_min_nb_visits( | |||||
5 | |||||
) # No results for nb_visits >= 5 (should throw error) | |||||
_update_nb_visits(5) | |||||
_check_min_nb_visits(5) # Works for = 5 | |||||
_check_min_nb_visits(3) # Works for < 5 | |||||
def test_origin_last_visit_date_update_search(self): | |||||
origin_url = "http://foobar.baz" | |||||
self.search.origin_update([{"url": origin_url}]) | |||||
self.search.flush() | |||||
def _update_last_visit_date(last_visit_date): | |||||
self.search.origin_update( | |||||
[{"url": origin_url, "last_visit_date": last_visit_date}] | |||||
) | |||||
self.search.flush() | |||||
def _check_min_last_visit_date(min_last_visit_date): | |||||
actual_page = self.search.origin_search( | |||||
url_pattern=origin_url, min_last_visit_date=min_last_visit_date, | |||||
) | |||||
assert actual_page.next_page_token is None | |||||
results = [r["url"] for r in actual_page.results] | |||||
expected_results = [origin_url] | |||||
assert sorted(results) == sorted(expected_results) | |||||
now = datetime.now(tz=timezone.utc).isoformat() | |||||
now_minus_5_hours = ( | |||||
datetime.now(tz=timezone.utc) - timedelta(hours=5) | |||||
vlorentz: It's hard to spot the difference. Could you make the date constants and comment that the… | |||||
).isoformat() | |||||
now_plus_5_hours = ( | |||||
datetime.now(tz=timezone.utc) + timedelta(hours=5) | |||||
).isoformat() | |||||
_update_last_visit_date(now) | |||||
_check_min_last_visit_date(now) # Works for = | |||||
_check_min_last_visit_date(now_minus_5_hours) # Works for < | |||||
with pytest.raises(AssertionError): | |||||
_check_min_last_visit_date(now_plus_5_hours) # Fails for > | |||||
_update_last_visit_date(now_plus_5_hours) | |||||
_check_min_last_visit_date(now_plus_5_hours) # Works for = | |||||
_check_min_last_visit_date(now) # Works for < | |||||
def test_origin_update_with_no_visit_types(self): | def test_origin_update_with_no_visit_types(self): | ||||
""" | """ | ||||
Update an origin with visit types first then with no visit types, | Update an origin with visit types first then with no visit types, | ||||
check origin can still be searched with visit types afterwards. | check origin can still be searched with visit types afterwards. | ||||
""" | """ | ||||
origin_url = "http://foobar.baz" | origin_url = "http://foobar.baz" | ||||
self.search.origin_update([{"url": origin_url, "visit_types": ["git"]}]) | self.search.origin_update([{"url": origin_url, "visit_types": ["git"]}]) | ||||
self.search.flush() | self.search.flush() | ||||
▲ Show 20 Lines • Show All 427 Lines • Show Last 20 Lines |
It's hard to spot the difference. Could you make the date constants and comment that the difference is the years?