Skip to content

Commit

Permalink
split test from the lib.rs into different rs files
Browse files Browse the repository at this point in the history
each new rs covers an area of test
  • Loading branch information
Dave Selph committed Jul 13, 2023
1 parent 7a6ea72 commit 73735d2
Show file tree
Hide file tree
Showing 18 changed files with 1,602 additions and 1,321 deletions.
45 changes: 45 additions & 0 deletions plrust-tests/src/alter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

/*
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")
}

}
72 changes: 72 additions & 0 deletions plrust-tests/src/argument.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

/*
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) $$;")
}
}
126 changes: 126 additions & 0 deletions plrust-tests/src/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@

/*
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::{datum::IntoDatum, prelude::*};


#[pg_test]
#[search_path(@extschema@)]
fn plrust_basic() -> spi::Result<()> {
let definition = r#"
CREATE FUNCTION sum_array(a BIGINT[]) RETURNS BIGINT
IMMUTABLE STRICT
LANGUAGE PLRUST AS
$$
Ok(Some(a.into_iter().map(|v| v.unwrap_or_default()).sum()))
$$;
"#;
Spi::run(definition)?;

let retval = Spi::get_one_with_args::<i64>(
r#"
SELECT sum_array($1);
"#,
vec![(
PgBuiltInOids::INT4ARRAYOID.oid(),
vec![1, 2, 3].into_datum(),
)],
);
assert_eq!(retval, Ok(Some(6)));
Ok(())
}


#[pg_test]
#[search_path(@extschema@)]
fn plrust_update() -> spi::Result<()> {
let definition = r#"
CREATE FUNCTION update_me() RETURNS TEXT
IMMUTABLE STRICT
LANGUAGE PLRUST AS
$$
Ok(String::from("booper").into())
$$;
"#;
Spi::run(definition)?;

let retval = Spi::get_one(
r#"
SELECT update_me();
"#,
);
assert_eq!(retval, Ok(Some("booper")));

let definition = r#"
CREATE OR REPLACE FUNCTION update_me() RETURNS TEXT
IMMUTABLE STRICT
LANGUAGE PLRUST AS
$$
Ok(Some(String::from("swooper")))
$$;
"#;
Spi::run(definition)?;

let retval = Spi::get_one(
r#"
SELECT update_me();
"#,
);
assert_eq!(retval, Ok(Some("swooper")));
Ok(())
}

#[pg_test]
#[search_path(@extschema@)]
fn plrust_spi() -> spi::Result<()> {
let random_definition = r#"
CREATE FUNCTION random_contributor_pet() RETURNS TEXT
STRICT
LANGUAGE PLRUST AS
$$
Ok(Spi::get_one("SELECT name FROM contributors_pets ORDER BY random() LIMIT 1")?)
$$;
"#;
Spi::run(random_definition)?;

let retval = Spi::get_one::<String>(
r#"
SELECT random_contributor_pet();
"#,
);
assert!(retval.is_ok());
assert!(retval.unwrap().is_some());

let specific_definition = r#"
CREATE FUNCTION contributor_pet(name TEXT) RETURNS BIGINT
STRICT
LANGUAGE PLRUST AS
$$
use pgrx::IntoDatum;
Ok(Spi::get_one_with_args(
"SELECT id FROM contributors_pets WHERE name = $1",
vec![(PgBuiltInOids::TEXTOID.oid(), name.into_datum())],
)?)
$$;
"#;
Spi::run(specific_definition)?;

let retval = Spi::get_one::<i64>(
r#"
SELECT contributor_pet('Nami');
"#,
);
assert_eq!(retval, Ok(Some(2)));
Ok(())
}
}
Loading

0 comments on commit 73735d2

Please sign in to comment.