Files

62 lines
2.5 KiB
Python

"""Record stores for tests, held to record_store.contract like every backend, and a fake Jira."""
import dataclasses
from failure import Failure
from record_store.held import Held
@dataclasses.dataclass
class MemoryStore:
"""A record held in memory, as a backend outside git holds one: versioned as a wiki page is, refusing a write over a version it no longer holds. Set fails to make every read and write fail with that reason. overs holds the version each write named, so a test can check that a caller wrote over the version it read."""
text: str = ""
label: str = "the record in memory"
tracked: str | None = None
fails: str | None = None
version: int = 0
overs: list[str | None] = dataclasses.field(default_factory=list)
def read(self) -> str | Failure:
return Failure(self.fails) if self.fails else self.text
def held(self) -> Held | Failure:
return Failure(self.fails) if self.fails else Held(self.text, str(self.version))
def write(self, text: str, over: str | None) -> Held | Failure:
self.overs.append(over)
if self.fails:
return Failure(self.fails)
if over is not None and over != str(self.version):
return Failure(f"someone saved {self.label} since version {over}")
self.text, self.version = text, self.version + 1
return Held(text, str(self.version))
class FakeJira:
"""One epic as Jira Cloud's REST API version 3 serves it: a description that is null or a document, and no version a write could check. Its documents hold the text in one paragraph, which fake_adf and fake_markdown convert."""
def __init__(self) -> None:
self.description: dict[str, object] | None = None
self.calls: list[tuple[str, str, str, object]] = []
def http(self, method: str, url: str, authorization: str, body: object) -> object:
self.calls.append((method, url, authorization, body))
if method == "GET":
return {"key": "ABC-12", "fields": {"description": self.description}}
assert isinstance(body, dict)
self.description = body["fields"]["description"]
return None
def teammate_saves(self, text: str) -> None:
self.description = fake_adf(text)
def puts(self) -> list[object]:
return [call[3] for call in self.calls if call[0] == "PUT"]
def fake_adf(text: str) -> dict[str, object]:
return {"type": "doc", "version": 1, "content": [{"type": "paragraph", "content": [{"type": "text", "text": text}]}]}
def fake_markdown(documents: list[dict[str, object]]) -> list[str]:
return [document["content"][0]["content"][0]["text"] for document in documents] # type: ignore[index]