From d269fce4422c681cfb756ded837042fcb35b96f5 Mon Sep 17 00:00:00 2001 From: inoas Date: Fri, 31 May 2024 22:19:09 +0200 Subject: [PATCH] fix some tests --- .../where_execution_result_test.accepted | 7 +++ .../where_prepared_statement_test.accepted | 20 ++++++++ birdie_snapshots/where_test.accepted | 36 ++++++++++++++ src/cake.gleam | 2 +- src/cake/adapter/postgres_adapter.gleam | 7 ++- src/cake/adapter/sqlite_adapter.gleam | 7 ++- src/cake/internal/query.gleam | 8 ++-- src/cake/query/where.gleam | 10 +++- test/cake/internal/query/select_test.gleam | 7 +-- test/cake/internal/query/where_test.gleam | 47 +++++++++++++++++++ 10 files changed, 131 insertions(+), 20 deletions(-) create mode 100644 birdie_snapshots/where_execution_result_test.accepted create mode 100644 birdie_snapshots/where_prepared_statement_test.accepted create mode 100644 birdie_snapshots/where_test.accepted create mode 100644 test/cake/internal/query/where_test.gleam diff --git a/birdie_snapshots/where_execution_result_test.accepted b/birdie_snapshots/where_execution_result_test.accepted new file mode 100644 index 0000000..d0fbfb0 --- /dev/null +++ b/birdie_snapshots/where_execution_result_test.accepted @@ -0,0 +1,7 @@ +--- +version: 1.1.6 +title: where_execution_result_test +file: ./test/cake/internal/query/where_test.gleam +test_name: where_execution_result_test +--- +#(Ok([]), Ok([])) \ No newline at end of file diff --git a/birdie_snapshots/where_prepared_statement_test.accepted b/birdie_snapshots/where_prepared_statement_test.accepted new file mode 100644 index 0000000..017150e --- /dev/null +++ b/birdie_snapshots/where_prepared_statement_test.accepted @@ -0,0 +1,20 @@ +--- +version: 1.1.6 +title: where_prepared_statement_test +file: ./test/cake/internal/query/where_test.gleam +test_name: where_prepared_statement_test +--- +#( + PreparedStatement( + "$", + "SELECT * FROM cats WHERE $1 = name", + [StringParam("Hello")], + 1, + ), + PreparedStatement( + "?", + "SELECT * FROM cats WHERE ?1 = name", + [StringParam("Hello")], + 1, + ), +) \ No newline at end of file diff --git a/birdie_snapshots/where_test.accepted b/birdie_snapshots/where_test.accepted new file mode 100644 index 0000000..a8529ba --- /dev/null +++ b/birdie_snapshots/where_test.accepted @@ -0,0 +1,36 @@ +--- +version: 1.1.6 +title: where_test +file: ./test/cake/internal/query/where_test.gleam +test_name: where_test +--- +#( + SelectQuery(Select( + NoSelects, + FromTable("cats"), + NoJoins, + WhereEqual( + WhereParam(StringParam("Hello")), + WhereColumn("name"), + ), + NoGroupBy, + NoWhere, + NoOrderBy, + NoLimitNoOffset, + NoEpilog, + )), + SelectQuery(Select( + NoSelects, + FromTable("cats"), + NoJoins, + WhereEqual( + WhereParam(StringParam("Hello")), + WhereColumn("name"), + ), + NoGroupBy, + NoWhere, + NoOrderBy, + NoLimitNoOffset, + NoEpilog, + )), +) \ No newline at end of file diff --git a/src/cake.gleam b/src/cake.gleam index 76b76d9..4c3d8e4 100644 --- a/src/cake.gleam +++ b/src/cake.gleam @@ -48,7 +48,7 @@ pub fn run_dummy_fragment() { |> s.where( w.col("name") |> w.eq( - w.fragment( + w.value_fragment( frgmt.prepared( "LOWER(" <> frgmt.placeholder diff --git a/src/cake/adapter/postgres_adapter.gleam b/src/cake/adapter/postgres_adapter.gleam index ee70fd8..21d5843 100644 --- a/src/cake/adapter/postgres_adapter.gleam +++ b/src/cake/adapter/postgres_adapter.gleam @@ -31,9 +31,8 @@ pub fn with_connection(f: fn(Connection) -> a) -> a { pub fn run_query(db_conn, query qry: Query, decoder dcdr) { let prp_stm = to_prepared_statement(qry) - let sql = - prepared_statement.get_sql(prp_stm) - |> iox.dbg + let sql = prepared_statement.get_sql(prp_stm) + // |> iox.dbg let params = prepared_statement.get_params(prp_stm) @@ -49,7 +48,7 @@ pub fn run_query(db_conn, query qry: Query, decoder dcdr) { } }) |> iox.print_tap("Params: ") - |> iox.dbg + // |> iox.dbg let result = sql diff --git a/src/cake/adapter/sqlite_adapter.gleam b/src/cake/adapter/sqlite_adapter.gleam index 4c50361..7edfd8d 100644 --- a/src/cake/adapter/sqlite_adapter.gleam +++ b/src/cake/adapter/sqlite_adapter.gleam @@ -19,9 +19,8 @@ pub fn with_memory_connection(callback_fun) { pub fn run_query(db_connection db_conn, query qry: Query, decoder dcdr) { let prp_stm = to_prepared_statement(qry) - let sql = - prepared_statement.get_sql(prp_stm) - |> iox.dbg + let sql = prepared_statement.get_sql(prp_stm) + // |> iox.dbg let params = prepared_statement.get_params(prp_stm) @@ -36,7 +35,7 @@ pub fn run_query(db_connection db_conn, query qry: Query, decoder dcdr) { } }) |> iox.print_tap("Params: ") - |> iox.dbg + // |> iox.dbg sql |> sqlight.query(on: db_conn, with: db_params, expecting: dcdr) diff --git a/src/cake/internal/query.gleam b/src/cake/internal/query.gleam index 2656865..e481aa8 100644 --- a/src/cake/internal/query.gleam +++ b/src/cake/internal/query.gleam @@ -271,8 +271,6 @@ fn select_clause_apply( } } -import gleam/io - fn select_value_apply( prepared_statement prp_stm: PreparedStatement, value v: SelectValue, @@ -345,6 +343,7 @@ pub type Where { OrWhere(parts: List(Where)) // TODO: XorWhere(List(Where)) NotWhere(part: Where) + RawWhereFragment(fragment: Fragment) WhereExistsInSubQuery(sub_query: Query) // WhereAllOfSubQuery(value: WhereValue, sub_query: Query) // WhereAnyOfSubQuery(value: WhereValue, sub_query: Query) @@ -417,6 +416,7 @@ fn where_apply( WhereIn(val, vals) -> prp_stm |> where_apply_value_in_values(val, vals) WhereBetween(val_a, val_b, val_c) -> prp_stm |> where_between_apply(val_a, val_b, val_c) + RawWhereFragment(fragment) -> prp_stm |> fragment_apply(fragment) WhereExistsInSubQuery(sub_query) -> prp_stm |> where_exists_in_sub_query_apply(sub_query) } @@ -436,7 +436,7 @@ fn where_apply_literal( prepared_statement prp_stm: PreparedStatement, value v: WhereValue, literal lt: String, -) { +) -> PreparedStatement { case v { WhereColumn(col) -> prp_stm |> prepared_statement.append_sql(col <> " " <> lt) @@ -455,7 +455,7 @@ fn where_comparison_apply( value_a val_a: WhereValue, operator oprtr: String, value_b val_b: WhereValue, -) { +) -> PreparedStatement { case val_a, val_b { WhereColumn(col_a), WhereColumn(col_b) -> prp_stm diff --git a/src/cake/query/where.gleam b/src/cake/query/where.gleam index 119c82b..24bc02a 100644 --- a/src/cake/query/where.gleam +++ b/src/cake/query/where.gleam @@ -1,4 +1,6 @@ -import cake/internal/query.{type Fragment, type Where, type WhereValue} +import cake/internal/query.{ + type Fragment, type Where, type WhereValue, RawWhereFragment, +} import cake/param pub fn col(name: String) -> WhereValue { @@ -109,6 +111,10 @@ pub fn similar(value val: WhereValue, to pttrn: String) -> Where { val |> query.WhereSimilar(pttrn) } -pub fn fragment(fragment frgmt: Fragment) -> WhereValue { +pub fn fragment(fragment frgmt: Fragment) -> Where { + RawWhereFragment(frgmt) +} + +pub fn value_fragment(fragment frgmt: Fragment) -> WhereValue { frgmt |> query.WhereFragment } diff --git a/test/cake/internal/query/select_test.gleam b/test/cake/internal/query/select_test.gleam index cff78b4..cdf1f26 100644 --- a/test/cake/internal/query/select_test.gleam +++ b/test/cake/internal/query/select_test.gleam @@ -8,7 +8,7 @@ import pprint.{format as to_string} import test_helper/postgres_test_helper import test_helper/sqlite_test_helper -const literal = "age" +const const_field = "age" fn selects_query() { f.table(name: "cats") @@ -19,7 +19,7 @@ fn selects_query() { // sut.float(1.0), // sut.int(1), sut.string("hello"), - sut.fragment(frgmt.literal(literal)), + sut.fragment(frgmt.literal(const_field)), sut.alias(sut.col("age"), "years_since_birth"), ]) |> sut.to_query @@ -50,8 +50,5 @@ pub fn selects_execution_result_test() { selects_query() #(expected_pgo, expected_sql) |> to_string - |> io.debug() |> birdie.snap("selects_execution_result_test") } - -import gleam/io diff --git a/test/cake/internal/query/where_test.gleam b/test/cake/internal/query/where_test.gleam new file mode 100644 index 0000000..427da7a --- /dev/null +++ b/test/cake/internal/query/where_test.gleam @@ -0,0 +1,47 @@ +import birdie +import cake/adapter/postgres_adapter +import cake/adapter/sqlite_adapter +import cake/query/fragment as frgmt +import cake/query/from as f +import cake/query/select as s +import cake/query/where as w +import pprint.{format as to_string} +import test_helper/postgres_test_helper +import test_helper/sqlite_test_helper + +fn where_query() { + f.table(name: "cats") + |> s.new_from + |> s.where(w.col("age") |> w.eq(w.int(10))) + |> s.where(w.fragment(frgmt.literal("1 = 1"))) + |> s.where(w.string("Hello") |> w.eq(w.col("name"))) + |> s.to_query +} + +pub fn where_test() { + let expected_pgo = where_query() + let expected_sql = where_query() + + #(expected_pgo, expected_sql) + |> to_string + |> birdie.snap("where_test") +} + +pub fn where_prepared_statement_test() { + let expected_pgo = where_query() |> postgres_adapter.to_prepared_statement + let expected_sql = where_query() |> sqlite_adapter.to_prepared_statement + + #(expected_pgo, expected_sql) + |> to_string + |> birdie.snap("where_prepared_statement_test") +} + +pub fn where_execution_result_test() { + let expected_pgo = where_query() |> postgres_test_helper.setup_and_run + let expected_sql = where_query() |> sqlite_test_helper.setup_and_run + + where_query() + #(expected_pgo, expected_sql) + |> to_string + |> birdie.snap("where_execution_result_test") +}