Skip to content

Commit 01f1d00

Browse files
danieljharveyGil Mizrahi
and
Gil Mizrahi
authored
chore: fresh DBs for mutation tests (#247)
<!-- The PR description should answer 2 (maybe 3) important questions: --> ### What We want to test mutations. To do this, we need fresh databases we can mutate and discard. This creates helpers for creating a fresh database. I think the abstraction could be improved, but hopefully it's not too arduous for the test writer. ### How Helpers to - make a UUID - create a DB with UUID as name - connect to that DB, import chinook - return new connection string - delete it afterwards --------- Co-authored-by: Gil Mizrahi <gil@hasura.io>
1 parent 3f1e402 commit 01f1d00

File tree

17 files changed

+362
-14
lines changed

17 files changed

+362
-14
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ generated/
2323
# nix outputs
2424
/result
2525
/result-*
26+
27+
# deployments created for testing
28+
static/temp-deploys/*.json

Cargo.lock

+108
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ndc-citus/tests/common/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub async fn create_router() -> axum::Router {
1212

1313
// work out where the deployment configs live
1414
let test_deployment_file =
15-
tests_common::deployment::get_deployment_file(CHINOOK_DEPLOYMENT_PATH);
15+
tests_common::deployment::helpers::get_path_from_project_root(CHINOOK_DEPLOYMENT_PATH);
1616

1717
// initialise server state with the static configuration.
1818
let state = ndc_sdk::default_main::init_server_state::<connector::Citus>(

crates/ndc-citus/tests/configuration_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use similar_asserts::assert_eq;
1212
// it should be switched the to the Citus one later
1313
use ndc_postgres::configuration;
1414

15-
use tests_common::deployment::get_deployment_file;
15+
use tests_common::deployment::helpers::get_path_from_project_root;
1616

1717
const CONFIGURATION_QUERY: &str = include_str!("../../ndc-postgres/src/configuration.sql");
1818

@@ -26,7 +26,7 @@ async fn test_configure() {
2626
};
2727

2828
let expected_value: serde_json::Value = {
29-
let file = fs::File::open(get_deployment_file(common::CHINOOK_DEPLOYMENT_PATH))
29+
let file = fs::File::open(get_path_from_project_root(common::CHINOOK_DEPLOYMENT_PATH))
3030
.expect("fs::File::open");
3131
let mut result: serde_json::Value =
3232
serde_json::from_reader(file).expect("serde_json::from_reader");

crates/ndc-cockroach/tests/common/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub async fn create_router() -> axum::Router {
1111

1212
// work out where the deployment configs live
1313
let test_deployment_file =
14-
tests_common::deployment::get_deployment_file(CHINOOK_DEPLOYMENT_PATH);
14+
tests_common::deployment::helpers::get_path_from_project_root(CHINOOK_DEPLOYMENT_PATH);
1515

1616
// initialise server state with the static configuration.
1717
let state = ndc_sdk::default_main::init_server_state::<connector::Cockroach>(

crates/ndc-cockroach/tests/configuration_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::fs;
88

99
use similar_asserts::assert_eq;
1010

11-
use tests_common::deployment::get_deployment_file;
11+
use tests_common::deployment::helpers::get_path_from_project_root;
1212

1313
const CONFIGURATION_QUERY: &str = include_str!("../src/configuration.sql");
1414

@@ -22,7 +22,7 @@ async fn test_configure() {
2222
};
2323

2424
let expected_value: serde_json::Value = {
25-
let file = fs::File::open(get_deployment_file(common::CHINOOK_DEPLOYMENT_PATH))
25+
let file = fs::File::open(get_path_from_project_root(common::CHINOOK_DEPLOYMENT_PATH))
2626
.expect("fs::File::open");
2727
let mut result: serde_json::Value =
2828
serde_json::from_reader(file).expect("serde_json::from_reader");

crates/ndc-postgres/tests/common/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ pub const CHINOOK_DEPLOYMENT_PATH: &str = "static/chinook-deployment.json";
66

77
/// Creates a router with a fresh state from the test deployment.
88
pub async fn create_router() -> axum::Router {
9+
create_router_from_deployment(CHINOOK_DEPLOYMENT_PATH).await
10+
}
11+
12+
/// Creates a router with a fresh state from a deployment file path
13+
pub async fn create_router_from_deployment(deployment_path: &str) -> axum::Router {
914
let _ = env_logger::builder().is_test(true).try_init();
1015

1116
// work out where the deployment configs live
1217
let test_deployment_file =
13-
tests_common::deployment::get_deployment_file(CHINOOK_DEPLOYMENT_PATH);
18+
tests_common::deployment::helpers::get_path_from_project_root(deployment_path);
1419

1520
// initialise server state with the static configuration.
1621
let state = ndc_sdk::default_main::init_server_state::<connector::Postgres>(

crates/ndc-postgres/tests/configuration_tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use similar_asserts::assert_eq;
1010

1111
use ndc_postgres::configuration;
1212

13-
use tests_common::deployment::get_deployment_file;
13+
use tests_common::deployment::helpers::get_path_from_project_root;
1414

1515
const POSTGRESQL_CONNECTION_STRING: &str = "postgresql://postgres:password@localhost:64002";
1616
const CHINOOK_DEPLOYMENT_PATH: &str = "static/chinook-deployment.json";
@@ -26,8 +26,8 @@ async fn test_configure() {
2626
};
2727

2828
let expected_value: serde_json::Value = {
29-
let file =
30-
fs::File::open(get_deployment_file(CHINOOK_DEPLOYMENT_PATH)).expect("fs::File::open");
29+
let file = fs::File::open(get_path_from_project_root(CHINOOK_DEPLOYMENT_PATH))
30+
.expect("fs::File::open");
3131
let mut result: serde_json::Value =
3232
serde_json::from_reader(file).expect("serde_json::from_reader");
3333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub mod common;
2+
use tests_common::deployment::{clean_up_deployment, create_fresh_deployment};
3+
use tests_common::request::run_query;
4+
5+
pub const POSTGRESQL_CONNECTION_STRING: &str = "postgresql://postgres:password@localhost:64002";
6+
pub const CHINOOK_DEPLOYMENT_PATH: &str = "static/chinook-deployment.json";
7+
8+
/// create a fresh db then run a query against it
9+
mod basic {
10+
use super::{clean_up_deployment, create_fresh_deployment, run_query};
11+
12+
#[tokio::test]
13+
async fn select_by_pk() {
14+
let deployment = create_fresh_deployment(
15+
super::POSTGRESQL_CONNECTION_STRING,
16+
super::CHINOOK_DEPLOYMENT_PATH,
17+
)
18+
.await;
19+
20+
let result = run_query(
21+
super::common::create_router_from_deployment(&deployment.deployment_path).await,
22+
"select_by_pk",
23+
)
24+
.await;
25+
26+
clean_up_deployment(deployment).await;
27+
insta::assert_json_snapshot!(result)
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: crates/ndc-postgres/tests/mutation_tests.rs
3+
expression: result
4+
---
5+
[
6+
{
7+
"rows": [
8+
{
9+
"Title": "Garage Inc. (Disc 1)"
10+
}
11+
]
12+
}
13+
]

crates/other-db-tests/src/aurora/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub async fn create_router() -> axum::Router {
1616

1717
// work out where the deployment configs live
1818
let test_deployment_file =
19-
tests_common::deployment::get_deployment_file(CHINOOK_DEPLOYMENT_PATH);
19+
tests_common::deployment::helpers::get_path_from_project_root(CHINOOK_DEPLOYMENT_PATH);
2020

2121
// initialise server state with the static configuration.
2222
let state = ndc_sdk::default_main::init_server_state::<connector::Postgres>(

crates/tests-common/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ tracing = "0.1.37"
1717
axum = "0.6.19"
1818
axum-test-helper = "0.3.0"
1919
reqwest = "0.11.20"
20+
tokio-postgres = "0.7.10"
21+
sqlx = { version = "0.7.1", features = [ "json", "postgres", "runtime-tokio-rustls" ] }
22+
uuid = {version = "1.4.1", features = [ "v4", "fast-rng", "macro-diagnostics" ]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Deployment configuration functions used across test cases.
2+
//! Use via helpers in `mod.rs` rather than directly.
3+
//!
4+
use super::helpers::get_path_from_project_root;
5+
use serde_json::Value;
6+
use std::fs;
7+
8+
/// Load deployment at `main_deployment_path`
9+
/// replace url with `new_postgres_url`
10+
/// save at `new_deployment_path`
11+
pub fn copy_deployment_with_new_postgres_url(
12+
main_deployment_path: &str,
13+
new_postgres_url: &str,
14+
new_deployment_path: &str,
15+
) {
16+
let full_path = get_path_from_project_root(main_deployment_path);
17+
18+
// load and decode deployment
19+
let deployment: Value = serde_json::from_str(&fs::read_to_string(full_path).unwrap()).unwrap();
20+
21+
let new_json = match deployment {
22+
Value::Object(mut map) => {
23+
map.insert(
24+
"postgres_database_url".to_string(),
25+
Value::String(new_postgres_url.to_string()),
26+
);
27+
Value::Object(map)
28+
}
29+
other => other,
30+
};
31+
32+
let new_absolute_deployment_path = get_path_from_project_root(new_deployment_path);
33+
34+
let new_deployment = new_json.to_string();
35+
36+
fs::write(new_absolute_deployment_path, new_deployment).unwrap()
37+
}
38+
39+
/// Erase test deployment file created at `deployment_path`
40+
pub fn delete_deployment(deployment_path: &str) {
41+
let absolute_path = get_path_from_project_root(deployment_path);
42+
43+
fs::remove_file(absolute_path).unwrap()
44+
}

0 commit comments

Comments
 (0)