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/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/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.

Where Next?

Popular in Questions Top

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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

We're in Beta

About us Mission Statement