Skip to content

Commit

Permalink
Test reorg (#358)
Browse files Browse the repository at this point in the history
The new `plrust-tests` create is a standalone pgrx crate that only
contains the unit tests from `plrust`.  These were copied over verbatim,
followed by minor organizational and formatting changes.

To run the tests:

```shell
$ cd plrust-tests
$ ./run-tests.sh pgXX [test_name]
```

Co-authored-by: Eric B. Ridge <[email protected]>
Co-authored-by: Brady Bonnette <[email protected]>
Co-authored-by: Jubilee Young <[email protected]>
  • Loading branch information
4 people authored Aug 28, 2023
1 parent 9368363 commit 291842d
Show file tree
Hide file tree
Showing 25 changed files with 1,814 additions and 1,418 deletions.
55 changes: 45 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,29 @@ jobs:
- name: Run cargo pgrx init
run: cargo pgrx init --pg$PG_VER $(which pg_config)

- name: Test PL/rust as "untrusted"
if: matrix.target == 'host'
run: cargo test --all --features "pg$PG_VER" --no-default-features
- name: Install PL/Rust as "trusted"
if: matrix.target == 'postgrestd'
run: cd plrust && STD_TARGETS="aarch64-postgres-linux-gnu" ./build && echo "\q" | cargo pgrx run "pg$PG_VER" --features "trusted"

- name: Test PL/Rust package as "trusted"
if: matrix.target == 'postgrestd'
run: cd plrust && cargo test --no-default-features --features "pg$PG_VER trusted"

- name: Test PL/rust as "trusted" (inc. postgrestd)
- name: Run PL/Rust integration tests as "trusted"
if: matrix.target == 'postgrestd'
run: cd plrust && STD_TARGETS="aarch64-postgres-linux-gnu" ./build && cargo test --verbose --no-default-features --features "pg$PG_VER trusted"
run: cd plrust && echo "\q" | cargo pgrx run "pg$PG_VER" --features "trusted" && cd ../plrust-tests && cargo test --no-default-features --features "pg$PG_VER trusted"

- name: Install PL/Rust as "untrusted"
if: matrix.target == 'host'
run: cd plrust && STD_TARGETS="aarch64-postgres-linux-gnu" ./build && echo "\q" | cargo pgrx run "pg$PG_VER"

- name: Test PL/Rust package as "untrusted"
if: matrix.target == 'host'
run: cd plrust && cargo test --no-default-features --features "pg$PG_VER"

- name: Run PL/Rust integration tests as "untrusted"
if: matrix.target == 'host'
run: cd plrust && echo "\q" | cargo pgrx run "pg$PG_VER" && cd ../plrust-tests && cargo test --no-default-features --features "pg$PG_VER"

- name: Print sccache stats (after build)
run: sccache --show-stats
Expand Down Expand Up @@ -315,6 +331,9 @@ jobs:
- name: Print sccache stats
run: sccache --show-stats

- name: Test plrustc
run: cd plrustc && cargo test

- name: Install plrustc
run: cd plrustc && ./build.sh && cp ../build/bin/plrustc ~/.cargo/bin

Expand All @@ -324,13 +343,29 @@ jobs:
- name: Run 'cargo pgrx init' against system-level ${{ matrix.version }}
run: cargo pgrx init --pg$PG_VER $(which pg_config)

- name: Test PL/rust as "untrusted"
if: matrix.target == 'host'
run: cargo test --all --features "pg$PG_VER" --no-default-features
- name: Install PL/Rust as "trusted"
if: matrix.target == 'postgrestd'
run: cd plrust && STD_TARGETS="x86_64-postgres-linux-gnu" ./build && echo "\q" | cargo pgrx run "pg$PG_VER" --features "trusted"

- name: Test PL/rust as "trusted" (inc. postgrestd)
- name: Test PL/Rust package as "trusted"
if: matrix.target == 'postgrestd'
run: cd plrust && STD_TARGETS="x86_64-postgres-linux-gnu" ./build && cargo test --verbose --no-default-features --features "pg$PG_VER trusted"
run: cd plrust && cargo test --no-default-features --features "pg$PG_VER trusted"

- name: Run PL/Rust integration tests as "trusted"
if: matrix.target == 'postgrestd'
run: cd plrust && echo "\q" | cargo pgrx run "pg$PG_VER" --features "trusted" && cd ../plrust-tests && cargo test --no-default-features --features "pg$PG_VER trusted"

- name: Install PL/Rust as "untrusted"
if: matrix.target == 'host'
run: cd plrust && STD_TARGETS="x86_64-postgres-linux-gnu" ./build && echo "\q" | cargo pgrx run "pg$PG_VER"

- name: Test PL/Rust package as "untrusted"
if: matrix.target == 'host'
run: cd plrust && cargo test --no-default-features --features "pg$PG_VER"

- name: Run PL/Rust integration tests as "untrusted"
if: matrix.target == 'host'
run: cd plrust && echo "\q" | cargo pgrx run "pg$PG_VER" && cd ../plrust-tests && cargo test --no-default-features --features "pg$PG_VER"

- name: Print sccache stats
run: sccache --show-stats
Expand Down
56 changes: 36 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"plrust",
"plrust-trusted-pgrx",
"plrust-tests",
]
exclude = ["plrustc"]#, "builder"]

Expand Down
25 changes: 25 additions & 0 deletions plrust-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "plrust-tests"
version = "0.0.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[features]
default = ["pg13"]
pg13 = ["pgrx/pg13", "pgrx-tests/pg13" ]
pg14 = ["pgrx/pg14", "pgrx-tests/pg14" ]
pg15 = ["pgrx/pg15", "pgrx-tests/pg15" ]
pg_test = []
trusted = []

