From bb53e551c06cba904b5c4f62f214f00980644230 Mon Sep 17 00:00:00 2001 From: Ben King <9087625+benfdking@users.noreply.github.com> Date: Tue, 30 Apr 2024 11:17:22 +0100 Subject: [PATCH] fix: sql test dont work in python because required alias --- .../quary-databases/src/databases_postgres.rs | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/rust/quary-databases/src/databases_postgres.rs b/rust/quary-databases/src/databases_postgres.rs index 915b9df9..566d41e5 100644 --- a/rust/quary-databases/src/databases_postgres.rs +++ b/rust/quary-databases/src/databases_postgres.rs @@ -719,6 +719,137 @@ models: } } + #[tokio::test] + async fn postgres_sql_test() { + // Start a PostgreSQL container + let docker = clients::Cli::default(); + let postgres_image = RunnableImage::from(TestcontainersPostgres::default()); + let pg_container = docker.run(postgres_image); + let pg_port = pg_container.get_host_port_ipv4(5432); + + let database = Postgres::new( + "localhost", + Some(pg_port.to_string()), + "postgres", + "postgres", + "postgres", + "transform", + None, + None, + None, + None, + None, + None, + ) + .await + .unwrap(); + + database.exec("CREATE SCHEMA other_schema").await.unwrap(); + database.exec("CREATE SCHEMA transform").await.unwrap(); + database + .exec("CREATE TABLE other_schema.test_table (id INTEGER, name VARCHAR(255))") + .await + .unwrap(); + database + .exec("INSERT INTO other_schema.test_table VALUES (1, 'test'), (2, 'rubbish')") + .await + .unwrap(); + database + .exec("CREATE TABLE transform.test_table (id INTEGER, name VARCHAR(255))") + .await + .unwrap(); + database + .exec("INSERT INTO transform.test_table VALUES (3, 'test_3'), (4, 'rubbish_rubiish')") + .await + .unwrap(); + + let file_system = FileSystem { + files: vec![ + ("quary.yaml", "postgres: {schema: transform}"), + ("models/test_model.sql", "SELECT id FROM q.test_source"), + ( + "models/test_model_same_schema.sql", + "SELECT id FROM q.test_source_same_schema", + ), + ("models/test_model_out.sql", "SELECT id FROM q.test_model"), + ( + "tests/test_model_out_is_unique.sql", + "SELECT id, COUNT(*) +FROM q.test_model_out +GROUP BY id +HAVING COUNT(*) > 1; +", + ), + ( + "models/schema.yaml", + " +sources: + - name: test_source + path: other_schema.test_table + - name: test_source_same_schema + path: transform.test_table +models: + - name: test_model_out + columns: + - name: id + tests: + - type: relationship + info: + column: id + model: test_model + - type: relationship + info: + column: id + model: test_source + - name: test_model_same_schema + columns: + - name: id + tests: + - type: relationship + info: + column: id + model: test_source_same_schema + ", + ), + ] + .into_iter() + .map(|(k, v)| { + ( + k.to_string(), + File { + name: k.to_string(), + contents: Bytes::from(v), + }, + ) + }) + .collect(), + }; + + let project = parse_project(&file_system, &database.query_generator(), "") + .await + .unwrap(); + + let tests = return_tests_sql( + &database.query_generator(), + &project, + &file_system, + true, + None, + None, + ) + .await + .unwrap(); + let tests = tests.iter().collect::>(); + + assert!(!tests.is_empty()); + + for (name, test) in tests.iter() { + let results = database.query(test).await.unwrap(); + + assert_eq!(results.rows.len(), 0, "test {} failed: {}", name, test); + } + } + #[tokio::test] async fn test_postgres_foreign_relationship_test_with_materialized_view_table() { let postgres_image = RunnableImage::from(TestcontainersPostgres::default())