Skip to content

Commit e7853dc

Browse files
author
Dave Selph
committed
split test from the lib.rs into different rs files
each new rs covers an area of test
1 parent 00a9bbe commit e7853dc

18 files changed

+1602
-1321
lines changed

plrust-tests/src/alter.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
/*
3+
Portions Copyright 2020-2021 ZomboDB, LLC.
4+
Portions Copyright 2021-2023 Technology Concepts & Design, Inc. <[email protected]>
5+
6+
All rights reserved.
7+
8+
Use of this source code is governed by the PostgreSQL license that can be found in the LICENSE.md file.
9+
*/
10+
11+
#[cfg(any(test, feature = "pg_test"))]
12+
#[pgrx::pg_schema]
13+
mod tests {
14+
use pgrx:: prelude::*;
15+
16+
17+
#[pg_test]
18+
#[search_path(@extschema@)]
19+
#[should_panic = "plrust functions cannot have their STRICT property altered"]
20+
fn plrust_cant_change_strict_off() -> spi::Result<()> {
21+
let definition = r#"
22+
CREATE FUNCTION cant_change_strict_off()
23+
RETURNS int
24+
LANGUAGE plrust
25+
AS $$ Ok(Some(1)) $$;
26+
"#;
27+
Spi::run(definition)?;
28+
Spi::run("ALTER FUNCTION cant_change_strict_off() CALLED ON NULL INPUT")
29+
}
30+
31+
#[pg_test]
32+
#[search_path(@extschema@)]
33+
#[should_panic = "plrust functions cannot have their STRICT property altered"]
34+
fn plrust_cant_change_strict_on() -> spi::Result<()> {
35+
let definition = r#"
36+
CREATE FUNCTION cant_change_strict_on()
37+
RETURNS int
38+
LANGUAGE plrust
39+
AS $$ Ok(Some(1)) $$;
40+
"#;
41+
Spi::run(definition)?;
42+
Spi::run("ALTER FUNCTION cant_change_strict_on() RETURNS NULL ON NULL INPUT")
43+
}
44+
45+
}

plrust-tests/src/argument.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
/*
3+
Portions Copyright 2020-2021 ZomboDB, LLC.
4+
Portions Copyright 2021-2023 Technology Concepts & Design, Inc. <[email protected]>
5+
6+
All rights reserved.
7+
8+
Use of this source code is governed by the PostgreSQL license that can be found in the LICENSE.md file.
9+
*/
10+
11+
#[cfg(any(test, feature = "pg_test"))]
12+
#[pgrx::pg_schema]
13+
mod tests {
14+
use pgrx:: prelude::*;
15+
16+
#[pg_test]
17+
#[search_path(@extschema@)]
18+
#[should_panic = "parameter name \"a\" used more than once"]
19+
fn plrust_dup_args() -> spi::Result<()> {
20+
let definition = r#"
21+
CREATE FUNCTION not_unique(a int, a int)
22+
RETURNS int AS
23+
$$
24+
Ok(a)
25+
$$ LANGUAGE plrust;
26+
"#;
27+
Spi::run(definition)?;
28+
let result = Spi::get_one::<i32>("SELECT not_unique(1, 2);\n");
29+
assert_eq!(Ok(Some(1)), result);
30+
Ok(())
31+
}
32+
33+
#[pg_test]
34+
#[search_path(@extschema@)]
35+
#[should_panic = "PL/Rust does not support unnamed arguments"]
36+
fn plrust_defaulting_dup_args() -> spi::Result<()> {
37+
let definition = r#"
38+
CREATE FUNCTION not_unique(int, arg0 int)
39+
RETURNS int AS
40+
$$
41+
Ok(arg0)
42+
$$ LANGUAGE plrust;
43+
"#;
44+
Spi::run(definition)?;
45+
let result = Spi::get_one::<i32>("SELECT not_unique(1, 2);\n");
46+
assert_eq!(Ok(Some(1)), result);
47+
Ok(())
48+
}
49+
50+
#[pg_test]
51+
#[search_path(@extschema@)]
52+
#[should_panic(expected = "PL/Rust does not support unnamed arguments")]
53+
fn unnamed_args() -> spi::Result<()> {
54+
Spi::run("CREATE FUNCTION unnamed_arg(int) RETURNS int LANGUAGE plrust as $$ Ok(None) $$;")
55+
}
56+
57+
#[pg_test]
58+
#[search_path(@extschema@)]
59+
#[should_panic(expected = "PL/Rust does not support unnamed arguments")]
60+
fn named_unnamed_args() -> spi::Result<()> {
61+
Spi::run("CREATE FUNCTION named_unnamed_arg(bob text, int) RETURNS int LANGUAGE plrust as $$ Ok(None) $$;")
62+
}
63+
64+
#[pg_test]
65+
#[search_path(@extschema@)]
66+
#[should_panic(
67+
expected = "is an invalid Rust identifier and cannot be used as an argument name"
68+
)]
69+
fn invalid_arg_identifier() -> spi::Result<()> {
70+
Spi::run("CREATE FUNCTION invalid_arg_identifier(\"this isn't a valid rust identifier\" int) RETURNS int LANGUAGE plrust as $$ Ok(None) $$;")
71+
}
72+
}

