From 36ad66735322908a8b39886d5d0b189f525198ed Mon Sep 17 00:00:00 2001 From: Andreas Raeder Date: Mon, 16 Nov 2026 15:50:52 +0100 Subject: [PATCH 1/8] test(backend): run one contract suite across all stores --- pyproject.toml | 2 ++ tests/conftest.py | 1 + tests/test_backends.py | 56 ++++++++++++++++++++---------------------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 82743b5..4d8d54f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -220,6 +220,8 @@ markers = [ # Opt-in parity tests against a local oold-schema checkout; see # tests/test_validation/test_parity_live.py. They skip unless OOLD_SCHEMA_DIR is set. "parity: compares this port against the reference harness", + # Needs a live PostgreSQL; skips unless OOLD_TEST_POSTGRES_URL is set. + "postgres: runs against a live PostgreSQL server", ] [tool.deptry] diff --git a/tests/conftest.py b/tests/conftest.py index 812100e..6a68071 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -62,6 +62,7 @@ def _restore_global_registries(): registries = [ interface._resolvers, + interface._backends, _types, _types_v1, _controller_types, diff --git a/tests/test_backends.py b/tests/test_backends.py index fd459cf..9240b2e 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -86,38 +86,42 @@ def _run(store: Backend): _store_procedure(store, pydantic_version="v2") -@pytest.mark.benchmark(group="backend") -def test_simple_dict_document_store(benchmark): - store = SimpleDictDocumentStore() +def _make_dict_store() -> Backend: + return SimpleDictDocumentStore() - if benchmark is not None: - benchmark(_run, store) - else: - _run(store) - -@pytest.mark.benchmark(group="backend") -def test_sqlite_document_store(benchmark): +def _make_sqlite_store() -> Backend: from oold.backend.document_store import SqliteDocumentStore - store = SqliteDocumentStore(db_path=":memory:") - - if benchmark is not None: - benchmark(_run, store) - else: - _run(store) + return SqliteDocumentStore(db_path=":memory:") -@pytest.mark.benchmark(group="backend") -def test_local_sparql_store(benchmark): +def _make_sparql_store() -> Backend: from oold.backend.sparql import LocalSparqlBackend - store = LocalSparqlBackend() + return LocalSparqlBackend() - if benchmark is not None: - benchmark(_run, store) - else: - _run(store) + +# One entry per store; a new backend only needs a factory here. +_STORE_FACTORIES = { + "dict": _make_dict_store, + "sqlite": _make_sqlite_store, + "sparql": _make_sparql_store, +} + + +@pytest.fixture(params=list(_STORE_FACTORIES)) +def store(request) -> Backend: + backend = _STORE_FACTORIES[request.param]() + yield backend + close = getattr(backend, "close", None) + if close is not None: + close() + + +@pytest.mark.benchmark(group="backend") +def test_store_roundtrip(store, benchmark): + benchmark(_run, store) def test_simple_dict_file_persistence(tmp_path): @@ -158,9 +162,3 @@ def test_simple_dict_file_no_file(): store = SimpleDictDocumentStore() assert store.file_path is None assert store._store == {} - - -if __name__ == "__main__": - test_simple_dict_document_store(None) - test_sqlite_document_store(None) - test_local_sparql_store(None) From bf87158dc0522e43576a1d5187226d85552d3117 Mon Sep 17 00:00:00 2001 From: Andreas Raeder Date: Mon, 16 Nov 2026 15:53:41 +0100 Subject: [PATCH 2/8] feat(backend): add PostgresDocumentStore behind oold[postgres] --- pyproject.toml | 5 ++ src/oold/backend/postgres.py | 54 +++++++++++++++++++ uv.lock | 101 ++++++++++++++++++++++++++++++++++- 3 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 src/oold/backend/postgres.py diff --git a/pyproject.toml b/pyproject.toml index 4d8d54f..5526f82 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,6 +67,9 @@ mcp = [ "referencing>=0.30", "click>=8.1", ] +postgres = [ + "psycopg[binary]>=3.2", +] UI-panel = [ "panel", "jupyter_bokeh", @@ -104,6 +107,7 @@ all = [ "referencing>=0.30", "click>=8.1", "mcp>=1.2", + "psycopg[binary]>=3.2", ] [dependency-groups] @@ -114,6 +118,7 @@ dev = [ "referencing>=0.30", "click>=8.1", "mcp>=1.2", + "psycopg[binary]>=3.2", "pytest", "pytest-cov>=7.0.0", "pytest-benchmark", diff --git a/src/oold/backend/postgres.py b/src/oold/backend/postgres.py new file mode 100644 index 0000000..9fac8e4 --- /dev/null +++ b/src/oold/backend/postgres.py @@ -0,0 +1,54 @@ +try: + import psycopg + from psycopg import sql + from psycopg.types.json import Jsonb +except ImportError as exc: + raise ImportError("PostgresDocumentStore needs psycopg, install it with: pip install 'oold[postgres]'") from exc + +from pydantic import Field + +from oold.backend.interface import Backend, LinkedDataFormat, StoreResult + + +class PostgresDocumentStore(Backend): + """Persist entities as JSONB rows in PostgreSQL, one row per IRI.""" + + dsn: str + table: str = Field(default="entities", pattern=r"^[A-Za-z_][A-Za-z0-9_]{0,62}$") + format: LinkedDataFormat = LinkedDataFormat.JSON + _conn: psycopg.Connection | None = None + + def __init__(self, **kwargs): + super().__init__(**kwargs) + with self._connection().cursor() as cur: + cur.execute( + sql.SQL("CREATE TABLE IF NOT EXISTS {} (id TEXT PRIMARY KEY, data JSONB)").format( + sql.Identifier(self.table) + ) + ) + + def _connection(self) -> psycopg.Connection: + if self._conn is None or self._conn.closed: + self._conn = psycopg.connect(self.dsn, autocommit=True) + return self._conn + + def close(self): + """Close the connection if it is open.""" + if self._conn is not None: + self._conn.close() + self._conn = None + + def resolve_iris(self, iris: list[str]) -> dict[str, dict]: + query = sql.SQL("SELECT id, data FROM {} WHERE id = ANY(%s::text[])").format(sql.Identifier(self.table)) + with self._connection().cursor() as cur: + cur.execute(query, (iris,)) + return dict(cur.fetchall()) + + def store_json_dicts(self, json_dicts: dict[str, dict]) -> StoreResult: + query = sql.SQL( + "INSERT INTO {} (id, data) VALUES (%s, %s) ON CONFLICT (id) DO UPDATE SET data = EXCLUDED.data" + ).format(sql.Identifier(self.table)) + conn = self._connection() + with conn.transaction(), conn.cursor() as cur: + cur.executemany(query, [(iri, Jsonb(d)) for iri, d in json_dicts.items()]) + return StoreResult(success=True) diff --git a/uv.lock b/uv.lock index 23c24af..6dcd529 100644 --- a/uv.lock +++ b/uv.lock @@ -2756,6 +2756,7 @@ all = [ { name = "nicegui" }, { name = "panel" }, { name = "param" }, + { name = "psycopg", extra = ["binary"] }, { name = "referencing" }, { name = "traitlets" }, ] @@ -2765,6 +2766,9 @@ mcp = [ { name = "mcp" }, { name = "referencing" }, ] +postgres = [ + { name = "psycopg", extra = ["binary"] }, +] ui = [ { name = "anywidget" }, { name = "ipykernel" }, @@ -2807,6 +2811,7 @@ dev = [ { name = "mkdocstrings-python" }, { name = "panel" }, { name = "pre-commit" }, + { name = "psycopg", extra = ["binary"] }, { name = "pytest" }, { name = "pytest-benchmark" }, { name = "pytest-cov" }, @@ -2852,6 +2857,8 @@ requires-dist = [ { name = "param", marker = "extra == 'all'" }, { name = "param", marker = "extra == 'ui'" }, { name = "param", marker = "extra == 'ui-panel'" }, + { name = "psycopg", extras = ["binary"], marker = "extra == 'all'", specifier = ">=3.2" }, + { name = "psycopg", extras = ["binary"], marker = "extra == 'postgres'", specifier = ">=3.2" }, { name = "pydantic", specifier = ">=2.0.0,!=2.13.*" }, { name = "pyld" }, { name = "pyyaml" }, @@ -2865,7 +2872,7 @@ requires-dist = [ { name = "traitlets", marker = "extra == 'ui-jupyter'" }, { name = "typing-extensions" }, ] -provides-extras = ["validation", "mcp", "ui-panel", "ui-jupyter", "ui-nicegui", "ui", "all"] +provides-extras = ["validation", "mcp", "postgres", "ui-panel", "ui-jupyter", "ui-nicegui", "ui", "all"] [package.metadata.requires-dev] dev = [ @@ -2877,6 +2884,7 @@ dev = [ { name = "mkdocstrings-python", specifier = ">=1.0.3" }, { name = "panel" }, { name = "pre-commit", specifier = ">=4.5.1" }, + { name = "psycopg", extras = ["binary"], specifier = ">=3.2" }, { name = "pytest" }, { name = "pytest-benchmark" }, { name = "pytest-cov", specifier = ">=7.0.0" }, @@ -3522,6 +3530,97 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/8c/c7/7bb2e321574b10df20cbde462a94e2b71d05f9bbda251ef27d104668306a/psutil-7.2.2-cp37-abi3-win_arm64.whl", hash = "sha256:8c233660f575a5a89e6d4cb65d9f938126312bca76d8fe087b947b3a1aaac9ee", size = 134617, upload-time = "2026-01-28T18:15:36.514Z" }, ] +[[package]] +name = "psycopg" +version = "3.3.9" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "typing-extensions", marker = "python_full_version < '3.13'" }, + { name = "tzdata", marker = "sys_platform == 'win32'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b4/74/ff710399dfa9fe9b546c2f5a05ea2f9b9d1dc2145c0b59e53a3fa2013679/psycopg-3.3.9.tar.gz", hash = "sha256:87c1d85c3e0e88e811d3c0b3808f947bd967c6faf34af6cd86fd021f77793d6c", size = 171122, upload-time = "2026-11-12T09:04:06.548Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/52/5d/a7957d1b5484f8e5094a15b1a400929324f978b670bd4a95179e1a012ec2/psycopg-3.3.9-py3-none-any.whl", hash = "sha256:d2d7d04a1ce6dab4f5b27316bea3b52585cafa2f2d631e5b09dd69823f5c26f6", size = 219041, upload-time = "2026-11-12T08:58:18.454Z" }, +] + +[package.optional-dependencies] +binary = [ + { name = "psycopg-binary", marker = "implementation_name != 'pypy'" }, +] + +[[package]] +name = "psycopg-binary" +version = "3.3.9" +source = { registry = "https://pypi.org/simple" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/92/a1/24138c35e4514f6ac9642041ffb6510735bad2a0be0d0e749724fafc214c/psycopg_binary-3.3.9-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:49fc0189d72ceba21853d3d35aa65801b44758350416cdf7ee9f884aa8fafa51", size = 4070198, upload-time = "2026-11-12T08:58:23.421Z" }, + { url = "https://files.pythonhosted.org/packages/24/be/4dcfea79c9d4f4b0119da58c5415ea13221ca8008a082669118f78ebd8c1/psycopg_binary-3.3.9-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:509affcdeeafd86077bdf037aa40e809989b935708e902613cd67f317c55fec6", size = 4119446, upload-time = "2026-11-12T08:58:28.512Z" }, + { url = "https://files.pythonhosted.org/packages/e5/ac/7b8539a83910e0da5827816e69978b3049fbb29712c26e3d4abfb1ebb3a6/psycopg_binary-3.3.9-cp310-cp310-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:ee2ec04c3cca2e6b989020233c81b5d6c61034ccd10ca0311a85796a189884db", size = 4248001, upload-time = "2026-11-12T08:58:34.331Z" }, + { url = "https://files.pythonhosted.org/packages/59/eb/b9789df6825143dc22bf99a82280b38fa8b3d2988a7a8f11326aeb4bdd09/psycopg_binary-3.3.9-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:b36b1f1488843301af90a25b135dd4373a9835c4df185e564f14e6565220c353", size = 3944125, upload-time = "2026-11-12T08:58:39.138Z" }, + { url = "https://files.pythonhosted.org/packages/5f/db/1eaa474692d8f9d5c57e5c6e1a1b978113f147fea4593dd522367f467128/psycopg_binary-3.3.9-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:fc256b0baa9786b732bf824dde7bf4bb16ab36642913c342d955c57b7b7e8c24", size = 4160390, upload-time = "2026-11-12T08:58:43.554Z" }, + { url = "https://files.pythonhosted.org/packages/9d/0b/1d7aff36863886be0405a064fb85ffc40ea03616097bd33d59fb4184d982/psycopg_binary-3.3.9-cp310-cp310-manylinux_2_38_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:a4abac9935ddf96baa19b831473cc4f4020e073750a5c3b4bfa2f14649ee8578", size = 3584415, upload-time = "2026-11-12T08:58:47.238Z" }, + { url = "https://files.pythonhosted.org/packages/db/05/ed56dbefcfc3ba529257a74e99b5d67e9935af07f2b0768c36e6ae0f1692/psycopg_binary-3.3.9-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:721cddea84b6da23937e80814589fdc5c25bf674f63146ce2463438798b51df8", size = 3928156, upload-time = "2026-11-12T08:58:52.076Z" }, + { url = "https://files.pythonhosted.org/packages/9b/c7/feea3247c2c261a76f5b2d2a6ba7286b4afa7a12bbf3116c4b98a32cab7f/psycopg_binary-3.3.9-cp310-cp310-musllinux_1_2_ppc64le.whl", hash = "sha256:b6733e1cd8ab3f1c256eb16e90cada43087beaec63411b6857000e8ad4759dbf", size = 3622619, upload-time = "2026-11-12T08:58:56.48Z" }, + { url = "https://files.pythonhosted.org/packages/26/01/a9577a8c7b7b7e327e7ec9ce96cf3db2543be379219ad46e0eb2f30542c5/psycopg_binary-3.3.9-cp310-cp310-musllinux_1_2_riscv64.whl", hash = "sha256:3c223b8e8040e05ad9f8f2af33c7f21f3ad907a17bdf69c79c97a71e69c9cc84", size = 3349495, upload-time = "2026-11-12T08:59:00.26Z" }, + { url = "https://files.pythonhosted.org/packages/02/b6/4c695a05c615ecbc11e5799e9239fd7cbafa7d8243398f8af28880d27a83/psycopg_binary-3.3.9-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:d7b911f2b0244f76c562492ec3628a2cde7f6f3e648da5ff3ae0ee2ecd056d8b", size = 3653831, upload-time = "2026-11-12T08:59:03.708Z" }, + { url = "https://files.pythonhosted.org/packages/d0/46/e54570009554a9f21a771e5375d7afbdea5067963365829f927bff6923bb/psycopg_binary-3.3.9-cp310-cp310-win_amd64.whl", hash = "sha256:7e7869bbefeefb9e621f3a8c86cd7b50bc0bf24e94b53091e2305d1d99a5b23b", size = 2924443, upload-time = "2026-11-12T08:59:07.751Z" }, + { url = "https://files.pythonhosted.org/packages/1a/15/35cd93a0bbbf80b2f47fdc572736a1a050b8eedfb463b7098ca4a144c813/psycopg_binary-3.3.9-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:96ef179795a800f7b8dc7c4abd14ab322cc4986726cfa6dabff552c3e16e446d", size = 4069799, upload-time = "2026-11-12T08:59:14.393Z" }, + { url = "https://files.pythonhosted.org/packages/a6/9d/7f2c869e905a1476c2d2a75b5b1f5c92b86fe54de398ee2e09dfeb4cea4c/psycopg_binary-3.3.9-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:4c064e4d9f5c6a59b975c6d7f6e2f03cab164359cfd3a592817db97f8b0f463a", size = 4118537, upload-time = "2026-11-12T08:59:22.302Z" }, + { url = "https://files.pythonhosted.org/packages/fd/d3/89e0c378206874afa1af7ac4b7ec01ed7c7d37b38124529e4aad9621f57c/psycopg_binary-3.3.9-cp311-cp311-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:79071f66f484d682b420f08386e835060b2338f23236d918a01a60281a807d3b", size = 4250697, upload-time = "2026-11-12T08:59:26.914Z" }, + { url = "https://files.pythonhosted.org/packages/2e/1d/76ec39728ef21f21b43a29987790509431393ee69adf5639978363390ca1/psycopg_binary-3.3.9-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:0953c1b94c73f97efce5b8fb1dad85cf5e4085b94902e439e27e6805ebc8be99", size = 3944426, upload-time = "2026-11-12T08:59:31.598Z" }, + { url = "https://files.pythonhosted.org/packages/d9/34/4e9e479697f249ad6ab8489b8ca06cc3e578306c4596a278df2368627934/psycopg_binary-3.3.9-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:9f6f4108a2e1d55303dfbb41d0f3b16a0aac95232a24ac8eb3df03f56fcb83aa", size = 4159271, upload-time = "2026-11-12T08:59:35.996Z" }, + { url = "https://files.pythonhosted.org/packages/58/ad/c69d9aa809f0e7fbf892a8b993e1fd2774abfc7852102c931ff8cf0700fb/psycopg_binary-3.3.9-cp311-cp311-manylinux_2_38_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:8fee8337812241ce26c6e16f73b2a20e94db643312c6901c00b57a0b2e203e6d", size = 3583287, upload-time = "2026-11-12T08:59:40.153Z" }, + { url = "https://files.pythonhosted.org/packages/4a/c2/689aa5cccb21c623f7e0506470dba9ed222b60b813c716861db495a551d2/psycopg_binary-3.3.9-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:c3dc32af4bed2339bac8110a2c5d65e23fe25d922e075f191e41f2b3a61adc0f", size = 3925250, upload-time = "2026-11-12T08:59:43.973Z" }, + { url = "https://files.pythonhosted.org/packages/bf/84/6825290efeea434c0d485fb07694f576626a565c0fb6b1d4ed7a5e1e63f4/psycopg_binary-3.3.9-cp311-cp311-musllinux_1_2_ppc64le.whl", hash = "sha256:5bd2e0315dd43b8bb28f4c124f91d9a8eb866d04bd66899ae5d82086f36ac0c2", size = 3620091, upload-time = "2026-11-12T08:59:47.729Z" }, + { url = "https://files.pythonhosted.org/packages/1a/7b/8e2cee1e7034bec74916ade25809d4ba3776d8b14ecead57af5bd327a856/psycopg_binary-3.3.9-cp311-cp311-musllinux_1_2_riscv64.whl", hash = "sha256:87e5b145e1363aa6bad904c430e18c3434e7b61565197d71d42157e3084fd9b0", size = 3346805, upload-time = "2026-11-12T08:59:52.153Z" }, + { url = "https://files.pythonhosted.org/packages/5b/f1/e996e9aa6e88e149b720472a934d366765d9477c3e8520a1243d1ce4fc0b/psycopg_binary-3.3.9-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:b2582957ef09a0315fab23cab6ff6445ad73b850bd951a936abec58cb2f3f86e", size = 3651228, upload-time = "2026-11-12T08:59:57.546Z" }, + { url = "https://files.pythonhosted.org/packages/73/08/dc42ab0f86083ce04509464c2cdd8cf24568718ff90debf322dd7d551c75/psycopg_binary-3.3.9-cp311-cp311-win_amd64.whl", hash = "sha256:75dc9ebbad779969174e59422c71444c2bd8d0362d5758a7b6f500eff11aa4ac", size = 2925715, upload-time = "2026-11-12T09:00:03.157Z" }, + { url = "https://files.pythonhosted.org/packages/6e/0c/7fc92c3135ce195bb86052d18f9ef5f64b1592d8f3a5f1d90e9b49c72448/psycopg_binary-3.3.9-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:fcdb76adaab57fdd5b789b13b20699bffaf17000a4bbc0527327581cfdd55e3d", size = 4048935, upload-time = "2026-11-12T09:00:08.958Z" }, + { url = "https://files.pythonhosted.org/packages/f0/a4/e62d11a85fc1a3a49f95b308bd5e2299dfc11125ef87043f6dfc29297d0c/psycopg_binary-3.3.9-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:08689018e9169ca8e6769f5b3f2ce748442bda95b5f1ec260995a8ff63e6d6a4", size = 4111565, upload-time = "2026-11-12T09:00:14.788Z" }, + { url = "https://files.pythonhosted.org/packages/4a/89/d9ca02dfe30a6dc51e7796617dd69cc5c2e66bcc89ced44df4f4a3b5e402/psycopg_binary-3.3.9-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:e7e2e3543ac17cf5bb3eaf738606fc52bcedf993500b9fc8c55a4475a3731ef9", size = 4207484, upload-time = "2026-11-12T09:00:23.781Z" }, + { url = "https://files.pythonhosted.org/packages/4d/aa/f1b6e1cd71aeafa78ab33875adf0339818fe014cde197a1c0c7c419dd0b0/psycopg_binary-3.3.9-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:a99f0b9a19ed7a56bf5d6776456b8f15f5364db8853b0f5d184db394a45c391d", size = 3913036, upload-time = "2026-11-12T09:00:30.144Z" }, + { url = "https://files.pythonhosted.org/packages/ae/fe/9e967bfacdda0fef48fee1d869b13de016d82d846a805745b0985ac94a5b/psycopg_binary-3.3.9-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:b081348d8992af26e9b252f04876c286428418a6b7845d8d4caae2044b259668", size = 4132657, upload-time = "2026-11-12T09:00:34.401Z" }, + { url = "https://files.pythonhosted.org/packages/cb/b3/c6a9f0bca0fdb06cf408235b698710f400a1d6376ab77e5fec887115aaa5/psycopg_binary-3.3.9-cp312-cp312-manylinux_2_38_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:66b20619acbd434e4e46d734a915768a1192db09754c2ad041d391d72f30e5eb", size = 3545187, upload-time = "2026-11-12T09:00:38.595Z" }, + { url = "https://files.pythonhosted.org/packages/61/24/0443e50d382d3f6dcc03530560e02a02646af6bd8fcbd5a60add68c80208/psycopg_binary-3.3.9-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:b7c4b2039d82f635311e41e7d392ff1bddd94a2447f9668c6531489cc86a8ce2", size = 3897551, upload-time = "2026-11-12T09:00:43.769Z" }, + { url = "https://files.pythonhosted.org/packages/c5/18/e576c5f50f4751ad9f1770134ad57ef216d00e5d79caa1c5b091f41275dc/psycopg_binary-3.3.9-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:3efe96cd98081b755ee3b0a50f2d35d9af55cb1ba462440a41048b9e7c6b9caa", size = 3577016, upload-time = "2026-11-12T09:00:49.375Z" }, + { url = "https://files.pythonhosted.org/packages/cb/c6/434e01b19af141540147427675c116b2205ea58e3ec99e26431edf2d0714/psycopg_binary-3.3.9-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:80ce081d2512ced75f1ca08ff80257a1288a3555079c5fa622164a0f9ed6533a", size = 3305336, upload-time = "2026-11-12T09:00:55.531Z" }, + { url = "https://files.pythonhosted.org/packages/eb/84/0ea715e106cc80a89aca36e34dd6685fa1ee75da9546d41a1d87356efb9f/psycopg_binary-3.3.9-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:30a95cb2b00b94b4ec4fd5d41de047598c60e0db26fd43bd9154152cc380b282", size = 3620366, upload-time = "2026-11-12T09:01:01.033Z" }, + { url = "https://files.pythonhosted.org/packages/81/a7/afe5fe01b4100454ea6543ac84c8b38a3713d6f27790253798ba50f2ef2a/psycopg_binary-3.3.9-cp312-cp312-win_amd64.whl", hash = "sha256:1ce1a6f04211626ca1a3deadee560235e8686e3888fb18f78d2be3e83bceabc4", size = 2907832, upload-time = "2026-11-12T09:01:04.735Z" }, + { url = "https://files.pythonhosted.org/packages/42/9c/68d2172aa2993b129edba644edbdbc7834ddb88f7d43dc1fac2626f59a84/psycopg_binary-3.3.9-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:b98ecad106cd48998517e03aaee56f9d88f8857e043b8e3dbc2aa7facd475db6", size = 4053851, upload-time = "2026-11-12T09:01:08.857Z" }, + { url = "https://files.pythonhosted.org/packages/52/82/f472e2df0b0f7b816efdd18dde10e8a980df74feb5e34564cb3d8aeeef67/psycopg_binary-3.3.9-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:e835af41e74176041d2290b167af92b74793b916ca3413598c39b535dd3674a5", size = 4114709, upload-time = "2026-11-12T09:01:14.257Z" }, + { url = "https://files.pythonhosted.org/packages/eb/0d/71abff2f3219cf61c790e45981ba6e4ff9b4c2f96855f5f89f7da41bf6fd/psycopg_binary-3.3.9-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:4b44a09bb7c92666aec8a00d5d390ff6f4e573d345ccde0f71b45597511449fb", size = 4210297, upload-time = "2026-11-12T09:01:20.266Z" }, + { url = "https://files.pythonhosted.org/packages/fc/b4/713b67741a31c3d93028276b2b634157fdfcf00417023f45bb4410de0ab9/psycopg_binary-3.3.9-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:180e4a99904d7a77f826fbb6dd16834bb3313675d8a6f055e39742fda00c33dc", size = 3915163, upload-time = "2026-11-12T09:01:28.02Z" }, + { url = "https://files.pythonhosted.org/packages/19/22/d67af2a2dd96603b381404d9ae657e3877c763a60cc7c5bf319162652334/psycopg_binary-3.3.9-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:366e2021a785afed9154f83f64793f662348a9ef244fef9779175c0906e8aacc", size = 4132455, upload-time = "2026-11-12T09:01:35.872Z" }, + { url = "https://files.pythonhosted.org/packages/60/c8/662b59cc3e9fb581af9f20d269035ce2a2020732584de05c4e17a3edf0ac/psycopg_binary-3.3.9-cp313-cp313-manylinux_2_38_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:13bfe814718c8a1b9b656c0d754962e0027cc3e43630661825fe197babc99320", size = 3548290, upload-time = "2026-11-12T09:01:41.316Z" }, + { url = "https://files.pythonhosted.org/packages/ec/97/9d1d54a21e881a268a5bc00cdd12a0a87534de07d123d18887ee0ff7f3f6/psycopg_binary-3.3.9-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:a0ed63b05046bc40d76109d95c757d3eac38488b17c68ae9e8160040d0ed0f89", size = 3898785, upload-time = "2026-11-12T09:01:45.019Z" }, + { url = "https://files.pythonhosted.org/packages/0e/85/8d7f1fe6161cb663374a06e2d16c408f6353e62e58bb48ff761bd95a028a/psycopg_binary-3.3.9-cp313-cp313-musllinux_1_2_ppc64le.whl", hash = "sha256:f85821eba1f8c27e080eba955eb9dddaa638f61e363b4f3b2266bd9ec4ff73bd", size = 3577036, upload-time = "2026-11-12T09:01:48.698Z" }, + { url = "https://files.pythonhosted.org/packages/50/bb/4482876d230fa2c330e28d22a135babad7a0f9b3446b54a153a9ffaf25b5/psycopg_binary-3.3.9-cp313-cp313-musllinux_1_2_riscv64.whl", hash = "sha256:eb74f5299501f83f3ad7b8e99405ca451b77c74d65767bac7f46d22c4e9411ae", size = 3309862, upload-time = "2026-11-12T09:01:52.208Z" }, + { url = "https://files.pythonhosted.org/packages/4c/1b/f49175ca5bef792c4e3820690b8e81cdf7699f3eee3a7f388c434c04b808/psycopg_binary-3.3.9-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:14666a85aa330058929e4abedd940f754f0e5589efae6366d95c513288856fb5", size = 3621566, upload-time = "2026-11-12T09:01:56.531Z" }, + { url = "https://files.pythonhosted.org/packages/9e/97/06d96b1e9c1b2369cd9d7ada4c70f0649df7c542b8c5da5bb01bacf05eaf/psycopg_binary-3.3.9-cp313-cp313-win_amd64.whl", hash = "sha256:417c3fe0ac2079a82a8c72ba214a65c66c22036a5d2d8c24c77c5708a58938b9", size = 2907276, upload-time = "2026-11-12T09:02:01.257Z" }, + { url = "https://files.pythonhosted.org/packages/25/ef/2369abcb3d7789117bca1f028eeb69e2798237f5b6e5a7b4172eb113a380/psycopg_binary-3.3.9-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:6bc7e10d05f236f53b184d9592e8f3d22587bc2e7b964fea9f5d0de2b1142fe5", size = 4055684, upload-time = "2026-11-12T09:02:06.492Z" }, + { url = "https://files.pythonhosted.org/packages/d3/22/3a2f68ce0e414a6e76f09987f447f155e2ef8b79d13ea6cf4aaa74c941a3/psycopg_binary-3.3.9-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:e853041acf10de2dcb595167138dc9fc27de293d67caa1d80ed282ec27a28e87", size = 4118288, upload-time = "2026-11-12T09:02:10.849Z" }, + { url = "https://files.pythonhosted.org/packages/af/1f/7e9e1f8c00e98fcf9d8be2d4d2120106100db5886cfce807c5d01523d64a/psycopg_binary-3.3.9-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:c25fa1ee28f8fd73aecc5e8612ab9e5700682e8e23f9422437a1617016029934", size = 4215854, upload-time = "2026-11-12T09:02:15.024Z" }, + { url = "https://files.pythonhosted.org/packages/2a/d5/ba4839c38d9d35daa815cb18dfe462582f783ba83b9d932692bb903fba01/psycopg_binary-3.3.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:4992acd5a7479013d52615b8c23fdce9bc22f3cacbfb1007fa474e6b5ebe94c7", size = 3918359, upload-time = "2026-11-12T09:02:19.433Z" }, + { url = "https://files.pythonhosted.org/packages/f1/47/243682e14cac97cb7500625a2ca3a53195ddc39e4bb81a59f289286f23d2/psycopg_binary-3.3.9-cp314-cp314-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:eed1ac135d652e4454777afb4e4bb52cf88520bbfa2f30ae83066cd4df656697", size = 4140188, upload-time = "2026-11-12T09:02:25.013Z" }, + { url = "https://files.pythonhosted.org/packages/c5/4c/5b1a99f245c3a22bd3cca9760f0595e0bd26b4a7c0d5f36fe0288ddc3b2e/psycopg_binary-3.3.9-cp314-cp314-manylinux_2_38_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:85b3892dbc4f5fb21880f82e3aed7329be6bee39fb58a0f059cbb1d0a382e98b", size = 3552025, upload-time = "2026-11-12T09:02:32.354Z" }, + { url = "https://files.pythonhosted.org/packages/c6/cc/32d34dd14d7215b987cb76ce72035963b85a9b4e75d3e3e78104abf7c831/psycopg_binary-3.3.9-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:4ba688a9402e3cc9d425e6ad20f97b96698f59f867279c265c1d0ca6df9dad57", size = 3907596, upload-time = "2026-11-12T09:02:38.276Z" }, + { url = "https://files.pythonhosted.org/packages/79/c4/ac9f35d331f987d83e5534f35d28f76aeac101eebaddc5c0c9f252a2d21f/psycopg_binary-3.3.9-cp314-cp314-musllinux_1_2_ppc64le.whl", hash = "sha256:300bd86d4b81d297e204a8ba223f54ec09a4b54fd069c32a74bf75b42860703f", size = 3587344, upload-time = "2026-11-12T09:02:41.864Z" }, + { url = "https://files.pythonhosted.org/packages/54/04/e91fedeeab7fe94df2364ad324c0de0e6ebd54b24f3dff396e24ffba9655/psycopg_binary-3.3.9-cp314-cp314-musllinux_1_2_riscv64.whl", hash = "sha256:e01a4f4fd0e4beee23af30517123bdde838d53c3ac0ae594da019e36b3adabda", size = 3314235, upload-time = "2026-11-12T09:02:47.199Z" }, + { url = "https://files.pythonhosted.org/packages/da/fc/e522309105d4c1b1f8bfa7e887355a414c1c84e94e32e532a6595c5ded13/psycopg_binary-3.3.9-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:5117a4745b3b850872140d3a66a8d83c55801916ae6e3cf502af0a794f64f9b7", size = 3626367, upload-time = "2026-11-12T09:02:52.842Z" }, + { url = "https://files.pythonhosted.org/packages/4e/50/643d5f523fa77a3b808e982716e7aa76da788218e40942721939c5e08c11/psycopg_binary-3.3.9-cp314-cp314-win_amd64.whl", hash = "sha256:109c5df5b0e5117e01a42d3b56a2f4ccd5431bd54d25efdfc89df095ac9f9605", size = 3009572, upload-time = "2026-11-12T09:02:58.474Z" }, + { url = "https://files.pythonhosted.org/packages/c1/81/06d36165c60e32d3909bf9ad036f4120284872ad00234ac384be20fcef5f/psycopg_binary-3.3.9-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:0b513957b283d181ed04961567b9893cae79309e23655d54c91cb8781ad81fc5", size = 4054822, upload-time = "2026-11-12T09:03:04.391Z" }, + { url = "https://files.pythonhosted.org/packages/4c/70/6c9eb0d0f1e124dbeba86dbd4c06b95bf15e7dfe60c0a3b91f6c7b45e237/psycopg_binary-3.3.9-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:a9ee20e3b314c1f36ec13093f1ea46161704a7304a6c334396318d36416bd330", size = 4117160, upload-time = "2026-11-12T09:03:10.7Z" }, + { url = "https://files.pythonhosted.org/packages/7f/84/99ecb47d6ef0752792b60b1fffd3739fae0605f4786aa9827646d1623f4b/psycopg_binary-3.3.9-cp315-cp315-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:0793764fac48b5f0bf4d7154e38c60def4fa0cce16caa60b737f770bd29016ac", size = 4214359, upload-time = "2026-11-12T09:03:17.69Z" }, + { url = "https://files.pythonhosted.org/packages/b0/32/2fbc8412c6d650886b201a0b1b2d522008d4a8858d885d2ca1b1f35df2d3/psycopg_binary-3.3.9-cp315-cp315-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:e51c7c493f07b51be56875cfa8a7674824749fa1cda3515339ca64e628f1b992", size = 3920287, upload-time = "2026-11-12T09:03:24.19Z" }, + { url = "https://files.pythonhosted.org/packages/cf/51/cc921a2ce634cbe8ebf3cb2b062d303d97afbf4ee941f8190003d867bea9/psycopg_binary-3.3.9-cp315-cp315-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:e5af7a6d4ca7781c03f9f628ef83ff739de8a7e7e0ffe4b6e7a8c274caea6f81", size = 4141389, upload-time = "2026-11-12T09:03:29.754Z" }, + { url = "https://files.pythonhosted.org/packages/db/37/74fd196f755b979de8d2ad8d84f54afe0628036a8d88b63f7fd204e2f8fc/psycopg_binary-3.3.9-cp315-cp315-manylinux_2_38_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:e047c843fb5cd3c9ebc370bf9a9394cb97feb71b678f07f58419de6d1c6cb5a5", size = 3558765, upload-time = "2026-11-12T09:03:37.484Z" }, + { url = "https://files.pythonhosted.org/packages/37/16/096ce6aae0063f42b1d6ae6f6fe0276208fb929daa5405a92718cf3061b2/psycopg_binary-3.3.9-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:9452241b4684a6321c8e047f4b31a029823fafceddb0809827ec1d506784a3cd", size = 3907982, upload-time = "2026-11-12T09:03:43.975Z" }, + { url = "https://files.pythonhosted.org/packages/34/09/1e8063b591433174211908d9797c7d0e85304a11c92069f66ec66c2c741b/psycopg_binary-3.3.9-cp315-cp315-musllinux_1_2_ppc64le.whl", hash = "sha256:9926a6f98fbfbb47ad34fab6151d5e6de917d640a07e78cf07a91005593f7c65", size = 3586055, upload-time = "2026-11-12T09:03:49.195Z" }, + { url = "https://files.pythonhosted.org/packages/f4/04/206ac77650b2313b3e6b2fb0d00ba3bb663095586b824d350a1937738575/psycopg_binary-3.3.9-cp315-cp315-musllinux_1_2_riscv64.whl", hash = "sha256:0f049ea20c87608eab3df8f055e8e29743a7af42f5e3e1bdb653611194fa64eb", size = 3322175, upload-time = "2026-11-12T09:03:55.836Z" }, + { url = "https://files.pythonhosted.org/packages/ec/27/22d7d9865c4722abc12be9a15be1f671624f7d440afb66cbd719dcb44a3e/psycopg_binary-3.3.9-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:6ab13f8174a0a63d0b9ec39ac27dd129a4d78f22099f146455b5c3a73957cfb2", size = 3629954, upload-time = "2026-11-12T09:04:01.437Z" }, + { url = "https://files.pythonhosted.org/packages/fb/4e/d066faaffcb5b9d0ef593c2903e767bdd22a12ee36e117c3f13d59cc5efb/psycopg_binary-3.3.9-cp315-cp315-win_amd64.whl", hash = "sha256:d5281f3453caebc911b2c5553398d59e217a8101f6b2596311256ba3772167fb", size = 3010099, upload-time = "2026-11-12T09:04:04.907Z" }, +] + [[package]] name = "psygnal" version = "0.15.1" From 9e1d78301038d82462c8a1b95b87648f483e95fd Mon Sep 17 00:00:00 2001 From: Andreas Raeder Date: Mon, 16 Nov 2026 16:00:01 +0100 Subject: [PATCH 3/8] test(backend): add gated postgres tests with per-test table --- tests/conftest.py | 25 +++++++++++++++++ tests/test_backends.py | 11 +++++++- tests/test_postgres_store.py | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/test_postgres_store.py diff --git a/tests/conftest.py b/tests/conftest.py index 6a68071..64d78cf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,8 @@ """ import importlib.util +import os +import uuid import pytest @@ -78,6 +80,29 @@ def _restore_global_registries(): registry.update(snapshot) +@pytest.fixture +def postgres_store(): + """A PostgresDocumentStore on its own table, dropped afterwards. + + Skips unless ``OOLD_TEST_POSTGRES_URL`` points at a live server, so the + default suite and the OS/Python matrix never need one. + """ + pytest.importorskip("psycopg") + dsn = os.environ.get("OOLD_TEST_POSTGRES_URL") + if not dsn: + pytest.skip("OOLD_TEST_POSTGRES_URL is not set") + + from psycopg import sql + + from oold.backend.postgres import PostgresDocumentStore + + table = f"entities_{uuid.uuid4().hex}" + store = PostgresDocumentStore(dsn=dsn, table=table) + yield store + store._connection().execute(sql.SQL("DROP TABLE IF EXISTS {}").format(sql.Identifier(table))) + store.close() + + def pytest_configure(config): """Register the ``benchmark`` mark so it is not an unknown-mark warning.""" config.addinivalue_line("markers", "benchmark(**kwargs): pytest-benchmark group settings") diff --git a/tests/test_backends.py b/tests/test_backends.py index 9240b2e..d0d5b8c 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -110,8 +110,17 @@ def _make_sparql_store() -> Backend: } -@pytest.fixture(params=list(_STORE_FACTORIES)) +@pytest.fixture( + params=[ + *_STORE_FACTORIES, + pytest.param("postgres", marks=pytest.mark.postgres), + ] +) def store(request) -> Backend: + if request.param == "postgres": + # Own fixture: it skips without a server and drops its table afterwards. + yield request.getfixturevalue("postgres_store") + return backend = _STORE_FACTORIES[request.param]() yield backend close = getattr(backend, "close", None) diff --git a/tests/test_postgres_store.py b/tests/test_postgres_store.py new file mode 100644 index 0000000..65b039c --- /dev/null +++ b/tests/test_postgres_store.py @@ -0,0 +1,53 @@ +import pytest + +pytest.importorskip("psycopg") + +from pydantic import ValidationError + +from oold.backend.postgres import PostgresDocumentStore + + +@pytest.mark.parametrize("table", ["", "1abc", "a-b", 'x"; DROP TABLE y;--', "a" * 64]) +def test_invalid_table_name_is_rejected_before_connecting(table): + with pytest.raises(ValidationError): + PostgresDocumentStore(dsn="postgresql://unused", table=table) + + +@pytest.mark.postgres +def test_store_overwrites_existing_iri(postgres_store): + postgres_store.store_json_dicts({"ex:a": {"name": "one"}}) + postgres_store.store_json_dicts({"ex:a": {"name": "two"}}) + + assert postgres_store.resolve_iris(["ex:a"]) == {"ex:a": {"name": "two"}} + + +@pytest.mark.postgres +def test_resolve_skips_unknown_iris(postgres_store): + postgres_store.store_json_dicts({"ex:a": {"name": "one"}}) + + assert postgres_store.resolve_iris(["ex:a", "ex:missing"]) == {"ex:a": {"name": "one"}} + assert postgres_store.resolve_iris([]) == {} + + +@pytest.mark.postgres +def test_nested_json_roundtrips(postgres_store): + doc = { + "name": "Zoƫ", + "tags": ["a", "b"], + "nested": {"n": 1, "f": 1.5, "flag": True, "none": None, "list": [{"k": "v"}]}, + } + postgres_store.store_json_dicts({"ex:doc": doc}) + + assert postgres_store.resolve_iris(["ex:doc"]) == {"ex:doc": doc} + + +@pytest.mark.postgres +def test_data_persists_across_instances(postgres_store): + postgres_store.store_json_dicts({"ex:a": {"name": "one"}}) + postgres_store.close() + + reopened = PostgresDocumentStore(dsn=postgres_store.dsn, table=postgres_store.table) + try: + assert reopened.resolve_iris(["ex:a"]) == {"ex:a": {"name": "one"}} + finally: + reopened.close() From 373457dbca15192f0d057ea627b6513542b6b827 Mon Sep 17 00:00:00 2001 From: Andreas Raeder Date: Mon, 16 Nov 2026 16:05:08 +0100 Subject: [PATCH 4/8] ci: test against postgres 16, 17 and 18 via service containers --- .github/workflows/main.yml | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 055eee2..044639a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -67,6 +67,51 @@ jobs: env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + tests-postgres: + # Real PostgreSQL via a service container, which only Linux runners support. One Python + # version is enough: the matrix above already covers the interpreters. + name: tests-postgres (PostgreSQL ${{ matrix.postgres }}) + runs-on: ubuntu-latest + strategy: + matrix: + postgres: ["16", "17", "18"] + fail-fast: false + services: + postgres: + image: postgres:${{ matrix.postgres }} + env: + POSTGRES_PASSWORD: postgres + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready -U postgres" + --health-interval 5s + --health-timeout 5s + --health-retries 10 + env: + OOLD_TEST_POSTGRES_URL: postgresql://postgres:postgres@localhost:5432/postgres + steps: + - name: Check out + uses: actions/checkout@v4 + + - name: Set up the environment + uses: ./.github/actions/setup-python-env + with: + python-version: "3.12" + + - name: Run the postgres tests + run: >- + uv run python -m pytest tests -m postgres --no-cov --benchmark-disable + --junitxml="$RUNNER_TEMP/postgres-junit.xml" + + - name: Fail if any postgres test was skipped + # A wrong URL or a missing driver skips these tests, and a skip counts as a pass. + run: | + if grep -q "&2 + exit 1 + fi + validate-schemas: # Runs the OO-LD validator over the committed fixtures, offline, so a regression in the # validation pipeline fails CI even when the unit tests still pass. From 20040e91a426867d8a50becbb878065238a4d4b6 Mon Sep 17 00:00:00 2001 From: Andreas Raeder Date: Mon, 16 Nov 2026 16:06:51 +0100 Subject: [PATCH 5/8] docs(backend): document PostgresDocumentStore and fix sqlite db_path --- README.md | 1 + docs/architecture.md | 1 + docs/contributing.md | 8 ++++++++ docs/how-to/backends.md | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 96041df..e37a5fe 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,7 @@ Built-in backends for entity persistence and resolution: | --------------------------- | ------------------------------------------------ | --------------- | | **SimpleDictDocumentStore** | In-memory dict, optional JSON file (`file_path`) | Filter by field | | **SqliteDocumentStore** | SQLite database (default `format=JSON`) | - | +| **PostgresDocumentStore** | PostgreSQL JSONB (`oold[postgres]`) | - | | **LocalSparqlBackend** | In-memory RDF graph (rdflib) | SPARQL | ```python diff --git a/docs/architecture.md b/docs/architecture.md index 58a1aed..04963c6 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -94,6 +94,7 @@ All backends implement the `Backend` interface from `backend/interface.py`: |---|---|---| | `SimpleDictDocumentStore` | In-memory dict, optional JSON file | No | | `SqliteDocumentStore` | SQLite database | No | +| `PostgresDocumentStore` | PostgreSQL JSONB | No | | `LocalSparqlBackend` | In-memory RDFLib graph | Yes | Backends are registered per IRI prefix via `set_resolver` / `set_backend`, so multiple backends can coexist in one application. diff --git a/docs/contributing.md b/docs/contributing.md index ae76c32..eaaa755 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -68,6 +68,14 @@ Requires `uv` and `git`. uv run python -m pytest --cov --cov-config=pyproject.toml --cov-report=xml ``` +### PostgreSQL tests + +The tests marked `postgres` need a live server and skip unless `OOLD_TEST_POSTGRES_URL` is set. CI runs them against PostgreSQL 16, 17 and 18. Each test uses its own table and drops it afterwards, so any database you can write to will do: + +```bash +OOLD_TEST_POSTGRES_URL=postgresql://user:password@localhost:5432/postgres make test +``` + ### Docs Doc sources live in `docs/`. The site is configured in `zensical.toml`. diff --git a/docs/how-to/backends.md b/docs/how-to/backends.md index 5453a94..8fe669e 100644 --- a/docs/how-to/backends.md +++ b/docs/how-to/backends.md @@ -87,7 +87,7 @@ Persists entities in a SQLite database. Drop-in replacement for `SimpleDictDocum ```python from oold.backend.document_store import SqliteDocumentStore -store = SqliteDocumentStore(file_path="./data/entities.db") +store = SqliteDocumentStore(db_path="./data/entities.db") # or ":memory:" set_resolver(SetResolverParam(iri="ex", resolver=store)) set_backend(SetBackendParam(iri="ex", backend=store)) @@ -99,6 +99,42 @@ alice = Person["ex:alice"] --- +## PostgresDocumentStore + +Persists entities as JSONB rows in PostgreSQL, one row per IRI. It needs the `postgres` extra: + +=== "uv (recommended)" + + ```bash + uv add "oold[postgres]" + ``` + +=== "pip" + + ```bash + pip install "oold[postgres]" + ``` + +```python +from oold.backend.postgres import PostgresDocumentStore + +store = PostgresDocumentStore(dsn="postgresql://user:password@localhost:5432/mydb") + +set_resolver(SetResolverParam(iri="ex", resolver=store)) +set_backend(SetBackendParam(iri="ex", backend=store)) + +store.store(StoreParam(nodes={"ex:alice": alice})) +alice = Person["ex:alice"] + +store.close() # the connection stays open until you close it +``` + +- Entities live in table `entities` (`id TEXT PRIMARY KEY, data JSONB`), created on first use. Pass `table="..."` to use another name (letters, digits and underscores, up to 63 characters). +- Storing an IRI again overwrites its row, and a batch is written in one transaction. +- Querying with `Model[Model.field == value]` is not supported yet. + +--- + ## LocalSparqlBackend An in-memory RDF graph (via [rdflib](https://rdflib.readthedocs.io/)) that supports SPARQL queries. @@ -153,7 +189,7 @@ Register different backends for different IRI prefixes: ```python local_store = SimpleDictDocumentStore() -remote_store = SqliteDocumentStore(file_path="remote.db") +remote_store = SqliteDocumentStore(db_path="remote.db") set_resolver(SetResolverParam(iri="local", resolver=local_store)) set_resolver(SetResolverParam(iri="remote", resolver=remote_store)) From dbe1a14b3efc08f9b34917ec40e2073572687a7f Mon Sep 17 00:00:00 2001 From: Andreas Raeder Date: Mon, 16 Nov 2026 16:27:16 +0100 Subject: [PATCH 6/8] test(backend): keep benchmark test names stable --- tests/test_backends.py | 52 +++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/tests/test_backends.py b/tests/test_backends.py index d0d5b8c..0c8403b 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -86,51 +86,35 @@ def _run(store: Backend): _store_procedure(store, pydantic_version="v2") -def _make_dict_store() -> Backend: - return SimpleDictDocumentStore() +# Test names are the benchmark baseline keys (scripts/compare_benchmarks.py matches on them), +# so keep them stable instead of parametrizing. +@pytest.mark.benchmark(group="backend") +def test_simple_dict_document_store(benchmark): + benchmark(_run, SimpleDictDocumentStore()) -def _make_sqlite_store() -> Backend: +@pytest.mark.benchmark(group="backend") +def test_sqlite_document_store(benchmark): from oold.backend.document_store import SqliteDocumentStore - return SqliteDocumentStore(db_path=":memory:") + store = SqliteDocumentStore(db_path=":memory:") + try: + benchmark(_run, store) + finally: + store.close() -def _make_sparql_store() -> Backend: +@pytest.mark.benchmark(group="backend") +def test_local_sparql_store(benchmark): from oold.backend.sparql import LocalSparqlBackend - return LocalSparqlBackend() - - -# One entry per store; a new backend only needs a factory here. -_STORE_FACTORIES = { - "dict": _make_dict_store, - "sqlite": _make_sqlite_store, - "sparql": _make_sparql_store, -} - - -@pytest.fixture( - params=[ - *_STORE_FACTORIES, - pytest.param("postgres", marks=pytest.mark.postgres), - ] -) -def store(request) -> Backend: - if request.param == "postgres": - # Own fixture: it skips without a server and drops its table afterwards. - yield request.getfixturevalue("postgres_store") - return - backend = _STORE_FACTORIES[request.param]() - yield backend - close = getattr(backend, "close", None) - if close is not None: - close() + benchmark(_run, LocalSparqlBackend()) @pytest.mark.benchmark(group="backend") -def test_store_roundtrip(store, benchmark): - benchmark(_run, store) +@pytest.mark.postgres +def test_postgres_document_store(postgres_store, benchmark): + benchmark(_run, postgres_store) def test_simple_dict_file_persistence(tmp_path): From 259a3e9bd95076551213306f29760858332715b0 Mon Sep 17 00:00:00 2001 From: Andreas Raeder Date: Mon, 16 Nov 2026 16:34:42 +0100 Subject: [PATCH 7/8] ci: upload postgres job coverage to codecov --- .github/workflows/main.yml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 044639a..2401d8b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -101,8 +101,15 @@ jobs: - name: Run the postgres tests run: >- - uv run python -m pytest tests -m postgres --no-cov --benchmark-disable - --junitxml="$RUNNER_TEMP/postgres-junit.xml" + uv run python -m pytest tests -m postgres --cov --cov-config=pyproject.toml + --cov-report=xml --benchmark-disable --junitxml="$RUNNER_TEMP/postgres-junit.xml" + + - name: Upload coverage reports to Codecov on PostgreSQL 17 + # Codecov merges this with the main upload, where these tests skip. + uses: codecov/codecov-action@v4 + if: ${{ matrix.postgres == '17' }} + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - name: Fail if any postgres test was skipped # A wrong URL or a missing driver skips these tests, and a skip counts as a pass. From 5508b55d6ca13f689416d70e6565a1cf618ebcf1 Mon Sep 17 00:00:00 2001 From: Andreas Raeder Date: Mon, 16 Nov 2026 16:39:24 +0100 Subject: [PATCH 8/8] test(backend): cover postgres store close and missing driver --- tests/test_postgres_store.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_postgres_store.py b/tests/test_postgres_store.py index 65b039c..0b30c8d 100644 --- a/tests/test_postgres_store.py +++ b/tests/test_postgres_store.py @@ -1,3 +1,6 @@ +import importlib +import sys + import pytest pytest.importorskip("psycopg") @@ -13,6 +16,23 @@ def test_invalid_table_name_is_rejected_before_connecting(table): PostgresDocumentStore(dsn="postgresql://unused", table=table) +def test_missing_driver_names_the_extra(monkeypatch): + monkeypatch.setitem(sys.modules, "psycopg", None) + monkeypatch.delitem(sys.modules, "oold.backend.postgres") + + with pytest.raises(ImportError, match=r"oold\[postgres\]"): + importlib.import_module("oold.backend.postgres") + + +@pytest.mark.postgres +def test_close_is_idempotent_and_the_store_reconnects(postgres_store): + postgres_store.store_json_dicts({"ex:a": {"name": "one"}}) + postgres_store.close() + postgres_store.close() + + assert postgres_store.resolve_iris(["ex:a"]) == {"ex:a": {"name": "one"}} + + @pytest.mark.postgres def test_store_overwrites_existing_iri(postgres_store): postgres_store.store_json_dicts({"ex:a": {"name": "one"}})