jyotigautam

jyotigautam

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 .

Showing Posts 1 to 3

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.

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.

jyotigautam

jyotigautam OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews