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

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
clayschick
I'm trying to create a simple query to select distinct values from a column. If I only use select and distinct I get back a list of uniqu...
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
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement