diff --git a/.travis.yml b/.travis.yml index 3b4adb2..04f341f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: elixir elixir: - - 1.3 + - 1.6.3 otp_release: - 18.3 diff --git a/lib/rethinkdb/connection.ex b/lib/rethinkdb/connection.ex index 7d9e906..d3506f1 100644 --- a/lib/rethinkdb/connection.ex +++ b/lib/rethinkdb/connection.ex @@ -51,14 +51,14 @@ defmodule RethinkDB.Connection do defmacro __using__(_opts) do quote location: :keep do def start_link(opts \\ []) do - if Dict.has_key?(opts, :name) && opts[:name] != __MODULE__ do + if Keyword.has_key?(opts, :name) && opts[:name] != __MODULE__ do # The whole point of this macro is to provide an implicit process # name, so subverting it is considered an error. raise ArgumentError.exception( "Process name #{inspect opts[:name]} conflicts with implicit name #{inspect __MODULE__} provided by `use RethinkDB.Connection`" ) end - RethinkDB.Connection.start_link(Dict.put_new(opts, :name, __MODULE__)) + RethinkDB.Connection.start_link(Keyword.put_new(opts, :name, __MODULE__)) end def run(query, opts \\ []) do @@ -100,12 +100,14 @@ defmodule RethinkDB.Connection do * `binary_format` - what format to return binary data in (default: :native). Set this to :raw if you want the raw pseudotype. """ def run(query, conn, opts \\ []) do - timeout = Dict.get(opts, :timeout, 5000) - conn_opts = Dict.drop(opts, [:timeout]) - noreply = Dict.get(opts, :noreply, false) - conn_opts = Connection.call(conn, :conn_opts) - |> Dict.take([:db]) - |> Dict.merge(conn_opts) + timeout = Keyword.get(opts, :timeout, 5000) + conn_opts = Keyword.drop(opts, [:timeout]) + noreply = Keyword.get(opts, :noreply, false) + conn_opts = Connection.call(conn, :conn_opts) |> + Map.take([:db]) |> + Enum.to_list |> + Keyword.merge(conn_opts) + query = prepare_and_encode(query, conn_opts) msg = case noreply do true -> {:query_noreply, query} @@ -172,7 +174,7 @@ defmodule RethinkDB.Connection do @doc """ Start connection as a linked process - Accepts a `Dict` of options. Supported options: + Accepts a `Keyword` of options. Supported options: * `:host` - hostname to use to connect to database. Defaults to `'localhost'`. * `:port` - port on which to connect to database. Defaults to `28015`. @@ -184,26 +186,26 @@ defmodule RethinkDB.Connection do * `:ca_certs` - a list of file paths to cacerts. """ def start_link(opts \\ []) do - args = Dict.take(opts, [:host, :port, :auth_key, :db, :sync_connect, :ssl, :max_pending]) + args = Keyword.take(opts, [:host, :port, :auth_key, :db, :sync_connect, :ssl, :max_pending]) Connection.start_link(__MODULE__, args, opts) end def init(opts) do - host = case Dict.get(opts, :host, 'localhost') do - x when is_binary(x) -> String.to_char_list x + host = case Keyword.get(opts, :host, 'localhost') do + x when is_binary(x) -> String.to_charlist x x -> x end - sync_connect = Dict.get(opts, :sync_connect, false) - ssl = Dict.get(opts, :ssl) - opts = Dict.put(opts, :host, host) - |> Dict.put_new(:port, 28015) - |> Dict.put_new(:auth_key, "") - |> Dict.put_new(:max_pending, 10000) - |> Dict.drop([:sync_connect]) + sync_connect = Keyword.get(opts, :sync_connect, false) + ssl = Keyword.get(opts, :ssl) + opts = Keyword.put(opts, :host, host) + |> Keyword.put_new(:port, 28015) + |> Keyword.put_new(:auth_key, "") + |> Keyword.put_new(:max_pending, 10000) + |> Keyword.drop([:sync_connect]) |> Enum.into(%{}) {transport, transport_opts} = case ssl do nil -> {%Transport.TCP{}, []} - x -> {%Transport.SSL{}, Enum.map(Dict.fetch!(x, :ca_certs), &({:cacertfile, &1})) ++ [verify: :verify_peer]} + x -> {%Transport.SSL{}, Enum.map(Keyword.fetch!(x, :ca_certs), &({:cacertfile, &1})) ++ [verify: :verify_peer]} end state = %{ pending: %{}, @@ -230,11 +232,11 @@ defmodule RethinkDB.Connection do :ok -> :ok = Transport.setopts(socket, [active: :once]) # TODO: investigate timeout vs hibernate - {:ok, Dict.put(state, :socket, socket)} + {:ok, Map.put(state, :socket, socket)} end {:error, :econnrefused} -> - backoff = min(Dict.get(state, :timeout, 1000), 64000) - {:backoff, backoff, Dict.put(state, :timeout, backoff*2)} + backoff = min(Map.get(state, :timeout, 1000), 64000) + {:backoff, backoff, Map.put(state, :timeout, backoff*2)} end end diff --git a/lib/rethinkdb/connection/request.ex b/lib/rethinkdb/connection/request.ex index b828dd6..88bc092 100644 --- a/lib/rethinkdb/connection/request.ex +++ b/lib/rethinkdb/connection/request.ex @@ -6,7 +6,7 @@ defmodule RethinkDB.Connection.Request do def make_request(query, token, from, state = %{pending: pending, socket: socket}) do new_pending = case from do :noreply -> pending - _ -> Dict.put_new(pending, token, from) + _ -> Map.put_new(pending, token, from) end bsize = :erlang.size(query) payload = token <> << bsize :: little-size(32) >> <> query @@ -41,7 +41,7 @@ defmodule RethinkDB.Connection.Request do case leftover <> data do << response :: binary-size(length), leftover :: binary >> -> Connection.reply(pending[token], {response, token}) - handle_recv("", %{state | current: {:start, leftover}, pending: Dict.delete(pending, token)}) + handle_recv("", %{state | current: {:start, leftover}, pending: Map.delete(pending, token)}) new_data -> {:noreply, %{state | current: {:length, length, token, new_data}}} end diff --git a/lib/rethinkdb/prepare.ex b/lib/rethinkdb/prepare.ex index ce0b0f7..9055d23 100644 --- a/lib/rethinkdb/prepare.ex +++ b/lib/rethinkdb/prepare.ex @@ -28,8 +28,8 @@ defmodule RethinkDB.Prepare do {Enum.into(map, %{}), state} end defp prepare(ref, state = {max, map}) when is_reference(ref) do - case Dict.get(map,ref) do - nil -> {max + 1, {max + 1, Dict.put_new(map, ref, max + 1)}} + case Map.get(map,ref) do + nil -> {max + 1, {max + 1, Map.put_new(map, ref, max + 1)}} x -> {x, state} end end diff --git a/lib/rethinkdb/query.ex b/lib/rethinkdb/query.ex index bdf929c..5f81899 100644 --- a/lib/rethinkdb/query.ex +++ b/lib/rethinkdb/query.ex @@ -12,12 +12,12 @@ defmodule RethinkDB.Query do @type reql_number :: integer|float|t @type reql_array :: [term]|t @type reql_bool :: boolean|t - @type reql_obj :: %{}|t + @type reql_obj :: map|t @type reql_datum :: term @type reql_func0 :: (() -> term)|t @type reql_func1 :: (term -> term)|t @type reql_func2 :: (term, term -> term)|t - @type reql_opts :: %{} + @type reql_opts :: map @type reql_binary :: %RethinkDB.Pseudotypes.Binary{}|binary|t @type reql_geo_point :: %RethinkDB.Pseudotypes.Geometry.Point{}|{reql_number,reql_number}|t @type reql_geo_line :: %RethinkDB.Pseudotypes.Geometry.Line{}|t @@ -1868,7 +1868,7 @@ defmodule RethinkDB.Query do args = case arity do 0 -> [] - _ -> Enum.map(1..arity, fn _ -> make_ref end) + _ -> Enum.map(1..arity, fn _ -> make_ref() end) end params = Enum.map(args, &var/1) diff --git a/lib/rethinkdb/response.ex b/lib/rethinkdb/response.ex index 91d05a3..73e5976 100644 --- a/lib/rethinkdb/response.ex +++ b/lib/rethinkdb/response.ex @@ -14,6 +14,7 @@ defmodule RethinkDB.Collection do def count(%{data: data}), do: Enumerable.count(data) def member?(%{data: data}, el), do: Enumerable.member?(data, el) + def slice(%{data: data}), do: Enumerable.slice(data) end end @@ -36,8 +37,9 @@ defmodule RethinkDB.Feed do end) stream.(acc, fun) end - def count(_changes), do: raise "count/1 not supported for changes" - def member?(_changes, _values), do: raise "member/2 not supported for changes" + def count(_changes), do: raise "count/1 is not supported for changes" + def member?(_changes, _values), do: raise "member/2 is not supported for changes" + def slice(_changes), do: raise "slice/1 is not supported for changes" end end diff --git a/mix.exs b/mix.exs index 9f9d676..b2501cf 100644 --- a/mix.exs +++ b/mix.exs @@ -5,9 +5,12 @@ defmodule RethinkDB.Mixfile do use Mix.Project version: "0.4.0", elixir: "~> 1.0", description: "RethinkDB driver for Elixir", - package: package, - deps: deps, - test_coverage: [tool: ExCoveralls]] + package: package(), + deps: deps(), + test_coverage: [tool: ExCoveralls], + dialyzer: [ + plt_add_apps: [:ssl] + ]] end def package do @@ -40,13 +43,13 @@ defmodule RethinkDB.Mixfile do use Mix.Project # Type `mix help deps` for more examples and options defp deps do [ - {:poison, "~> 3.0"}, - {:earmark, "~> 0.1", only: :dev}, - {:ex_doc, "~> 0.7", only: :dev}, + {:poison, "~> 3.1"}, + {:ex_doc, "~> 0.18", only: :dev}, {:flaky_connection, github: "hamiltop/flaky_connection", only: :test}, - {:connection, "~> 1.0.1"}, - {:excoveralls, "~> 0.3.11", only: :test}, - {:dialyze, "~> 0.2.0", only: :test} + {:connection, "~> 1.0"}, + {:excoveralls, "~> 0.8", only: :test}, + + {:dialyxir, "~> 0.4", only: :dev}, ] end end diff --git a/mix.lock b/mix.lock index 02cdc0b..fa68fa2 100644 --- a/mix.lock +++ b/mix.lock @@ -1,16 +1,22 @@ -%{"certifi": {:hex, :certifi, "0.3.0", "389d4b126a47895fe96d65fcf8681f4d09eca1153dc2243ed6babad0aac1e763", [:rebar3], []}, - "connection": {:hex, :connection, "1.0.1", "16bf178158088f29513a34a742d4311cd39f2c52425559d679ecb28a568c5c0b", [:mix], []}, +%{ + "certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [:rebar3], [], "hexpm"}, + "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"}, + "dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"}, "dialyze": {:hex, :dialyze, "0.2.0", "ecabf292e9f4bd0f7d844981f899a85c0300b30ff2dd1cdfef0c81a6496466f1", [:mix], []}, - "earmark": {:hex, :earmark, "0.1.19", "ffec54f520a11b711532c23d8a52b75a74c09697062d10613fa2dbdf8a9db36e", [:mix], []}, - "ex_doc": {:hex, :ex_doc, "0.10.0", "f49c237250b829df986486b38f043e6f8e19d19b41101987f7214543f75947ec", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]}, - "excoveralls": {:hex, :excoveralls, "0.3.11", "cd1abaf07db5bed9cf7891d86470247c8b3c8739d7758679071ce1920bb09dbc", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]}, - "exjsx": {:hex, :exjsx, "3.2.0", "7136cc739ace295fc74c378f33699e5145bead4fdc1b4799822d0287489136fb", [:mix], [{:jsx, "~> 2.6.2", [hex: :jsx, optional: false]}]}, + "earmark": {:hex, :earmark, "1.2.4", "99b637c62a4d65a20a9fb674b8cffb8baa771c04605a80c911c4418c69b75439", [:mix], [], "hexpm"}, + "ex_doc": {:hex, :ex_doc, "0.18.3", "f4b0e4a2ec6f333dccf761838a4b253d75e11f714b85ae271c9ae361367897b7", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}, + "excoveralls": {:hex, :excoveralls, "0.8.1", "0bbf67f22c7dbf7503981d21a5eef5db8bbc3cb86e70d3798e8c802c74fa5e27", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"}, + "exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"}, "flaky_connection": {:git, "https://github.com/hamiltop/flaky_connection.git", "e3a09e7198e1b155f35291ffad438966648a8156", []}, - "hackney": {:hex, :hackney, "1.4.8", "c8c6977ed55cc5095e3929f6d94a6f732dd2e31ae42a7b9236d5574ec3f5be13", [:rebar3], [{:certifi, "0.3.0", [hex: :certifi, optional: false]}, {:idna, "1.0.3", [hex: :idna, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_hostname, "1.0.5", [hex: :ssl_verify_hostname, optional: false]}]}, - "idna": {:hex, :idna, "1.0.3", "d456a8761cad91c97e9788c27002eb3b773adaf5c893275fc35ba4e3434bbd9b", [:rebar3], []}, + "hackney": {:hex, :hackney, "1.11.0", "4951ee019df102492dabba66a09e305f61919a8a183a7860236c0fde586134b6", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, + "idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"}, "inch_ex": {:hex, :inch_ex, "0.2.4"}, - "jsx": {:hex, :jsx, "2.6.2", "213721e058da0587a4bce3cc8a00ff6684ced229c8f9223245c6ff2c88fbaa5a", [:mix, :rebar], []}, - "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []}, + "jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm"}, + "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], [], "hexpm"}, + "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, - "ranch": {:hex, :ranch, "1.1.0", "f7ed6d97db8c2a27cca85cacbd543558001fc5a355e93a7bff1e9a9065a8545b", [:make], []}, - "ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5", "2e73e068cd6393526f9fa6d399353d7c9477d6886ba005f323b592d389fb47be", [:make], []}} + "ranch": {:hex, :ranch, "1.1.0", "f7ed6d97db8c2a27cca85cacbd543558001fc5a355e93a7bff1e9a9065a8545b", [:make], [], "hexpm"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [], [], "hexpm"}, + "ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5", "2e73e068cd6393526f9fa6d399353d7c9477d6886ba005f323b592d389fb47be", [:make], []}, + "unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [], [], "hexpm"}, +} diff --git a/test/changes_test.exs b/test/changes_test.exs index f7179d3..dad341b 100644 --- a/test/changes_test.exs +++ b/test/changes_test.exs @@ -1,16 +1,21 @@ defmodule ChangesTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false alias RethinkDB.Feed use RethinkDB.Connection import RethinkDB.Query @table_name "changes_test_table_1" + setup_all do - start_link + start_link() + + db_create("test") |> run table_create(@table_name) |> run + on_exit fn -> - start_link - table_drop(@table_name) |> run + start_link() + + db_drop("test") |> run end :ok end @@ -43,18 +48,18 @@ defmodule ChangesTest do q = table(@table_name) |> insert(data) {:ok, res} = run(q) expected = res.data["id"] - {:ok, changes} = Task.await(t) + {:ok, changes} = Task.await(t) ^expected = changes.data |> hd |> Map.get("id") # test Enumerable t = Task.async fn -> - changes |> Enum.take(5) + changes |> Enum.take(5) end 1..6 |> Enum.each(fn _ -> q = table(@table_name) |> insert(data) run(q) end) - data = Task.await(t) + data = Task.await(t) 5 = Enum.count(data) end @@ -67,7 +72,10 @@ defmodule ChangesTest do data = %{"id" => "0"} q = table(@table_name) |> insert(data) {:ok, res} = run(q) - expected = res.data["id"] + + # Unused? + _expected = res.data["id"] + [h|[]] = Task.await(t) assert %{"new_val" => %{"id" => "0"}} = h end diff --git a/test/connection_test.exs b/test/connection_test.exs index e95c2c8..64da1a1 100644 --- a/test/connection_test.exs +++ b/test/connection_test.exs @@ -1,5 +1,5 @@ defmodule ConnectionTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false import Supervisor.Spec use RethinkDB.Connection import RethinkDB.Query @@ -29,9 +29,15 @@ defmodule ConnectionTest do test "reconnects if initial connect fails" do {:ok, c} = start_link(port: 28014) Process.unlink(c) - %RethinkDB.Exception.ConnectionClosed{} = table_list |> run + %RethinkDB.Exception.ConnectionClosed{} = table_list() |> run conn = FlakyConnection.start('localhost', 28015, [local_port: 28014]) :timer.sleep(1000) + + RethinkDB.Query.db_create("test") |> run + on_exit fn -> + RethinkDB.Query.db_drop("test") |> run + end + {:ok, %RethinkDB.Record{}} = RethinkDB.Query.table_list |> run ref = Process.monitor(c) FlakyConnection.stop(conn) @@ -45,14 +51,19 @@ defmodule ConnectionTest do {:ok, c} = start_link(port: conn.port) Process.unlink(c) table = "foo_flaky_test" - RethinkDB.Query.table_create(table)|> run + + RethinkDB.Query.db_create("test") |> run + RethinkDB.Query.table_create(table) |> run + on_exit fn -> - start_link + start_link() :timer.sleep(100) - RethinkDB.Query.table_drop(table) |> run + RethinkDB.Query.db_drop("test") |> run GenServer.cast(__MODULE__, :stop) end + table(table) |> index_wait |> run + {:ok, change_feed} = table(table) |> changes |> run task = Task.async fn -> RethinkDB.Connection.next change_feed @@ -84,15 +95,16 @@ defmodule ConnectionTest do {:ok, c} = RethinkDB.Connection.start_link(db: "new_test") db_create("new_test") |> RethinkDB.run(c) db("new_test") |> table_create("new_test_table") |> RethinkDB.run(c) - {:ok, %{data: data}} = table_list |> RethinkDB.run(c) + {:ok, %{data: data}} = table_list() |> RethinkDB.run(c) assert data == ["new_test_table"] + db_drop("new_test") |> RethinkDB.run(c) end test "connection accepts max_pending" do {:ok, c} = RethinkDB.Connection.start_link(max_pending: 1) res = Enum.map(1..100, fn (_) -> Task.async fn -> - now |> RethinkDB.run(c) + now() |> RethinkDB.run(c) end end) |> Enum.map(&Task.await/1) assert Enum.any?(res, &(&1 == %RethinkDB.Exception.TooManyRequests{})) @@ -109,17 +121,18 @@ defmodule ConnectionTest do test "ssl connection" do conn = FlakyConnection.start('localhost', 28015, [ssl: [keyfile: "./test/cert/host.key", certfile: "./test/cert/host.crt"]]) {:ok, c} = RethinkDB.Connection.start_link(port: conn.port, ssl: [ca_certs: ["./test/cert/rootCA.pem"]], sync_connect: true) - {:ok, %{data: _}} = table_list |> RethinkDB.run(c) + {:ok, %{data: _}} = table_list() |> RethinkDB.run(c) end end defmodule ConnectionRunTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query setup_all do - start_link + start_link() + :ok end @@ -127,7 +140,7 @@ defmodule ConnectionRunTest do db_create("db_option_test") |> run table_create("db_option_test_table") |> run(db: "db_option_test") - {:ok, %{data: data}} = db("db_option_test") |> table_list |> run + {:ok, %{data: data}} = db("db_option_test") |> table_list() |> run db_drop("db_option_test") |> run @@ -135,6 +148,11 @@ defmodule ConnectionRunTest do end test "run(conn, opts) with :durability option" do + db_create("test") |> run + on_exit fn -> + db_drop("test") |> run + end + table_drop("durability_test_table") |> run {:ok, response} = table_create("durability_test_table") |> run(durability: "soft") durability = response.data["config_changes"] @@ -149,7 +167,7 @@ defmodule ConnectionRunTest do test "run with :noreply option" do :ok = make_array([1,2,3]) |> run(noreply: true) - noreply_wait + noreply_wait() end test "run with :profile options" do diff --git a/test/prepare_test.exs b/test/prepare_test.exs index f5c94c2..9541f91 100644 --- a/test/prepare_test.exs +++ b/test/prepare_test.exs @@ -4,25 +4,25 @@ defmodule PrepareTest do test "single elements" do assert prepare(1) == 1 - assert prepare(make_ref) == 1 + assert prepare(make_ref()) == 1 end test "list" do assert prepare([1,2,3]) == [1,2,3] - assert prepare([1,2,make_ref,make_ref]) == [1,2,1,2] + assert prepare([1,2,make_ref(),make_ref()]) == [1,2,1,2] end test "nested list" do list = [1, [1,2], [1, [1, 2]]] assert prepare(list) == list - list = [1, [make_ref, make_ref], make_ref, [1, 2]] + list = [1, [make_ref(), make_ref()], make_ref(), [1, 2]] assert prepare(list) == [1, [1, 2], 3, [1, 2]] end test "map" do map = %{a: 1, b: 2} assert prepare(map) == map - map = %{a: 1, b: make_ref, c: make_ref} + map = %{a: 1, b: make_ref(), c: make_ref()} assert prepare(map) == %{a: 1, b: 1, c: 2} end end diff --git a/test/query/administration_query_test.exs b/test/query/administration_query_test.exs index b3fadef..4a81c01 100644 --- a/test/query/administration_query_test.exs +++ b/test/query/administration_query_test.exs @@ -1,22 +1,33 @@ defmodule AdministrationQueryTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query + @table_name "administration_table_1" + setup_all do - start_link + start_link() + + db_create("test") |> run + table_create(@table_name) |> run + + on_exit fn -> + start_link() + + db_drop("test") |> run + end + :ok end - @table_name "administration_table_1" setup do - table_create(@table_name) |> run on_exit fn -> - table_drop(@table_name) |> run + table(@table_name) |> delete |> run end + :ok end - + test "config" do {:ok, r} = table(@table_name) |> config |> run assert %RethinkDB.Record{data: %{"db" => "test"}} = r diff --git a/test/query/aggregation_test.exs b/test/query/aggregation_test.exs index bd5d716..6deae78 100644 --- a/test/query/aggregation_test.exs +++ b/test/query/aggregation_test.exs @@ -10,7 +10,8 @@ defmodule AggregationTest do import RethinkDB.Lambda setup_all do - start_link + start_link() + :ok end @@ -224,7 +225,7 @@ defmodule AggregationTest do end end) {:ok, %Record{data: data}} = run query - assert data == 2 + assert data == 2 end test "max" do diff --git a/test/query/control_structures_adv_test.exs b/test/query/control_structures_adv_test.exs index d10cdb5..404f95b 100644 --- a/test/query/control_structures_adv_test.exs +++ b/test/query/control_structures_adv_test.exs @@ -1,5 +1,5 @@ defmodule ControlStructuresAdvTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query @@ -7,12 +7,15 @@ defmodule ControlStructuresAdvTest do @table_name "control_test_table_1" setup_all do - start_link - q = table_create(@table_name) - run(q) + start_link() + + db_create("test") |> run + table_create(@table_name) |> run + on_exit fn -> - start_link - table_drop(@table_name) |> run + start_link() + + db_drop("test") |> run end :ok end diff --git a/test/query/control_structures_test.exs b/test/query/control_structures_test.exs index dbd5bfb..9df6f21 100644 --- a/test/query/control_structures_test.exs +++ b/test/query/control_structures_test.exs @@ -1,5 +1,5 @@ defmodule ControlStructuresTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query @@ -7,7 +7,16 @@ defmodule ControlStructuresTest do alias RethinkDB.Response setup_all do - start_link + start_link() + + db_create("test") |> run + + on_exit fn -> + start_link() + + db_drop("test") |> run + end + :ok end @@ -80,12 +89,6 @@ defmodule ControlStructuresTest do assert data == "test" end - test "js" do - q = js "[40,100,1,5,25,10].sort()" - {:ok, %Record{data: data}} = run q - assert data == [1,10,100,25,40,5] # couldn't help myself... - end - test "coerce_to" do q = "91" |> coerce_to("number") {:ok, %Record{data: data}} = run q @@ -116,16 +119,8 @@ defmodule ControlStructuresTest do assert data == %{"a" => 5, "b" => 6} end - test "http" do - q = "http://httpbin.org/get" |> http - {:ok, %Record{data: data}} = run q - %{"args" => %{}, - "headers" => _, - "origin" => _, "url" => "http://httpbin.org/get"} = data - end - test "uuid" do - q = uuid + q = uuid() {:ok, %Record{data: data}} = run q assert String.length(String.replace(data, "-", "")) == 32 end diff --git a/test/query/database_test.exs b/test/query/database_test.exs index a2d0dcc..509cf45 100644 --- a/test/query/database_test.exs +++ b/test/query/database_test.exs @@ -1,12 +1,13 @@ defmodule DatabaseTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query alias RethinkDB.Record setup_all do - start_link + start_link() + :ok end @@ -29,14 +30,14 @@ defmodule DatabaseTest do q = db_create(@db_name) {:ok, %Record{data: %{"dbs_created" => 1}}} = run(q) - q = db_list + q = db_list() {:ok, %Record{data: dbs}} = run(q) assert Enum.member?(dbs, @db_name) q = db_drop(@db_name) {:ok, %Record{data: %{"dbs_dropped" => 1}}} = run(q) - q = db_list + q = db_list() {:ok, %Record{data: dbs}} = run(q) assert !Enum.member?(dbs, @db_name) end diff --git a/test/query/date_time_test.exs b/test/query/date_time_test.exs index 8f80ca5..f357643 100644 --- a/test/query/date_time_test.exs +++ b/test/query/date_time_test.exs @@ -7,7 +7,8 @@ defmodule DateTimeTest do alias RethinkDB.Pseudotypes.Time setup_all do - start_link + start_link() + :ok end diff --git a/test/query/document_manipulation_test.exs b/test/query/document_manipulation_test.exs index 4572e2f..1673324 100644 --- a/test/query/document_manipulation_test.exs +++ b/test/query/document_manipulation_test.exs @@ -6,7 +6,8 @@ defmodule DocumentManipulationTest do alias RethinkDB.Record setup_all do - start_link + start_link() + :ok end @@ -113,7 +114,7 @@ defmodule DocumentManipulationTest do {:ok, %Record{data: data}} = [1,2,3,4] |> change_at(1,7) |> run assert data == [1,7,3,4] end - + test "keys" do {:ok, %Record{data: data}} = %{a: 5, b: 6} |> keys |> run assert data == ["a", "b"] diff --git a/test/query/geospatial_adv_test.exs b/test/query/geospatial_adv_test.exs index bc09c62..dda1ffc 100644 --- a/test/query/geospatial_adv_test.exs +++ b/test/query/geospatial_adv_test.exs @@ -1,19 +1,25 @@ defmodule GeospatialAdvTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query alias RethinkDB.Record @table_name "geo_test_table_1" + setup_all do - start_link + start_link() + + db_create("test") |> run + table_create(@table_name) |> run table(@table_name) |> index_create("location", geo: true) |> run table(@table_name) |> index_wait("location") |> run + on_exit fn -> - start_link - table_drop(@table_name) |> run + start_link() + + db_drop("test") |> run end :ok end diff --git a/test/query/geospatial_test.exs b/test/query/geospatial_test.exs index d5bc5c3..8c9c326 100644 --- a/test/query/geospatial_test.exs +++ b/test/query/geospatial_test.exs @@ -1,5 +1,5 @@ defmodule GeospatialTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query @@ -9,7 +9,10 @@ defmodule GeospatialTest do alias RethinkDB.Pseudotypes.Geometry.Polygon setup_all do - start_link + start_link() + + db_create("test") |> run + :ok end @@ -24,7 +27,8 @@ defmodule GeospatialTest do end test "distance" do {:ok, %Record{data: data}} = distance(point({1,1}), point({2,2})) |> run - assert data == 156876.14940188665 + + assert_in_delta data, 156876.14940188665, 0.0000000001 end test "fill" do diff --git a/test/query/joins_test.exs b/test/query/joins_test.exs index 6b28e1a..e01df60 100644 --- a/test/query/joins_test.exs +++ b/test/query/joins_test.exs @@ -1,5 +1,5 @@ defmodule JoinsTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query @@ -11,17 +11,25 @@ defmodule JoinsTest do @table_name "joins_test_table_1" setup_all do - start_link + start_link() + + db_create("test") |> run table_create(@table_name) |> run + on_exit fn -> - start_link - table_drop(@table_name) |> run + start_link() + + db_drop("test") |> run end + :ok end setup do - table(@table_name) |> delete |> run + on_exit fn -> + table(@table_name) |> delete |> run + end + :ok end diff --git a/test/query/math_logic_test.exs b/test/query/math_logic_test.exs index 505a03a..e08506f 100644 --- a/test/query/math_logic_test.exs +++ b/test/query/math_logic_test.exs @@ -4,9 +4,10 @@ defmodule MathLogicTest do import RethinkDB.Query alias RethinkDB.Record - + setup_all do - start_link + start_link() + :ok end @@ -76,7 +77,7 @@ defmodule MathLogicTest do test "find remainder when dividing two numbers" do {:ok, %Record{data: data}} = mod(23, 4) |> run - assert data == 3 + assert data == 3 end test "logical and of two values" do @@ -86,7 +87,7 @@ defmodule MathLogicTest do test "logical and of list" do {:ok, %Record{data: data}} = and_r([true, true, false]) |> run - assert data == false + assert data == false end test "logical or of two values" do @@ -96,89 +97,89 @@ defmodule MathLogicTest do test "logical or of list" do {:ok, %Record{data: data}} = or_r([false, false, false]) |> run - assert data == false + assert data == false end test "two numbers are equal" do {:ok, %Record{data: data}} = eq(1, 1) |> run - assert data == true + assert data == true {:ok, %Record{data: data}} = eq(2, 1) |> run - assert data == false + assert data == false end - + test "values in a list are equal" do {:ok, %Record{data: data}} = eq([1, 1, 1]) |> run - assert data == true + assert data == true {:ok, %Record{data: data}} = eq([1, 2, 1]) |> run - assert data == false + assert data == false end - + test "two numbers are not equal" do {:ok, %Record{data: data}} = ne(1, 1) |> run - assert data == false + assert data == false {:ok, %Record{data: data}} = ne(2, 1) |> run - assert data == true + assert data == true end - + test "values in a list are not equal" do {:ok, %Record{data: data}} = ne([1, 1, 1]) |> run - assert data == false + assert data == false {:ok, %Record{data: data}} = ne([1, 2, 1]) |> run assert data == true end - + test "a number is less than the other" do {:ok, %Record{data: data}} = lt(2, 1) |> run - assert data == false + assert data == false {:ok, %Record{data: data}} = lt(1, 2) |> run - assert data == true + assert data == true end - + test "values in a list less than the next" do {:ok, %Record{data: data}} = lt([1, 4, 2]) |> run - assert data == false + assert data == false {:ok, %Record{data: data}} = lt([1, 4, 5]) |> run assert data == true end - + test "a number is less than or equal to the other" do {:ok, %Record{data: data}} = le(1, 1) |> run - assert data == true + assert data == true {:ok, %Record{data: data}} = le(1, 2) |> run - assert data == true + assert data == true end - + test "values in a list less than or equal to the next" do {:ok, %Record{data: data}} = le([1, 4, 2]) |> run - assert data == false + assert data == false {:ok, %Record{data: data}} = le([1, 4, 4]) |> run assert data == true end - + test "a number is greater than the other" do {:ok, %Record{data: data}} = gt(1, 1) |> run - assert data == false + assert data == false {:ok, %Record{data: data}} = gt(2, 1) |> run - assert data == true + assert data == true end - + test "values in a list greater than the next" do {:ok, %Record{data: data}} = gt([1, 4, 2]) |> run - assert data == false + assert data == false {:ok, %Record{data: data}} = gt([10, 4, 2]) |> run assert data == true end - + test "a number is greater than or equal to the other" do {:ok, %Record{data: data}} = ge(1, 1) |> run - assert data == true + assert data == true {:ok, %Record{data: data}} = ge(2, 1) |> run - assert data == true + assert data == true end - + test "values in a list greater than or equal to the next" do {:ok, %Record{data: data}} = ge([1, 4, 2]) |> run - assert data == false + assert data == false {:ok, %Record{data: data}} = ge([10, 4, 4]) |> run assert data == true end @@ -189,7 +190,7 @@ defmodule MathLogicTest do end test "random operator" do - {:ok, %Record{data: data}} = random |> run + {:ok, %Record{data: data}} = random() |> run assert data >= 0.0 && data <= 1.0 {:ok, %Record{data: data}} = random(100) |> run assert is_integer(data) && data >= 0 && data <= 100 @@ -217,7 +218,7 @@ defmodule MathLogicTest do test "floor" do {:ok, %Record{data: data}} = floor(0.3) |> run - assert data == 0 + assert data == 0 {:ok, %Record{data: data}} = floor(0.6) |> run assert data == 0 end diff --git a/test/query/selection_test.exs b/test/query/selection_test.exs index 922b0eb..f9f1a4b 100644 --- a/test/query/selection_test.exs +++ b/test/query/selection_test.exs @@ -1,5 +1,5 @@ defmodule SelectionTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query @@ -9,18 +9,28 @@ defmodule SelectionTest do import RethinkDB.Lambda @table_name "selection_test_table_1" + setup_all do - start_link + start_link() + + db_create("test") |> run table_create(@table_name) |> run + on_exit fn -> - start_link - table_drop(@table_name) |> run + start_link() + + db_drop("test") |> run end + :ok end setup do - table(@table_name) |> delete |> run + + on_exit fn -> + table(@table_name) |> delete |> run + end + :ok end @@ -63,7 +73,7 @@ defmodule SelectionTest do table(@table_name) |> insert(%{id: "c", a: 5}) |> run {:ok, %RethinkDB.Collection{data: data}} = table(@table_name) |> between("b", "d") |> run assert Enum.count(data) == 2 - {:ok, %RethinkDB.Collection{data: data}} = table(@table_name) |> between(minval, maxval) |> run + {:ok, %RethinkDB.Collection{data: data}} = table(@table_name) |> between(minval(), maxval()) |> run assert Enum.count(data) == 3 end diff --git a/test/query/table_db_test.exs b/test/query/table_db_test.exs index 4558a9e..dd0dd58 100644 --- a/test/query/table_db_test.exs +++ b/test/query/table_db_test.exs @@ -6,10 +6,11 @@ defmodule TableDBTest do alias RethinkDB.Record setup_all do - start_link + start_link() + :ok end - + @db_name "table_db_test_db_1" @table_name "table_db_test_table_1" diff --git a/test/query/table_index_test.exs b/test/query/table_index_test.exs index 0e5c790..a8436cc 100644 --- a/test/query/table_index_test.exs +++ b/test/query/table_index_test.exs @@ -1,14 +1,23 @@ defmodule TableIndexTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query alias RethinkDB.Record setup_all do - start_link + start_link() + + db_create("test") |> run + + on_exit fn -> + start_link() + + db_drop("test") |> run + end + :ok end - + @table_name "table_index_test_table_1" setup do table_create(@table_name) |> run diff --git a/test/query/table_test.exs b/test/query/table_test.exs index 25aec55..cb5ea56 100644 --- a/test/query/table_test.exs +++ b/test/query/table_test.exs @@ -1,14 +1,23 @@ defmodule TableTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query alias RethinkDB.Record setup_all do - start_link + start_link() + + db_create("test") |> run + + on_exit fn -> + start_link() + + db_drop("test") |> run + end + :ok end - + @table_name "table_test_table_1" test "tables" do @@ -19,14 +28,14 @@ defmodule TableTest do q = table_create(@table_name) {:ok, %Record{data: %{"tables_created" => 1}}} = run q - q = table_list + q = table_list() {:ok, %Record{data: tables}} = run q assert Enum.member?(tables, @table_name) q = table_drop(@table_name) {:ok, %Record{data: %{"tables_dropped" => 1}}} = run q - q = table_list + q = table_list() {:ok, %Record{data: tables}} = run q assert !Enum.member?(tables, @table_name) diff --git a/test/query/transformation_test.exs b/test/query/transformation_test.exs index 3d6d38d..77d9648 100644 --- a/test/query/transformation_test.exs +++ b/test/query/transformation_test.exs @@ -10,7 +10,8 @@ defmodule TransformationTest do import RethinkDB.Lambda setup_all do - start_link + start_link() + :ok end diff --git a/test/query/writing_data_test.exs b/test/query/writing_data_test.exs index 1a37d3b..0b8277c 100644 --- a/test/query/writing_data_test.exs +++ b/test/query/writing_data_test.exs @@ -1,5 +1,5 @@ defmodule WritingDataTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false use RethinkDB.Connection import RethinkDB.Query @@ -8,12 +8,17 @@ defmodule WritingDataTest do @table_name "writing_data_test_table_1" setup_all do - start_link + start_link() + + db_create("test") |> run table_create(@table_name) |> run + on_exit fn -> - start_link - table_drop(@table_name) |> run + start_link() + + db_drop("test") |> run end + :ok end @@ -43,9 +48,9 @@ defmodule WritingDataTest do test "insert conflict options" do table_query = table(@table_name) - q = insert(table_query, [%{name: "Hello", value: 1}]) + q = insert(table_query, [%{name: "Hello", value: 1}]) {:ok, %Record{data: %{"generated_keys"=> [id], "inserted" => 1}}} = run(q) - + q = insert(table_query, [%{name: "Hello", id: id, value: 2}]) {:ok, %Record{data: %{"errors" => 1}}} = run(q) @@ -57,11 +62,11 @@ defmodule WritingDataTest do {:ok, %Record{data: %{"replaced" => 1}}} = run(q) {:ok, %Collection{data: [%{"id" => ^id, "name" => "World", "value" => 3}]}} = run(table_query) - q = insert(table_query, [%{id: id, value: 3}], %{conflict: fn(_id, old, new) -> + q = insert(table_query, [%{id: id, value: 3}], %{conflict: fn(_id, old, new) -> merge(old, %{value: add(get_field(old, "value"), get_field(new, "value"))}) end}) {:ok, %Record{data: %{"replaced" => 1}}} = run(q) {:ok, %Collection{data: [%{"id" => ^id, "name" => "World", "value" => 6}]}} = run(table_query) - end + end test "update" do table_query = table(@table_name) diff --git a/test/query_test.exs b/test/query_test.exs index 42f4389..c187ac9 100644 --- a/test/query_test.exs +++ b/test/query_test.exs @@ -1,5 +1,5 @@ defmodule QueryTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false alias RethinkDB.Query alias RethinkDB.Record alias RethinkDB.Collection @@ -8,18 +8,28 @@ defmodule QueryTest do require RethinkDB.Lambda @table_name "query_test_table_1" + setup_all do - start_link + start_link() + + db_create("test") |> run table_create(@table_name) |> run + on_exit fn -> - start_link - table_drop(@table_name) |> run + start_link() + + db_drop("test") |> run end + :ok end setup do - table(@table_name) |> delete |> run + + on_exit fn -> + table(@table_name) |> delete |> run + end + :ok end