plrust-tests/src/basic.rs

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
2+
/*
3+
Portions Copyright 2020-2021 ZomboDB, LLC.
4+
Portions Copyright 2021-2023 Technology Concepts & Design, Inc. <[email protected]>
5+
6+
All rights reserved.
7+
8+
Use of this source code is governed by the PostgreSQL license that can be found in the LICENSE.md file.
9+
*/
10+
11+
#[cfg(any(test, feature = "pg_test"))]
12+
#[pgrx::pg_schema]
13+
mod tests {
14+
use pgrx::{datum::IntoDatum, prelude::*};
15+
16+
17+
#[pg_test]
18+
#[search_path(@extschema@)]
19+
fn plrust_basic() -> spi::Result<()> {
20+
let definition = r#"
21+
CREATE FUNCTION sum_array(a BIGINT[]) RETURNS BIGINT
22+
IMMUTABLE STRICT
23+
LANGUAGE PLRUST AS
24+
$$
25+
Ok(Some(a.into_iter().map(|v| v.unwrap_or_default()).sum()))
26+
$$;
27+
"#;
28+
Spi::run(definition)?;
29+
30+
let retval = Spi::get_one_with_args::<i64>(
31+
r#"
32+
SELECT sum_array($1);
33+
"#,
34+
vec![(
35+
PgBuiltInOids::INT4ARRAYOID.oid(),
36+
vec![1, 2, 3].into_datum(),
37+
)],
38+
);
39+
assert_eq!(retval, Ok(Some(6)));
40+
Ok(())
41+
}
42+
43+
44+
#[pg_test]
45+
#[search_path(@extschema@)]
46+
fn plrust_update() -> spi::Result<()> {
47+
let definition = r#"
48+
CREATE FUNCTION update_me() RETURNS TEXT
49+
IMMUTABLE STRICT
50+
LANGUAGE PLRUST AS
51+
$$
52+
Ok(String::from("booper").into())
53+
$$;
54+
"#;
55+
Spi::run(definition)?;
56+
57+
let retval = Spi::get_one(
58+
r#"
59+
SELECT update_me();
60+
"#,
61+
);
62+
assert_eq!(retval, Ok(Some("booper")));
63+
64+
let definition = r#"
65+
CREATE OR REPLACE FUNCTION update_me() RETURNS TEXT
66+
IMMUTABLE STRICT
67+
LANGUAGE PLRUST AS
68+
$$
69+
Ok(Some(String::from("swooper")))
70+
$$;
71+
"#;
72+
Spi::run(definition)?;
73+
74+
let retval = Spi::get_one(
75+
r#"
76+
SELECT update_me();
77+
"#,
78+
);
79+
assert_eq!(retval, Ok(Some("swooper")));
80+
Ok(())
81+
}
82+
83+
#[pg_test]
84+
#[search_path(@extschema@)]
85+
fn plrust_spi() -> spi::Result<()> {
86+
let random_definition = r#"
87+
CREATE FUNCTION random_contributor_pet() RETURNS TEXT
88+
STRICT
89+
LANGUAGE PLRUST AS
90+
$$
91+
Ok(Spi::get_one("SELECT name FROM contributors_pets ORDER BY random() LIMIT 1")?)
92+
$$;
93+
"#;
94+
Spi::run(random_definition)?;
95+
96+
let retval = Spi::get_one::<String>(
97+
r#"
98+
SELECT random_contributor_pet();
99+
"#,
100+
);
101+
assert!(retval.is_ok());
102+
assert!(retval.unwrap().is_some());
103+
104+
let specific_definition = r#"
105+
CREATE FUNCTION contributor_pet(name TEXT) RETURNS BIGINT
106+
STRICT
107+
LANGUAGE PLRUST AS
108+
$$
109+
use pgrx::IntoDatum;
110+
Ok(Spi::get_one_with_args(
111+
"SELECT id FROM contributors_pets WHERE name = $1",
112+
vec![(PgBuiltInOids::TEXTOID.oid(), name.into_datum())],
113+
)?)
114+
$$;
115+
"#;
116+
Spi::run(specific_definition)?;
117+
118+
let retval = Spi::get_one::<i64>(
119+
r#"
120+
SELECT contributor_pet('Nami');
121+
"#,
122+
);
123+
assert_eq!(retval, Ok(Some(2)));
124+
Ok(())
125+
}
126+
}

0 commit comments

Comments
 (0)