Skip to content

Commit fa6fe94

Browse files
committed
added more tests
1 parent 6bacd69 commit fa6fe94

File tree

6 files changed

+55
-26
lines changed

6 files changed

+55
-26
lines changed

coveralls.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"skip_files": [
3+
"lib/bson/utils.ex",
4+
"lib/mongo/binary_utils.ex"
5+
]
6+
}

lib/bson.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ defmodule BSON do
1414
"""
1515
@spec encode(document) :: iodata
1616
def encode(map) when is_map(map) do
17-
BSON.Encoder.encode(map)
17+
case Map.has_key?(map, :__struct__) do
18+
true -> BSON.Encoder.encode(Map.to_list(map))
19+
false -> BSON.Encoder.encode(map)
20+
end
1821
end
1922

2023
def encode([{_, _}|_] = keyword) do

lib/bson/encoder.ex

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,10 @@ defmodule BSON.Encoder do
9898
def document(doc) do
9999
{_, iodata} =
100100
Enum.reduce(doc, {:unknown, ""}, fn
101-
{:__struct__, _value}, {:binary, _acc} ->
102-
invalid_doc(doc)
103-
104-
{:__struct__, _value}, {_, acc} ->
105-
{:atom, acc}
106-
107-
{key, _value}, {:binary, _acc} when is_atom(key) ->
108-
invalid_doc(doc)
109-
110-
{key, _value}, {:atom, _acc} when is_binary(key) ->
111-
invalid_doc(doc)
112-
101+
{:__struct__, _value}, {:binary, _acc} -> invalid_doc(doc)
102+
{:__struct__, _value}, {_, acc} -> {:atom, acc}
103+
{key, _value}, {:binary, _acc} when is_atom(key) -> invalid_doc(doc)
104+
{key, _value}, {:atom, _acc} when is_binary(key) -> invalid_doc(doc)
113105
{key, value}, {_, acc} ->
114106
{key_type, key} = key(key)
115107
type = type(value)
@@ -122,10 +114,8 @@ defmodule BSON.Encoder do
122114

123115
defp cstring(string), do: [string, 0x00]
124116

125-
defp key(value) when is_atom(value),
126-
do: {:atom, cstring(Atom.to_string(value))}
127-
defp key(value) when is_binary(value),
128-
do: {:binary, cstring(value)}
117+
defp key(value) when is_atom(value), do: {:atom, cstring(Atom.to_string(value))}
118+
defp key(value) when is_binary(value), do: {:binary, cstring(value)}
129119

130120
defp array([], _ix),
131121
do: []

lib/mongo/query.ex

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ defmodule Mongo.Query do
22
@moduledoc """
33
This is the query implementation for the Query Protocoll
44
5-
The action attribute describes as atom the desired action. There are currently two
6-
  * :command
7-
  * :wire_version
8-
    
95
  Encoding and decoding does not take place at this point, but is directly performed
