SArthur

SArthur

Test database vs. dev database when connecting Livebook notebook to local app

I am in love with Livebook! My hope is to use it for everything from data exploration and feature planning, documentation, and TDD in a cohesive way… amazing if it comes together!

On the TDD front, I have successfully connected a livebook notebook to my local app and am able to run basic tests. However, I’m stumped trying to test data creation. The snippet below worked the first time this was run (without the ref to the DataCase), but then fails thereafter because the original record is not being deleted after the test as happens with mix test. I tried adding the use DataCase line, but that results in the compile error below. Is this possible and can someone point me to any documentation to assist?

Many thanks in advance!

ExUnit.start(autorun: false)

defmodule MyTest do
  use ExUnit.Case, async: true
  # Everything above is boiler plate for livebook testing

  use myApp.DataCase  
  alias myApp.Members
  alias myApp.Members.Member

  test "creates new Member with valid attrs and sets member_uuid" do
      assert {:ok, %Member{member_uuid: member_uuid}} =
               Members.create_member(%{name: "New Member", member_type: "foe", collapsed_name: "newmember"})

      refute nil == member_uuid
    end
end

# run below is boiler plate for livebook testing
ExUnit.run()

** (CompileError) priv/livebook/Adventures_in_testing.livemd#cell:6: module PreOrderApp.DataCase is not > loaded and could not be found
(elixir 1.13.2) expanding macro: Kernel.use/1
priv/livebook/Adventures_in_testing.livemd#cell:6: MyTest (module)

Most Liked

moogle19

moogle19

Your mix.exs file probably contains the following lines:

  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

so the "test/support" folder which contains your DataCase isn’t compiled in the default env for the notebook (which is :dev).

But I think there is currently no way to set the MIX_ENV before attaching to the notebook.

You could add:

  defp elixirc_paths(:dev), do: ["lib", "test/support"]

above elixirc_paths(_) to compile the test/support files also in :dev.

Nezteb

Nezteb

Funnily enough I ran into this issue again, and my previous answer was close, but what I ended up doing is something like this:

MIX_ENV=test iex --sname nezteb --cookie nezteb -S mix phx.server

The MIX_ENV=test part is what I was missing last time. Livebook itself doesn’t need to run in test mode as long as you are using “Attached node” mode and connecting to your local iex sessions which is running with MIX_ENV=test. :smile:

Connect to the node in Livebook and then try something like this:

ExUnit.start(autorun: false)

defmodule MyAppTest do
  # use ExUnit.Case, async: true
  use MyApp.DataCase # this should work fine

  test "it works" do
    # Example using ExMachina (within the DataCase)
    user = insert(:user)
    assert user.id == 1
  end
end

ExUnit.run()

Example evaluation:

Where Next?

Popular in Questions Top

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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
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

We're in Beta

About us Mission Statement