-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_protocol_serialization.py
More file actions
159 lines (140 loc) · 5.54 KB
/
test_protocol_serialization.py
File metadata and controls
159 lines (140 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""Round-trip and repr tests for protocol DTO serialization."""
from __future__ import annotations
import json
from datetime import datetime, timezone
import pytest
from core.activity_record import GenericActivityRecord
from core.activity_types import ActivityType, SourceSystem, actor_external_id
from core.incremental_state import GenericIncrementalState
from core.tracker_result import GenericTrackerResult
from boost_library_docs_tracker.protocol_impl import LibraryDocsTrackerResult
from boost_library_tracker.protocol_impl import CollectBoostLibrariesResult
from boost_library_usage_dashboard.protocol_impl import UsageDashboardTrackerResult
from boost_mailing_list_tracker.protocol_impl import (
MailingListIncrementalState,
MailingListTrackerResult,
)
from clang_github_tracker.protocol_impl import (
ClangGithubIncrementalState,
ClangGithubTrackerResult,
)
from cppa_pinecone_sync.protocol_impl import PineconeSyncTrackerResult
from cppa_slack_tracker.protocol_impl import SlackIncrementalState, SlackTrackerResult
from cppa_youtube_script_tracker.protocol_impl import YoutubeScriptTrackerResult
from discord_activity_tracker.protocol_impl import (
DiscordActivityRecord,
DiscordCollectionTrackerResult,
DiscordIncrementalState,
)
from github_activity_tracker.protocol_impl import (
GitHubActivityRecord,
GitHubIncrementalState,
GitHubSyncTrackerResult,
)
from wg21_paper_tracker.protocol_impl import Wg21PaperTrackerResult
_TRACKER_RESULTS = [
GenericTrackerResult.ok(),
GenericTrackerResult.failed("oops", items=0),
GenericTrackerResult(
success=False,
counts={"items": 1},
errors=("e1", "e2"),
duration_seconds=1.5,
),
GitHubSyncTrackerResult(success=True, counts={"issues": 1}),
DiscordCollectionTrackerResult(success=True, counts={"messages": 2}),
PineconeSyncTrackerResult.from_sync_dict(
{"upserted": 1, "total": 1, "failed_count": 0}
),
UsageDashboardTrackerResult.from_stats({"repos_analyzed": 3}),
Wg21PaperTrackerResult.dry_run(),
ClangGithubTrackerResult.dry_run(),
MailingListTrackerResult.from_run(fetched=1, created=1, skipped=0),
SlackTrackerResult.dry_run(),
YoutubeScriptTrackerResult.from_run(videos=1),
LibraryDocsTrackerResult.from_run(versions=1, pages=5),
CollectBoostLibrariesResult.empty(),
]
_INCREMENTAL_STATES = [
GenericIncrementalState(checkpoint_token="t", human_readable_marker="m"),
GitHubIncrementalState.from_repo_watermark(repo_id=1, marker="2024"),
DiscordIncrementalState.from_after_date(after=None),
DiscordIncrementalState.from_after_date(
after=datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc),
last_message_id=100,
channel_id=55,
),
MailingListIncrementalState.from_start_date("2024-01-01"),
SlackIncrementalState.from_team(team_id="T1", start_date="2024-01-01"),
ClangGithubIncrementalState.from_watermarks(start_commit="abc", start_item="2024"),
]
_ACTIVITY_RECORDS = [
GitHubActivityRecord.from_issue(repo_id=7, issue_number=123, summary="title"),
DiscordActivityRecord.from_converted_export_dict(
{
"id": 5,
"created_at": "2024-01-01T00:00:00.0000000Z",
"message_type": "Reply",
"content": "hello",
"author": {"id": 7},
},
server_id=1,
channel_id=2,
),
GenericActivityRecord(
source_system=SourceSystem.GITHUB,
external_id="1:issue:1",
occurred_at=None,
activity_type=ActivityType.github_issue(),
actor_external_id=actor_external_id(""),
source_url=None,
summary="x" * 200,
),
]
@pytest.mark.parametrize("obj", _TRACKER_RESULTS, ids=lambda o: type(o).__name__)
def test_tracker_result_round_trip(obj) -> None:
restored = type(obj).from_dict(obj.asdict())
assert restored == obj
assert json.loads(obj.to_json()) == obj.asdict()
json.dumps(obj.asdict())
@pytest.mark.parametrize("obj", _INCREMENTAL_STATES, ids=lambda o: type(o).__name__)
def test_incremental_state_round_trip(obj) -> None:
restored = type(obj).from_dict(obj.asdict())
assert restored == obj
assert json.loads(obj.to_json()) == obj.asdict()
json.dumps(obj.asdict())
@pytest.mark.parametrize("obj", _ACTIVITY_RECORDS, ids=lambda o: type(o).__name__)
def test_activity_record_round_trip(obj) -> None:
restored = type(obj).from_dict(obj.asdict())
assert restored == obj
assert json.loads(obj.to_json()) == obj.asdict()
json.dumps(obj.asdict())
@pytest.mark.parametrize(
"obj", _TRACKER_RESULTS + _INCREMENTAL_STATES + _ACTIVITY_RECORDS
)
def test_to_json_is_deterministic(obj) -> None:
again = type(obj).from_dict(json.loads(obj.to_json()))
assert again.to_json() == obj.to_json()
def test_tracker_result_repr_truncates_many_errors() -> None:
obj = GenericTrackerResult(
success=False,
counts={"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6},
errors=("one", "two", "three"),
)
text = repr(obj)
assert "errors=<3 items>" in text
assert "counts=" in text
assert "<6 keys>" in text
def test_activity_record_repr_truncates_summary() -> None:
obj = GenericActivityRecord(
source_system=SourceSystem.DISCORD,
external_id="1:2:3",
occurred_at=datetime(2024, 1, 1, tzinfo=timezone.utc),
activity_type=ActivityType.discord_message("Default"),
actor_external_id=actor_external_id("1"),
source_url=None,
summary="word " * 50,
)
text = repr(obj)
assert "..." in text
assert len(text) < 400