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

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
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

Other popular topics Top

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 29377 241
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement