|
| 1 | +"""Tests for --incremental-by-files timestamp comparison. |
| 2 | +
|
| 3 | +A saved file is skipped when its modification time is newer than the item's |
| 4 | +``updated_at``. GitHub's timestamps are UTC, so the file's modification time |
| 5 | +must be compared in UTC too, whatever the local time zone is. |
| 6 | +""" |
| 7 | + |
| 8 | +import calendar |
| 9 | +import json |
| 10 | +import os |
| 11 | +import time |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from github_backup import github_backup |
| 16 | + |
| 17 | +UPDATED_AT = "2026-07-01T12:00:00Z" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def east_of_utc(monkeypatch): |
| 22 | + """Run the test with the local time zone at UTC+10 (no daylight saving).""" |
| 23 | + monkeypatch.setenv("TZ", "Etc/GMT-10") |
| 24 | + time.tzset() |
| 25 | + yield |
| 26 | + monkeypatch.undo() |
| 27 | + time.tzset() |
| 28 | + |
| 29 | + |
| 30 | +def _stored(path, number, written_at): |
| 31 | + """Save an item file whose modification time is ``written_at`` (UTC).""" |
| 32 | + path.mkdir(parents=True, exist_ok=True) |
| 33 | + item_file = path / "{0}.json".format(number) |
| 34 | + item_file.write_text(json.dumps({"number": number, "title": "stored"})) |
| 35 | + mtime = calendar.timegm(time.strptime(written_at, "%Y-%m-%dT%H:%M:%SZ")) |
| 36 | + os.utime(item_file, (mtime, mtime)) |
| 37 | + return item_file |
| 38 | + |
| 39 | + |
| 40 | +def _fake_retrieve(items): |
| 41 | + def _retrieve(args, template, query_args=None, **kwargs): |
| 42 | + return items if template.endswith(("/issues", "/pulls")) else [] |
| 43 | + |
| 44 | + return _retrieve |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "written_at, refetched", |
| 49 | + [ |
| 50 | + ("2026-07-01T10:00:00Z", True), # updated 2 hours after it was saved |
| 51 | + ("2026-07-01T14:00:00Z", False), # saved 2 hours after the update |
| 52 | + ], |
| 53 | +) |
| 54 | +def test_issue_skip_compares_in_utc( |
| 55 | + create_args, tmp_path, monkeypatch, east_of_utc, written_at, refetched |
| 56 | +): |
| 57 | + issue_file = _stored(tmp_path / "issues", 1, written_at) |
| 58 | + issue = {"number": 1, "title": "updated", "updated_at": UPDATED_AT} |
| 59 | + monkeypatch.setattr(github_backup, "retrieve_data", _fake_retrieve([issue])) |
| 60 | + args = create_args(include_issues=True, incremental_by_files=True, since=None) |
| 61 | + |
| 62 | + github_backup.backup_issues( |
| 63 | + args, str(tmp_path), {"full_name": "owner/repo"}, "https://api.github.com/repos" |
| 64 | + ) |
| 65 | + |
| 66 | + title = json.loads(issue_file.read_text())["title"] |
| 67 | + assert title == ("updated" if refetched else "stored") |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + "written_at, refetched", |
| 72 | + [ |
| 73 | + ("2026-07-01T10:00:00Z", True), |
| 74 | + ("2026-07-01T14:00:00Z", False), |
| 75 | + ], |
| 76 | +) |
| 77 | +def test_pull_skip_compares_in_utc( |
| 78 | + create_args, tmp_path, monkeypatch, east_of_utc, written_at, refetched |
| 79 | +): |
| 80 | + pull_file = _stored(tmp_path / "pulls", 1, written_at) |
| 81 | + pull = {"number": 1, "title": "updated", "updated_at": UPDATED_AT} |
| 82 | + monkeypatch.setattr(github_backup, "retrieve_data", _fake_retrieve([pull])) |
| 83 | + args = create_args(include_pulls=True, incremental_by_files=True, since=None) |
| 84 | + |
| 85 | + github_backup.backup_pulls( |
| 86 | + args, str(tmp_path), {"full_name": "owner/repo"}, "https://api.github.com/repos" |
| 87 | + ) |
| 88 | + |
| 89 | + title = json.loads(pull_file.read_text())["title"] |
| 90 | + assert title == ("updated" if refetched else "stored") |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize( |
| 94 | + "written_at, refetched", |
| 95 | + [ |
| 96 | + ("2026-07-01T10:00:00Z", True), |
| 97 | + ("2026-07-01T14:00:00Z", False), |
| 98 | + ], |
| 99 | +) |
| 100 | +def test_discussion_skip_compares_in_utc( |
| 101 | + create_args, tmp_path, monkeypatch, east_of_utc, written_at, refetched |
| 102 | +): |
| 103 | + discussion_file = _stored(tmp_path / "discussions", 1, written_at) |
| 104 | + summary = {"number": 1, "updatedAt": UPDATED_AT} |
| 105 | + monkeypatch.setattr( |
| 106 | + github_backup, |
| 107 | + "retrieve_discussion_summaries", |
| 108 | + lambda args, repository, since=None: ([summary], UPDATED_AT, True, 1), |
| 109 | + ) |
| 110 | + monkeypatch.setattr( |
| 111 | + github_backup, |
| 112 | + "retrieve_discussion", |
| 113 | + lambda args, repository, number: {"number": number, "title": "updated"}, |
| 114 | + ) |
| 115 | + args = create_args( |
| 116 | + include_discussions=True, incremental_by_files=True, token_classic="ghp_test" |
| 117 | + ) |
| 118 | + |
| 119 | + github_backup.backup_discussions(args, str(tmp_path), {"full_name": "owner/repo"}) |
| 120 | + |
| 121 | + title = json.loads(discussion_file.read_text())["title"] |
| 122 | + assert title == ("updated" if refetched else "stored") |
| 123 | + |
| 124 | + |
| 125 | +def test_checkpoint_fallback_time_is_utc(east_of_utc): |
| 126 | + before = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) |
| 127 | + checkpoint = github_backup.get_repository_checkpoint_time({}) |
| 128 | + after = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) |
| 129 | + |
| 130 | + assert before <= checkpoint <= after |
0 commit comments