happycollision
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 working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming












Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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