ollien

ollien

Using Timex in a unit test

I run my unit tests with mix test --no-start, as I don’t want my application to start when I run my unit tests. However, I also want to test some code that uses Timex for a calculation. If I naively attempt to use Timex in my test, I get the following

    test "can get next instance of time in a trivial instance" do
      next_occurrence =
        Util.Time.get_next_occurrence_of_time(~D[2022-01-01], ~T[13:37:00], "America/New_York")

      assert next_occurrence == Timex.to_datetime({{2022, 1, 1}, {13, 37, 0}}, "America/New_York")
    end
  1) test get_next_occurrence_of_time can get next instance of time in a trivial instance (PillminderTest.Util.Time)
     test/pillminder/util/time_test.exs:8
     ** (ArgumentError) errors were found at the given arguments:
     
       * 1st argument: the table identifier does not refer to an existing ETS table
     
     code: assert next_occurrence == Timex.to_datetime({{2022, 1, 1}, {13, 37, 0}}, "America/New_York")
     stacktrace:
       (stdlib 3.17.2.1) :ets.lookup(:tzdata_current_release, :release_version)
       (tzdata 1.1.1) lib/tzdata/release_reader.ex:74: Tzdata.ReleaseReader.current_release_from_table/0
       (tzdata 1.1.1) lib/tzdata/release_reader.ex:17: Tzdata.ReleaseReader.simple_lookup/1
       (tzdata 1.1.1) lib/tzdata/release_reader.ex:9: Tzdata.ReleaseReader.zone_and_link_list/0
       (tzdata 1.1.1) lib/tzdata.ex:61: Tzdata.zone_exists?/1
       (timex 3.7.9) lib/timezone/timezone.ex:230: Timex.Timezone.name_of/1
       (timex 3.7.9) lib/timezone/timezone.ex:262: Timex.Timezone.get/2
       (timex 3.7.9) lib/timezone/timezone.ex:585: Timex.Timezone.convert/2
       (timex 3.7.9) lib/datetime/erlang.ex:46: Timex.Protocol.Tuple.to_datetime/2
       test/pillminder/util/time_test.exs:12: (test)

I can understand why this might be; tzdata likely needs to be started in order to read from this ETS table. I’m not sure the best way to fix this, because

  1. If I remove --no-start, my application will start, which has side effects I don’t want to produce just by running my unit tests.
  2. Even if I somehow prevent my application from running (is there a way?), but allow Timex to run tzdata, I’m still going to have side effects in my tests, Timex will need to update the timezone database before running my tests; I try to keep my unit tests free of network calls wherever I can.

How can I best deal with this? Is perhaps there some way I can “mock” the tzdata, or somehow pre-seed it for my unit tests?

Thanks!

Marked As Solved

ollien

ollien

Thanks for the suggestions! Looking at the tzdata docs it turns out you can also store the ets table locally, so I checked that into a testdata directory (originally in ./deps/tzdata/priv) and then added the following to test.exs

import Config

config :tzdata,
  data_dir: "./test/testdata/tzdata",
  autoupdate: :disabled,
  # Definitely a hack, but tzdata uses this key to determine which http client it uses, so if it
  # actually tries to use hackney, it will get an error
  http_client: nil

and made added Application.ensure_all_started(:tzdata) to my test setup.

Also Liked

trisolaran

trisolaran

Hi @ollien and welcome!

You should be able to turn off tzdata automatic updates in your tests by setting:

config :tzdata, :autoupdate, :disabled

in your test.exs config (GitHub - lau/tzdata: tzdata for Elixir. Born from the Calendar library. · GitHub)

Btw: you can now do time zone conversions directly in Elixir with the tzdata library without Timex: GitHub - lau/tzdata: tzdata for Elixir. Born from the Calendar library. · GitHub

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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

Other popular topics Top

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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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 records...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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

We're in Beta

About us Mission Statement