DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

MarcΓΈs
MarcΓΈs

Posted on

Elixir - be sorted, not sordid

See code on GitHub

I notice that when devs use libraries like ExMachina to generate fake data for tests, they forget to test for case insensitivity. So they end up with unexpected results like all the uppercase letters appearing before all of the lowercase letters.

iex(1)> Enum.sort(["b", "a", "c", "B", "A", "C"])
["A", "B", "C", "a", "b", "c"]
Enter fullscreen mode Exit fullscreen mode

To help with testing, I've written a helper function to randomize the case of a string.

  def random_case(string) when is_binary(string) do
Enter fullscreen mode Exit fullscreen mode

It can be combined with the [sequence] function (https://hexdocs.pm/ex_machina/ExMachina.html#sequence/1) in a factory

def article_factory do
  %Article{
    title: sequence("Article Title") |> ExMachinaHelper.random_case()
  }
end
Enter fullscreen mode Exit fullscreen mode

There is some overhead in changing the case of strings so only choose the fields related to sorting.

Top comments (0)

Hey 😍

Want to help the DEV Community feel more like a community?

Head over to the Welcome Thread and greet some new community members!

It only takes a minute of your time, and goes a long way!