mirth

mirth

Accessing postgres database with another microservice while testing phoenix app

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?

First Post!

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.

Where Next?

Popular in Questions Top

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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
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
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

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
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement