Skip to content

Commit

Permalink
add test to db connection for utf 16 handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobby Grayson committed May 9, 2022
1 parent eec84d1 commit bcfa8ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/snowflex/db_connection/result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ defmodule Snowflex.DBConnection.Result do

defp handle_encoding(data) when is_list(data) do
raw = :erlang.list_to_binary(data)

case :unicode.characters_to_binary(raw) do
utf8 when is_binary(utf8) -> utf8
_ -> :unicode.characters_to_binary(raw, :latin1)
Expand Down
12 changes: 12 additions & 0 deletions test/snowflex/db_connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ defmodule Snowflex.DBConnectionTest do
{:ok, %{}}
end

def handle_call({:sql_query, "return_utf_16"}, _from, state) do
{:reply, {:ok, {:selected, ['col'], [{'CVS Pharmacy®'}]}}, state}
end

def handle_call({:sql_query, "insert " <> _}, _from, state) do
{:reply, {:ok, {:updated, 123}}, state}
end
Expand Down Expand Up @@ -62,6 +66,14 @@ defmodule Snowflex.DBConnectionTest do
assert {:ok, result} = SnowflakeDBConnection.execute("insert query")
assert {:updated, 123} == SnowflakeDBConnection.process_result(result)
end

test "should execute a sql query with utf-16 artifacts and scrub it" do
assert {:ok, %{rows: [{charlist}]}} = SnowflakeDBConnection.execute("return_utf_16")
# above we set this to return 'CVS Pharmacy®', as we saw in the real world, and then
# here we ensure that we just have the ® character
string = Enum.into(charlist, <<>>, fn bit -> <<bit>> end)
assert string == "CVS Pharmacy®"
end
end

describe "execute/2" do
Expand Down

0 comments on commit bcfa8ac

Please sign in to comment.