Skip to content

module Athena::ORM::Spec::DatabaseTransaction #

Runs each test of an ADI::Spec::ContainerTestCase in a transaction that's rolled back once the test is done, for applications using the ORM through AORM::Bundle.

struct UserControllerTest < ATH::Spec::APITestCase
  include AORM::Spec::DatabaseTransaction

  def test_show : Nil
    user = User.new
    user.name = "George"

    self.em.persist user
    self.em.flush

    self.get("/user/#{user.id}").body.should contain "George"
  end
end

#em is the entity manager the test's container injects, so the services the test uses, and the requests an ATH::Spec::APITestCase makes, run within the test's transaction. It's cleared after each request, so the next request and the test itself load entities from the database again, see AORM::Bundle::Registry#reset. The transaction is rolled back once the test is done, when its container closes the entity manager, see AORM::Bundle::Registry#close.

Note

MySQL and MariaDB implicitly commit the transaction on statements that change the schema, such as CREATE TABLE. PostgreSQL aborts the transaction when a statement fails, so the test's later statements fail too.

Note

A test case that overrides #tear_down must call super, which closes the entity manager.

Constructors#

Methods#

#em : AORM::EntityManager#

Returns the entity manager of the current test, which the test's container injects, with the test's transaction begun.

View source