From b4ff59869965d8de2640e344fe93d395c6a42df7 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Tue, 24 Mar 2020 11:44:16 +0300 Subject: [PATCH 01/15] FT SCP-998: Fixed, ignored dialyzer issues before function specification updates --- .dialyzer-ignore.exs | 3 +++ lib/mongo/session.ex | 1 + mix.exs | 1 + 3 files changed, 5 insertions(+) create mode 100644 .dialyzer-ignore.exs diff --git a/.dialyzer-ignore.exs b/.dialyzer-ignore.exs new file mode 100644 index 00000000..4a7f31cd --- /dev/null +++ b/.dialyzer-ignore.exs @@ -0,0 +1,3 @@ +[ + {":0:unknown_type Unknown type: Mongo.WriteError.t/0." } +] \ No newline at end of file diff --git a/lib/mongo/session.ex b/lib/mongo/session.ex index 178bdcdc..a733373e 100644 --- a/lib/mongo/session.ex +++ b/lib/mongo/session.ex @@ -339,6 +339,7 @@ defmodule Mongo.Session do @impl :gen_statem # Abort all pending transactions if there any and end session itself. def terminate(_reason, state, %{pid: pid} = data) do + _= if state == :in_transaction do _ = try_run_txn_command(data, :abortTransaction) end diff --git a/mix.exs b/mix.exs index 718d7dd2..ed2374f8 100644 --- a/mix.exs +++ b/mix.exs @@ -15,6 +15,7 @@ defmodule Mongodb.Mixfile do description: description(), package: package(), dialyzer: [ + ignore_warnings: ".dialyzer-ignore.exs", flags: [:underspecs, :unknown, :unmatched_returns], plt_add_apps: [:logger, :connection, :db_connection, :mix, :elixir, :ssl, :public_key], plt_add_deps: :transitive From 7f839d6a5cfae2886fe0996259e10570ac4c7d8c Mon Sep 17 00:00:00 2001 From: Hakan B Date: Tue, 24 Mar 2020 12:32:07 +0300 Subject: [PATCH 02/15] FT SCP-998: Added missing error returns as generic structure {:error Mongo.Error} --- lib/mongo.ex | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index a7151bc0..b368c063 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -167,7 +167,7 @@ defmodule Mongo do * `:max_time` - Specifies a time limit in milliseconds * `:use_cursor` - Use a cursor for a batched response (Default: true) """ - @spec aggregate(GenServer.server(), collection, [BSON.document()], Keyword.t()) :: cursor + @spec aggregate(GenServer.server(), collection, [BSON.document()], Keyword.t()) :: cursor | {:error, Mongo.Error.t()} def aggregate(topology_pid, coll, pipeline, opts \\ []) do query = [ @@ -482,7 +482,7 @@ defmodule Mongo do * `:projection` - Limits the fields to return for all matching document * `:skip` - The number of documents to skip before returning (Default: 0) """ - @spec find(GenServer.server(), collection, BSON.document(), Keyword.t()) :: cursor + @spec find(GenServer.server(), collection, BSON.document(), Keyword.t()) :: cursor | {:error, Mongo.Error.t()} def find(topology_pid, coll, filter, opts \\ []) do query = [ @@ -1001,7 +1001,7 @@ defmodule Mongo do @doc """ Returns a cursor to enumerate all indexes """ - @spec list_indexes(GenServer.server(), String.t(), Keyword.t()) :: cursor + @spec list_indexes(GenServer.server(), String.t(), Keyword.t()) :: cursor | {:error, Mongo.Error.t()} def list_indexes(topology_pid, coll, opts \\ []) do with {:ok, conn, _, _} <- select_server(topology_pid, :read, opts) do aggregation_cursor(conn, "$cmd", [listIndexes: coll], nil, opts) @@ -1011,7 +1011,7 @@ defmodule Mongo do @doc """ Convenient function that returns a cursor with the names of the indexes. """ - @spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: %Stream{} + @spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: %Stream{} | {:error, Mongo.Error.t()} def list_index_names(topology_pid, coll, opts \\ []) do list_indexes(topology_pid, coll, opts) |> Stream.map(fn %{"name" => name} -> name end) @@ -1020,7 +1020,7 @@ defmodule Mongo do @doc """ Getting Collection Names """ - @spec show_collections(GenServer.server(), Keyword.t()) :: cursor + @spec show_collections(GenServer.server(), Keyword.t()) :: cursor | {:error, Mongo.Error.t()} def show_collections(topology_pid, opts \\ []) do ## # from the specs From 07c970cc4635f6242edc114834625de85d442147 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Mon, 6 Jul 2020 15:11:20 +0300 Subject: [PATCH 03/15] Added error return pattern for find_one --- lib/mongo.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index b368c063..70584ac6 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -538,7 +538,7 @@ defmodule Mongo do * `:skip` - The number of documents to skip before returning (Default: 0) """ @spec find_one(GenServer.server(), collection, BSON.document(), Keyword.t()) :: - BSON.document() | nil + BSON.document() | nil | {:error, Mongo.Error.t()} def find_one(conn, coll, filter, opts \\ []) do opts = opts From cbda7fcce0684683f8957a0ca738c889d9900104 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Tue, 7 Jul 2020 17:27:24 +0300 Subject: [PATCH 04/15] Formatting changes --- .dialyzer-ignore.exs | 2 +- lib/mongo.ex | 23 ++++++++++++++--------- lib/mongo/session.ex | 8 ++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/.dialyzer-ignore.exs b/.dialyzer-ignore.exs index 4a7f31cd..3c1537bf 100644 --- a/.dialyzer-ignore.exs +++ b/.dialyzer-ignore.exs @@ -1,3 +1,3 @@ [ {":0:unknown_type Unknown type: Mongo.WriteError.t/0." } -] \ No newline at end of file +] diff --git a/lib/mongo.ex b/lib/mongo.ex index 70584ac6..3afc3303 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -167,7 +167,8 @@ defmodule Mongo do * `:max_time` - Specifies a time limit in milliseconds * `:use_cursor` - Use a cursor for a batched response (Default: true) """ - @spec aggregate(GenServer.server(), collection, [BSON.document()], Keyword.t()) :: cursor | {:error, Mongo.Error.t()} + @spec aggregate(GenServer.server(), collection, [BSON.document()], Keyword.t()) :: + cursor | {:error, Mongo.Error.t()} def aggregate(topology_pid, coll, pipeline, opts \\ []) do query = [ @@ -482,7 +483,8 @@ defmodule Mongo do * `:projection` - Limits the fields to return for all matching document * `:skip` - The number of documents to skip before returning (Default: 0) """ - @spec find(GenServer.server(), collection, BSON.document(), Keyword.t()) :: cursor | {:error, Mongo.Error.t()} + @spec find(GenServer.server(), collection, BSON.document(), Keyword.t()) :: + cursor | {:error, Mongo.Error.t()} def find(topology_pid, coll, filter, opts \\ []) do query = [ @@ -597,10 +599,10 @@ defmodule Mongo do rp = ReadPreference.defaults(%{mode: :primary}) rp_opts = [read_preference: Keyword.get(opts, :read_preference, rp)] - with {:ok, conn, slave_ok, _} <- select_server(topology_pid, :read, rp_opts) do - opts = Keyword.put(opts, :slave_ok, slave_ok) - direct_command(conn, query, opts) - end + with {:ok, conn, slave_ok, _} <- select_server(topology_pid, :read, rp_opts) do + opts = Keyword.put(opts, :slave_ok, slave_ok) + direct_command(conn, query, opts) + end end @doc false @@ -1001,7 +1003,8 @@ defmodule Mongo do @doc """ Returns a cursor to enumerate all indexes """ - @spec list_indexes(GenServer.server(), String.t(), Keyword.t()) :: cursor | {:error, Mongo.Error.t()} + @spec list_indexes(GenServer.server(), String.t(), Keyword.t()) :: + cursor | {:error, Mongo.Error.t()} def list_indexes(topology_pid, coll, opts \\ []) do with {:ok, conn, _, _} <- select_server(topology_pid, :read, opts) do aggregation_cursor(conn, "$cmd", [listIndexes: coll], nil, opts) @@ -1011,7 +1014,8 @@ defmodule Mongo do @doc """ Convenient function that returns a cursor with the names of the indexes. """ - @spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: %Stream{} | {:error, Mongo.Error.t()} + @spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: + %Stream{} | {:error, Mongo.Error.t()} def list_index_names(topology_pid, coll, opts \\ []) do list_indexes(topology_pid, coll, opts) |> Stream.map(fn %{"name" => name} -> name end) @@ -1095,7 +1099,8 @@ defmodule Mongo do select_servers(topology_pid, type, opts, start_time) {:error, :selection_timeout} -> - {:error, %Mongo.Error{type: :network, message: "Topology selection timeout", code: 89}} + {:error, + %Mongo.Error{type: :network, message: "Topology selection timeout", code: 89}} end else {:ok, servers, slave_ok, mongos?} diff --git a/lib/mongo/session.ex b/lib/mongo/session.ex index a733373e..94ed72ca 100644 --- a/lib/mongo/session.ex +++ b/lib/mongo/session.ex @@ -339,10 +339,10 @@ defmodule Mongo.Session do @impl :gen_statem # Abort all pending transactions if there any and end session itself. def terminate(_reason, state, %{pid: pid} = data) do - _= - if state == :in_transaction do - _ = try_run_txn_command(data, :abortTransaction) - end + _ = + if state == :in_transaction do + _ = try_run_txn_command(data, :abortTransaction) + end query = %{ endSessions: [data.id] From 1c98096d5ba6d449cd8beef986383f74fb7a8136 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Wed, 8 Jul 2020 10:20:47 +0300 Subject: [PATCH 05/15] Removed unnecesary assignment within if block --- lib/mongo/session.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo/session.ex b/lib/mongo/session.ex index 94ed72ca..6c83055e 100644 --- a/lib/mongo/session.ex +++ b/lib/mongo/session.ex @@ -341,7 +341,7 @@ defmodule Mongo.Session do def terminate(_reason, state, %{pid: pid} = data) do _ = if state == :in_transaction do - _ = try_run_txn_command(data, :abortTransaction) + try_run_txn_command(data, :abortTransaction) end query = %{ From d8128c5c16099499e7527d9cbb9468db58231f8f Mon Sep 17 00:00:00 2001 From: Hakan B Date: Thu, 9 Jul 2020 10:10:45 +0300 Subject: [PATCH 06/15] return pattern of aggregate function has been changed as a reponse to PR comment --- lib/mongo.ex | 23 +++++++++++------- test/mongo_test.exs | 59 ++++++++++++++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index 3afc3303..ba287216 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -168,7 +168,7 @@ defmodule Mongo do * `:use_cursor` - Use a cursor for a batched response (Default: true) """ @spec aggregate(GenServer.server(), collection, [BSON.document()], Keyword.t()) :: - cursor | {:error, Mongo.Error.t()} + {:ok, cursor} | {:error, Mongo.Error.t()} def aggregate(topology_pid, coll, pipeline, opts \\ []) do query = [ @@ -191,10 +191,10 @@ defmodule Mongo do if cursor? do query = query ++ [cursor: filter_nils(%{batchSize: opts[:batch_size]})] - aggregation_cursor(conn, "$cmd", query, nil, opts) + {:ok, aggregation_cursor(conn, "$cmd", query, nil, opts)} else query = query ++ [cursor: %{}] - aggregation_cursor(conn, "$cmd", query, nil, opts) + {:ok, aggregation_cursor(conn, "$cmd", query, nil, opts)} end end end @@ -1004,10 +1004,10 @@ defmodule Mongo do Returns a cursor to enumerate all indexes """ @spec list_indexes(GenServer.server(), String.t(), Keyword.t()) :: - cursor | {:error, Mongo.Error.t()} + {:ok, cursor} | {:error, Mongo.Error.t()} def list_indexes(topology_pid, coll, opts \\ []) do with {:ok, conn, _, _} <- select_server(topology_pid, :read, opts) do - aggregation_cursor(conn, "$cmd", [listIndexes: coll], nil, opts) + {:ok, aggregation_cursor(conn, "$cmd", [listIndexes: coll], nil, opts)} end end @@ -1024,7 +1024,8 @@ defmodule Mongo do @doc """ Getting Collection Names """ - @spec show_collections(GenServer.server(), Keyword.t()) :: cursor | {:error, Mongo.Error.t()} + @spec show_collections(GenServer.server(), Keyword.t()) :: + {:ok, cursor} | {:error, Mongo.Error.t()} def show_collections(topology_pid, opts \\ []) do ## # from the specs @@ -1032,10 +1033,14 @@ defmodule Mongo do # # In versions 2.8.0-rc3 and later, the listCollections command returns a cursor! # + with {:ok, conn, _, _} <- select_server(topology_pid, :read, opts) do - aggregation_cursor(conn, "$cmd", [listCollections: 1], nil, opts) - |> Stream.filter(fn coll -> coll["type"] == "collection" end) - |> Stream.map(fn coll -> coll["name"] end) + cursor = + aggregation_cursor(conn, "$cmd", [listCollections: 1], nil, opts) + |> Stream.filter(fn coll -> coll["type"] == "collection" end) + |> Stream.map(fn coll -> coll["name"] end) + + {:ok, cursor} end end diff --git a/test/mongo_test.exs b/test/mongo_test.exs index 8c5cbe6a..3c0ab7f7 100644 --- a/test/mongo_test.exs +++ b/test/mongo_test.exs @@ -44,9 +44,10 @@ defmodule Mongo.Test do cmd = [createIndexes: coll_2, indexes: [[key: [foo: 1, bar: 1], name: "not-a-collection"]]] assert {:ok, _} = Mongo.command(c.pid, cmd) + assert {:ok, colls0} = c.pid |> Mongo.show_collections() + colls = - c.pid - |> Mongo.show_collections() + colls0 |> Enum.to_list() assert Enum.member?(colls, coll_1) @@ -68,9 +69,12 @@ defmodule Mongo.Test do cmd = [createIndexes: coll_1, indexes: [[key: [foo: 1, bar: 1], name: "foo-bar"]]] assert {:ok, _} = Mongo.command(c.pid, cmd) - indexes = + {:ok, indexes_colls} = c.pid |> Mongo.list_index_names(coll_1) + + indexes = + indexes_colls |> Enum.to_list() assert Enum.count(indexes) == 3 @@ -104,27 +108,44 @@ defmodule Mongo.Test do } ] - assert [%{"_id" => "foo", "total" => 89}] = - c.pid |> Mongo.aggregate(coll, query) |> Enum.to_list() + assert {:ok, colls1} = c.pid |> Mongo.aggregate(coll, query) + assert [%{"_id" => "foo", "total" => 89}] = colls1 |> Enum.to_list() + + assert {:ok, colls2} = c.pid |> Mongo.aggregate(coll, []) + assert [] = colls2 |> Enum.take(0) + + assert {:ok, colls3} = c.pid |> Mongo.aggregate(coll, []) + assert [] = colls3 |> Enum.drop(4) + + assert {:ok, colls4} = c.pid |> Mongo.aggregate(coll, []) + assert [%{"foo" => 42}] = colls4 |> Enum.take(1) + + assert {:ok, colls5} = c.pid |> Mongo.aggregate(coll, []) + assert [%{"foo" => 45}] = colls5 |> Enum.drop(3) + + assert {:ok, colls6} = Mongo.aggregate(c.pid, coll, [], use_cursor: false) + assert [] = colls6 |> Enum.take(0) + + assert {:ok, colls7} = Mongo.aggregate(c.pid, coll, [], use_cursor: false) + assert [] = colls7 |> Enum.drop(4) + + assert {:ok, colls8} = c.pid |> Mongo.aggregate(coll, [], use_cursor: false) + assert [%{"foo" => 42}] = colls8 |> Enum.take(1) - assert [] = c.pid |> Mongo.aggregate(coll, []) |> Enum.take(0) - assert [] = c.pid |> Mongo.aggregate(coll, []) |> Enum.drop(4) - assert [%{"foo" => 42}] = c.pid |> Mongo.aggregate(coll, []) |> Enum.take(1) - assert [%{"foo" => 45}] = c.pid |> Mongo.aggregate(coll, []) |> Enum.drop(3) + assert {:ok, colls9} = c.pid |> Mongo.aggregate(coll, [], use_cursor: false) + assert [%{"foo" => 45}] = colls9 |> Enum.drop(3) - assert [] = Mongo.aggregate(c.pid, coll, [], use_cursor: false) |> Enum.take(0) - assert [] = Mongo.aggregate(c.pid, coll, [], use_cursor: false) |> Enum.drop(4) + assert {:ok, colls10} = Mongo.aggregate(c.pid, coll, [], batch_size: 1) + assert [] = colls10 |> Enum.take(0) - assert [%{"foo" => 42}] = - c.pid |> Mongo.aggregate(coll, [], use_cursor: false) |> Enum.take(1) + assert {:ok, colls11} = Mongo.aggregate(c.pid, coll, [], batch_size: 1) + assert [] = colls11 |> Enum.drop(4) - assert [%{"foo" => 45}] = - c.pid |> Mongo.aggregate(coll, [], use_cursor: false) |> Enum.drop(3) + assert {:ok, colls12} = c.pid |> Mongo.aggregate(coll, [], batch_size: 1) + assert [%{"foo" => 42}] = colls12 |> Enum.take(1) - assert [] = Mongo.aggregate(c.pid, coll, [], batch_size: 1) |> Enum.take(0) - assert [] = Mongo.aggregate(c.pid, coll, [], batch_size: 1) |> Enum.drop(4) - assert [%{"foo" => 42}] = c.pid |> Mongo.aggregate(coll, [], batch_size: 1) |> Enum.take(1) - assert [%{"foo" => 45}] = c.pid |> Mongo.aggregate(coll, [], batch_size: 1) |> Enum.drop(3) + assert {:ok, colls13} = c.pid |> Mongo.aggregate(coll, [], batch_size: 1) + assert [%{"foo" => 45}] = colls13 |> Enum.drop(3) end test "count", c do From 7f0627af32fc3c2b2c50e6331e2d018dd04e2954 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Thu, 9 Jul 2020 10:35:09 +0300 Subject: [PATCH 07/15] fixed the remaining changed return pattern --- test/mongo_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/mongo_test.exs b/test/mongo_test.exs index 3c0ab7f7..a9ded064 100644 --- a/test/mongo_test.exs +++ b/test/mongo_test.exs @@ -91,8 +91,8 @@ defmodule Mongo.Test do assert {:ok, _} = Mongo.insert_one(c.pid, coll, %{foo: 44}) assert {:ok, _} = Mongo.insert_one(c.pid, coll, %{foo: 45}) - assert [%{"foo" => 42}, %{"foo" => 43}, %{"foo" => 44}, %{"foo" => 45}] = - c.pid |> Mongo.aggregate(coll, []) |> Enum.to_list() + assert {:ok, colls0} =c.pid |> Mongo.aggregate(coll, []) + assert [%{"foo" => 42}, %{"foo" => 43}, %{"foo" => 44}, %{"foo" => 45}] = colls0 |> Enum.to_list() query = [ %{ From 77b71f3928d79d75de951d727b85c928f3e547f1 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Thu, 9 Jul 2020 11:00:13 +0300 Subject: [PATCH 08/15] Fixed more return pattern mismatches --- test/mongo_test.exs | 5 +---- test/support/specifications/crud/helpers.ex | 3 ++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/test/mongo_test.exs b/test/mongo_test.exs index a9ded064..4028eb10 100644 --- a/test/mongo_test.exs +++ b/test/mongo_test.exs @@ -69,12 +69,9 @@ defmodule Mongo.Test do cmd = [createIndexes: coll_1, indexes: [[key: [foo: 1, bar: 1], name: "foo-bar"]]] assert {:ok, _} = Mongo.command(c.pid, cmd) - {:ok, indexes_colls} = + indexes = c.pid |> Mongo.list_index_names(coll_1) - - indexes = - indexes_colls |> Enum.to_list() assert Enum.count(indexes) == 3 diff --git a/test/support/specifications/crud/helpers.ex b/test/support/specifications/crud/helpers.ex index d1194974..75c6ae20 100644 --- a/test/support/specifications/crud/helpers.ex +++ b/test/support/specifications/crud/helpers.ex @@ -68,7 +68,8 @@ defmodule Mongo.Specification.CRUD.Helpers do |> Map.drop(["pipeline"]) |> atomize_keys() - pid |> Mongo.aggregate(collection, pipeline, opts) |> Enum.to_list() + {:ok, cursor} = pid |> Mongo.aggregate(collection, pipeline, opts) |> Enum.to_list() + cursor end def match_operation_result?(expected, actual) do From 0ea52e744d7bb8fc4390dd85aae1a515303b25b9 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Thu, 9 Jul 2020 12:01:15 +0300 Subject: [PATCH 09/15] More changes on return pattern --- lib/mongo.ex | 2 +- test/mongo_test.exs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index ba287216..014d8f5e 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -1015,7 +1015,7 @@ defmodule Mongo do Convenient function that returns a cursor with the names of the indexes. """ @spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: - %Stream{} | {:error, Mongo.Error.t()} + {:ok, %Stream{}} | {:error, Mongo.Error.t()} def list_index_names(topology_pid, coll, opts \\ []) do list_indexes(topology_pid, coll, opts) |> Stream.map(fn %{"name" => name} -> name end) diff --git a/test/mongo_test.exs b/test/mongo_test.exs index 4028eb10..c6dac5e7 100644 --- a/test/mongo_test.exs +++ b/test/mongo_test.exs @@ -69,9 +69,12 @@ defmodule Mongo.Test do cmd = [createIndexes: coll_1, indexes: [[key: [foo: 1, bar: 1], name: "foo-bar"]]] assert {:ok, _} = Mongo.command(c.pid, cmd) + {:ok, colls00}= + c.pid + Mongo.list_index_names(coll_1) + indexes = - c.pid - |> Mongo.list_index_names(coll_1) + colls00 |> Enum.to_list() assert Enum.count(indexes) == 3 From 238e35ec539e79aff714ca993c78654897be6343 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Thu, 9 Jul 2020 12:15:30 +0300 Subject: [PATCH 10/15] A fix and formatting improvements --- lib/mongo.ex | 10 +++++++--- test/mongo_test.exs | 13 +++++++------ test/support/specifications/crud/helpers.ex | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index 014d8f5e..09247eb2 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -1015,10 +1015,14 @@ defmodule Mongo do Convenient function that returns a cursor with the names of the indexes. """ @spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: - {:ok, %Stream{}} | {:error, Mongo.Error.t()} + %Stream{} | {:error, Mongo.Error.t()} def list_index_names(topology_pid, coll, opts \\ []) do - list_indexes(topology_pid, coll, opts) - |> Stream.map(fn %{"name" => name} -> name end) + case list_indexes(topology_pid, coll, opts) do + {:ok, colls} -> + colls |> Stream.map(fn %{"name" => name} -> name end) + error={:error, _ } -> + error + end end @doc """ diff --git a/test/mongo_test.exs b/test/mongo_test.exs index c6dac5e7..1a7611a3 100644 --- a/test/mongo_test.exs +++ b/test/mongo_test.exs @@ -69,12 +69,12 @@ defmodule Mongo.Test do cmd = [createIndexes: coll_1, indexes: [[key: [foo: 1, bar: 1], name: "foo-bar"]]] assert {:ok, _} = Mongo.command(c.pid, cmd) - {:ok, colls00}= - c.pid - Mongo.list_index_names(coll_1) + {:ok, colls00} = + c.pid + |> Mongo.list_index_names(coll_1) indexes = - colls00 + colls00 |> Enum.to_list() assert Enum.count(indexes) == 3 @@ -91,8 +91,9 @@ defmodule Mongo.Test do assert {:ok, _} = Mongo.insert_one(c.pid, coll, %{foo: 44}) assert {:ok, _} = Mongo.insert_one(c.pid, coll, %{foo: 45}) - assert {:ok, colls0} =c.pid |> Mongo.aggregate(coll, []) - assert [%{"foo" => 42}, %{"foo" => 43}, %{"foo" => 44}, %{"foo" => 45}] = colls0 |> Enum.to_list() + assert {:ok, colls0} = c.pid |> Mongo.aggregate(coll, []) + assert [%{"foo" => 42}, %{"foo" => 43}, %{"foo" => 44}, %{"foo" => 45}] = + colls0 |> Enum.to_list() query = [ %{ diff --git a/test/support/specifications/crud/helpers.ex b/test/support/specifications/crud/helpers.ex index 75c6ae20..bc3c487b 100644 --- a/test/support/specifications/crud/helpers.ex +++ b/test/support/specifications/crud/helpers.ex @@ -69,7 +69,7 @@ defmodule Mongo.Specification.CRUD.Helpers do |> atomize_keys() {:ok, cursor} = pid |> Mongo.aggregate(collection, pipeline, opts) |> Enum.to_list() - cursor + cursor end def match_operation_result?(expected, actual) do From 34ec575f296e750e111bfb909f53b7c4d73e77a1 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Thu, 9 Jul 2020 13:14:41 +0300 Subject: [PATCH 11/15] More changes for return pattern changes for aggregate function --- lib/mongo.ex | 5 ++++- test/mongo_test.exs | 7 ++----- test/support/specifications/crud/helpers.ex | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index 09247eb2..ef6106a4 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -389,9 +389,12 @@ defmodule Mongo do |> filter_nils |> Enum.map(&List.wrap/1) - documents = + {:ok, cursor} = topology_pid |> Mongo.aggregate(coll, pipeline, opts) + + documents = + cursor |> Enum.to_list() case documents do diff --git a/test/mongo_test.exs b/test/mongo_test.exs index 1a7611a3..441f197e 100644 --- a/test/mongo_test.exs +++ b/test/mongo_test.exs @@ -69,13 +69,10 @@ defmodule Mongo.Test do cmd = [createIndexes: coll_1, indexes: [[key: [foo: 1, bar: 1], name: "foo-bar"]]] assert {:ok, _} = Mongo.command(c.pid, cmd) - {:ok, colls00} = + indexes = c.pid |> Mongo.list_index_names(coll_1) - - indexes = - colls00 - |> Enum.to_list() + |> Enum.to_list() assert Enum.count(indexes) == 3 assert Enum.member?(indexes, "_id_") diff --git a/test/support/specifications/crud/helpers.ex b/test/support/specifications/crud/helpers.ex index bc3c487b..b699237a 100644 --- a/test/support/specifications/crud/helpers.ex +++ b/test/support/specifications/crud/helpers.ex @@ -68,8 +68,8 @@ defmodule Mongo.Specification.CRUD.Helpers do |> Map.drop(["pipeline"]) |> atomize_keys() - {:ok, cursor} = pid |> Mongo.aggregate(collection, pipeline, opts) |> Enum.to_list() - cursor + {:ok, cursor} = pid |> Mongo.aggregate(collection, pipeline, opts) + cursor|> Enum.to_list() end def match_operation_result?(expected, actual) do From bb1c333f7434733ff48413ed26e971f98facf613 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Thu, 9 Jul 2020 13:34:51 +0300 Subject: [PATCH 12/15] And fixed one more mismatched return pattern issue --- lib/mongo.ex | 19 ++++++++++--------- lib/mongo/grid_fs/bucket.ex | 7 +++++-- test/mongo_test.exs | 3 ++- test/support/specifications/crud/helpers.ex | 4 ++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index ef6106a4..35003bfe 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -392,9 +392,9 @@ defmodule Mongo do {:ok, cursor} = topology_pid |> Mongo.aggregate(coll, pipeline, opts) - - documents = - cursor + + documents = + cursor |> Enum.to_list() case documents do @@ -1018,14 +1018,15 @@ defmodule Mongo do Convenient function that returns a cursor with the names of the indexes. """ @spec list_index_names(GenServer.server(), String.t(), Keyword.t()) :: - %Stream{} | {:error, Mongo.Error.t()} + %Stream{} | {:error, Mongo.Error.t()} def list_index_names(topology_pid, coll, opts \\ []) do - case list_indexes(topology_pid, coll, opts) do - {:ok, colls} -> + case list_indexes(topology_pid, coll, opts) do + {:ok, colls} -> colls |> Stream.map(fn %{"name" => name} -> name end) - error={:error, _ } -> - error - end + + error = {:error, _} -> + error + end end @doc """ diff --git a/lib/mongo/grid_fs/bucket.ex b/lib/mongo/grid_fs/bucket.ex index 9b5c8dd7..f0225051 100644 --- a/lib/mongo/grid_fs/bucket.ex +++ b/lib/mongo/grid_fs/bucket.ex @@ -178,8 +178,11 @@ defmodule Mongo.GridFs.Bucket do # returns true if the collection contains a index with the given name defp index_member?(topology_pid, coll, index, opts) do - topology_pid - |> Mongo.list_indexes(coll, opts) + {:ok, cursor} = + topology_pid + |> Mongo.list_indexes(coll, opts) + + cursor |> Enum.member?(index) end diff --git a/test/mongo_test.exs b/test/mongo_test.exs index 441f197e..e584054c 100644 --- a/test/mongo_test.exs +++ b/test/mongo_test.exs @@ -72,7 +72,7 @@ defmodule Mongo.Test do indexes = c.pid |> Mongo.list_index_names(coll_1) - |> Enum.to_list() + |> Enum.to_list() assert Enum.count(indexes) == 3 assert Enum.member?(indexes, "_id_") @@ -89,6 +89,7 @@ defmodule Mongo.Test do assert {:ok, _} = Mongo.insert_one(c.pid, coll, %{foo: 45}) assert {:ok, colls0} = c.pid |> Mongo.aggregate(coll, []) + assert [%{"foo" => 42}, %{"foo" => 43}, %{"foo" => 44}, %{"foo" => 45}] = colls0 |> Enum.to_list() diff --git a/test/support/specifications/crud/helpers.ex b/test/support/specifications/crud/helpers.ex index b699237a..81173fad 100644 --- a/test/support/specifications/crud/helpers.ex +++ b/test/support/specifications/crud/helpers.ex @@ -68,8 +68,8 @@ defmodule Mongo.Specification.CRUD.Helpers do |> Map.drop(["pipeline"]) |> atomize_keys() - {:ok, cursor} = pid |> Mongo.aggregate(collection, pipeline, opts) - cursor|> Enum.to_list() + {:ok, cursor} = pid |> Mongo.aggregate(collection, pipeline, opts) + cursor |> Enum.to_list() end def match_operation_result?(expected, actual) do From 594a7d97f2f2d0b03d1f35e6f9a89d618ddf2d76 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Fri, 10 Jul 2020 11:29:54 +0300 Subject: [PATCH 13/15] Created basic_result return pattern and started applying them --- lib/mongo.ex | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index 35003bfe..8b04cef3 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -61,10 +61,12 @@ defmodule Mongo do @type conn :: DbConnection.Conn @type collection :: String.t() @opaque cursor :: Mongo.Cursor.t() | Mongo.AggregationCursor.t() + @type basic_result(t) :: {:ok, t} | {:error, Mongo.Error.t()} @type result(t) :: :ok | {:ok, t} | {:error, Mongo.Error.t()} @type write_result(t) :: :ok | {:ok, t} | {:error, Mongo.Error.t()} | {:error, Mongo.WriteError.t()} @type result!(t) :: nil | t | no_return + @type basic_result!(t) :: t | no_return defmacrop bangify(result) do quote do @@ -76,6 +78,15 @@ defmodule Mongo do end end + defmacrop bangify_basic_result(basic_result) do + quote do + case unquote(basic_result) do + {:ok, value} -> value + {:error, error} -> raise error + end + end + end + @type initial_type :: :unknown | :single | :replica_set_no_primary | :sharded @doc """ @@ -222,7 +233,7 @@ defmodule Mongo do BSON.document(), BSON.document(), Keyword.t() - ) :: result(BSON.document()) | {:ok, nil} + ) :: basic_result(BSON.document()) | {:ok, nil} def find_one_and_update(topology_pid, coll, filter, update, opts \\ []) do _ = modifier_docs(update, :update) @@ -277,7 +288,7 @@ defmodule Mongo do BSON.document(), BSON.document(), Keyword.t() - ) :: result(BSON.document()) + ) :: basic_result(BSON.document()) def find_one_and_replace(topology_pid, coll, filter, replacement, opts \\ []) do _ = modifier_docs(replacement, :replace) @@ -321,7 +332,7 @@ defmodule Mongo do * `:collation` - Optionally specifies a collation to use in MongoDB 3.4 and higher. """ @spec find_one_and_delete(GenServer.server(), collection, BSON.document(), Keyword.t()) :: - result(BSON.document()) + basic_result(BSON.document()) def find_one_and_delete(topology_pid, coll, filter, opts \\ []) do query = filter_nils( @@ -343,7 +354,7 @@ defmodule Mongo do @doc false @spec count(GenServer.server(), collection, BSON.document(), Keyword.t()) :: - result(non_neg_integer) + basic_result(non_neg_integer) def count(topology_pid, coll, filter, opts \\ []) do query = filter_nils( @@ -364,9 +375,9 @@ defmodule Mongo do @doc false @spec count!(GenServer.server(), collection, BSON.document(), Keyword.t()) :: - result!(non_neg_integer) + basic_result!(non_neg_integer) def count!(topology_pid, coll, filter, opts \\ []) do - bangify(count(topology_pid, coll, filter, opts)) + bangify_basic_result(count(topology_pid, coll, filter, opts)) end @doc """ @@ -377,7 +388,7 @@ defmodule Mongo do * `:skip` - Number of documents to skip before returning the first """ @spec count_documents(GenServer.server(), collection, BSON.document(), Keyword.t()) :: - result(non_neg_integer) + basic_result(non_neg_integer) def count_documents(topology_pid, coll, filter, opts \\ []) do pipeline = [ @@ -407,7 +418,7 @@ defmodule Mongo do Similar to `count_documents/4` but unwraps the result and raises on error. """ @spec count_documents!(GenServer.server(), collection, BSON.document(), Keyword.t()) :: - result!(non_neg_integer) + basic_result!(non_neg_integer) def count_documents!(topology_pid, coll, filter, opts \\ []) do bangify(count_documents(topology_pid, coll, filter, opts)) end @@ -416,7 +427,7 @@ defmodule Mongo do Estimate the number of documents in a collection using collection metadata. """ @spec estimated_document_count(GenServer.server(), collection, Keyword.t()) :: - result(non_neg_integer) + basic_result(non_neg_integer) def estimated_document_count(topology_pid, coll, opts) do opts = Keyword.drop(opts, [:skip, :limit, :hint, :collation]) count(topology_pid, coll, %{}, opts) @@ -427,9 +438,9 @@ defmodule Mongo do error. """ @spec estimated_document_count!(GenServer.server(), collection, Keyword.t()) :: - result!(non_neg_integer) + basic_result!(non_neg_integer) def estimated_document_count!(topology_pid, coll, opts) do - bangify(estimated_document_count(topology_pid, coll, opts)) + bangify_basic_result(estimated_document_count(topology_pid, coll, opts)) end @doc """ @@ -441,7 +452,7 @@ defmodule Mongo do * `:collation` - Optionally specifies a collation to use in MongoDB 3.4 and """ @spec distinct(GenServer.server(), collection, String.t() | atom, BSON.document(), Keyword.t()) :: - result([BSON.t()]) + basic_result([BSON.t()]) def distinct(topology_pid, coll, field, filter, opts \\ []) do query = filter_nils( @@ -463,9 +474,9 @@ defmodule Mongo do Similar to `distinct/5` but unwraps the result and raises on error. """ @spec distinct!(GenServer.server(), collection, String.t() | atom, BSON.document(), Keyword.t()) :: - result!([BSON.t()]) + basic_result!([BSON.t()]) def distinct!(topology_pid, coll, field, filter, opts \\ []) do - bangify(distinct(topology_pid, coll, field, filter, opts)) + bangify_basic_result(distinct(topology_pid, coll, field, filter, opts)) end @doc """ From 39f25f6429a9c524353721d78b0da733ceaed623 Mon Sep 17 00:00:00 2001 From: Hakan B Date: Fri, 10 Jul 2020 14:54:05 +0300 Subject: [PATCH 14/15] Applied basic_result return pattern to more functions --- lib/mongo.ex | 32 ++++++++++++++++---------------- lib/mongo/grid_fs/bucket.ex | 30 +++++++++++++++--------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index 8b04cef3..c3ee750d 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -64,7 +64,7 @@ defmodule Mongo do @type basic_result(t) :: {:ok, t} | {:error, Mongo.Error.t()} @type result(t) :: :ok | {:ok, t} | {:error, Mongo.Error.t()} @type write_result(t) :: - :ok | {:ok, t} | {:error, Mongo.Error.t()} | {:error, Mongo.WriteError.t()} + {:ok, t} | {:error, Mongo.Error.t()} | {:error, Mongo.WriteError.t()} @type result!(t) :: nil | t | no_return @type basic_result!(t) :: t | no_return @@ -420,7 +420,7 @@ defmodule Mongo do @spec count_documents!(GenServer.server(), collection, BSON.document(), Keyword.t()) :: basic_result!(non_neg_integer) def count_documents!(topology_pid, coll, filter, opts \\ []) do - bangify(count_documents(topology_pid, coll, filter, opts)) + bangify_basic_result(count_documents(topology_pid, coll, filter, opts)) end @doc """ @@ -715,9 +715,9 @@ defmodule Mongo do Similar to `insert_one/4` but unwraps the result and raises on error. """ @spec insert_one!(GenServer.server(), collection, BSON.document(), Keyword.t()) :: - result!(Mongo.InsertOneResult.t()) + basic_result!(Mongo.InsertOneResult.t()) def insert_one!(topology_pid, coll, doc, opts \\ []) do - bangify(insert_one(topology_pid, coll, doc, opts)) + bangify_basic_result(insert_one(topology_pid, coll, doc, opts)) end @doc """ @@ -783,9 +783,9 @@ defmodule Mongo do Similar to `insert_many/4` but unwraps the result and raises on error. """ @spec insert_many!(GenServer.server(), collection, [BSON.document()], Keyword.t()) :: - result!(Mongo.InsertManyResult.t()) + basic_result!(Mongo.InsertManyResult.t()) def insert_many!(topology_pid, coll, docs, opts \\ []) do - bangify(insert_many(topology_pid, coll, docs, opts)) + bangify_basic_result(insert_many(topology_pid, coll, docs, opts)) end @doc """ @@ -801,9 +801,9 @@ defmodule Mongo do Similar to `delete_one/4` but unwraps the result and raises on error. """ @spec delete_one!(GenServer.server(), collection, BSON.document(), Keyword.t()) :: - result!(Mongo.DeleteResult.t()) + basic_result!(Mongo.DeleteResult.t()) def delete_one!(topology_pid, coll, filter, opts \\ []) do - bangify(delete_one(topology_pid, coll, filter, opts)) + bangify_basic_result(delete_one(topology_pid, coll, filter, opts)) end @doc """ @@ -819,9 +819,9 @@ defmodule Mongo do Similar to `delete_many/4` but unwraps the result and raises on error. """ @spec delete_many!(GenServer.server(), collection, BSON.document(), Keyword.t()) :: - result!(Mongo.DeleteResult.t()) + basic_result!(Mongo.DeleteResult.t()) def delete_many!(topology_pid, coll, filter, opts \\ []) do - bangify(delete_many(topology_pid, coll, filter, opts)) + bangify_basic_result(delete_many(topology_pid, coll, filter, opts)) end defp do_delete(topology_pid, coll, filter, limit, opts) do @@ -887,9 +887,9 @@ defmodule Mongo do BSON.document(), BSON.document(), Keyword.t() - ) :: result!(Mongo.UpdateResult.t()) + ) :: basic_result!(Mongo.UpdateResult.t()) def replace_one!(topology_pid, coll, filter, replacement, opts \\ []) do - bangify(replace_one(topology_pid, coll, filter, replacement, opts)) + bangify_basic_result(replace_one(topology_pid, coll, filter, replacement, opts)) end @doc """ @@ -923,9 +923,9 @@ defmodule Mongo do Similar to `update_one/5` but unwraps the result and raises on error. """ @spec update_one!(GenServer.server(), collection, BSON.document(), BSON.document(), Keyword.t()) :: - result!(Mongo.UpdateResult.t()) + basic_result!(Mongo.UpdateResult.t()) def update_one!(topology_pid, coll, filter, update, opts \\ []) do - bangify(update_one(topology_pid, coll, filter, update, opts)) + bangify_basic_result(update_one(topology_pid, coll, filter, update, opts)) end @doc """ @@ -957,9 +957,9 @@ defmodule Mongo do BSON.document(), BSON.document(), Keyword.t() - ) :: result!(Mongo.UpdateResult.t()) + ) :: basic_result!(Mongo.UpdateResult.t()) def update_many!(topology_pid, coll, filter, update, opts \\ []) do - bangify(update_many(topology_pid, coll, filter, update, opts)) + bangify_basic_result(update_many(topology_pid, coll, filter, update, opts)) end defp do_update(topology_pid, coll, filter, update, multi, opts) do diff --git a/lib/mongo/grid_fs/bucket.ex b/lib/mongo/grid_fs/bucket.ex index f0225051..5d28fcc6 100644 --- a/lib/mongo/grid_fs/bucket.ex +++ b/lib/mongo/grid_fs/bucket.ex @@ -72,31 +72,30 @@ defmodule Mongo.GridFs.Bucket do Given a `id`, delete this stored file’s files collection document and associated chunks from a GridFS bucket. """ - @spec delete(Bucket.t(), String.t()) :: {:ok, %Mongo.DeleteResult{}} + @spec delete(Bucket.t(), String.t()) :: {:ok, %Mongo.DeleteResult{}} | {:error, Mongo.Error.t()} def delete(%Bucket{} = bucket, file_id) when is_binary(file_id) do delete(bucket, ObjectId.decode!(file_id)) end - @spec delete(Bucket.t(), BSON.ObjectId.t()) :: {:ok, %Mongo.DeleteResult{}} + @spec delete(Bucket.t(), BSON.ObjectId.t()) :: + {:ok, %Mongo.DeleteResult{}} | {:error, Mongo.Error.t()} def delete(%Bucket{topology_pid: topology_pid, opts: opts} = bucket, %BSON.ObjectId{} = oid) do - # first delete files document - collection = files_collection_name(bucket) - - {:ok, %Mongo.DeleteResult{deleted_count: _}} = - Mongo.delete_one(topology_pid, collection, %{_id: oid}, opts) - - # then delete all chunk documents - collection = chunks_collection_name(bucket) - - {:ok, %Mongo.DeleteResult{deleted_count: _}} = - Mongo.delete_many(topology_pid, collection, %{files_id: oid}, opts) + with collection <- files_collection_name(bucket), + # first delete files document + {:ok, %Mongo.DeleteResult{deleted_count: _}} <- + Mongo.delete_one(topology_pid, collection, %{_id: oid}, opts), + # then delete all chunk documents + collection <- chunks_collection_name(bucket), + {:ok, %Mongo.DeleteResult{deleted_count: del_count}} <- + Mongo.delete_many(topology_pid, collection, %{files_id: oid}, opts), + do: {:ok, %Mongo.DeleteResult{deleted_count: del_count}} end @doc """ Drops the files and chunks collections associated with this bucket. """ - @spec drop(Bucket.t()) :: Mongo.result(BSON.document()) + @spec drop(Bucket.t()) :: Mongo.basic_result(BSON.document()) def drop(%Bucket{topology_pid: topology_pid, opts: opts} = bucket) do {:ok, _} = Mongo.command(topology_pid, %{drop: files_collection_name(bucket)}, opts) {:ok, _} = Mongo.command(topology_pid, %{drop: chunks_collection_name(bucket)}, opts) @@ -105,7 +104,8 @@ defmodule Mongo.GridFs.Bucket do @doc """ Returns a cursor from the fs.files collection. """ - @spec find(Bucket.t(), BSON.document(), Keyword.t()) :: Mongo.cursor() + @spec find(Bucket.t(), BSON.document(), Keyword.t()) :: + Mongo.cursor() | {:error, Mongo.Error.t()} def find(%Bucket{topology_pid: topology_pid} = bucket, filter, opts \\ []) do Mongo.find(topology_pid, files_collection_name(bucket), filter, opts) end From 5e058ef60e0a8e52e5817b76c887a21e005b876a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jan=20Niemier?= Date: Fri, 30 Oct 2020 11:04:57 +0100 Subject: [PATCH 15/15] Remove pipelines with only one step --- lib/mongo.ex | 10 ++--- lib/mongo/grid_fs/bucket.ex | 7 +-- test/mongo_test.exs | 50 ++++++++++----------- test/support/specifications/crud/helpers.ex | 3 +- 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index c3ee750d..f5d12a75 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -400,13 +400,9 @@ defmodule Mongo do |> filter_nils |> Enum.map(&List.wrap/1) - {:ok, cursor} = - topology_pid - |> Mongo.aggregate(coll, pipeline, opts) + {:ok, cursor} = Mongo.aggregate(topology_pid, coll, pipeline, opts) - documents = - cursor - |> Enum.to_list() + documents = Enum.to_list(cursor) case documents do [%{"n" => count}] -> {:ok, count} @@ -1033,7 +1029,7 @@ defmodule Mongo do def list_index_names(topology_pid, coll, opts \\ []) do case list_indexes(topology_pid, coll, opts) do {:ok, colls} -> - colls |> Stream.map(fn %{"name" => name} -> name end) + Stream.map(colls, fn %{"name" => name} -> name end) error = {:error, _} -> error diff --git a/lib/mongo/grid_fs/bucket.ex b/lib/mongo/grid_fs/bucket.ex index 5d28fcc6..4403c5e3 100644 --- a/lib/mongo/grid_fs/bucket.ex +++ b/lib/mongo/grid_fs/bucket.ex @@ -178,12 +178,9 @@ defmodule Mongo.GridFs.Bucket do # returns true if the collection contains a index with the given name defp index_member?(topology_pid, coll, index, opts) do - {:ok, cursor} = - topology_pid - |> Mongo.list_indexes(coll, opts) + {:ok, cursor} = Mongo.list_indexes(topology_pid, coll, opts) - cursor - |> Enum.member?(index) + Enum.member?(cursor, index) end ## diff --git a/test/mongo_test.exs b/test/mongo_test.exs index e584054c..5153897b 100644 --- a/test/mongo_test.exs +++ b/test/mongo_test.exs @@ -44,7 +44,7 @@ defmodule Mongo.Test do cmd = [createIndexes: coll_2, indexes: [[key: [foo: 1, bar: 1], name: "not-a-collection"]]] assert {:ok, _} = Mongo.command(c.pid, cmd) - assert {:ok, colls0} = c.pid |> Mongo.show_collections() + assert {:ok, colls0} = Mongo.show_collections(c.pid) colls = colls0 @@ -88,10 +88,10 @@ defmodule Mongo.Test do assert {:ok, _} = Mongo.insert_one(c.pid, coll, %{foo: 44}) assert {:ok, _} = Mongo.insert_one(c.pid, coll, %{foo: 45}) - assert {:ok, colls0} = c.pid |> Mongo.aggregate(coll, []) + assert {:ok, colls0} = Mongo.aggregate(c.pid, coll, []) assert [%{"foo" => 42}, %{"foo" => 43}, %{"foo" => 44}, %{"foo" => 45}] = - colls0 |> Enum.to_list() + Enum.to_list(colls0) query = [ %{ @@ -107,44 +107,44 @@ defmodule Mongo.Test do } ] - assert {:ok, colls1} = c.pid |> Mongo.aggregate(coll, query) - assert [%{"_id" => "foo", "total" => 89}] = colls1 |> Enum.to_list() + assert {:ok, colls1} = Mongo.aggregate(c.pid, coll, query) + assert [%{"_id" => "foo", "total" => 89}] == Enum.to_list(colls1) - assert {:ok, colls2} = c.pid |> Mongo.aggregate(coll, []) - assert [] = colls2 |> Enum.take(0) + assert {:ok, colls2} = Mongo.aggregate(c.pid, coll, []) + assert [] == Enum.take(colls2, 0) - assert {:ok, colls3} = c.pid |> Mongo.aggregate(coll, []) - assert [] = colls3 |> Enum.drop(4) + assert {:ok, colls3} = Mongo.aggregate(c.pid, coll, []) + assert [] == Enum.drop(colls3, 4) - assert {:ok, colls4} = c.pid |> Mongo.aggregate(coll, []) - assert [%{"foo" => 42}] = colls4 |> Enum.take(1) + assert {:ok, colls4} = Mongo.aggregate(c.pid, coll, []) + assert [%{"foo" => 42}] == Enum.take(colls4, 1) - assert {:ok, colls5} = c.pid |> Mongo.aggregate(coll, []) - assert [%{"foo" => 45}] = colls5 |> Enum.drop(3) + assert {:ok, colls5} = Mongo.aggregate(c.pid, coll, []) + assert [%{"foo" => 45}] == Enum.drop(colls5, 3) assert {:ok, colls6} = Mongo.aggregate(c.pid, coll, [], use_cursor: false) - assert [] = colls6 |> Enum.take(0) + assert [] == Enum.take(colls6, 0) assert {:ok, colls7} = Mongo.aggregate(c.pid, coll, [], use_cursor: false) - assert [] = colls7 |> Enum.drop(4) + assert [] == Enum.drop(colls7, 4) - assert {:ok, colls8} = c.pid |> Mongo.aggregate(coll, [], use_cursor: false) - assert [%{"foo" => 42}] = colls8 |> Enum.take(1) + assert {:ok, colls8} = Mongo.aggregate(c.pid, coll, [], use_cursor: false) + assert [%{"foo" => 42}] = Enum.take(colls8, 1) - assert {:ok, colls9} = c.pid |> Mongo.aggregate(coll, [], use_cursor: false) - assert [%{"foo" => 45}] = colls9 |> Enum.drop(3) + assert {:ok, colls9} = Mongo.aggregate(c.pid, coll, [], use_cursor: false) + assert [%{"foo" => 45}] == Enum.drop(colls9, 3) assert {:ok, colls10} = Mongo.aggregate(c.pid, coll, [], batch_size: 1) - assert [] = colls10 |> Enum.take(0) + assert [] == Enum.take(colls10, 0) assert {:ok, colls11} = Mongo.aggregate(c.pid, coll, [], batch_size: 1) - assert [] = colls11 |> Enum.drop(4) + assert [] == Enum.drop(colls11, 4) - assert {:ok, colls12} = c.pid |> Mongo.aggregate(coll, [], batch_size: 1) - assert [%{"foo" => 42}] = colls12 |> Enum.take(1) + assert {:ok, colls12} = Mongo.aggregate(c.pid, coll, [], batch_size: 1) + assert [%{"foo" => 42}] == Enum.take(colls12, 1) - assert {:ok, colls13} = c.pid |> Mongo.aggregate(coll, [], batch_size: 1) - assert [%{"foo" => 45}] = colls13 |> Enum.drop(3) + assert {:ok, colls13} = Mongo.aggregate(c.pid, coll, [], batch_size: 1) + assert [%{"foo" => 45}] == Enum.drop(colls13, 3) end test "count", c do diff --git a/test/support/specifications/crud/helpers.ex b/test/support/specifications/crud/helpers.ex index 81173fad..8abfd812 100644 --- a/test/support/specifications/crud/helpers.ex +++ b/test/support/specifications/crud/helpers.ex @@ -69,7 +69,8 @@ defmodule Mongo.Specification.CRUD.Helpers do |> atomize_keys() {:ok, cursor} = pid |> Mongo.aggregate(collection, pipeline, opts) - cursor |> Enum.to_list() + + Enum.to_list(cursor) end def match_operation_result?(expected, actual) do