106
into the functions of Mongo.MongoDBConnection.Utils.
117
"""
128
defstruct action: nil
139
end
1410

1511
defimpl DBConnection.Query, for: Mongo.Query do
16-
def parse(query, _opts), do: query
17-
def describe(query, _opts), do: query
12+
# coveralls-ignore-start
13+
def parse(query, _opts), do: query # gets never called
14+
def describe(query, _opts), do: query # gets never called
15+
# coveralls-ignore-stop
1816
def encode(_query, params, _opts), do: params
19-
def decode(_query, :ok, _opts), do: :ok
20-
def decode(_query, wire_version, _opts) when is_integer(wire_version), do: wire_version
2117
def decode(_query, reply, _opts), do: reply
2218
end

test/bson_test.exs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
defmodule TestUser do
2+
defstruct name: "John", age: 27
3+
end
4+
15
defmodule BSONTest do
26
use ExUnit.Case, async: true
37

@@ -75,6 +79,19 @@ defmodule BSONTest do
7579
@map21 %{"q" => %BSON.Binary{binary: <<1,2,3>>, subtype: :binary_old}}
7680
@bin21 <<20, 0, 0, 0, 5, 113, 0, 7, 0, 0, 0, 2, 3, 0, 0, 0, 1, 2, 3, 0>>
7781

82+
@map22 %{"regex" => %BSON.Regex{pattern: "acme.*corp", options: "i"}}
83+
@bin22 <<25, 0, 0, 0, 11, 114, 101, 103, 101, 120, 0, 97, 99, 109, 101, 46, 42, 99, 111, 114, 112, 0, 105, 0, 0>>
84+
85+
@map23 %{"number" => %BSON.LongNumber{value: 123}}
86+
@bin23 <<21, 0, 0, 0, 18, 110, 117, 109, 98, 101, 114, 0, 123, 0, 0, 0, 0, 0, 0, 0, 0>>
87+
88+
@map24 %{"number" => Decimal.new("0.33")}
89+
@bin24 <<29, 0, 0, 0, 19, 110, 117, 109, 98, 101, 114, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 48, 0>>
90+
91+
@map25 %TestUser{}
92+
@bin25 <<29, 0, 0, 0, 16, 97, 103, 101, 0, 27, 0, 0, 0, 2, 110, 97, 109, 101, 0, 5, 0, 0, 0, 74, 111, 104, 110, 0, 0>>
93+
94+
7895
test "encode" do
7996
assert encode(@map1) == @bin1
8097
assert encode(@map2) == @bin2
@@ -97,6 +114,10 @@ defmodule BSONTest do
97114
assert encode(@map19) == @bin19
98115
assert encode(@map20) == @bin20
99116
assert encode(@map21) == @bin21
117+
assert encode(@map22) == @bin22
118+
assert encode(@map23) == @bin23
119+
assert encode(@map24) == @bin24
120+
assert encode(@map25) == @bin25
100121
end
101122

102123
test "decode" do
@@ -121,6 +142,9 @@ defmodule BSONTest do
121142
assert decode(@bin19) == @map19
122143
assert decode(@bin20) == @map20
123144
assert decode(@bin21) == @map21
145+
assert decode(@bin22) == @map22
146+
assert decode(@bin23) == %{"number" => 123}
147+
assert decode(@bin24) == @map24
124148
end
125149

126150
test "keywords" do
@@ -181,6 +205,16 @@ defmodule BSONTest do
181205
assert encode(@mapNegInf) == @binNegInf
182206
end
183207

208+
test "mixing atoms with binaries" do
209+
document = 1..33 |> Enum.reduce(%{}, fn(x, acc) -> Map.put(acc,to_string(x),x) end ) |> Map.put(:a, 10)
210+
assert_raise ArgumentError, fn -> encode(document) end
211+
document = %{:key => "value", "id" => 10}
212+
assert_raise ArgumentError, fn -> encode(document) end
213+
document = 1..33 |> Enum.reduce(%{}, fn(x, acc) -> Map.put(acc,to_string(x),x) end ) |> Map.put(:__struct__, TestUser)
214+
assert_raise ArgumentError, fn -> encode(document) end
215+
216+
end
217+
184218
defp encode(value) do
185219
value |> BSON.encode |> IO.iodata_to_binary
186220
end

test/mongo/cursor_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule Mongo.CursorTest do
1818
docs = Stream.cycle([%{foo: 42}]) |> Enum.take(100)
1919

2020
assert {:ok, _} = Mongo.insert_many(c.pid, coll, docs)
21-
assert [%{"foo" => 42}, %{"foo" => 42}] = Mongo.find(c.pid, coll, %{}, limit: 2) |> Enum.to_list |> Enum.map(fn m -> Map.pop(m, "_id") |> elem(1) end)
21+
assert [%{"foo" => 42}, %{"foo" => 42}] = Mongo.find(c.pid, coll, %{}, limit: 2)|> Enum.to_list |> Enum.map(fn m -> Map.pop(m, "_id") |> elem(1) end)
2222
end
2323

2424
# issue #35: Crash executing find function without enough permission

0 commit comments

Comments
 (0)