Unverified Commit 7a48cf7a authored by Maximilian Bosch's avatar Maximilian Bosch Committed by GitHub
Browse files

Merge: postgresqlPackages: add "asserts" option to postgresqlTestExtensions (#398205)

parents 2cb444f4 722e4bf4
Loading
Loading
Loading
Loading

nixos/tests/postgresql/citus.nix

deleted100644 → 0
+0 −73
Original line number Diff line number Diff line
{
  pkgs,
  makeTest,
  genTests,
}:

let
  inherit (pkgs) lib;

  test-sql = pkgs.writeText "postgresql-test" ''
    CREATE EXTENSION citus;

    CREATE TABLE examples (
      id bigserial,
      shard_key int,
      PRIMARY KEY (id, shard_key)
    );

    SELECT create_distributed_table('examples', 'shard_key');

    INSERT INTO examples (shard_key) SELECT shard % 10 FROM generate_series(1,1000) shard;
  '';

  makeTestFor =
    package:
    makeTest {
      name = "citus-${package.name}";
      meta = with lib.maintainers; {
        maintainers = [ typetetris ];
      };

      nodes.machine =
        { ... }:
        {
          services.postgresql = {
            inherit package;
            enable = true;
            enableJIT = lib.hasInfix "-jit-" package.name;
            extensions =
              ps: with ps; [
                citus
              ];
            settings = {
              shared_preload_libraries = "citus";
            };
          };
        };

      testScript = ''
        def check_count(statement, lines):
            return 'test $(sudo -u postgres psql postgres -tAc "{}") -eq {}'.format(
                statement, lines
            )


        machine.start()
        machine.wait_for_unit("postgresql")

        with subtest("Postgresql with extension citus is available just after unit start"):
            machine.succeed(
                "sudo -u postgres psql -f ${test-sql}"
            )

        machine.succeed(check_count("SELECT count(*) FROM examples;", 1000))

        machine.shutdown()
      '';
    };
in
genTests {
  inherit makeTestFor;
  filter = _: p: !p.pkgs.citus.meta.broken;
}
+0 −4
Original line number Diff line number Diff line
@@ -36,10 +36,6 @@ in

  # extensions
  anonymizer = importWithArgs ./anonymizer.nix;
  citus = importWithArgs ./citus.nix;
  pgjwt = importWithArgs ./pgjwt.nix;
  pgvecto-rs = importWithArgs ./pgvecto-rs.nix;
  timescaledb = importWithArgs ./timescaledb.nix;
  tsja = importWithArgs ./tsja.nix;
  wal2json = importWithArgs ./wal2json.nix;
}
+0 −79
Original line number Diff line number Diff line
{
  pkgs,
  makeTest,
  genTests,
}:

let
  inherit (pkgs) lib;

  # Test cases from https://docs.vectorchord.ai/use-case/hybrid-search.html
  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 ];
      };

      nodes.machine =
        { ... }:
        {
          services.postgresql = {
            inherit package;
            enable = true;
            enableJIT = lib.hasInfix "-jit-" package.name;
            extensions =
              ps: with ps; [
                pgvecto-rs
              ];
            settings.shared_preload_libraries = "vectors";
          };
        };

      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
              )


          machine.start()
          machine.wait_for_unit("postgresql")

          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))

          machine.succeed("sudo -u postgres psql -f ${test-sql}")

          machine.succeed(check_count("SELECT content, embedding FROM items WHERE to_tsvector('english', content) @@ 'cat & rat'::tsquery;", 2))

          machine.shutdown()
        '';
    };
in
genTests {
  inherit makeTestFor;
  filter = _: p: !p.pkgs.pgvecto-rs.meta.broken;
}
+0 −98
Original line number Diff line number Diff line
{
  pkgs,
  makeTest,
  genTests,
}:

let
  inherit (pkgs) lib;

  test-sql = pkgs.writeText "postgresql-test" ''
    CREATE EXTENSION timescaledb;
    CREATE EXTENSION timescaledb_toolkit;

    CREATE TABLE sth (
      time TIMESTAMPTZ NOT NULL,
      value DOUBLE PRECISION
    );

    SELECT create_hypertable('sth', 'time');

    INSERT INTO sth (time, value) VALUES
    ('2003-04-12 04:05:06 America/New_York', 1.0),
    ('2003-04-12 04:05:07 America/New_York', 2.0),
    ('2003-04-12 04:05:08 America/New_York', 3.0),
    ('2003-04-12 04:05:09 America/New_York', 4.0),
    ('2003-04-12 04:05:10 America/New_York', 5.0)
    ;

    WITH t AS (
      SELECT
        time_bucket('1 day'::interval, time) AS dt,
        stats_agg(value) AS stats
      FROM sth
      GROUP BY time_bucket('1 day'::interval, time)
    )
    SELECT
      average(stats)
    FROM t;

    SELECT * FROM sth;
  '';

  makeTestFor =
    package:
    makeTest {
      name = "timescaledb-${package.name}";
      meta = with lib.maintainers; {
        maintainers = [ typetetris ];
      };

      nodes.machine =
        { ... }:
        {
          services.postgresql = {
            inherit package;
            enable = true;
            enableJIT = lib.hasInfix "-jit-" package.name;
            extensions =
              ps: with ps; [
                timescaledb
                timescaledb_toolkit
              ];
            settings = {
              shared_preload_libraries = "timescaledb, timescaledb_toolkit";
            };
          };
        };

      testScript = ''
        def check_count(statement, lines):
            return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
                statement, lines
            )


        machine.start()
        machine.wait_for_unit("postgresql")

        with subtest("Postgresql with extensions timescaledb and timescaledb_toolkit is available just after unit start"):
            machine.succeed(
                "sudo -u postgres psql -f ${test-sql}"
            )

        machine.fail(check_count("SELECT * FROM sth;", 3))
        machine.succeed(check_count("SELECT * FROM sth;", 5))
        machine.fail(check_count("SELECT * FROM sth;", 4))

        machine.shutdown()
      '';
    };
in
# Not run by default, because this requires allowUnfree.
# To run these tests:
#   NIXPKGS_ALLOW_UNFREE=1 nix-build -A nixosTests.postgresql.timescaledb
lib.dontRecurseIntoAttrs (genTests {
  inherit makeTestFor;
  filter = _: p: !p.pkgs.timescaledb.meta.broken;
})

nixos/tests/postgresql/tsja.nix

deleted100644 → 0
+0 −48
Original line number Diff line number Diff line
{
  pkgs,
  makeTest,
  genTests,
}:

let
  inherit (pkgs) lib;

  makeTestFor =
    package:
    makeTest {
      name = "tsja-${package.name}";
      meta = {
        maintainers = with lib.maintainers; [ chayleaf ];
      };

      nodes.master =
        { ... }:
        {
          services.postgresql = {
            inherit package;
            enable = true;
            enableJIT = lib.hasInfix "-jit-" package.name;
            extensions =
              ps: with ps; [
                tsja
              ];
          };
        };

      testScript = ''
        start_all()
        master.wait_for_unit("postgresql")
        master.succeed("sudo -u postgres psql -f /run/current-system/sw/share/postgresql/extension/libtsja_dbinit.sql")
        # make sure "日本語" is parsed as a separate lexeme
        master.succeed("""
          sudo -u postgres \\
            psql -c "SELECT * FROM ts_debug('japanese', 'PostgreSQLで日本語のテキスト検索ができます。')" \\
              | grep "{日本語}"
        """)
      '';
    };
in
genTests {
  inherit makeTestFor;
  filter = _: p: !p.pkgs.tsja.meta.broken;
}
Loading