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:
- Search for
idon database - If sucess, return the id’s related value
- Otherwise create a default data structure and save it on the database under the
idkey
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:
- Am I using doctests wrong?
- There is a way to perform a delete operation after each doctest?
- How did you guys would approach this problem?
Marked As Solved
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
- this seems very ambitious for a doctest.
- 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.
1
qhwa
- No, there’s nothing wrong with it.
- Yes, as mentioned by @ityonemo, Ecto’s sandbox adapter helps. What’s inside your
test/test_helper.exs?
1
Popular in Questions
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
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
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...
New
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
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
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
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
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New
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







