diff --git a/swh/web/common/origin_visits.py b/swh/web/common/origin_visits.py --- a/swh/web/common/origin_visits.py +++ b/swh/web/common/origin_visits.py @@ -138,9 +138,9 @@ return visit[0] if not visit_ts: - # returns the latest full visit when no timestamp is provided + # returns the latest visit with a valid snapshot when no timestamp is provided for v in reversed(visits): - if v["status"] == "full": + if v["snapshot"] is not None: return v return visits[-1] diff --git a/swh/web/tests/common/test_origin_visits.py b/swh/web/tests/common/test_origin_visits.py --- a/swh/web/tests/common/test_origin_visits.py +++ b/swh/web/tests/common/test_origin_visits.py @@ -3,29 +3,53 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information +from hypothesis import given + import pytest +from swh.model.hashutil import hash_to_hex from swh.web.common.exc import NotFoundExc from swh.web.common.origin_visits import get_origin_visits, get_origin_visit +from swh.web.tests.strategies import new_snapshots -def test_get_origin_visits(mocker): +@given(new_snapshots(3)) +def test_get_origin_visits(mocker, snapshots): mock_service = mocker.patch("swh.web.common.service") mock_service.MAX_LIMIT = 2 def _lookup_origin_visits(*args, **kwargs): if kwargs["last_visit"] is None: return [ - {"visit": 1, "date": "2017-05-06T00:59:10+00:00", "metadata": {}}, - {"visit": 2, "date": "2017-08-06T00:59:10+00:00", "metadata": {}}, + { + "visit": 1, + "date": "2017-05-06T00:59:10+00:00", + "status": "full", + "snapshot": hash_to_hex(snapshots[0].id), + "type": "git", + }, + { + "visit": 2, + "date": "2017-08-06T00:59:10+00:00", + "status": "full", + "snapshot": hash_to_hex(snapshots[1].id), + "type": "git", + }, ] else: - return [{"visit": 3, "date": "2017-09-06T00:59:10+00:00", "metadata": {}}] + return [ + { + "visit": 3, + "date": "2017-09-06T00:59:10+00:00", + "status": "full", + "snapshot": hash_to_hex(snapshots[2].id), + "type": "git", + } + ] mock_service.lookup_origin_visits.side_effect = _lookup_origin_visits origin_info = { - "id": 1, "url": "https://github.com/foo/bar", } @@ -34,7 +58,8 @@ assert len(origin_visits) == 3 -def test_get_origin_visit(mocker): +@given(new_snapshots(5)) +def test_get_origin_visit(mocker, snapshots): mock_origin_visits = mocker.patch("swh.web.common.origin_visits.get_origin_visits") origin_info = { "url": "https://github.com/foo/bar", @@ -46,6 +71,7 @@ "visit": 1, "origin": "https://github.com/foo/bar", "type": "git", + "snapshot": hash_to_hex(snapshots[0].id), }, { "status": "full", @@ -53,6 +79,7 @@ "visit": 2, "origin": "https://github.com/foo/bar", "type": "git", + "snapshot": hash_to_hex(snapshots[1].id), }, { "status": "full", @@ -60,6 +87,7 @@ "visit": 3, "origin": "https://github.com/foo/bar", "type": "git", + "snapshot": hash_to_hex(snapshots[2].id), }, { "status": "full", @@ -67,6 +95,7 @@ "visit": 4, "origin": "https://github.com/foo/bar", "type": "git", + "snapshot": hash_to_hex(snapshots[3].id), }, { "status": "full", @@ -74,6 +103,7 @@ "visit": 5, "origin": "https://github.com/foo/bar", "type": "git", + "snapshot": hash_to_hex(snapshots[4].id), }, ] mock_origin_visits.return_value = visits @@ -108,3 +138,59 @@ visit = get_origin_visit(origin_info, visit_ts="2018-01-01") assert visit == visits[-1] + + +def test_get_origin_visit_latest_valid_snapshot(mocker): + mock_origin_visits = mocker.patch("swh.web.common.origin_visits.get_origin_visits") + origin_info = { + "url": "https://nix-community.github.io/nixpkgs-swh/sources-unstable.json", + } + visits = [ + { + "date": "2020-04-15T12:42:52.936520+00:00", + "origin": origin_info["url"], + "snapshot": "d820451681c74eec63693b6ea4e4b8417c76bb7a", + "status": "partial", + "type": "nixguix", + "visit": 16, + }, + { + "date": "2020-04-17T17:25:13.738789+00:00", + "origin": origin_info["url"], + "snapshot": "d20627c1ae2b5e553e8adcf625f37e37cc5190dd", + "status": "partial", + "type": "nixguix", + "visit": 17, + }, + { + "date": "2020-04-19T19:02:42.906079+00:00", + "origin": origin_info["url"], + "snapshot": None, + "status": "partial", + "type": "nixguix", + "visit": 18, + }, + { + "date": "2020-04-20T12:43:41.120422+00:00", + "origin": origin_info["url"], + "snapshot": None, + "status": "partial", + "type": "nixguix", + "visit": 19, + }, + { + "date": "2020-04-20T12:46:40.255418+00:00", + "origin": origin_info["url"], + "snapshot": None, + "status": "partial", + "type": "nixguix", + "visit": 20, + }, + ] + + mock_origin_visits.return_value = visits + + visit = get_origin_visit(origin_info) + + assert visit["snapshot"] is not None + assert visit["visit"] == 17