Skip to content

Commit

Permalink
Reorganize some tests and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
boustrophedon committed Feb 16, 2024
1 parent 155efd0 commit ac1e11c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ impl PgTempDBBuilder {
self
}

/// Set an arbitrary PostgreSQL server configuration parameter that will be inserted into
/// `postgresql.conf` by initdb.
/// Set an arbitrary PostgreSQL server configuration parameter that will passed to the
/// postgresql process at runtime.
#[must_use]
pub fn with_config_param(mut self, key: &str, value: &str) -> Self {
let _old = self.server_configs.insert(key.into(), value.into());
Expand Down
15 changes: 14 additions & 1 deletion tests/basic_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ async fn buider_setters() {
.with_username("testuser")
.with_password("potato")
.with_port(9954)
.with_dbname("testdb1");
.with_dbname("testdb1")
.with_config_param("max_connections", "777");
assert_eq!(db.get_user(), "testuser");
assert_eq!(db.get_password(), "potato");
assert_eq!(db.get_port_or_set_random(), 9954);
Expand All @@ -63,6 +64,9 @@ async fn buider_setters() {
assert_eq!(db.get_dbname(), db2.get_dbname());

let db = db.start();
// test the debug and libpq conn strings formatters don't panic
println!("{:?}", db);
println!("{}", db.connection_string());
let mut conn = PgConnection::connect(&db.connection_uri())
.await
.expect("failed to connect to db");
Expand All @@ -74,6 +78,15 @@ async fn buider_setters() {

let name: String = row.get(0);
assert_eq!(name, "testdb1");

// check config param setting as well
let row = sqlx::query("SELECT setting from pg_settings WHERE name = 'max_connections'")
.fetch_one(&mut conn)
.await
.expect("failed to execute current db query");

let name: &str = row.get(0);
assert_eq!(name, "777");
}

#[tokio::test]
Expand Down
11 changes: 0 additions & 11 deletions tests/misc.rs

This file was deleted.

46 changes: 6 additions & 40 deletions tests/startup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// TODO: these are disabled in ci because ci doesn't have postgres16 (and presumably neither does
// many existing deployments) but really they aren't needed since all the other tests basically
// test these things anyway
#![cfg(feature = "pg16")]
use pgtemp::PgTempDB;

#[test]
Expand All @@ -11,12 +7,8 @@ fn test_tempdb_bringup_shutdown() {
let data_dir = db.data_dir().clone();
let conf_file = data_dir.join("postgresql.conf");

// Read the conf file and check it isn't empty and the port is correct
let res = std::fs::read_to_string(&conf_file);
assert!(res.is_ok());
let text = res.unwrap();
assert!(!text.is_empty());
assert!(text.contains(&format!("port = {}", db.db_port())));
assert!(conf_file.exists());

drop(db);

assert!(!conf_file.exists());
Expand All @@ -29,12 +21,8 @@ async fn test_tempdb_bringup_shutdown_async() {
let data_dir = db.data_dir().clone();
let conf_file = data_dir.join("postgresql.conf");

// Read the conf file and check it isn't empty and the port is correct
let res = tokio::fs::read_to_string(&conf_file).await;
assert!(res.is_ok());
let text = res.unwrap();
assert!(!text.is_empty());
assert!(text.contains(&format!("port = {}", db.db_port())));
assert!(conf_file.exists());

drop(db);

assert!(!conf_file.exists());
Expand All @@ -51,32 +39,10 @@ fn test_tempdb_bringup_shutdown_persist() {
.start();
let data_dir = db.data_dir().clone();
let conf_file = data_dir.join("postgresql.conf");
let port = db.db_port();
drop(db);

// Read the conf file and check it isn't empty and the port is correct
let res = std::fs::read_to_string(&conf_file);
assert!(res.is_ok());
let text = res.unwrap();
assert!(!text.is_empty());
assert!(text.contains(&format!("port = {}", port)));

assert!(conf_file.exists());
}

#[test]
/// Test extra config parameter option
fn test_extra_config_setter() {
let db = PgTempDB::builder()
.with_config_param("max_connections", "1")
.start();

let data_dir = db.data_dir().clone();
let conf_file = data_dir.join("postgresql.conf");
drop(db);

// Read the conf file and check it has the setting
let res = std::fs::read_to_string(conf_file);
assert!(res.is_ok());
let text = res.unwrap();
assert!(text.contains("max_connections = 1"));
assert!(conf_file.exists());
}

0 comments on commit ac1e11c

Please sign in to comment.