IwoHerka

IwoHerka

How to approach property-based testing for features which heavily rely on the database?

Let’s say I’m writing a (rather typical) application using Ecto, which heavily relies on a database.
Most features, let’s say, read from or write to the database. Let’s say I have feature “foo” which reads from multiple tables, does some non-trivial transformations and splits out the result. Let’s also say this feature has a simple interface in form of a single public function foo/1.

How to approach property-testing this function?

My approach, so far, is as follows (I’m using PropCheck):

  • Write some generators
  • Setup test framework (ExUnit + Ecto.Adapters.SQL.Sandbox)
  • Spin forall in a single test
  • Seed generated data into the database
  • Perform the test
  • Repeat last three steps X number of times

In code:

property "bla bla bla" do
  forall dataset <- Gen.foo_dataset() do
    Repo.transaction(fn ->
      seed(dataset)
     
      # perfom the test
      assert ... = foo(...)
     
      Repo.rollback(:test_end)
    end)
  end
end

(I’m skipping some non-essential bits)

The problems are:

  • a lot of data is generated, so tests are very expensive due to all the database inserts
  • Ecto sandbox is useless here because I’m inside a property (which basically is a loop) and transaction must be used to rollback data at the end of the test

I’m relatively new to Elixir, so I’m wondering, is this acceptable approach? Can this be done better conceptually?

Thanks!

Most Liked

sodapopcan

sodapopcan

Is there are any business logic happening the database itself? If not, I would decouple storage from the processing code and unit test the processing code with properties. Then have simple integration tests that interacts with the db.

If you’re concerned with making things public for testing purposes, this is where module hierarchy can come into play. Undocumented modules (@moduledoc false) are conventionally considered private. You can have public foo facade function and break stuff up undernearth into modules. If you want safety and explicitness around this, there is a brilliant library called Boundary.

If your db does do some business logic then I’m sorry I don’t have a good suggestion and am interested in this myself :slight_smile: I’m wondering if there is a way to combine mocks with properties so you could mock the database :thinking: This is something I haven’t looked into.

LostKobrakai

LostKobrakai

You can still use the sandbox for that. You need to manually tell it when to run though: Possibility to give forall a cleanup hook? · Issue #169 · alfert/propcheck · GitHub

a8t

a8t

You might want to pick a random subset of the data for local dev, and only in CI run all the inserts.

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New

We're in Beta

About us Mission Statement