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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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 I...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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

Other popular topics Top

johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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

We're in Beta

About us Mission Statement