MCPcopy Create free account
hub / github.com/HypothesisWorks/hypothesis / InMemoryExampleDatabase

Class InMemoryExampleDatabase

hypothesis/src/hypothesis/database.py:366–413  ·  view source on GitHub ↗

A non-persistent example database, implemented in terms of an in-memory dictionary. This can be useful if you call a test function several times in a single session, or for testing other database implementations, but because it does not persist between runs we do not recommend it fo

Source from the content-addressed store, hash-verified

364
365
366class InMemoryExampleDatabase(ExampleDatabase):
367 """A non-persistent example database, implemented in terms of an in-memory
368 dictionary.
369
370 This can be useful if you call a test function several times in a single
371 session, or for testing other database implementations, but because it
372 does not persist between runs we do not recommend it for general use.
373 """
374
375 def __init__(self) -> None:
376 super().__init__()
377 self.data: dict[bytes, set[bytes]] = {}
378
379 def __repr__(self) -> str:
380 return f"InMemoryExampleDatabase({self.data!r})"
381
382 def __eq__(self, other: object) -> bool:
383 return isinstance(other, InMemoryExampleDatabase) and self.data is other.data
384
385 def fetch(self, key: bytes) -> Iterable[bytes]:
386 yield from self.data.get(key, ())
387
388 def save(self, key: bytes, value: bytes) -> None:
389 value = bytes(value)
390 values = self.data.setdefault(key, set())
391 changed = value not in values
392 values.add(value)
393
394 if changed:
395 self._broadcast_change(("save", (key, value)))
396
397 def delete(self, key: bytes, value: bytes) -> None:
398 value = bytes(value)
399 values = self.data.get(key, set())
400 changed = value in values
401 values.discard(value)
402
403 if changed:
404 self._broadcast_change(("delete", (key, value)))
405
406 def _start_listening(self) -> None:
407 # declare compatibility with the listener api, but do the actual
408 # implementation in .delete and .save, since we know we are the only
409 # writer to .data.
410 pass
411
412 def _stop_listening(self) -> None:
413 pass
414
415
416def _hash(key: bytes) -> str:

Calls

no outgoing calls