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"]

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

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

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

Discussion (0)