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)

First 4 of 4 Posts Switch mode

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.

SArthur

SArthur

Hi - I am so sorry about not responding to this. I missed the reply entirely. I’m buried with another item that I just posted on, but will circle back to this when I have some breathing room. I loved the idea of embedding tests rather than iex examples in the docs.

Cheers

Nezteb

Nezteb

I usually have an easier time running my app first via iex -S mix with a short name and cookie. After that you can configure Livebook to connect to your newly spawned iex process/node, as described in this blog post:

Here’s a similar thread on it as well:

Once that’s set up, all of your application code (including your use myApp.DataCase) should be mostly usable from Livebook.

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:

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement