ollien

ollien

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!

First 3 of 3 Posts Switch mode

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

Marcus

Marcus

Welcome @ollien

You could try to start timex in your test/test_helper.exs with Application.ensure_all_started(:timex).

ollien

ollien OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement