Repository navigation
Expand file tree
/
Copy pathtest_reporting.py
More file actions
428 lines (375 loc) · 16.9 KB
/
Copy pathtest_reporting.py
File metadata and controls
428 lines (375 loc) · 16.9 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""Complete reporting reads fail before malformed/truncated data becomes an aggregate."""
from __future__ import annotations
from types import SimpleNamespace
import httpx
import pyarrow as pa
import pytest
from tests.helpers import install
from tests.test_functions import _input, _Out, _Params
from vgi_github import auth, reporting
from vgi_github import functions as fn
from vgi_github import github_api as api
from vgi_github.caching import result_cache_control
from vgi_github.paging import PagedScanState, emit_search_page
from vgi_github.schemas import flatten_repo
from vgi_github.worker import GitHubCatalog
REPORTING = auth.encode_attach("off", "", reporting=True, operation_timeout_seconds=120)
def pull(index=1, **changes):
return {
"id": index * 10,
"number": index,
"state": "closed",
"draft": False,
"created_at": "2026-01-01T00:00:00Z",
"closed_at": "2026-01-01T02:00:00Z",
"merged_at": "2026-01-01T02:00:00Z",
"labels": [{"name": "bug"}],
**changes,
}
def run_pulls(monkeypatch, payload, *, max_rows=0, output=None):
install(monkeypatch, lambda r: httpx.Response(200, json=payload))
out = _Out()
fn.PullsFunction.process(
_Params(
fn.PullsArgs(repo="", max_rows=max_rows), output or fn.PULL_SCHEMA, attach_opaque_data=REPORTING
),
None,
_input(repo=["o/r"]),
out,
)
return out
class TestReportingAttach:
def test_policy_and_origin_are_discoverable(self):
result = GitHubCatalog().catalog_attach(
name="github",
options={"reporting": True, "operation_timeout_seconds": 17},
data_version_spec=None,
implementation_version=None,
)
assert auth.reporting_of(result.attach_opaque_data)
assert auth.operation_timeout_of(result.attach_opaque_data) == 17
assert result.tags["vgi_github_reporting"] == "true"
assert result.tags["vgi_github_operation_timeout_seconds"] == "17"
assert result.tags["vgi_github_api_origin"] == api.base_url()
@pytest.mark.parametrize("value", ["true", 1, None])
def test_reporting_is_boolean(self, value):
with pytest.raises(ValueError, match="reporting must be a boolean"):
GitHubCatalog().catalog_attach(name="github", options={"reporting": value})
@pytest.mark.parametrize("value", [0, -1, 3601, True, "120", 1.5])
def test_deadline_is_finite_bounded_integer(self, value):
with pytest.raises(ValueError, match="operation_timeout_seconds"):
GitHubCatalog().catalog_attach(name="github", options={"operation_timeout_seconds": value})
def test_bind_identity_nullability_does_not_request_data(self, monkeypatch):
seen = install(monkeypatch, lambda r: pytest.fail("bind must not fetch data"))
bound = fn.PullsFunction.on_bind(SimpleNamespace(attach_opaque_data=REPORTING))
assert not bound.output_schema.field("id").nullable
assert not bound.output_schema.field("repo").nullable
assert bound.output_schema.field("labels").type == pa.list_(pa.string())
assert fn.PULL_SCHEMA.field("id").nullable
assert seen == []
class TestReportingCompleteness:
def test_follows_all_pages_including_empty_page_with_next_link(self, monkeypatch):
base = api.base_url()
pages = iter(
[
httpx.Response(200, json=[pull(1)], headers={"link": f'<{base}/next>; rel="next"'}),
httpx.Response(200, json=[], headers={"link": f'<{base}/last>; rel="next"'}),
httpx.Response(200, json=[pull(2)]),
]
)
seen = install(monkeypatch, lambda r: next(pages))
out = _Out()
fn.PullsFunction.process(
_Params(fn.PullsArgs(repo="", max_rows=0), fn.PULL_SCHEMA, attach_opaque_data=REPORTING),
None,
_input(repo=["o/r"]),
out,
)
assert out.batches[0].column("number").to_pylist() == [1, 2]
assert len(seen) == 3
assert out.cache[0].no_store
def test_nonzero_cap_is_rejected_before_any_fetch(self, monkeypatch):
with pytest.raises(api.GitHubReportingError, match="max_rows => 0"):
run_pulls(monkeypatch, [], max_rows=1)
@pytest.mark.parametrize("status", [401, 403, 404, 409, 410, 451])
def test_missing_or_inaccessible_is_never_zero(self, monkeypatch, status):
install(monkeypatch, lambda r: httpx.Response(status, json={"message": "Not Found"}))
with api.reporting_operation(REPORTING):
with pytest.raises(api.GitHubError):
api.collect("/repos/o/r/pulls")
with pytest.raises(api.GitHubError):
api.lookup("/repos/o/r")
def test_only_explicit_empty_commit_repository_is_empty(self, monkeypatch):
install(monkeypatch, lambda r: httpx.Response(409, json={"message": "Git Repository is empty."}))
with api.reporting_operation(REPORTING):
assert api.collect("/repos/o/r/commits") == []
with pytest.raises(api.GitHubError):
api.collect("/repos/o/r/pulls")
@pytest.mark.parametrize("payload", [None, {}, [None], ["oops"]])
def test_malformed_payload_never_becomes_zero(self, monkeypatch, payload):
with pytest.raises(api.GitHubReportingError):
run_pulls(monkeypatch, payload)
def test_page_limit_still_errors(self, monkeypatch):
monkeypatch.setattr(api, "MAX_PAGES", 1)
install(
monkeypatch,
lambda r: httpx.Response(
200, json=[pull()], headers={"link": f'<{api.base_url()}/next>; rel="next"'}
),
)
with api.reporting_operation(REPORTING), pytest.raises(api.GitHubPageLimitError):
api.collect("/repos/o/r/pulls")
def test_workflow_endpoint_truncation_errors(self, monkeypatch):
install(
monkeypatch,
lambda r: httpx.Response(200, json={"total_count": 1001, "workflow_runs": [{"id": 1}]}),
)
with api.reporting_operation(REPORTING), pytest.raises(api.GitHubReportingError, match="total_count"):
api.collect("/repos/o/r/actions/runs", key="workflow_runs")
def test_reported_cache_policy_never_serves_stale(self):
hint = api.CacheHint(max_age=60)
assert result_cache_control(hint, 1000, REPORTING).no_store
assert result_cache_control(hint, 0, REPORTING).stale_if_error is None
assert result_cache_control(hint, 1000, None).stale_if_error == api.STALE_IF_ERROR
class TestReportingValidation:
@pytest.mark.parametrize(
"changes",
[
{"id": None},
{"id": 1.5},
{"id": True},
{"id": 0},
{"number": None},
{"created_at": None},
{"created_at": "yesterday"},
{"created_at": "2026-01-01T00:00:00"},
{"merged_at": "bad"},
{"merged_at": "2025-12-01T00:00:00Z"},
{"closed_at": None},
{"state": "open"},
],
)
def test_bad_identity_or_duration_rejected_even_if_projected_away(self, monkeypatch, changes):
with pytest.raises(api.GitHubReportingError):
run_pulls(monkeypatch, [pull(**changes)], output=pa.schema([fn.PULL_SCHEMA.field("labels")]))
def test_missing_merge_evidence_is_not_unmerged(self, monkeypatch):
row = pull()
del row["merged_at"]
with pytest.raises(api.GitHubReportingError, match="missing merged_at"):
run_pulls(monkeypatch, [row])
def test_duplicate_ids_error_before_output(self, monkeypatch):
with pytest.raises(api.GitHubReportingError, match="duplicate id"):
run_pulls(monkeypatch, [pull(1), pull(2, id=10)])
def test_nullable_unmerged_timestamp_and_list_details_survive(self, monkeypatch):
out = run_pulls(monkeypatch, [pull(1, merged_at=None), pull(2)])
assert out.batches[0].column("merged_at").null_count == 1
assert out.batches[0].column("labels").to_pylist() == [["bug"], ["bug"]]
class TestReportingDeadline:
def test_retry_wait_larger_than_remaining_budget_fails_without_sleep(self, monkeypatch):
install(monkeypatch, lambda r: httpx.Response(429, json={}, headers={"retry-after": "3"}))
waits = []
monkeypatch.setattr(api.time, "sleep", waits.append)
with (
api.reporting_operation(
auth.encode_attach("off", "", reporting=True, operation_timeout_seconds=1)
),
pytest.raises(api.GitHubDeadlineError, match="retry wait"),
):
api._get("/x")
assert waits == []
def test_late_success_is_rejected_and_request_timeout_is_clamped(self, monkeypatch):
clock = [10.0]
monkeypatch.setattr(api.time, "monotonic", lambda: clock[0])
def handler(request):
assert request.extensions["timeout"]["read"] == 5.0
clock[0] += 6
return httpx.Response(200, json=[])
install(monkeypatch, handler)
with pytest.raises(api.GitHubDeadlineError), api.reporting_operation(REPORTING, deadline=15):
api.collect("/x")
assert not api.reporting_enabled()
def test_operation_deadline_is_shared_across_pages(self, monkeypatch):
clock = [10.0]
monkeypatch.setattr(api.time, "monotonic", lambda: clock[0])
def handler(request):
clock[0] += 3
return httpx.Response(
200, json=[{"id": 1}], headers={"link": f'<{api.base_url()}/next>; rel="next"'}
)
seen = install(monkeypatch, handler)
with pytest.raises(api.GitHubDeadlineError), api.reporting_operation(REPORTING, deadline=15):
api.collect("/x")
assert len(seen) == 2
def test_search_is_capped_discovery_with_no_store(self, monkeypatch):
install(
monkeypatch,
lambda r: httpx.Response(
200,
json={
"total_count": 10000,
"incomplete_results": True,
"items": [{"id": 1, "full_name": "o/r"}],
},
),
)
out = _Out()
state = PagedScanState()
emit_search_page(
_Params(SimpleNamespace(cache_ttl=None), fn.REPO_SCHEMA, attach_opaque_data=REPORTING),
state,
out,
path="/search/repositories",
query={"q": "duckdb"},
flatten=flatten_repo,
)
assert out.cache[0].no_store
assert state.deadline > 0
assert out.batches[0].num_rows == 1
class TestReportingMeasures:
@pytest.mark.parametrize("value", [None, -1, 1.5, True, "5", 2**63])
def test_raw_release_download_counts_are_not_repaired(self, value):
with api.reporting_operation(REPORTING), pytest.raises(api.GitHubReportingError):
reporting.validate_release_payloads([{"assets": [{"download_count": value}]}])
@pytest.mark.parametrize("value", [None, {}, [None]])
def test_missing_or_malformed_assets_are_not_an_empty_release(self, value):
with api.reporting_operation(REPORTING), pytest.raises(api.GitHubReportingError):
reporting.validate_release_payloads([{"assets": value}])
def test_release_and_language_zero_counts_are_valid(self):
with api.reporting_operation(REPORTING):
reporting.validate_release_payloads([{"assets": []}, {"assets": [{"download_count": 0}]}])
reporting.validate_languages({"Python": 0})
@pytest.mark.parametrize("value", [None, -1, 1.5, True, "5"])
def test_language_bytes_are_not_coerced(self, value):
with api.reporting_operation(REPORTING), pytest.raises(api.GitHubReportingError):
reporting.validate_languages({"Python": value})
@pytest.mark.parametrize(
"name, row",
[
("contributors", {"repo": "o/r", "id": 1, "contributions": None}),
("repo", {"id": 1, "full_name": "o/r", "stargazers_count": None, "forks_count": 3}),
("repos", {"id": 1, "full_name": "o/r", "stargazers_count": 2, "forks_count": -1}),
],
)
def test_required_metric_counters_cannot_be_null_or_negative(self, name, row):
with api.reporting_operation(REPORTING), pytest.raises(api.GitHubReportingError):
reporting.validate_rows(name, [row])
def test_negative_issue_closure_duration_is_rejected(self):
row = {"repo": "o/r", **pull(closed_at="2025-12-01T00:00:00Z")}
with (
api.reporting_operation(REPORTING),
pytest.raises(api.GitHubReportingError, match="negative closure"),
):
reporting.validate_rows("issues", [row])
class TestReportingCohortFields:
@pytest.mark.parametrize(
"column,value",
[
("committer_date", None),
("committer_date", "not-a-date"),
("author_date", "not-a-date"),
("author_date", "2026-01-01T12:00:00"),
("is_verified", None),
("is_verified", "false"),
],
)
def test_commit_cohort_and_verification_cannot_silently_drop_rows(self, column, value):
row = {
"repo": "o/r",
"sha": "abc",
"committer_date": "2026-01-01T12:00:00Z",
"author_date": None,
"is_verified": False,
column: value,
}
with api.reporting_operation(REPORTING), pytest.raises(api.GitHubReportingError):
reporting.validate_rows("commits", [row])
@pytest.mark.parametrize("value", [None, "false", 0])
def test_pr_draft_flag_must_be_boolean_before_projection(self, monkeypatch, value):
with pytest.raises(api.GitHubReportingError, match="is_draft"):
run_pulls(monkeypatch, [pull(draft=value)], output=pa.schema([fn.PULL_SCHEMA.field("labels")]))
@pytest.mark.parametrize(
"changes",
[
{"is_draft": None},
{"is_prerelease": None},
{"is_prerelease": "false"},
{"published_at": None},
{"published_at": "not-a-date"},
],
)
def test_published_release_cohort_requires_its_evidence(self, changes):
row = {
"repo": "o/r",
"id": 1,
"download_count": 0,
"is_draft": False,
"is_prerelease": False,
"created_at": "2026-01-01T00:00:00Z",
"published_at": "2026-01-02T00:00:00Z",
**changes,
}
with api.reporting_operation(REPORTING), pytest.raises(api.GitHubReportingError):
reporting.validate_rows("releases", [row])
def test_draft_release_can_have_no_publication_timestamp(self):
row = {
"repo": "o/r",
"id": 1,
"download_count": 0,
"is_draft": True,
"is_prerelease": False,
"created_at": "2026-01-01T00:00:00Z",
"published_at": None,
}
with api.reporting_operation(REPORTING):
reporting.validate_rows("releases", [row])
@pytest.mark.parametrize(
"changes",
[
{"status": None},
{"status": "bogus"},
{"conclusion": None},
{"conclusion": "unknown"},
{"status": "queued", "conclusion": "success"},
],
)
def test_workflow_outcome_denominator_requires_a_valid_lifecycle(self, changes):
row = {
"repo": "o/r",
"id": 1,
"status": "completed",
"conclusion": "success",
"created_at": "2026-01-01T00:00:00Z",
"updated_at": "2026-01-01T01:00:00Z",
**changes,
}
with api.reporting_operation(REPORTING), pytest.raises(api.GitHubReportingError):
reporting.validate_rows("workflow_runs", [row])
@pytest.mark.parametrize("status,conclusion", [("queued", None), ("completed", "cancelled")])
def test_pending_or_never_started_cancelled_runs_are_valid(self, status, conclusion):
row = {
"repo": "o/r",
"id": 1,
"status": status,
"conclusion": conclusion,
"created_at": "2026-01-01T00:00:00Z",
"updated_at": "2026-01-01T01:00:00Z",
"run_started_at": None,
}
with api.reporting_operation(REPORTING):
reporting.validate_rows("workflow_runs", [row])
@pytest.mark.parametrize(
"function,args,column",
[
(fn.RepoFunction, fn.RepoArgs(repo=""), "repo"),
(fn.UserFunction, fn.LoginArgs(login=""), "login"),
],
)
def test_reporting_empty_object_is_not_a_missing_point_lookup(monkeypatch, function, args, column):
install(monkeypatch, lambda r: httpx.Response(200, json={}))
with pytest.raises(api.GitHubReportingError, match="exactly one valid object"):
function.process(
_Params(args, function.FIXED_SCHEMA, attach_opaque_data=REPORTING),
None,
_input(**{column: ["o/r" if column == "repo" else "alice"]}),
_Out(),
)