[dependencies]
pgrx = "=0.9.7"
tempfile = "3.8.0"
once_cell = "1.18.0"

[dev-dependencies]
pgrx-tests = "=0.9.7"
tempfile = "3.8.0"
once_cell = "1.18.0"
6 changes: 6 additions & 0 deletions plrust-tests/plrust_tests.control
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
comment = 'plrust_tests: Created by pgrx'
default_version = '@CARGO_VERSION@'
module_pathname = '$libdir/plrust_tests'
relocatable = false
superuser = true
requires = 'plrust'
30 changes: 30 additions & 0 deletions plrust-tests/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#! /bin/bash

VERSION=$1

if [ -z ${VERSION} ]; then
echo "usage: ./run-tests.sh pgXX [test-name]"
exit 1
fi

TEST_DIR=`pwd`

set -e

# install the plrust extension into the pgrx-managed postgres
echo "============================"
echo " installing plrust"
echo
cd ../plrust
echo "\q" | cargo pgrx run ${VERSION}

# run the test suite from this crate
cd ${TEST_DIR}

echo
echo "============================"
echo " running plrust-tests suite"
echo

cargo pgrx test ${VERSION} $2

42 changes: 42 additions & 0 deletions plrust-tests/src/alter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Portions Copyright 2020-2021 ZomboDB, LLC.
Portions Copyright 2021-2023 Technology Concepts & Design, Inc. <[email protected]>
All rights reserved.
Use of this source code is governed by the PostgreSQL license that can be found in the LICENSE.md file.
*/

#[cfg(any(test, feature = "pg_test"))]
#[pgrx::pg_schema]
mod tests {
use pgrx::prelude::*;

#[pg_test]
#[search_path(@extschema@)]
#[should_panic = "plrust functions cannot have their STRICT property altered"]
fn plrust_cant_change_strict_off() -> spi::Result<()> {
let definition = r#"
CREATE FUNCTION cant_change_strict_off()
RETURNS int
LANGUAGE plrust
AS $$ Ok(Some(1)) $$;
"#;
Spi::run(definition)?;
Spi::run("ALTER FUNCTION cant_change_strict_off() CALLED ON NULL INPUT")
}

#[pg_test]
#[search_path(@extschema@)]
#[should_panic = "plrust functions cannot have their STRICT property altered"]
fn plrust_cant_change_strict_on() -> spi::Result<()> {
let definition = r#"
CREATE FUNCTION cant_change_strict_on()
RETURNS int
LANGUAGE plrust
AS $$ Ok(Some(1)) $$;
"#;
Spi::run(definition)?;
Spi::run("ALTER FUNCTION cant_change_strict_on() RETURNS NULL ON NULL INPUT")
}
}
71 changes: 71 additions & 0 deletions plrust-tests/src/argument.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Portions Copyright 2020-2021 ZomboDB, LLC.
Portions Copyright 2021-2023 Technology Concepts & Design, Inc. <[email protected]>
All rights reserved.
Use of this source code is governed by the PostgreSQL license that can be found in the LICENSE.md file.
*/

#[cfg(any(test, feature = "pg_test"))]
#[pgrx::pg_schema]
mod tests {
use pgrx::prelude::*;

#[pg_test]
#[search_path(@extschema@)]
#[should_panic = "parameter name \"a\" used more than once"]
fn plrust_dup_args() -> spi::Result<()> {
let definition = r#"
CREATE FUNCTION not_unique(a int, a int)
RETURNS int AS
$$
Ok(a)
$$ LANGUAGE plrust;
"#;
Spi::run(definition)?;
let result = Spi::get_one::<i32>("SELECT not_unique(1, 2);\n");
assert_eq!(Ok(Some(1)), result);
Ok(())
}

#[pg_test]
#[search_path(@extschema@)]
#[should_panic = "PL/Rust does not support unnamed arguments"]
fn plrust_defaulting_dup_args() -> spi::Result<()> {
let definition = r#"
CREATE FUNCTION not_unique(int, arg0 int)
RETURNS int AS
$$
Ok(arg0)
$$ LANGUAGE plrust;
"#;
Spi::run(definition)?;
let result = Spi::get_one::<i32>("SELECT not_unique(1, 2);\n");
assert_eq!(Ok(Some(1)), result);
Ok(())
}

#[pg_test]
#[search_path(@extschema@)]
#[should_panic(expected = "PL/Rust does not support unnamed arguments")]
fn unnamed_args() -> spi::Result<()> {
Spi::run("CREATE FUNCTION unnamed_arg(int) RETURNS int LANGUAGE plrust as $$ Ok(None) $$;")
}

#[pg_test]
#[search_path(@extschema@)]
#[should_panic(expected = "PL/Rust does not support unnamed arguments")]
fn named_unnamed_args() -> spi::Result<()> {
Spi::run("CREATE FUNCTION named_unnamed_arg(bob text, int) RETURNS int LANGUAGE plrust as $$ Ok(None) $$;")
}

#[pg_test]
#[search_path(@extschema@)]
#[should_panic(
expected = "is an invalid Rust identifier and cannot be used as an argument name"
)]
fn invalid_arg_identifier() -> spi::Result<()> {
Spi::run("CREATE FUNCTION invalid_arg_identifier(\"this isn't a valid rust identifier\" int) RETURNS int LANGUAGE plrust as $$ Ok(None) $$;")
}
}
Loading

0 comments on commit 291842d

Please sign in to comment.