MCPcopy Create free account
hub / github.com/internetarchive/openlibrary / FakeServer

Class FakeServer

scripts/tests/test_copydocs.py:22–65  ·  view source on GitHub ↗

Mimics OpenLibrary's API class

Source from the content-addressed store, hash-verified

20
21
22class FakeServer:
23 """Mimics OpenLibrary's API class"""
24
25 def __init__(self, docs: list[dict]):
26 """
27 :param list[dict] docs:
28 """
29 self.db: dict = {} # Mapping of key to (Mp of revision to doc)
30 self.save_many(docs)
31
32 def get(self, key: str, revision: int | None = None) -> dict | None:
33 """
34 :param str key:
35 :param int or None revision:
36 :return: dict or None
37 """
38 revisions = self.db.get(key, {})
39 if revision is None and len(revisions) > 0:
40 return max(list(revisions.values()), key=lambda d: d["revision"])
41 else:
42 return revisions.get(revision, None)
43
44 def get_many(self, keys: list[str], max_length: int = 500) -> dict:
45 """
46 :param list of str keys:
47 :return: Map of key to document
48 """
49 result = {}
50 for k in keys:
51 if k in self.db:
52 result[k] = self.get(k)
53 return result
54
55 def save_many(self, docs: list[dict], comment: str | None = None) -> None:
56 """
57 :param list[dict] docs:
58 :param str or None comment:
59 """
60 for doc in docs:
61 key = doc["key"]
62 revision = doc["revision"]
63 if key not in self.db:
64 self.db[key] = {}
65 self.db[doc["key"]][revision] = doc
66
67
68class TestCopy:

Callers 1

setup_methodMethod · 0.85

Calls

no outgoing calls

Tested by 1

setup_methodMethod · 0.68