I have a list of maps that may in the future have multiple different keys-pair values inside and the key that I am looking for may not exist in each map, this right here is my current problem. I can currently get the correct values when the key does exist inside a map, but when it doesn’t exist I get an error. I will show below.
def get_company_case_totals(cases) do
cases
|> Enum.map( fn %{casetype: c} -> c end)
end
The Test I am developing from
test "get_company_case_totals/1 with dummy case list" do
dummy_data = [
%{casetype: "dummy1"},
%{casetype: "dummy1"},
%{casetype: "dummy1"},
%{casetype: "dummy2"},
%{casetype: "dummy2"},
%{casetype: "dummy2"},
%{casetype: "dummy3"},
%{casetype: "dummy3"},
%{casetype: "dummy4"},
%{casetype: "dummy1"},
%{casetype: "dummy1"},
]
correct_resp = %{"dummy1" => 5, "dummy2" => 3, "dummy3" => 2, "dummy4" => 1}
fake_type = dummy_data ++ %{faketype: "dummy1"}
assert Reporting.get_company_case_totals(dummy_data) == correct_resp
assert Reporting.get_company_case_totals(fake_type) == correct_resp
end
So the first assert works correctly because each map in the list contains the casetype: key, but when I add the last map i get this error when running Mix Test.
test 'tablename' get_company_case_totals/1 with dummy case list (PhoenixApi.ReportingTest)
test/phoenixApi/reporting_test.exs:165
** (FunctionClauseError) no function clause matching in Enum."-map/2-lists^map/1-0-"/2
The following arguments were given to Enum."-map/2-lists^map/1-0-"/2:
# 1
#Function<5.56924159/1 in PhoenixApi.Reporting.get_company_case_totals/2>
# 2
%{faketype: "dummy1"}
code: assert Reporting.get_company_case_totals(fake_type) == %{"dummy1" => 5, "dummy2" => 3, "dummy3" => 2, "dummy4" => 1}
stacktrace:
(elixir 1.11.2) lib/enum.ex:1399: Enum."-map/2-lists^map/1-0-"/2
(elixir 1.11.2) lib/enum.ex:1399: Enum."-map/2-lists^map/1-0-"/2
(phoenixApi 0.1.0) lib/phoenixApi/contexts/reporting.ex:231: PhoenixApi.Reporting.get_company_case_totals/1
test/phoenixApi/reporting_test.exs:183: (test)
At the end of the day I wish to be able to go through a list of maps and only retrieve the values with the key ‘casetype:’ but because there is no key in the map passed to Enum.map it throws the above error.
Thank you for reading
The solved solution:
def get_company_case_totals(cases) do
cases
|> Enum.filter(& &1[:casetype])
|> Enum.map( fn %{casetype: c} -> c end)
end