Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: stringfy_map/1 changes order of Map keys intermittently. #56

Closed
1 task done
nelsonic opened this issue Sep 14, 2023 · 2 comments
Closed
1 task done

Chore: stringfy_map/1 changes order of Map keys intermittently. #56

nelsonic opened this issue Sep 14, 2023 · 2 comments
Assignees
Labels
chore a tedious but necessary task often paying technical debt enhancement New feature or request help wanted Extra attention is needed priority-1 Highest priority issue. This is costing us money every minute that passes. T25m Time Estimate 25 Minutes technical A technical issue that requires understanding of the code, infrastructure or dependencies user-feedback Feedback from people using the App

Comments

@nelsonic
Copy link
Member

nelsonic commented Sep 14, 2023

while the Useful.stringfy_map/1 function works to stringify the Map,
the tests assertions fail intermittently because the map keys are being positioned in different order.
Best explained by the following test failures:

 1) test stringfy_map/1 converts nested maps into strings (UsefulTest)
     test/useful_test.exs:199
     Assertion with == failed
     code:  assert Useful.stringify_map(map) == "data__name: Vitor, data__other__data: info, id: 1"
     left:  "id: 1, data__name: Vitor, data__other__data: info"
     right: "data__name: Vitor, data__other__data: info, id: 1"
     stacktrace:
       test/useful_test.exs:201: (test)

  2) doctest Useful.stringify_map/1 (5) (UsefulTest)
     test/useful_test.exs:4
     Doctest failed
     doctest:
       iex> map = %{name: "alex", data: %{age: 17, height: 185}}
       iex> Useful.stringify_map(map)
       "data__age: 17, data__height: 185, name: alex"
     code:  Useful.stringify_map(map) === "data__age: 17, data__height: 185, name: alex"
     left:  "name: alex, data__age: 17, data__height: 185"
     right: "data__age: 17, data__height: 185, name: alex"
     stacktrace:
       lib/useful.ex:142: Useful (module)

  3) test stringfy_map/1 converts nested lists into strings (UsefulTest)
     test/useful_test.exs:204
     Assertion with == failed
     code:  assert Useful.stringify_map(map) == "data__names: \"Vitor, alex\", id: 1"
     left:  "id: 1, data__names: \"Vitor, alex\""
     right: "data__names: \"Vitor, alex\", id: 1"
     stacktrace:
       test/useful_test.exs:206: (test)

This isn't really a "bug" per se. More of an annoying quirk that we need to fix.
Again, the function works but we want the assertions to be 100% consistent.
Therefore ...

Todo

  • Alphabetise the Map keys before stringifying them.
@nelsonic nelsonic added enhancement New feature or request help wanted Extra attention is needed chore a tedious but necessary task often paying technical debt priority-1 Highest priority issue. This is costing us money every minute that passes. T25m Time Estimate 25 Minutes user-feedback Feedback from people using the App technical A technical issue that requires understanding of the code, infrastructure or dependencies labels Sep 14, 2023
@nelsonic nelsonic self-assigned this Sep 14, 2023
@nelsonic
Copy link
Member Author

The function definition was:

useful/lib/useful.ex

Lines 149 to 154 in 370a032

def stringify_map(map) when is_map(map) do
map
|> flatten_map()
|> Enum.map(&stringify_tuple/1)
|> Enum.join(", ")
end

Adding the |> Enum.sort() after the call to flatten_map() did the trick:

  def stringify_map(map) when is_map(map) do
    map
    |> flatten_map()
    |> Enum.sort()
    |> Enum.map(&stringify_tuple/1)
    |> Enum.join(", ")
  end

Ref: https://stackoverflow.com/questions/30627233/sort-list-elements-in-elixir

@nelsonic
Copy link
Member Author

Included in PR: #57 :shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore a tedious but necessary task often paying technical debt enhancement New feature or request help wanted Extra attention is needed priority-1 Highest priority issue. This is costing us money every minute that passes. T25m Time Estimate 25 Minutes technical A technical issue that requires understanding of the code, infrastructure or dependencies user-feedback Feedback from people using the App
Projects
Status: Done
Status: Done
Development

No branches or pull requests

1 participant