-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
21 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |