Commit 9dc7a05a authored by Eric Wolf's avatar Eric Wolf Committed by Eric Wolf
Browse files

cargo-pgx/timescaledb_toolkit: add nixos test

parent 4e189d76
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -683,6 +683,7 @@ in {
  terminal-emulators = handleTest ./terminal-emulators.nix {};
  tiddlywiki = handleTest ./tiddlywiki.nix {};
  tigervnc = handleTest ./tigervnc.nix {};
  timescaledb = handleTest ./timescaledb.nix {};
  timezone = handleTest ./timezone.nix {};
  tinc = handleTest ./tinc {};
  tinydns = handleTest ./tinydns.nix {};
+93 −0
Original line number Diff line number Diff line
# mostly copied from ./postgresql.nix as it seemed unapproriate to
# test additional extensions for postgresql there.

{ system ? builtins.currentSystem
, config ? { }
, pkgs ? import ../.. { inherit system config; }
}:

with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib;

let
  postgresql-versions = import ../../pkgs/servers/sql/postgresql pkgs;
  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;
  '';
  make-postgresql-test = postgresql-name: postgresql-package: makeTest {
    name = postgresql-name;
    meta = with pkgs.lib.maintainers; {
      maintainers = [ typetetris ];
    };

    nodes.machine = { ... }:
      {
        services.postgresql = {
          enable = true;
          package = postgresql-package;
          extraPlugins = with postgresql-package.pkgs; [
            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()
    '';

  };
  applicablePostgresqlVersions = filterAttrs (_: value: versionAtLeast value.version "12") postgresql-versions;
in
mapAttrs'
  (name: package: {
    inherit name;
    value = make-postgresql-test name package;
  })
  applicablePostgresqlVersions
+5 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
, buildPgxExtension
, postgresql
, stdenv
, nixosTests
}:

buildPgxExtension rec {
@@ -21,6 +22,10 @@ buildPgxExtension rec {
  cargoSha256 = "sha256-ukjJ11LmfG+k8D20rj68i43gOWUN80nf3hIAjUWXihI=";
  buildAndTestSubdir = "extension";

  passthru.tests = {
    timescaledb_toolkit = nixosTests.timescaledb;
  };

  # tests take really long
  doCheck = false;