Skip to content

Commit

Permalink
Add tests for coerce_json_field
Browse files Browse the repository at this point in the history
  • Loading branch information
elias-ba committed Mar 24, 2022
1 parent b5453e0 commit c544f25
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions test/lightning/helpers_test.exs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
defmodule Lightning.HelpersTest do
describe "Helpers" do
import Lightning.Helpers, only: [coerce_json_field: 2]
use ExUnit.Case, async: true

test "coerce a good json string" do
json = %{name: "Sadio Mane", stats: "{'goals':126, 'teams': ['Metz', 'Liverpool']}"}
import Lightning.Helpers, only: [coerce_json_field: 2]

coerced_map = coerce_json_field(json, "stats")
test "coerce_json_field/2 will transform a json string inside a map by it's key" do
input = %{
"body" =>
"{\n \"a\": 1,\n \"b\": {\n \"sadio\": true, \"other\": [1,2,\"bah\"]\n }\n}",
"name" => "My Credential"
}

assert coerced_map = %{
name: "Sadio Mane",
stats: %{goals: 126, teams: ['Metz', 'Liverpool']}
}
end
assert coerce_json_field(input, "body") == %{
"body" => %{"a" => 1, "b" => %{"other" => [1, 2, "bah"], "sadio" => true}},
"name" => "My Credential"
}
end

test "coerce_json_field/2 will not do anything if the json string inside a map is invalid" do
input = %{name: "Sadio Mane", stats: "goals:126, teams: Metz, Liverpool"}

assert coerce_json_field(input, "stats") == %{
name: "Sadio Mane",
stats: "goals:126, teams: Metz, Liverpool"
}
end
end

0 comments on commit c544f25

Please sign in to comment.