Skip to content

Commit 7b92981

Browse files
committed
Add more tests and align them
1 parent 06f48d0 commit 7b92981

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

test/jsonpatch/operation/add_test.exs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,20 @@ defmodule Jsonpatch.Operation.AddTest do
5353
assert %{"a" => [0, 1, 3]} = Operation.apply_op(patch, target)
5454
end
5555

56-
test "Add a value to an empty array" do
56+
test "Add a value to an empty array with binary key" do
5757
patch = %Add{path: "/a/0", value: 3}
5858
target = %{"a" => []}
5959

6060
assert %{"a" => [3]} = Operation.apply_op(patch, target)
6161
end
6262

63+
test "Add a value to an empty array with atom key" do
64+
patch = %Add{path: "/a/0", value: 3}
65+
target = %{a: []}
66+
67+
assert %{a: [3]} = Operation.apply_op(patch, target, keys: :atoms)
68+
end
69+
6370
test "Add a value to an array with invalid index" do
6471
patch = %Add{path: "/a/b", value: 3}
6572
target = %{"a" => [0, 1, 2]}

test/jsonpatch/operation/remove_test.exs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ defmodule RemoveTest do
5454
}
5555

5656
remove_patch = %Remove{path: "/nameX"}
57-
assert {:error, :invalid_path, "nameX"} = Jsonpatch.apply_patch(remove_patch, target)
57+
assert {:error, :invalid_path, "nameX"} = Operation.apply_op(remove_patch, target)
58+
end
59+
60+
test "Remove element in map with atom keys" do
61+
target = %{name: "Ceasar", age: 66}
62+
63+
remove_patch = %Remove{path: "/age"}
64+
65+
assert %{name: "Ceasar"} = Operation.apply_op(remove_patch, target, keys: :atoms)
5866
end
5967

6068
test "Remove element by invalid index" do
@@ -66,7 +74,7 @@ defmodule RemoveTest do
6674
}
6775

6876
remove_patch = %Remove{path: "/hobbies/a"}
69-
assert {:error, :invalid_index, "a"} = Jsonpatch.apply_patch(remove_patch, target)
77+
assert {:error, :invalid_index, "a"} = Operation.apply_op(remove_patch, target)
7078

7179
# Longer path
7280
target = %{
@@ -77,11 +85,11 @@ defmodule RemoveTest do
7785
}
7886

7987
remove_patch = %Remove{path: "/hobbies/b/description"}
80-
assert {:error, :invalid_index, "b"} = Jsonpatch.apply_patch(remove_patch, target)
88+
assert {:error, :invalid_index, "b"} = Operation.apply_op(remove_patch, target)
8189

8290
# Longer path, numeric - out of
8391
remove_patch = %Remove{path: "/hobbies/1/description"}
84-
assert {:error, :invalid_index, "1"} = Jsonpatch.apply_patch(remove_patch, target)
92+
assert {:error, :invalid_index, "1"} = Operation.apply_op(remove_patch, target)
8593
end
8694

8795
test "Return error when patch error was provided to remove operation" do

test/jsonpatch/operation/test_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ defmodule Jsonpatch.Operation.TestTest do
2929
assert ^target = Operation.apply_op(test_op, target)
3030
end
3131

32+
test "Test with atom as key" do
33+
target = %{role: "Developer"}
34+
35+
test_op = %Test{path: "/role", value: "Developer"}
36+
37+
assert ^target = Operation.apply_op(test_op, target, keys: :atoms)
38+
end
39+
3240
test "Fail to test element with path with multiple indices" do
3341
target = %{
3442
"a" => %{

0 commit comments

Comments
 (0)