Skip to content

Commit

Permalink
postgis: add tests with and without sfcgal
Browse files Browse the repository at this point in the history
  • Loading branch information
autra committed Jan 2, 2025
1 parent 175ce0f commit 136196c
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nixos/tests/postgresql/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ in
anonymizer = importWithArgs ./anonymizer.nix;
pgjwt = importWithArgs ./pgjwt.nix;
pgvecto-rs = importWithArgs ./pgvecto-rs.nix;
postgis = importWithArgs ./postgis.nix;
postgis_sfcgal = importWithArgs ./postgis_sfcgal.nix;
timescaledb = importWithArgs ./timescaledb.nix;
tsja = importWithArgs ./tsja.nix;
wal2json = importWithArgs ./wal2json.nix;
Expand Down
84 changes: 84 additions & 0 deletions nixos/tests/postgresql/postgis.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
pkgs,
makeTest,
}:

let
inherit (pkgs) lib;

# Test cases from https://www.postgis.net/workshops/postgis-intro/geometries.html
# This should be enough to check if the extension is correctly loaded
test-sql = pkgs.writeText "postgresql-test" ''
\set ON_ERROR_STOP on
CREATE EXTENSION postgis;
CREATE TABLE geometries (
name varchar,
geom geometry NOT NULL
);
INSERT INTO geometries VALUES
('Point', 'POINT(0 0)'),
('Linestring', 'LINESTRING(0 0, 1 1, 2 1, 2 2)'),
('Polygon', 'POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))'),
('PolygonWithHole', 'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1))'),
('Collection', 'GEOMETRYCOLLECTION(POINT(2 0),POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)))');
'';

makeTestFor =
package:
makeTest {
name = "postgis-${package.name}";
meta = {
maintainers = lib.teams.geospatial.members;
};

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

testScript =
{ nodes, ... }:
let
inherit (nodes.machine.services.postgresql.package.pkgs) postgis;
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 postgis is available just after unit start"):
machine.succeed(check_count("SELECT * FROM pg_available_extensions WHERE name = 'postgis' AND default_version = '${postgis.version}';", 1))
machine.succeed("sudo -u postgres psql -f ${test-sql}")
machine.succeed(check_count("SELECT name, st_astext(geom) FROM geometries WHERE name like 'Polygon%';", 2))
machine.shutdown()
'';
};
in
lib.recurseIntoAttrs (
lib.concatMapAttrs (n: p: { ${n} = makeTestFor p; }) (
lib.filterAttrs (_: p: !p.pkgs.postgis.meta.broken) pkgs.postgresqlVersions
)
// {
passthru.override = p: makeTestFor p;
}
)
85 changes: 85 additions & 0 deletions nixos/tests/postgresql/postgis_sfcgal.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
pkgs,
makeTest,
}:

let
inherit (pkgs) lib;

# Test cases from https://www.postgis.net/workshops/postgis-intro/geometries.html
# This should be enough to check if the extension is correctly loaded
test-sql = pkgs.writeText "postgresql-test" ''
\set ON_ERROR_STOP on
CREATE EXTENSION postgis_sfcgal cascade;
CREATE TABLE geometries (
name varchar,
geom geometry(PolygonZ) NOT NULL
);
INSERT INTO geometries(name, geom) VALUES
('planar geom', 'PolygonZ((1 1 0, 1 2 0, 2 2 0, 2 1 0, 1 1 0))'),
('nonplanar geom', 'PolygonZ((1 1 1, 1 2 -1, 2 2 2, 2 1 0, 1 1 1))');
'';

makeTestFor =
package:
makeTest {
name = "postgis_sfcgal-${package.name}";
meta = {
maintainers = lib.teams.geospatial.members;
};

nodes.machine =
{ ... }:
{
services.postgresql = {
inherit package;
enable = true;
enableJIT = lib.hasInfix "-jit-" package.name;
extensions =
ps: with ps; [
(postgis.override ({ withSfcgal = true; }))
];
};
};

testScript =
{ nodes, ... }:
let
inherit (nodes.machine.services.postgresql.package.pkgs) postgis;
in
''
def check_count(statement, lines):
return 'test $(sudo -u postgres psql postgres -tAc "{}"|wc -l) -eq {}'.format(
statement, lines
)
def check_return(statement, expected):
return 'test $(sudo -u postgres psql -tAc "{}") = {}'.format(statement, expected)
machine.start()
machine.wait_for_unit("postgresql")
with subtest("Postgresql with extension postgis is available just after unit start"):
machine.succeed(check_count("SELECT * FROM pg_available_extensions WHERE name = 'postgis_sfcgal' AND default_version = '${postgis.version}';", 1))
machine.succeed("sudo -u postgres psql -f ${test-sql}")
machine.succeed(check_return("select postgis_sfcgal_version()", "${pkgs.sfcgal.version}"))
machine.succeed(check_count("SELECT name from geometries where cg_isplanar(geom)", 1))
machine.shutdown()
'';
};
in
lib.recurseIntoAttrs (
lib.concatMapAttrs (n: p: { ${n} = makeTestFor p; }) (
lib.filterAttrs (_: p: !p.pkgs.postgis.meta.broken) pkgs.postgresqlVersions
)
// {
passthru.override = p: makeTestFor p;
}
)

0 comments on commit 136196c

Please sign in to comment.