Skip to content

Commit

Permalink
Fix bug where an empty map would be stored as an empty array. (#101)
Browse files Browse the repository at this point in the history
* Fix bug where an empty map %{} would be stored as an empty array [].

* Add unit test. Small fix refactor.

* Fix ugly newline in tests.
  • Loading branch information
DanielZendejas authored and michalmuskala committed Dec 18, 2016
1 parent d38f994 commit 35ec641
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/mongo_ecto/conversions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ defmodule Mongo.Ecto.Conversions do
def from_ecto_pk(_value, _pk),
do: :error

defp document(doc, _pk) when is_map(doc) and map_size(doc) == 0,
do: {:ok, %{}}
defp document(doc, pk) do
map(doc, fn {key, value} ->
pair(key, value, pk, &from_ecto_pk(&1, pk))
end)
end

defp document(doc, params, pk) do
map(doc, fn {key, value} ->
pair(key, value, pk, &inject_params(&1, params, pk))
Expand Down
13 changes: 7 additions & 6 deletions test/mongo_ecto/normalized_query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Mongo.Ecto.NormalizedQueryTest do
use Ecto.Model

schema "model" do
field :m, :map
field :x, :integer
field :y, :integer, default: 5
field :z, {:array, :integer}
Expand Down Expand Up @@ -42,7 +43,7 @@ defmodule Mongo.Ecto.NormalizedQueryTest do
test "bare model" do
query = Model |> from |> normalize
assert_query(query, coll: "model", query: %{},
projection: %{_id: true, x: true, y: true, z: true},
projection: %{_id: true, x: true, y: true, z: true, m: true},
opts: [])
assert [{:&, _, _}] = query.fields
end
Expand Down Expand Up @@ -79,11 +80,11 @@ defmodule Mongo.Ecto.NormalizedQueryTest do
assert [{:field, :x, _}, {:field, :y, _}] = query.fields

query = Model |> select([r], [r, r.x]) |> normalize
assert_query(query, projection: %{_id: true, x: true, y: true, z: true})
assert_query(query, projection: %{_id: true, x: true, y: true, z: true, m: true})
assert [{:&, _, _}, {:field, :x, _}] = query.fields

query = Model |> select([r], [r]) |> normalize
assert_query(query, projection: %{_id: true, x: true, y: true, z: true})
assert_query(query, projection: %{_id: true, x: true, y: true, z: true, m: true})
assert [{:&, _, _}] = query.fields

query = Model |> select([r], {1}) |> normalize
Expand All @@ -95,7 +96,7 @@ defmodule Mongo.Ecto.NormalizedQueryTest do
assert [{:field, :id, _}] = query.fields

query = from(r in Model) |> normalize
assert_query(query, projection: %{_id: true, x: true, y: true, z: true})
assert_query(query, projection: %{_id: true, x: true, y: true, z: true, m: true})
assert [{:&, _, _}] = query.fields
end

Expand Down Expand Up @@ -441,7 +442,7 @@ defmodule Mongo.Ecto.NormalizedQueryTest do
query = Model |> insert(x: nil, y: nil, z: nil)
assert_query(query, command: [y: nil, z: nil])

query = Model |> insert(x: 1, y: 5, z: [])
assert_query(query, command: [x: 1, y: 5, z: []])
query = Model |> insert(x: 1, y: 5, z: [], m: %{})
assert_query(query, command: [x: 1, y: 5, z: [], m: %{}])
end
end

0 comments on commit 35ec641

Please sign in to comment.