Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inoas committed May 31, 2024
1 parent deb837f commit d269fce
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 20 deletions.
7 changes: 7 additions & 0 deletions birdie_snapshots/where_execution_result_test.accepted
Original file line number Diff line number Diff line change
@@ -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([]))
20 changes: 20 additions & 0 deletions birdie_snapshots/where_prepared_statement_test.accepted
Original file line number Diff line number Diff line change
@@ -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,
),
)
36 changes: 36 additions & 0 deletions birdie_snapshots/where_test.accepted
Original file line number Diff line number Diff line change
@@ -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,
)),
)
2 changes: 1 addition & 1 deletion src/cake.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/cake/adapter/postgres_adapter.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/cake/adapter/sqlite_adapter.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/cake/internal/query.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,6 @@ fn select_clause_apply(
}
}

import gleam/io

fn select_value_apply(
prepared_statement prp_stm: PreparedStatement,
value v: SelectValue,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/cake/query/where.gleam
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}
7 changes: 2 additions & 5 deletions test/cake/internal/query/select_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
47 changes: 47 additions & 0 deletions test/cake/internal/query/where_test.gleam
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit d269fce

Please sign in to comment.