Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleDsz committed May 24, 2022
1 parent d605baf commit 26bd5b2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/kino_db/sql_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,19 @@ defmodule KinoDB.SQLCell do

defp to_quoted(attrs, quote(do: ReqBigQuery), next) do
{query, params} = parameterize(attrs["query"], next)
opts = query_opts_args(attrs) ++ [[bigquery: {quoted_query(query), params}]]
bigquery = {quoted_query(query), params}
opts = query_opts_args(attrs)

req_opts =
if opts == [],
do: [[bigquery: bigquery]],
else: [Keyword.put(Enum.at(opts, 0), :bigquery, bigquery)]

quote do
unquote(quoted_var(attrs["result_variable"])) =
Req.post!(
unquote(quoted_var(attrs["connection"]["variable"])),
unquote_splicing(opts)
unquote_splicing(req_opts)
)
end
end
Expand Down
38 changes: 38 additions & 0 deletions test/kino_db/connection_cell_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,44 @@ defmodule KinoDB.ConnectionCellTest do
{:ok, db} = Kino.start_child({Exqlite, opts})\
"""
end

test "restores source code from attrs with BigQuery" do
attrs = %{
"variable" => "db",
"type" => "bigquery",
"project_id" => "",
"private_key_id" => "",
"private_key" => "",
"client_email" => "",
"client_id" => "",
"default_dataset_id" => ""
}

{_kino, source} = start_smart_cell!(ConnectionCell, attrs)

assert source ==
"""
scopes = ["https://www.googleapis.com/auth/cloud-platform"]
credentials = %{
"project_id" => "",
"private_key_id" => "",
"private_key" => "",
"client_email" => "",
"client_id" => ""
}
goth_opts = [
name: Goth,
http_client: &Req.request/1,
source: {:service_account, credentials, scopes: scopes}
]
opts = [goth: Goth, project_id: "", default_dataset_id: ""]
db = ReqBigQuery.attach(Req.new(), opts)
{:ok, _goth_pid} = Kino.start_child({Goth, goth_opts})\
"""
end
end

test "when a field changes, broadcasts the change and sends source update" do
Expand Down
39 changes: 39 additions & 0 deletions test/kino_db/sql_cell_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ defmodule KinoDB.SQLCellTest do
assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlite")) == """
result = Exqlite.query!(conn, "SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "bigquery")) == """
result = Req.post!(conn, bigquery: {\"SELECT id FROM users\", []})\
"""
end

test "uses heredoc string for a multi-line query" do
Expand Down Expand Up @@ -127,6 +131,17 @@ defmodule KinoDB.SQLCellTest do
[]
)\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "bigquery")) == ~s'''
result =
Req.post!(conn,
bigquery:
{"""
SELECT id FROM users
WHERE last_name = 'Sherlock'
""", []}
)\
'''
end

test "parses parameter expressions" do
Expand Down Expand Up @@ -160,6 +175,14 @@ defmodule KinoDB.SQLCellTest do
search <> "%"
])\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "bigquery")) == ~s'''
result =
Req.post!(conn,
bigquery:
{"SELECT id FROM users WHERE id ? AND name LIKE ?", [user_id, search <> "%"]}
)\
'''
end

test "ignores parameters inside comments" do
Expand Down Expand Up @@ -212,6 +235,18 @@ defmodule KinoDB.SQLCellTest do
[user_id3]
)\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "bigquery")) == ~s'''
result =
Req.post!(conn,
bigquery:
{"""
SELECT id from users
-- WHERE id = {{user_id1}}
/* WHERE id = {{user_id2}} */ WHERE id = ?
""", [user_id3]}
)\
'''
end

test "passes timeout option when a timeout is specified" do
Expand All @@ -233,6 +268,10 @@ defmodule KinoDB.SQLCellTest do
assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlite")) == """
result = Exqlite.query!(conn, "SELECT id FROM users", [], timeout: 30000)\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "bigquery")) == """
result = Req.post!(conn, bigquery: {"SELECT id FROM users", []}, timeout: 30000)\
"""
end
end
end

0 comments on commit 26bd5b2

Please sign in to comment.