Skip to content

Commit ee333d5

Browse files
committed
fix: improved the spec of the function to respect nil values (reported by eseres)
1 parent 83b1955 commit ee333d5

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/mongo/results.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ defmodule Mongo.FindAndModifyResult do
7373
"""
7474

7575
@type t :: %__MODULE__{
76-
value: BSON.document(),
76+
value: BSON.document() | nil,
7777
matched_count: non_neg_integer(),
78-
upserted_id: String.t(),
78+
upserted_id: String.t() | nil,
7979
updated_existing: boolean()
8080
}
8181

test/mongo/find_one_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,29 @@ defmodule Mongo.FindOneTest do
1616
assert %{"name" => "Oska"} == Mongo.find_one(top, coll, %{}, sort: %{age: 1}) |> Map.take(["name"])
1717
assert %{"name" => "Tom"} == Mongo.find_one(top, coll, %{}, sort: %{age: -1}) |> Map.take(["name"])
1818
end
19+
20+
test "find_one_and_update", %{pid: top} do
21+
coll = unique_collection()
22+
Mongo.insert_one(top, coll, %{name: "Greta", age: 10})
23+
24+
assert {:ok,
25+
%Mongo.FindAndModifyResult{
26+
value: %{
27+
"_id" => _id,
28+
"age" => 10,
29+
"name" => "Greta"
30+
},
31+
matched_count: 1,
32+
upserted_id: nil,
33+
updated_existing: true
34+
}} = Mongo.find_one_and_update(top, coll, %{name: "Greta"}, %{"$set": %{age: 14}})
35+
36+
assert {:ok,
37+
%Mongo.FindAndModifyResult{
38+
value: nil,
39+
matched_count: 0,
40+
upserted_id: nil,
41+
updated_existing: false
42+
}} = Mongo.find_one_and_update(top, coll, %{name: "Greta-2"}, %{"$set": %{age: 14}})
43+
end
1944
end

0 commit comments

Comments
 (0)