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_visit_update_search(self): | |||||
origin_url = "http://foobar.baz" | |||||
self.search.origin_update([{"url": origin_url}]) | |||||
self.search.flush() | |||||
def _update_nb_visit(nb_visit): | |||||
self.search.origin_update([{"url": origin_url, "nb_visit": nb_visit}]) | |||||
self.search.flush() | |||||
def _check_nb_visit(nb_visit): | |||||
actual_page = self.search.origin_search( | |||||
url_pattern=origin_url, nb_visit=nb_visit, | |||||
) | |||||
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_visit(5) | |||||
_check_nb_visit(5) | |||||
_update_nb_visit(3) | |||||
_check_nb_visit(5) # nb_visit shouldn't get updated and remain remain 5 | |||||
with pytest.raises(AssertionError): | |||||
_check_nb_visit(3) | |||||
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_last_visit_date(last_visit_date): | |||||
actual_page = self.search.origin_search( | |||||
url_pattern=origin_url, last_visit_date=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_years = ( | |||||
datetime.now(tz=timezone.utc) - timedelta(years=5) | |||||
).isoformat() | |||||
_update_last_visit_date(now) | |||||
_check_last_visit_date(now) | |||||
vlorentz: It's hard to spot the difference. Could you make the date constants and comment that the… | |||||
_update_last_visit_date(now_minus_5_years) | |||||
_check_last_visit_date(now) | |||||
with pytest.raises(AssertionError): | |||||
_check_last_visit_date(now_minus_5_years) | |||||
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 422 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?