I am trying to create a test case to reliably handle the case where there is a collision for a value in the database. When the user performs some action in my application a random 12 digit number is saved to the database attached to that action. The number is padded to 12 digits if it isn't the full length. If that number already exists in the database then the program picks another random number.
This probably sounds dumb but the expected user base is likely going to be less than 100-1000 users.
My code looks something like
def gen_random_unique() do
unique = (:rand.uniform(1_000_000_000_000) - 1)
|> Integer.to_string()
|> String.pad_leading(12, ["0"])
case get_from_database(unique) do
nil ->
unique
_ ->
gen_random_unique()
end
end
Apart from testing the second condition millions of times until it passes, is there an easier way in Elixir to force that path? Since the function is recursive I'm not sure how I could mock it only on the first call (if that's the path I need to take.)
Thank you!
1_000_000_000_000
and in tests pass it a smaller value, e.g.100
. – Dogbert