Skip to content

Commit 9dc7a05

Browse files
typetetrisEric Wolf
authored andcommitted
cargo-pgx/timescaledb_toolkit: add nixos test
1 parent 4e189d7 commit 9dc7a05

3 files changed

Lines changed: 99 additions & 0 deletions

File tree

nixos/tests/all-tests.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ in {
683683
terminal-emulators = handleTest ./terminal-emulators.nix {};
684684
tiddlywiki = handleTest ./tiddlywiki.nix {};
685685
tigervnc = handleTest ./tigervnc.nix {};
686+
timescaledb = handleTest ./timescaledb.nix {};
686687
timezone = handleTest ./timezone.nix {};
687688
tinc = handleTest ./tinc {};
688689
tinydns = handleTest ./tinydns.nix {};

nixos/tests/timescaledb.nix

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# mostly copied from ./postgresql.nix as it seemed unapproriate to
2+
# test additional extensions for postgresql there.
3+
4+
{ system ? builtins.currentSystem
5+
, config ? { }
6+
, pkgs ? import ../.. { inherit system config; }
7+
}:
8+
9+
with import ../lib/testing-python.nix { inherit system pkgs; };
10+
with pkgs.lib;
11+
12+
let
13+
postgresql-versions = import ../../pkgs/servers/sql/postgresql pkgs;
14+
test-sql = pkgs.writeText "postgresql-test" ''
15+
CREATE EXTENSION timescaledb;
16+
CREATE EXTENSION timescaledb_toolkit;
17+
18+
CREATE TABLE sth (
19+
time TIMESTAMPTZ NOT NULL,
20+
value DOUBLE PRECISION
21+
);
22+
23+
SELECT create_hypertable('sth', 'time');
24+
25+
INSERT INTO sth (time, value) VALUES
26+
('2003-04-12 04:05:06 America/New_York', 1.0),
27+
('2003-04-12 04:05:07 America/New_York', 2.0),
28+
('2003-04-12 04:05:08 America/New_York', 3.0),
29+
('2003-04-12 04:05:09 America/New_York', 4.0),
30+
('2003-04-12 04:05:10 America/New_York', 5.0)
31+
;
32+
33+
WITH t AS (
34+
SELECT
35+
time_bucket('1 day'::interval, time) AS dt,
36+
stats_agg(value) AS stats
37+
FROM sth
38+
GROUP BY time_bucket('1 day'::interval, time)
39+
)
40+
SELECT
41+
average(stats)
42+
FROM t;
43+
'';
44+
make-postgresql-test = postgresql-name: postgresql-package: makeTest {
45+
name = postgresql-name;
46+
meta = with pkgs.lib.maintainers; {
47+
maintainers = [ typetetris ];
48+
};
49+
50+
nodes.machine = { ... }:
51+
{
52+
services.postgresql = {
53+
enable = true;
54+
package = postgresql-package;
55+
extraPlugins = with postgresql-package.pkgs; [
56+
timescaledb
57+
timescaledb_toolkit
58+
];
59+
settings = { shared_preload_libraries = "timescaledb, timescaledb_toolkit"; };
60+
};
61+
};
62+
63+
testScript = ''
64+
def check_count(statement, lines):
65+
return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
66+
statement, lines
67+
)
68+
69+
70+
machine.start()
71+
machine.wait_for_unit("postgresql")
72+
73+
with subtest("Postgresql with extensions timescaledb and timescaledb_toolkit is available just after unit start"):
74+
machine.succeed(
75+
"sudo -u postgres psql -f ${test-sql}"
76+
)
77+
78+
machine.fail(check_count("SELECT * FROM sth;", 3))
79+
machine.succeed(check_count("SELECT * FROM sth;", 5))
80+
machine.fail(check_count("SELECT * FROM sth;", 4))
81+
82+
machine.shutdown()
83+
'';
84+
85+
};
86+
applicablePostgresqlVersions = filterAttrs (_: value: versionAtLeast value.version "12") postgresql-versions;
87+
in
88+
mapAttrs'
89+
(name: package: {
90+
inherit name;
91+
value = make-postgresql-test name package;
92+
})
93+
applicablePostgresqlVersions

pkgs/servers/sql/postgresql/ext/timescaledb_toolkit.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
, buildPgxExtension
44
, postgresql
55
, stdenv
6+
, nixosTests
67
}:
78

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

25+
passthru.tests = {
26+
timescaledb_toolkit = nixosTests.timescaledb;
27+
};
28+
2429
# tests take really long
2530
doCheck = false;
2631

0 commit comments

Comments
 (0)