-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_protocols.py
More file actions
204 lines (172 loc) · 6.64 KB
/
test_protocols.py
File metadata and controls
204 lines (172 loc) · 6.64 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""Runtime checks for :mod:`core.protocols` and ``core/pyright_samples`` Pyright snippets."""
from __future__ import annotations
import json
import subprocess
import sys
from pathlib import Path
import pytest
from core.activity_types import (
ActivityType,
SourceSystem,
actor_external_id,
parse_activity_occurred_at,
)
from core.protocols import (
ActivityRecord,
IncrementalState,
TrackerResult,
require_activity_record,
require_incremental_state,
require_tracker_result,
)
from core.tracker_result import GenericTrackerResult
from discord_activity_tracker.protocol_impl import (
DiscordActivityRecord,
DiscordCollectionTrackerResult,
)
from github_activity_tracker.protocol_impl import (
GitHubActivityRecord,
GitHubIncrementalState,
GitHubSyncTrackerResult,
)
_REPO_ROOT = Path(__file__).resolve().parents[2]
_TYPING_DIR = _REPO_ROOT / "core" / "pyright_samples"
def test_tracker_result_isinstance_github_dataclass() -> None:
r = GitHubSyncTrackerResult(success=True, counts={"issues": 2, "pull_requests": 1})
assert isinstance(r, TrackerResult)
assert r.counts["issues"] == 2
assert r.errors == ()
assert r.duration_seconds is None
def test_tracker_result_isinstance_generic_dataclass() -> None:
r = GenericTrackerResult.ok(items=1)
assert isinstance(r, TrackerResult)
assert r.errors == ()
def test_require_incremental_state_raises_type_error_on_bad_object() -> None:
class NotState:
checkpoint_token = "x"
with pytest.raises(TypeError, match="IncrementalState"):
require_incremental_state(NotState())
def test_activity_record_isinstance_discord_dataclass() -> None:
rec = DiscordActivityRecord(
source_system=SourceSystem.DISCORD,
external_id="1:2:3",
occurred_at=parse_activity_occurred_at("2024-01-01T00:00:00Z"),
activity_type=ActivityType.discord_message("Default"),
actor_external_id=actor_external_id("99"),
source_url="https://discord.com/channels/1/2/3",
summary="hi",
)
assert isinstance(rec, ActivityRecord)
def test_incremental_state_isinstance_github() -> None:
st = GitHubIncrementalState.from_repo_watermark(repo_id=42, marker="2024-06")
assert isinstance(st, IncrementalState)
def test_activity_record_isinstance_github_from_issue() -> None:
rec = GitHubActivityRecord.from_issue(repo_id=7, issue_number=123, summary="title")
assert isinstance(rec, ActivityRecord)
assert "7:issue:123" in rec.external_id
def test_tracker_result_isinstance_discord_dataclass() -> None:
r = DiscordCollectionTrackerResult(
success=True, counts={"messages": 5, "channels": 1}
)
assert isinstance(r, TrackerResult)
def test_require_tracker_result_raises_type_error_on_bad_object() -> None:
class NotAResult:
success = True
# missing counts
with pytest.raises(TypeError, match="TrackerResult"):
require_tracker_result(NotAResult())
def test_require_activity_record_raises_type_error_on_bad_object() -> None:
class NotARecord:
source_system = "x"
# missing fields
with pytest.raises(TypeError, match="ActivityRecord"):
require_activity_record(NotARecord())
def test_pyright_positive_protocol_assignment_file() -> None:
path = _TYPING_DIR / "protocol_assignment_positive.py"
proc = subprocess.run(
[sys.executable, "-m", "pyright", str(path)],
cwd=_REPO_ROOT,
capture_output=True,
text=True,
check=False,
)
if proc.returncode != 0 and "No module named pyright" in (proc.stderr or ""):
pytest.skip("pyright not installed (pip install -r requirements-dev.lock)")
assert proc.returncode == 0, proc.stdout + proc.stderr
def test_pyright_negative_activity_record_assignment_file(tmp_path: Path) -> None:
src = _TYPING_DIR / "activity_record_assignment_negative.py"
dest = tmp_path / "activity_record_assignment_negative.py"
dest.write_text(src.read_text(encoding="utf-8"), encoding="utf-8")
cfg_path = tmp_path / "pyrightconfig.json"
cfg_path.write_text(
json.dumps(
{
"include": ["activity_record_assignment_negative.py"],
"exclude": [],
"pythonVersion": "3.13",
"typeCheckingMode": "basic",
"reportMissingImports": True,
"executionEnvironments": [
{
"root": str(tmp_path.resolve()),
"extraPaths": [str(_REPO_ROOT.resolve())],
}
],
}
),
encoding="utf-8",
)
proc = subprocess.run(
[sys.executable, "-m", "pyright", "--project", str(tmp_path)],
cwd=_REPO_ROOT,
capture_output=True,
text=True,
check=False,
)
stderr = proc.stderr or ""
if "No module named pyright" in stderr:
pytest.skip("pyright not installed (pip install -r requirements-dev.lock)")
assert proc.returncode != 0, (
"expected pyright errors for activity_record_assignment_negative.py; "
f"stdout={proc.stdout!r} stderr={stderr!r}"
)
def test_pyright_negative_protocol_assignment_file(tmp_path: Path) -> None:
"""Run Pyright in an isolated project so root ``pyrightconfig`` excludes do not skip the file."""
src = _TYPING_DIR / "protocol_assignment_negative.py"
dest = tmp_path / "protocol_assignment_negative.py"
dest.write_text(src.read_text(encoding="utf-8"), encoding="utf-8")
cfg_path = tmp_path / "pyrightconfig.json"
cfg_path.write_text(
json.dumps(
{
"include": ["protocol_assignment_negative.py"],
"exclude": [],
"pythonVersion": "3.11",
"typeCheckingMode": "basic",
"reportMissingImports": True,
"stubPath": "",
"executionEnvironments": [
{
"root": str(tmp_path.resolve()),
"extraPaths": [str(_REPO_ROOT.resolve())],
}
],
}
),
encoding="utf-8",
)
proc = subprocess.run(
[sys.executable, "-m", "pyright", "--project", str(tmp_path)],
cwd=_REPO_ROOT,
capture_output=True,
text=True,
check=False,
)
stderr = proc.stderr or ""
stdout = proc.stdout or ""
if "No module named pyright" in stderr:
pytest.skip("pyright not installed (pip install -r requirements-dev.lock)")
assert proc.returncode != 0, (
"expected pyright errors for protocol_assignment_negative.py; "
f"stdout={stdout!r} stderr={stderr!r}"
)