oliveiragahenrique

oliveiragahenrique

Doctest that perform I/O operations

I’m building a very simple application, every time you call ModuleX.get(id) the system perform the folowing actions:

  1. Search for id on database
  2. If sucess, return the id’s related value
  3. Otherwise create a default data structure and save it on the database under the id key

The problem is my docTests are failing because I use the same id on all of them. Because of it, operations on former tests impact on the result of the later ones, because of the data was persisted

Questions:

  1. Am I using doctests wrong?
  2. There is a way to perform a delete operation after each doctest?
  3. How did you guys would approach this problem?

Marked As Solved

oliveiragahenrique

oliveiragahenrique

I managed to make it work using the setup macro as following:

  setup do
    # Get the pids of all currently alive processes
    accounts_used_pids =
      DynamicSupervisor.which_children(Account.Cache)
      |> Stream.map(fn entry ->
        case entry do
          {_, pid, :worker, [Account.Server]} -> pid
          _ -> nil
        end
      end)
      |> Enum.filter(fn ele -> ele !== nil end)
    
    # Terminate all processes
    Enum.each(accounts_used_pids, &Process.exit(&1, :clean_up))
    
    # Reset the "database"
    File.rm_rf(Account.Database.folder_path())
    File.mkdir_p!(Account.Database.folder_path())

    :ok
  end

The setup macro runs the code inside it after each test case. So I clean up the database and terminate all processes related to the account, after every test case.

It might not be the optimal solution, but work for me! Thank you so much for the help!

Also Liked

ityonemo

ityonemo

  1. this seems very ambitious for a doctest.
  2. are you using ecto sandboxes? I’m not 100% sure, but It should clean up after each doctest group. Try separating each of your cases into distinct ``` groups.
qhwa

qhwa

  1. No, there’s nothing wrong with it.
  2. Yes, as mentioned by @ityonemo, Ecto’s sandbox adapter helps. What’s inside your test/test_helper.exs ?

Where Next?

Popular in Questions Top

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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

Other popular topics 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
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
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
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