Coverage for tests/test_gather_snapshots.py: 100%
22 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-28 16:50 +0200
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-28 16:50 +0200
1import unittest
2from typing import Any
4from archive_md_urls import gather_snapshots
7class TestClosestSnapshot(unittest.TestCase):
8 """Test functions for gathering snapshots."""
10 def test_build_api_calls(self) -> None:
11 """Test if correct URL is constructed for API calls."""
12 # Base URL contained in every API call
13 api_base: str = "https://archive.org/wayback/available?url="
14 # Test with existing URL, first without and then with timestamp
15 existing_url: str = "google.com"
16 timestamp: str = "20140703"
17 self.assertEqual(
18 gather_snapshots.build_api_call(existing_url),
19 f"{api_base}{existing_url}"
20 )
21 self.assertEqual(
22 gather_snapshots.build_api_call(existing_url, timestamp),
23 f"{api_base}{existing_url}×tamp={timestamp}"
24 )
25 # Same tests again, but with invalid URL and bad timestamp
26 non_existend_url: str = "www.dsjfhldsjf.com"
27 bad_timestamp: str = "djshfjdls"
28 self.assertEqual(
29 gather_snapshots.build_api_call(non_existend_url),
30 f"{api_base}{non_existend_url}"
31 )
32 self.assertEqual(
33 gather_snapshots.build_api_call(non_existend_url, bad_timestamp),
34 f"{api_base}{non_existend_url}×tamp={bad_timestamp}"
35 )
37 def test_get_closest(self) -> None:
38 """Test if correct value is returned given various API responses."""
39 test_url: str = "http://web.archive.org/web/20210605231254/https://example.com/"
40 # We ignore the 'available' status
41 not_available: dict[str, Any] = {"archived_snapshots": {"closest":
42 {"available": False,
43 "url": test_url}}}
44 self.assertEqual(
45 gather_snapshots.get_closest(not_available), test_url
46 )
47 # Empty API response = None
48 empty: dict[str, Any] = {"archived_snapshots": {}}
49 self.assertEqual(
50 gather_snapshots.get_closest(empty), None
51 )
52 # More generic regex test
53 available: dict[str, Any] = {"archived_snapshots": {"closest":
54 {"available": True,
55 "url": test_url}}}
56 self.assertRegex(
57 gather_snapshots.get_closest(available),
58 r"http://web.archive.org/web/\d+/https://example.com/"
59 )