-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path_fakes.py
More file actions
63 lines (49 loc) · 2.02 KB
/
Copy path_fakes.py
File metadata and controls
63 lines (49 loc) · 2.02 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
"""Shared test doubles for the unified harness test suites.
A single superset implementation of the in-memory tracing backend used across
the harness tests. Three recording shapes were previously duplicated:
- Shape-1 (richest): ``started`` = ``(name, parent_id, input)`` 3-tuples,
``ended`` = ``(name, output)`` 2-tuples, plus an ``ended_spans`` list of the
closed ``FakeSpan`` objects (which carry ``.name``, ``.output``, ``.data``).
- Shape-2: ``started`` = ``(name, parent_id)`` 2-tuples, ``ended`` =
``(name, output)``.
- Shape-3: ``started`` = bare names, ``ended`` = bare outputs.
``FakeTracing`` records the richest (shape-1) form and exposes read-only
convenience properties (``started_names``, ``started_pairs``,
``ended_outputs``) so shape-2 and shape-3 assertions stay clean.
"""
from __future__ import annotations
from typing import Any
class FakeSpan:
def __init__(self, name: str) -> None:
self.name = name
self.output: Any = None
self.data: Any = None
class FakeTracing:
def __init__(self) -> None:
self.started: list[tuple[str, Any, Any]] = []
self.ended: list[tuple[str, Any]] = []
self.ended_spans: list[FakeSpan] = []
async def start_span(
self,
*,
trace_id: str,
name: str,
input: Any = None,
parent_id: Any = None,
data: Any = None,
task_id: Any = None,
) -> FakeSpan:
self.started.append((name, parent_id, input))
return FakeSpan(name)
async def end_span(self, *, trace_id: str, span: FakeSpan) -> None:
self.ended.append((span.name, span.output))
self.ended_spans.append(span)
@property
def started_names(self) -> list[str]:
return [name for (name, _parent, _input) in self.started]
@property
def started_pairs(self) -> list[tuple[str, Any]]:
return [(name, parent) for (name, parent, _input) in self.started]
@property
def ended_outputs(self) -> list[Any]:
return [output for (_name, output) in self.ended]