nixpkgs/nixos/tests/postgresql/pgvecto-rs.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

79 lines
2.2 KiB
Nix
Raw Normal View History

{
pkgs,
makeTest,
postgresql: refactor `postgresqlVersions` attribute & tests Every postgresql testcase essentially does the following things: * Filter `postgresqlVersions` for server packages * Filter postgresql server packages for suitable ones (i.e. extensions must support the given version) * Generate an attribute-set of testcases The first item became necessary in 7ab1e888334ddc2745b285e3df0b0efd5839d0f8 given that `postgresql/default.nix` now exposes JIT and non-JIT servers AND a `libpq` that is not suitable for the tests here. This changes restructures this a little bit, i.e.: * Having an attribute-set that contains a bunch of postgresql servers and a single client package seems odd (and the sole consumer of `postgresqlVersions` in nixpkgs, the test suite, has to take that into account). Hence, postgresql's default.nix now provides `libpq` (the client) and a `postgresqlVersions` attribute with all supported JIT and non-JIT variants of postgresql. * Each test-case gets a third argument, a function called `genTests`: this function sets `recurseForDerivations = true;` and generates an attribute-set of tests for each postgresql version given a function that returns a testcase or multiple test-cases (`makeTestFor`). The argument to `makeTestFor` is a postgresql server package. This function also accepts a filter predicate that is passed against `filterAttrs` to remove postgresql server packages that are not suitable for the test (e.g. because the version isn't supported by the extension to test). I checked by making sure that the `.drv` doesn't change on staging with this change on top for postgresq, postgresql-jit, postgresql-wal-receiver, postgresql-tls-client-cert, anonymizer, pgjwt, pgvecto-rs, timescaledb, tsja and wal2json.
2025-01-26 13:10:53 +01:00
genTests,
2024-02-18 16:53:15 +01:00
}:
let
inherit (pkgs) lib;
# Test cases from https://docs.vectorchord.ai/use-case/hybrid-search.html
2024-02-18 16:53:15 +01:00
test-sql = pkgs.writeText "postgresql-test" ''
CREATE EXTENSION vectors;
CREATE TABLE items (
id bigserial PRIMARY KEY,
content text NOT NULL,
embedding vectors.vector(3) NOT NULL -- 3 dimensions
);
INSERT INTO items (content, embedding) VALUES
('a fat cat sat on a mat and ate a fat rat', '[1, 2, 3]'),
('a fat dog sat on a mat and ate a fat rat', '[4, 5, 6]'),
('a thin cat sat on a mat and ate a thin rat', '[7, 8, 9]'),
('a thin dog sat on a mat and ate a thin rat', '[10, 11, 12]');
'';
makeTestFor =
package:
makeTest {
name = "pgvecto-rs-${package.name}";
meta = with lib.maintainers; {
maintainers = [ diogotcorreia ];
2024-02-18 16:53:15 +01:00
};
nodes.machine =
{ ... }:
{
services.postgresql = {
inherit package;
enable = true;
extensions =
ps: with ps; [
pgvecto-rs
];
settings.shared_preload_libraries = "vectors";
};
};
2024-02-18 16:53:15 +01:00
testScript =
{ nodes, ... }:
let
inherit (nodes.machine.services.postgresql.package.pkgs) pgvecto-rs;
in
''
def check_count(statement, lines):
return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
statement, lines
)
2024-02-18 16:53:15 +01:00
machine.start()
machine.wait_for_unit("postgresql")
2024-02-18 16:53:15 +01:00
with subtest("Postgresql with extension vectors is available just after unit start"):
machine.succeed(check_count("SELECT * FROM pg_available_extensions WHERE name = 'vectors' AND default_version = '${pgvecto-rs.version}';", 1))
2024-02-18 16:53:15 +01:00
machine.succeed("sudo -u postgres psql -f ${test-sql}")
2024-02-18 16:53:15 +01:00
machine.succeed(check_count("SELECT content, embedding FROM items WHERE to_tsvector('english', content) @@ 'cat & rat'::tsquery;", 2))
2024-02-18 16:53:15 +01:00
machine.shutdown()
'';
};
2024-02-18 16:53:15 +01:00
in
postgresql: refactor `postgresqlVersions` attribute & tests Every postgresql testcase essentially does the following things: * Filter `postgresqlVersions` for server packages * Filter postgresql server packages for suitable ones (i.e. extensions must support the given version) * Generate an attribute-set of testcases The first item became necessary in 7ab1e888334ddc2745b285e3df0b0efd5839d0f8 given that `postgresql/default.nix` now exposes JIT and non-JIT servers AND a `libpq` that is not suitable for the tests here. This changes restructures this a little bit, i.e.: * Having an attribute-set that contains a bunch of postgresql servers and a single client package seems odd (and the sole consumer of `postgresqlVersions` in nixpkgs, the test suite, has to take that into account). Hence, postgresql's default.nix now provides `libpq` (the client) and a `postgresqlVersions` attribute with all supported JIT and non-JIT variants of postgresql. * Each test-case gets a third argument, a function called `genTests`: this function sets `recurseForDerivations = true;` and generates an attribute-set of tests for each postgresql version given a function that returns a testcase or multiple test-cases (`makeTestFor`). The argument to `makeTestFor` is a postgresql server package. This function also accepts a filter predicate that is passed against `filterAttrs` to remove postgresql server packages that are not suitable for the test (e.g. because the version isn't supported by the extension to test). I checked by making sure that the `.drv` doesn't change on staging with this change on top for postgresq, postgresql-jit, postgresql-wal-receiver, postgresql-tls-client-cert, anonymizer, pgjwt, pgvecto-rs, timescaledb, tsja and wal2json.
2025-01-26 13:10:53 +01:00
genTests {
inherit makeTestFor;
filter = _: p: !p.pkgs.pgvecto-rs.meta.broken;
}