hauks96

hauks96

Proper unit test mocking for timezone aware DateTime objects

Does anyone have any experience with unit testing modules that need to be tested using DateTime objects with different timezones without having a real timezone database?

I am having trouble understanding how I would mock the timezone database in a way that would allow using methods such as DateTime.new!(Date.t, Time.t, Calendar.time_zone, Calendar.time_zone_database) without failing since the timezone database is not available.

Most Liked

dimitarvp

dimitarvp

Just now saw your edit but it doesn’t change much. You can still make your mock respect timezone offsets.

Alternatively, I think you can just use a proper timezone database and instruct it to not do network requests.

tz has very configurable behaviour on when (or if?) to do automatic updates.

Here’s a link to the docs of tzdata that show you how to disable automatic updates.

dimitarvp

dimitarvp

Intuitively, I’d say just inspect Elixir’s source and see which timezone functions it uses when a timezone database is supplied?

Here’s what Elixir itself does when ultimately it gets down to calling a function from the timezone module: elixir/lib/elixir/lib/calendar/datetime.ex at 1a1eda52b46e37b0c77835e374c16ec64bb5c638 · elixir-lang/elixir · GitHub

Basically it calls its time_zone_periods_from_wall_datetime/2 function (first argument is datetime, second argument is a timezone).

Another example in Elixir itself is how such a timezone database can be faked. Here: elixir/lib/elixir/test/elixir/calendar/fakes.exs at 1a1eda52b46e37b0c77835e374c16ec64bb5c638 · elixir-lang/elixir · GitHub

The link is to the above function but the entire module should be looked at.

You can combine that knowledge with either mock or patch so you have a local offline fake timezone database.

dimitarvp

dimitarvp

Never tried what you need but I’d think that mock or patch will do the job.

Last Post!

LostKobrakai

LostKobrakai

Using past datetimes on non disputed regions should give you a rather stable timezone definition.

If you still need more control then you can use Mox with the time zone database behaviour and mock that.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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

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
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
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
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 44167 214
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
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