slouchpie

slouchpie

Bypassing backend validation in :test env to make integration tests pass - how to avoid this hack?

I am writing a graphql API for a restaurant. It allows clients to place orders. There are lots of integration tests that post graphql queries the API.

Tonight, I added the concept of “opening hours” and a test that posts a graphql “mutation” (a HTTP POST) is now failing, because the backend is checking DateTime.utc_now against the “opening hours” for the actual test time and deciding (correctly) that the business is closed.

Everything I read (tonight) about this sort of problem, talks about the functional way - directly injecting my own module to get utc_now. However, this is impossible for a request-response test of the API (you cannot send DateTime.utc_now over the network).

So, I went with this approach:

  1. I tested the is_business_open logic on its own, in a functional way.
  2. I got the API integration tests to pass by actually putting this in my code:
def validate_request_time(changeset) do
  if Mix.env == :test do
    changeset
  else
    if is_business_open() do
      changeset
    else
      put_error(changeset, :closed, "go to bed stupid")
    end
  end
end

Is this a bad idea? Please give me your unfiltered opinions/thoughts about this. Maybe somebody here has had a similar problem with real time in integration tests.

Marked As Solved

al2o3cr

al2o3cr

Like others are posting: Mix.env won’t be available at runtime in production if you’re using the standard release mechanism.

You could get a similar result by setting an application config to turn off time checking.

OR

You could approach the problem from the other direction: the test is failing because you’re not being sufficiently specific about what it needs from its setup. It doesn’t need “a business”, it needs “a business open now”. Fix that and the test will pass without any special-casing.

Also Liked

slouchpie

slouchpie

Both answers so far merit being marked as “Solution”. I don’t know which to select.

EDIT: I selected the solution as being the one telling me to give my test what it wants. However the info about the “compile-time attribute” is very helpful too. I just think the “re-examine your test setup” is the more correct path.

CONCLUSION: It was so much clearer in the morning light. I changed my “opening hours seeds” to accept “default hours” arg with default value and just changed this to be now - 200 seconds and now + 200 seconds in the shared test setup for these api tests.

Thanks again for the info about Mix.env not being available at runtime in releases. I heard this before but had forgotten about it.

ityonemo

ityonemo

This is a bad idea because if you do a release, the Mix module won’t be available.

You should do a dependency injection on DateTime with Mox if you can. If you can’t, if you can hoist the boolean check, that’s better. So make a compile-time attribute @assume_open Mix.env == :test

And make your if statement if @assume_open || business_open?() do...

As a side note, idiomatically is_ functions should be guards, and making your function business_open? is more idiomatic.

Where Next?

Popular in Questions 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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New

Other popular topics 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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement