DEV Community

Marcøs
Marcøs

Posted on

2 1

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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay