fuelen

fuelen

I finally found time to write down my journey away from ExMachina, including the workaround I tried first and the API experiments that followed.

Showing Posts 1 to 5

rhcarvalho

rhcarvalho

Thanks for sharing!

“The problem is the model itself, which looks good in Hello World examples but forces you to work around it in a real business application.”
From Database Factories (Like ExMachina) to Business State - DEV Community.

I was surprised that at the end you replaced something that created loose relations (ExMachina) with something that creates eagerly tight relations (SeedFactory) :slight_smile:

My experience is that sometimes you need one behavior, sometimes the other, and sometimes something in between.

The explicit state construction you had in the middle of the article felt like the best way to demonstrate intent in the tests!

Building on one of your examples, in the same test file you may want to construct a project such that owner and manager users are the same, and another one where they are different. You may even need to test some rule related to their relative attributes (e.g. say that if the manager is newer than the owner then something special applies to the project).

Why not leave the decision of how to construct state to be a local decision per-project, per-test/describe/module?

mudasobwa

mudasobwa

Creator of Cure

As a big fan of property-based testing, I use my estructura library to generate data.

fuelen

fuelen OP

I would not call it loose vs tight, rather invalid vs valid. The tight graph is a side effect of reusing what is already in the context, not the goal.

What to construct is still decided in the test. The schema centralizes how and gives a sane default, so the part a test does not care about is not repeated in every test. You decide what that default is, and it should be the graph that repeats most in your application. Where a test needs another branch, it goes there with exec.

produce is built on top of exec, and they are meant to be mixed. produce is for “give me a project, any project”. Where the order or the specifics matter, you write exec:

# owner and manager are the same user
ctx = produce(ctx, :project)

# different users of the same organization
ctx =
  ctx
  |> produce(user: :owner)
  |> produce(user: :manager)

ctx = exec(ctx, :create_project, owner: ctx.owner, manager: ctx.manager)

Your example with the manager newer than the owner goes the same way, with the dates as parameters to exec. When a variation repeats across tests, it becomes a trait. A trait is an alias for an exec call, so a test can write produce(ctx, user: [:registered_early, as: :owner]) instead.

For the “something in between”, the context is a plain map, so forking it means using the same variable twice:

base = produce(ctx, :organization)

ctx_a = produce(base, :project)
ctx_b = produce(base, :project)

Both projects share the organization and nothing else

rhcarvalho

rhcarvalho

Thanks. The additional examples also made realize another nice aspect of your solution: explicit context, so two tests don’t share a global execution order (which would be flaky), right?

:purple_heart:

fuelen

fuelen OP

Right, tests can be async.

— All posts loaded —

Where Next? Top

Trending in Blog Posts Top

bartblast
My first real blog post is up! Got five minutes? Come break some stuff. I put five demos in it, and they’re meant to be played with. Kill...
New
T0ha666
I run a kanban of AI coding agents, and one afternoon the board showed three tasks running while the profile page on the same screen said...
New
danschultzer
I wrote a post on how to implement a feature flag system and A/B testing in an Elixir app with no need for third-party services or librar...
New
videsnelson
Following up on Making `dns_erlang` fast: binary tricks for the BEAM, there’s our second post in our series on making erldns, an authorit...
New
danschultzer
A small quality-of-life improvement to my blog that makes it easier to keep technical posts up to date with a revision history. https://...
New
danschultzer
Wrote a post about how I set up HTML email templates in Phoenix using the template renderer with Gettext and verified routes, CSS inlinin...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews