Skip to content

Commit 27ad39f

Browse files
committed
fixes the incorrect creation of indices, added more test cases for bucket api
1 parent e0aeac8 commit 27ad39f

File tree

5 files changed

+115
-29
lines changed

5 files changed

+115
-29
lines changed

lib/mongo.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,16 @@ defmodule Mongo do
10301030
|> Stream.map(fn %{"name" => name } -> name end)
10311031
end
10321032

1033+
@doc """
1034+
Convenient function that drops the index `name` in the collection `coll`.
1035+
"""
1036+
@spec drop_index(GenServer.server, String.t, String.t, Keyword.t) :: :ok | {:error, Mongo.Error.t}
1037+
def drop_index(topology_pid, coll, name, opts \\ []) do
1038+
cmd = [dropIndexes: coll, index: name]
1039+
with {:ok, _} <- Mongo.issue_command(topology_pid, cmd, :write, opts) do
1040+
:ok
1041+
end
1042+
end
10331043

10341044
@doc """
10351045
Getting Collection Names

lib/mongo/grid_fs/bucket.ex

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ defmodule Mongo.GridFs.Bucket do
156156

157157
def check_and_create_files_index(bucket) do
158158
case check_files_index(bucket) do
159-
true -> create_files_index(bucket)
160-
false -> :ok
159+
false -> create_files_index(bucket)
160+
true -> :ok
161161
end
162162
end
163163

164164
def check_and_chunks_files_index(bucket) do
165165
case check_chunks_index(bucket) do
166-
true -> create_chunks_index(bucket)
167-
false -> :ok
166+
false -> create_chunks_index(bucket)
167+
true -> :ok
168168
end
169169
end
170170

@@ -228,21 +228,4 @@ defmodule Mongo.GridFs.Bucket do
228228
end
229229
end
230230

231-
232-
defimpl Inspect, for: Bucket do
233-
234-
def inspect(%Bucket{name: fs, chunk_size: size, topology_pid: topology_pid}, _opts) do
235-
"#Bucket(" <> fs <> ", " <> to_string(size) <> ", topology_pid: " <> (inspect topology_pid) <> ")"
236-
end
237-
238-
end
239-
240-
defimpl String.Chars, for: Bucket do
241-
242-
def to_string(%Bucket{name: fs, chunk_size: size, topology_pid: topology_pid}) do
243-
"#Bucket(" <> fs <> ", " <> Kernel.to_string(size) <> ", topology_pid: " <> (inspect topology_pid) <> ")"
244-
end
245-
246-
end
247-
248231
end

mix.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ defmodule Mongodb.Mixfile do
1313
docs: docs(),
1414
description: description(),
1515
package: package(),
16-
dialyzer: [
16+
test_coverage: [tool: ExCoveralls],
17+
preferred_cli_env: [docs: :docs, coveralls: :test, "coveralls.detail": :test, "coveralls.post": :test, "coveralls.html": :test],
18+
dialyzer: [
1719
flags: [:underspecs, :unknown, :unmatched_returns],
1820
plt_add_apps: [:logger, :connection, :db_connection, :mix, :elixir, :ssl, :public_key],
1921
plt_add_deps: :transitive,
@@ -38,6 +40,7 @@ defmodule Mongodb.Mixfile do
3840
[
3941
{:db_connection, "~> 2.1.1"},
4042
{:decimal, "~> 1.8"},
43+
{:excoveralls, "~> 0.12.1", only: :test},
4144
{:benchee, "~> 1.0", only: :dev},
4245
{:jason, "~> 1.0.0", only: :test},
4346
{:ex_doc, "~> 0.20.1 ", only: :dev},

mix.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
%{
22
"benchee": {:hex, :benchee, "1.0.1", "66b211f9bfd84bd97e6d1beaddf8fc2312aaabe192f776e8931cb0c16f53a521", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}], "hexpm"},
3+
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
34
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
45
"db_connection": {:hex, :db_connection, "2.1.1", "a51e8a2ee54ef2ae6ec41a668c85787ed40cb8944928c191280fe34c15b76ae5", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
56
"decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [:mix], [], "hexpm"},
@@ -9,9 +10,17 @@
910
"elixir_uuid": {:hex, :elixir_uuid, "1.2.0", "ff26e938f95830b1db152cb6e594d711c10c02c6391236900ddd070a6b01271d", [:mix], [], "hexpm"},
1011
"erlex": {:hex, :erlex, "0.1.6", "c01c889363168d3fdd23f4211647d8a34c0f9a21ec726762312e08e083f3d47e", [:mix], [], "hexpm"},
1112
"ex_doc": {:hex, :ex_doc, "0.20.2", "1bd0dfb0304bade58beb77f20f21ee3558cc3c753743ae0ddbb0fd7ba2912331", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
13+
"excoveralls": {:hex, :excoveralls, "0.12.1", "a553c59f6850d0aff3770e4729515762ba7c8e41eedde03208182a8dc9d0ce07", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
14+
"hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
15+
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
1216
"jason": {:hex, :jason, "1.0.1", "ef108e64c6e086364b9f15b0073cf794061670af8f331d545d7308c0ba2e67f9", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
1317
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
1418
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
19+
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
20+
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
1521
"nimble_parsec": {:hex, :nimble_parsec, "0.5.1", "c90796ecee0289dbb5ad16d3ad06f957b0cd1199769641c961cfe0b97db190e0", [:mix], [], "hexpm"},
22+
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
1623
"poolboy": {:hex, :poolboy, "1.5.2", "392b007a1693a64540cead79830443abf5762f5d30cf50bc95cb2c1aaafa006b", [:rebar3], [], "hexpm"},
24+
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm"},
25+
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
1726
}

test/mongo/grid_fs/bucket_test.exs

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
defmodule Mongo.GridFs.BucketTest do
2-
use ExUnit.Case
2+
use ExUnit.Case, async: false
33

44
alias Mongo.GridFs.Bucket
55
alias Mongo.GridFs.Upload
6+
alias BSON.ObjectId
7+
alias Mongo.Session
68

79
setup_all do
810
assert {:ok, pid} = Mongo.TestConnection.connect
911
{:ok, [pid: pid]}
1012
end
1113

12-
test "check implementation of the protocols Inspect and String.Chars", c do
13-
b = Bucket.new(c.pid)
14-
assert inspect b == to_string(b)
15-
end
16-
1714
test "check if the name can be overridden", c do
1815
new_name = "my_fs"
1916
%Bucket{name: fs} = Bucket.new(c.pid, name: "my_fs")
@@ -47,6 +44,27 @@ defmodule Mongo.GridFs.BucketTest do
4744
assert chunk == nil
4845
end
4946

47+
test "delete a file with ID a string", c do
48+
bucket = Bucket.new(c.pid)
49+
upload_stream = Upload.open_upload_stream(bucket, "my-file-to-delete.txt")
50+
src_filename = "./test/data/test.txt"
51+
52+
File.stream!(src_filename, [], 512) |> Stream.into(upload_stream) |> Stream.run()
53+
54+
file_id = upload_stream.id
55+
56+
file = Bucket.find_one(bucket, file_id)
57+
assert file != nil
58+
59+
Bucket.delete(bucket, ObjectId.encode!(file_id))
60+
61+
file = Bucket.find_one(bucket, file_id)
62+
assert file == nil
63+
64+
chunk = Mongo.find_one(c.pid, Bucket.chunks_collection_name(bucket), %{files_id: file_id})
65+
assert chunk == nil
66+
end
67+
5068
test "rename a file", c do
5169

5270
bucket = Bucket.new(c.pid)
@@ -70,7 +88,7 @@ defmodule Mongo.GridFs.BucketTest do
7088

7189
test "drop bucket", c do
7290

73-
bucket = Bucket.new(c.pid, fs: "killme")
91+
bucket = Bucket.new(c.pid, name: "killme")
7492
upload_stream = Upload.open_upload_stream(bucket, "my-example-file.txt")
7593
src_filename = "./test/data/test.txt"
7694

@@ -90,4 +108,67 @@ defmodule Mongo.GridFs.BucketTest do
90108

91109
end
92110

111+
test "check find and find_one", c do
112+
113+
bucket = Bucket.new(c.pid, name: "killme")
114+
upload_stream = Upload.open_upload_stream(bucket, "my-example-file.txt")
115+
src_filename = "./test/data/test.txt"
116+
117+
File.stream!(src_filename, [], 512) |> Stream.into(upload_stream) |> Stream.run()
118+
119+
file_id = upload_stream.id
120+
[file] = Bucket.find(bucket, [_id: file_id]) |> Enum.to_list()
121+
assert file != nil
122+
file = Bucket.find_one(bucket, ObjectId.encode!(file_id))
123+
assert file != nil
124+
125+
Bucket.drop(bucket)
126+
end
127+
128+
test "explicit sessions", c do
129+
130+
top = c.pid
131+
{:ok, session} = Session.start_session(top, :write, [])
132+
assert :ok = Session.start_transaction(session)
133+
134+
bucket = Bucket.new(top, name: "sessions") |> Bucket.add_session(session: session)
135+
136+
upload_stream = Upload.open_upload_stream(bucket, "my-example-file.txt")
137+
src_filename = "./test/data/test.txt"
138+
139+
File.stream!(src_filename, [], 512) |> Stream.into(upload_stream) |> Stream.run()
140+
141+
file_id = upload_stream.id
142+
file = Bucket.find_one(bucket, file_id)
143+
assert file != nil
144+
145+
assert :ok = Session.abort_transaction(session)
146+
assert :ok == Session.end_session(top, session)
147+
148+
bucket = Bucket.new(top, name: "sessions")
149+
file = Bucket.find_one(bucket, file_id)
150+
assert file == nil
151+
152+
end
153+
154+
test "check if indices are created", c do
155+
156+
top = c.pid
157+
_bucket = Bucket.new(top, name: "index_test")
158+
159+
assert Mongo.list_index_names(top, "index_test.files") |> Enum.any?(fn name -> name == "filename_1_uploadDate_1" end)
160+
assert Mongo.list_index_names(top, "index_test.chunks") |> Enum.any?(fn name -> name == "files_id_1_n_1" end)
161+
162+
assert :ok == Mongo.drop_index(top, "index_test.files", "filename_1_uploadDate_1")
163+
assert :ok == Mongo.drop_index(top, "index_test.chunks", "files_id_1_n_1")
164+
165+
assert false == Mongo.list_index_names(top, "index_test.files") |> Enum.any?(fn name -> name == "filename_1_uploadDate_1" end)
166+
assert false == Mongo.list_index_names(top, "index_test.chunks") |> Enum.any?(fn name -> name == "files_id_1_n_1" end)
167+
168+
_bucket = Bucket.new(top, name: "index_test")
169+
170+
assert Mongo.list_index_names(top, "index_test.files") |> Enum.any?(fn name -> name == "filename_1_uploadDate_1" end)
171+
assert Mongo.list_index_names(top, "index_test.chunks") |> Enum.any?(fn name -> name == "files_id_1_n_1" end)
172+
end
173+
93174
end

0 commit comments

Comments
 (0)