Skip to content

Commit

Permalink
Update to DBConnection 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Aug 31, 2018
1 parent c1c7475 commit 02bc0fb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Mariaex.Mixfile do

defp deps do
[{:decimal, "~> 1.2"},
{:db_connection, "~> 1.1", github: "elixir-ecto/db_connection", ref: "4947966"},
{:db_connection, "~> 2.0.0-dev", github: "elixir-ecto/db_connection", ref: "6d477c"},
{:coverex, "~> 1.4.10", only: :test},
{:ex_doc, ">= 0.0.0", only: :dev},
{:poison, ">= 0.0.0", optional: true}]
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], [], "hexpm"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
"coverex": {:hex, :coverex, "1.4.10", "f6b68f95b3d51d04571a09dd2071c980e8398a38cf663db22b903ecad1083d51", [:mix], [{:httpoison, "~> 0.9", [hex: :httpoison, repo: "hexpm", optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"db_connection": {:git, "https://github.com/elixir-ecto/db_connection.git", "49479667131329376adf1c2c0e9a16bcf470aa84", [ref: "4947966"]},
"db_connection": {:git, "https://github.com/elixir-ecto/db_connection.git", "6d477c1030cd527fb87b01e2ca61b73bc4a9cd21", [ref: "6d477c"]},
"decimal": {:hex, :decimal, "1.5.0", "b0433a36d0e2430e3d50291b1c65f53c37d56f83665b43d79963684865beab68", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down
17 changes: 10 additions & 7 deletions test/query_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule QueryTest do
use ExUnit.Case, async: true
import Mariaex.TestHelper
import ExUnit.CaptureLog

@opts [database: "mariaex_test", username: "mariaex_user", password: "mariaex_pass", cache_size: 2, backoff_type: :stop]
@opts [database: "mariaex_test", username: "mariaex_user", password: "mariaex_pass", cache_size: 2, backoff_type: :stop, max_restarts: 0]

setup context do
connection_opts = context[:connection_opts] || []
Expand Down Expand Up @@ -36,14 +37,16 @@ defmodule QueryTest do
end

test "queries are dequeued after previous query is processed", context do
Process.flag(:trap_exit, true)
conn = context[:pid]

Process.flag(:trap_exit, true)
capture_log fn ->
assert_raise DBConnection.ConnectionError, "tcp recv: closed",
fn -> query("DO SLEEP(10)", [], timeout: 50) end
assert_receive {:EXIT, ^conn, {:shutdown, %DBConnection.ConnectionError{}}}
end
assert capture_log(fn ->
assert_raise DBConnection.ConnectionError, "tcp recv: closed", fn ->
query("DO SLEEP(10)", [], timeout: 50)
end

assert_receive {:EXIT, ^conn, :killed}, 5000
end) =~ "** (DBConnection.ConnectionError)"
end

test "support primitive data types using prepared statements", context do
Expand Down
25 changes: 18 additions & 7 deletions test/start_test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
defmodule StartTest do
use ExUnit.Case, async: true
import ExUnit.CaptureLog

test "connection_errors" do
Process.flag :trap_exit, true
assert {:error, {%Mariaex.Error{mariadb: %{message: "Unknown database 'non_existing'"}}, _}} =
Mariaex.Connection.start_link(username: "mariaex_user", password: "mariaex_pass", database: "non_existing", sync_connect: true, backoff_type: :stop)
assert {:error, {%Mariaex.Error{mariadb: %{message: "Access denied for user " <> _}}, _}} =
Mariaex.Connection.start_link(username: "non_existing", database: "mariaex_test", sync_connect: true, backoff_type: :stop)
assert {:error, {%Mariaex.Error{message: "tcp connect: econnrefused"}, _}} =
Mariaex.Connection.start_link(username: "mariaex_user", password: "mariaex_pass", database: "mariaex_test", port: 60999, sync_connect: true, backoff_type: :stop)
Process.flag(:trap_exit, true)
opts = [sync_connect: true, backoff_type: :stop, max_restarts: 0]

assert capture_log(fn ->
{:ok, pid} = Mariaex.start_link([username: "mariaex_user", password: "mariaex_pass", database: "non_existing"] ++ opts)
assert_receive {:EXIT, ^pid, :killed}, 5000
end) =~ "** (Mariaex.Error) (1049): Unknown database 'non_existing'"

assert capture_log(fn ->
{:ok, pid} = Mariaex.start_link([username: "non_existing", database: "mariaex_test"] ++ opts)
assert_receive {:EXIT, ^pid, :killed}, 5000
end) =~ "** (Mariaex.Error) (1045): Access denied for user 'non_existing'@'localhost'"

assert capture_log(fn ->
{:ok, pid} = Mariaex.start_link([username: "mariaex_user", password: "mariaex_pass", database: "mariaex_test", port: 60999] ++ opts)
assert_receive {:EXIT, ^pid, :killed}, 5000
end) =~ "** (Mariaex.Error) tcp connect: econnrefused"
end

## Tests tagged with :ssl_tests are excluded from running by default (see test_helper.exs)
Expand Down
6 changes: 0 additions & 6 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,6 @@ defmodule Mariaex.TestHelper do
end
end

def capture_log(fun) do
Logger.remove_backend(:console)
fun.()
Logger.add_backend(:console, flush: true)
end

def length_encode_row(row) do
Enum.map_join(row, &(<<String.length(&1)>> <> &1))
end
Expand Down
1 change: 0 additions & 1 deletion test/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ defmodule TransactionTest do
{conn, _} = DBConnection.begin!(pid, opts)
assert {:error, %DBConnection.TransactionError{status: :transaction}} =
DBConnection.begin(conn, opts)
DBConnection.commit!(conn, opts)
end

test "can not commit or rollback transaction if not begun", context do
Expand Down

0 comments on commit 02bc0fb

Please sign in to comment.