mirth

mirth

It’s kind of complicated question and possible something wrong with my own code but I almost gave up.
tldr: in dev version of phoenix app python microservice can fetch the row from postgres table, while in test – it can’t

In the Elixir app I have the test where i calling the :create method:

test "successfully add new review source", %{
   ...
 conn = Accounts.sign_in_user(conn, user)
 conn = post conn, review_source_path(conn, :create, review_source: %{
   ...
 })

the :create method:

def create(conn, %{"review_source" => review_source_params}) do
   ...
   changeset = ReviewSource.changeset(%ReviewSource{}, review_source_params)
   case Repo.insert(changeset) do
          {:ok, review_source} ->
          #test = Repo.get!(ReviewSource, review_source.id) this return not nil, so the object existing in database
          url = "mocroservice_url/#{review_source.id}"
          response = HTTPoison.patch!(url, "{}")

So in the HTTPoison.patch! line the microservice being called but it can’t get the object with such id:

20:39:50 web.1  | Review_SourcesDoesNotExist: <class '__main__.Review_Sources'> instance matching query does not exist:
20:39:50 web.1  | SQL: SELECT "t1"."id", "t1"."type", "t1"."url", "t1"."review_widget_id", "t1"."inserted_at", "t1"."updated_at" FROM "review_sources" AS "t1" WHERE ("t1"."id" = %s) LIMIT 1 OFFSET 0

In both elixir app and flask microservice while testing the databases name are identical – projname_test. The object id also identical but i also tried selecting all row from database in microservice route handler and it’s empty. It works correclty in dev when databases set to projname_dev, the dev version also works when databases set to projname_test.
So is there something i should know about database condition when testing?

Showing Posts 1 to 6

idi527

idi527

So is there something i should know about database condition when testing?

Maybe that it happens inside a transaction and this transaction never gets committed. I think I had a similar problem recently, see Postgrex.Notifications process doesn't receive notifications in tests.

Try running your test inside Ecto.Adapters.SQL.Sandbox.unboxed_run/2:

test "successfully add new review source", %{conn: conn} do
  Ecto.Adapters.SQL.Sandbox.unboxed_run(YourApp.Repo, fn ->
    conn = Accounts.sign_in_user(conn, user)
    conn = post conn, review_source_path(conn, :create, review_source: %{
      # ...
    })
  end)
  # don't forget to clean up inserted data afterwards
end

This would actually commit the transaction, so your other service will see the inserted data.

mirth

mirth OP

I put all test code to Ecto.Adapters.SQL.Sandbox.unboxed_run and it works. But it seems it doesn’t clean the database then.

idi527

idi527

Yes, it doesn’t.

mirth

mirth OP

Yes, it doesn’t.

So what would be the workaround here?

LostKobrakai

LostKobrakai

The ecto.sandbox is built around the idea of testing against a long running transaction, but never commiting any data to the database. If you need to commit you data to the db you’d need to come up with different means of cleaning up the database / preventing async tests against the same data.

idi527

idi527

I delete the test data manually after the test has run.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to &lt;%= web_namespace %&gt;.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

CodeSync
AI-Powered Search at Scale - Jeff Weiss | ElixirConf EU 2026 https://www.youtube.com/watch?v=-a4A33hDtzA Comments welcome! View the <s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews