Following up on Making a field unique in ecto and Testing Validations in Elixir and Ecto, I am failing to setup a controller test for unique constraints:
test "POST /imports will not store duplicate address" do
# Create the existing (conflicting) record:
location = "melee island"
MyApp.Repo.insert! %MyApp.Location{address: location}
post("/imports", %{"location" => location})
# Other assertions omitted
# Check if the location was indeed NOT inserted
query = from l in MyApp.Location,
where: [address: ^location],
select: l.address
assert MyApp.Repo.all(query) == [location]
end
The last assertion fails with the following error:
(Postgrex.Error) ERROR (in_failed_sql_transaction): current transaction is aborted, commands ignored until end of transaction block
I understand why – everything is wrapped into a test transaction – but I cannot rollback without losing the initially inserted record. Is there a way to get this behaviour tested?