jyotigautam

jyotigautam

Testing an elixir function using Mock

I have a Product schema which has a UPI(unique product identifier) eg. A985748BNG6784C . This is an autogenerated unique product identifier.

I have a function upi_generate() which calls another external function gen_nano_id() to generate this random unique id.

If by chance, the id generated by gen_nano_id() has already been generated, the function upi_generate() calls itself recursively till the time gen_nano_id() generates a unique id. Thus generating a unique UPI .

gen_nano_id() can sometimes return duplicate ids and for that purpose I have written the below code with recursive call.

  def gen_nano_id() do  #external function
      Nanoid.generate(10, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  end

  # TODO: write test case for this
  def upi_generate() do # required function
      upi = "A" <> gen_nano_id() <> "C"

      case get_product_with_upi(upi) do
         nil ->
            upi 
         _ ->
            upi_generate()
      end
  end

  # Check if product with upi already exists
  defp get_product_with_upi(upi) do
      from(p in "snitch_products", select: p.upi, where: p.upi == ^upi) 
      |> Repo.one()
  end

Now, I have to test the id regeneration logic for duplicate ids.

My testing approach involves following logic. Create two products with duplicate UPI and try to reach the _ part of the case comparison.

For this purpose, I have mocked(I don’t control the behaviour of this function) the gen_nano_id() .

Now, the problem that I am facing is mocking results in creation of always the same ids no matter what and I go in an infinite loop.

I am not able to figure out a way to reach the exit condition(nil) part of case comparison with this mocking approach of gen_nano_id .

Marked As Solved

dimitarvp

dimitarvp

First, here’s a stateful Agent that holds a counter and a simple logic that forces it to return the same value several times, before starting to return random values afterwards. Like this:

defmodule NanoidMock do
  @moduledoc ~S"""
  Mocks Nanoid value producer with a simple counter.
  Initialize with `start_link(0)` to reproduce the following:
  For the first 4 invocations of `get_nano_id` the counter
  will move from 0 to 3 and will always return 0.
  After 4 invocations of `get_nano_id` (when the counter
  reaches 4) and onwards, it will return a random number.
  """

  use Agent

  # Initialize with a non-negative integer counter.
  def start_link(val) when val >= 0 do
    Agent.start_link(fn -> val end, name: __MODULE__)
  end

  def stop(), do: Agent.stop(__MODULE__)

  def clear(), do: Agent.update(__MODULE__, fn(_) -> 0 end)

  def get_nano_id() do
    new_val = Agent.get_and_update(__MODULE__, fn(x) -> {x, x + 1} end)
    case div(new_val, 4) do
      0 -> 0
      _ -> :rand.uniform(1_000_000)
    end
  end
end

Then let’s compose a test class:

defmodule NanoidTestWithMock do
  def get_nano_id(), do: NanoidMock.get_nano_id()

  def upi_generate() do
    upi = "A#{get_nano_id()}C"

    case get_product_with_upi(upi) do
      nil -> upi
      _   -> upi_generate()
    end
  end

  defp get_product_with_upi(upi) do
    case upi do
      "A0C" ->
        IO.puts "Duplicated UPI=A0C. Retrying."
        %{error: "product already exists"}

      _ ->
        nil
    end
  end

  def test() do
    NanoidMock.start_link(0)
    upi = upi_generate()
    NanoidMock.stop()
    IO.puts "Successfuly got unique UPI=#{upi}"
  end
end

Put these modules in a single Elixir file and then run NanoidTestWithMock.test() in an iex session.

(Or add the above expression at the bottom of the file and just tell Elixir to run it.)

You should see something like this:

Duplicated UPI=A0C. Retrying.
Duplicated UPI=A0C. Retrying.
Duplicated UPI=A0C. Retrying.
Duplicated UPI=A0C. Retrying.
Successfuly got unique UPI=A394747C

Hope that helps.

EDIT: As for mocking the Nanoid library itself, I’ll leave that exercise to you.

Also Liked

gausby

gausby

Don’t mock it; instead pass in the random uuid as an argument; have it return something like {:error, :uuid_already_in_use} in the function generating the upi so you can try with a new one if the UUID should already be taken.

In general I think it is a good idea to pass this kind of data into functions, including timestamps, as it will make testing a lot easier.

jyotigautam

jyotigautam

Thanks for suggesting the approach. This nicely tests the scenario of duplicate UPIs.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement