|
| 1 | +"""A collection of shared PyTest fixtures for timerun.""" |
| 2 | + |
| 3 | +from contextlib import contextmanager |
| 4 | +from typing import Callable, ContextManager, Iterable, Iterator, Tuple |
| 5 | +from unittest.mock import Mock |
| 6 | + |
| 7 | +from pytest import MonkeyPatch, fixture |
| 8 | + |
| 9 | +from timerun import ElapsedTime, Stopwatch, Timer |
| 10 | + |
| 11 | +__all__: Tuple[str, ...] = ( |
| 12 | + # -- Patcheres -- |
| 13 | + "patch_clock", |
| 14 | + "patch_split", |
| 15 | + # -- Initiated Instances -- |
| 16 | + "stopwatch", |
| 17 | + "timer", |
| 18 | + # -- Elapsed Time -- |
| 19 | + "elapsed_1_ns", |
| 20 | + "elapsed_100_ns", |
| 21 | + "elapsed_1_ms", |
| 22 | + "elapsed_1_pt_5_ms", |
| 23 | + "elapsed_1_sec", |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +# =========================================================================== # |
| 28 | +# Patcheres # |
| 29 | +# =========================================================================== # |
| 30 | + |
| 31 | + |
| 32 | +@fixture |
| 33 | +def patch_clock(monkeypatch: MonkeyPatch) -> Callable[[int], ContextManager]: |
| 34 | + """Patch the clock method in Stopwatch. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + monkeypatch : MonkeyPatch |
| 39 | + The fixture has been used to patch the clock method. |
| 40 | +
|
| 41 | + Returns |
| 42 | + ------- |
| 43 | + Callable[[int], ContextManager] |
| 44 | + A context manager takes integer argument and patch that value as |
| 45 | + the return value of the clock method. |
| 46 | +
|
| 47 | + Examples |
| 48 | + -------- |
| 49 | + >>> with patch_clock(elapsed_ns=1): |
| 50 | + ... pass |
| 51 | + """ |
| 52 | + |
| 53 | + @contextmanager |
| 54 | + def patch(elapsed_ns: int) -> Iterator[None]: |
| 55 | + """Patch clock method through monkeypatch context. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + elapsed_ns : int |
| 60 | + The value should be returned by the clock method. |
| 61 | + """ |
| 62 | + monkeypatch.setattr(Stopwatch, "_clock", lambda self: elapsed_ns) |
| 63 | + yield |
| 64 | + |
| 65 | + return patch |
| 66 | + |
| 67 | + |
| 68 | +@fixture |
| 69 | +def patch_split( |
| 70 | + monkeypatch: MonkeyPatch, |
| 71 | +) -> Callable[[Iterable[int]], ContextManager]: |
| 72 | + """Patch the split method in Timer. |
| 73 | +
|
| 74 | + Parameters |
| 75 | + ---------- |
| 76 | + monkeypatch : MonkeyPatch |
| 77 | + The fixture has been used to patch the split method. |
| 78 | +
|
| 79 | + Returns |
| 80 | + ------- |
| 81 | + Callable[[Iterable[int]], ContextManager] |
| 82 | + A context manager takes a list of integers as nanoseconds and |
| 83 | + patch those as the return values of the elapse method. |
| 84 | +
|
| 85 | + Examples |
| 86 | + -------- |
| 87 | + >>> with patch_split(elapsed_times=[100, 200, 300]): |
| 88 | + ... pass |
| 89 | + """ |
| 90 | + |
| 91 | + @contextmanager |
| 92 | + def patch(elapsed_times: Iterable[int]) -> Iterator[None]: |
| 93 | + """Patch split method through monkeypatch context. |
| 94 | +
|
| 95 | + Parameters |
| 96 | + ---------- |
| 97 | + elapsed_times : Iterable[int] |
| 98 | + The nanoseconds should be returned by the split method. |
| 99 | + """ |
| 100 | + mock_stopwatch = Mock(spec=["reset", "split"]) |
| 101 | + mock_stopwatch.split.side_effect = [ |
| 102 | + ElapsedTime(nanoseconds=t) for t in elapsed_times |
| 103 | + ] |
| 104 | + |
| 105 | + monkeypatch.setattr(Timer, "_stopwatch", mock_stopwatch) |
| 106 | + yield |
| 107 | + |
| 108 | + return patch |
| 109 | + |
| 110 | + |
| 111 | +# =========================================================================== # |
| 112 | +# Initiated Instances # |
| 113 | +# =========================================================================== # |
| 114 | + |
| 115 | + |
| 116 | +@fixture |
| 117 | +def stopwatch() -> Stopwatch: |
| 118 | + """A newly created Stopwatch started at time ``0``.""" |
| 119 | + watch: Stopwatch = Stopwatch() |
| 120 | + watch._start = 0 |
| 121 | + return watch |
| 122 | + |
| 123 | + |
| 124 | +@fixture |
| 125 | +def timer() -> Timer: |
| 126 | + """A newly created Timer with unlimited storage size.""" |
| 127 | + return Timer() |
| 128 | + |
| 129 | + |
| 130 | +# =========================================================================== # |
| 131 | +# Elapsed Time # |
| 132 | +# =========================================================================== # |
| 133 | + |
| 134 | + |
| 135 | +@fixture |
| 136 | +def elapsed_1_ns() -> ElapsedTime: |
| 137 | + """Elapsed Time of 1 nanosecond.""" |
| 138 | + return ElapsedTime(nanoseconds=1) |
| 139 | + |
| 140 | + |
| 141 | +@fixture |
| 142 | +def elapsed_100_ns() -> ElapsedTime: |
| 143 | + """Elapsed Time of 100 nanoseconds.""" |
| 144 | + return ElapsedTime(nanoseconds=100) |
| 145 | + |
| 146 | + |
| 147 | +@fixture |
| 148 | +def elapsed_1_ms() -> ElapsedTime: |
| 149 | + """Elapsed Time of 1 microsecond.""" |
| 150 | + return ElapsedTime(nanoseconds=1000) |
| 151 | + |
| 152 | + |
| 153 | +@fixture |
| 154 | +def elapsed_1_pt_5_ms() -> ElapsedTime: |
| 155 | + """Elapsed Time of 1.5 microseconds.""" |
| 156 | + return ElapsedTime(nanoseconds=1500) |
| 157 | + |
| 158 | + |
| 159 | +@fixture |
| 160 | +def elapsed_1_sec() -> ElapsedTime: |
| 161 | + """Elapsed Time of 1 second.""" |
| 162 | + return ElapsedTime(nanoseconds=int(1e9)) |
0 commit comments