happycollision
Queries (within validations) potentially behave differently in ex_unit tests
I’ve got two functions that run the same code, one inside an ex_unit test, and one just in a Livebook cell. They are both designed to test that my validation is working, so both should return an error. But the ex_unit one fails the test (of not returning an error).
# inside ex_unit, inside a Livebook cell
ExUnit.start(auto_run: false)
defmodule BoxOfficeFeaturesTest do
use ExUnit.Case, async: false
import BoxOffice.Core.Test
setup do
BoxOffice.Core.Test.reset()
end
describe "Reservations" do
test "a reserved seat cannot be reserved again for the same performance" do
create_seating_chart()
[seat] = create_seats().records
populate_seating_chart()
create_production()
performance = create_performance()
assert {:ok, _} =
BoxOffice.Core.Reservation.make(create_patron().id, performance.id, seat.id)
assert {:error, _} =
BoxOffice.Core.Reservation.make(create_patron().id, performance.id, seat.id)
end
end
end
results = ExUnit.run()
# Livebook cell
defmodule WhoTestsTheTesters do
import BoxOffice.Core.Test
def run() do
create_seating_chart()
[seat] = create_seats().records
populate_seating_chart()
create_production()
performance = create_performance()
{:ok, _} =
BoxOffice.Core.Reservation.make(create_patron().id, performance.id, seat.id)
{:error, _} =
BoxOffice.Core.Reservation.make(create_patron().id, performance.id, seat.id)
end
end
BoxOffice.Core.Test.reset()
WhoTestsTheTesters.run()
The latter is not throwing because it (correctly) matches {:error, _}. Just returns the error value.
Any idea why these two things would behave differently? Here’s the resource under test. In particular, I’ve discovered that previous_reservation is always [] in the test scenario:
defmodule BoxOffice.Core.Reservation do
require Ash.Query
use Ash.Resource,
data_layer: Ash.DataLayer.Ets
actions do
defaults([:read, :update])
# On creation set the patron by providing the id
create(:make, do: accept([:patron_id, :performance_id, :seat_id]))
end
attributes do
uuid_primary_key(:id)
create_timestamp(:created_at)
update_timestamp(:updated_at)
end
relationships do
belongs_to(:patron, BoxOffice.Core.Patron) do
# Set to writable so you can directly set the patron_id inside the :open action
attribute_writable?(true)
end
belongs_to(:seat, BoxOffice.Core.Seat) do
attribute_writable?(true)
end
belongs_to(:performance, BoxOffice.Core.Performance) do
attribute_writable?(true)
end
end
code_interface do
define_for(BoxOffice.Core)
define(:make, args: [:patron_id, :performance_id, :seat_id])
end
identities do
# pre_check_with might only be necessary for ETS data layer.
# see https://hexdocs.pm/ash/identities.html#pre-checking
identity(:reservation_identity, [:seat_id, :performance_id], pre_check_with: BoxOffice.Core)
end
validations do
validate(fn changeset ->
seat_id = Ash.Changeset.get_attribute(changeset, :seat_id)
performance_id = Ash.Changeset.get_attribute(changeset, :performance_id)
previous_reservation =
__MODULE__
|> Ash.Query.filter(performance_id == ^performance_id and seat_id == ^seat_id)
|> BoxOffice.Core.read!()
|> dbg
case previous_reservation do
[] -> :ok
[_] -> {:error, field: :seat_id, message: "This seat is already reserved"}
end
end)
end
end
Trending in Questions
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
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
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
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
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
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
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
@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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security












First 6 of 6 Posts
zachdaniel
Have you added this to your test config in
test.exs?happycollision
Seems like that could be it, because when I eliminate all the other tests, the one in question passes. But I am in Livebook and am not sure how to translate that config. I mean, I’ve done configs in Livebook before, but I also see some
put_envstuff happening with Ash that I don’t understand. So I tried both.I appreciate your help, and I’ll probably keep digging when Friday rolls around again. But if you are curious, or overly-generous with your time, here’s the entire Livebook:
zachdaniel
Oh, interesting. Probably in the beginning of your Livebook or in setup you can do something like
Application.put_env(:ash, :disable_async?, true)happycollision
I tried that and also tried using the configuration portion of
Mix.install(both can be seen in the code above), but no dice.But hey, the code works, it just doesn’t test well in Livebook for me, so I’ll move on and maybe come back and try to figure that out later. Those
assertmacros are nice, but I can just use regular matching for test purposes if I need to.zachdaniel
Yeah, that is super strange.
zachdaniel
Thinking about it, I don’t think the
disable_asyncbit really comes into play at all. That is standard for testing but in your case you aren’t using the sandbox adapter for the underlying repo, so it wouldn’t really matter.I really can’t think of anything special that would cause this to behave differently in a test in a livebook