Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to have fixtures per test in Elixir?

Background

I have an OTP application that reads data from json files. Depending on what these files have, the application may or may not modify them with new data.

Problem

The challenge here arrives when I am creating integration/e2e tests. I want to test the entire flow.

Normally I have my fixtures in tests/fixtures/my_file.json or something amongst those lines. I want my tests to run with async: true, so this creates a problem:

  • because my fixture file is static, different processes cannot read/write to the fixture file without causing race condition issues and messing up other tests
  • i also have to forceably reset the fixture file every time I run a test
  • becasue I have to use async: false this will cause my tests to be slow

Possible solution

I could in theory, create a fixture file per test running. I would have to spice the file name with a seed (like a random number) and then have the process know in advance which file it has to use.

This sounds to me like overly compex and would force me to have to rewrite a good chunk of the application, since I am only using the normal config/test.exs to tell where the files are located, and as far as I know this cannot be dynamic (at least not to the level I require):

config :store,
  products: Path.expand("#{__DIR__}/../apps/store/test/fixtures/products.json") |> Path.split(),
  setup: Path.expand("#{__DIR__}/../apps/store/test/fixtures/setup.json") |> Path.split(),

Questions

Given that the only solution I can think of seems rather complex, I would like to know if:

  • is there a standard way in Elixir to achieve dynamic fixtures for each test process?
  • are there any libraries that can help?
  • can you recommend any material (blogs, videos, etc) that deal with problems similar to this?

Ideally I am looking for something like bypass, which allows to async: true as well.

Marked As Solved

LostKobrakai

LostKobrakai

Yes, but with many processes involved you need to deal with Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.12.1 as well. There’s a whole lot of machinery involved to make this injection of state happening (especially if it’s implicit). Caller lookups are the backbone of most mocking libraries in elixir as well.

Things will be way simpler if you don’t need the implicit part and explicitly provide configuration by passing it down the process tree / into processes. You can still do the following in your Application.start/2 callback to use config to provide those values when processes are started there.

children = [
   {Child, Application.get_env(…)}
]

Also Liked

eahanson

eahanson

For tests that will be modifying a fixture file, maybe you could make a copy of the fixture file in a temp dir and have the test use that (ExUnit has a @tmp_dir tag).

LostKobrakai

LostKobrakai

Are you really testing at the “application” level? Because you cannot have multiple instances of an application running at the same time on the beam, so the requirement to be able to use async true would be moot.

If you’re not testing this at the application level, but at the process level I’d suggest passing the paths to the files in question as part of their startup, not having them depend on global state like the application environment.

Where Next?

Popular in Questions Top

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
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 record...
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
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
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
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
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
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
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

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement