phoenixframework/phoenix_ecto

Name: phoenix_ecto

Owner: phoenixframework

Description: Phoenix and Ecto integration with support for concurrent acceptance testing

Created: 2015-03-06 16:47:23.0

Updated: 2018-05-19 01:23:51.0

Pushed: 2018-05-03 18:07:31.0

Homepage:

Size: 148

Language: Elixir

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

A project that integrates Phoenix with Ecto, implementing all relevant protocols.

Usage

You can use phoenix_ecto in your projects in two steps:

  1. Add it to your mix.exs dependencies:

    deps do
    :phoenix_ecto, "~> 3.0"}]
    
    
  2. List it as your application dependency:

    application do
    pplications: [:logger, :phoenix_ecto]]
    
    
Concurrent browser tests

This library also provides a plug called Phoenix.Ecto.SQL.Sandbox that allows developers to run acceptance tests powered by headless browsers such as Phantom.js and Selenium concurrently. If you are not familiar with Ecto's SQL sandbox, we recommend you to first get acquainted with it by reading Ecto.Adapters.SQL.Sandbox documentation.

To enable concurrent acceptance tests, make sure you are using PostgreSQL and follow the instructions below:

  1. Set a flag to enable the sandbox in config/test.exs:

    ig :your_app, sql_sandbox: true
    
  2. And use the flag to conditionally add the plug to lib/your_app/endpoint.ex:

    pplication.get_env(:your_app, :sql_sandbox) do
    ug Phoenix.Ecto.SQL.Sandbox
    
    

    Make sure that this is placed before the line plug YourApp.Router (or any other plug that may access the database).

You can now checkout a sandboxed connection and pass the connection information to an acceptance testing tool like Hound or Wallaby.

Hound

To write concurrent acceptance tests with Hound, first add it as a dependency to your mix.exs:

und, "~> 1.0"}

Make sure to start it at the top of your test/test_helper.exs:

, _} = Application.ensure_all_started(:hound)

Then add the following to your test case (or to your case template):

Hound.Helpers

p do
k = Ecto.Adapters.SQL.Sandbox.checkout(YourApp.Repo)
tadata = Phoenix.Ecto.SQL.Sandbox.metadata_for(YourApp.Repo, self())
und.start_session(metadata: metadata)

Hound supports multiple drivers like Chrome, Firefox, etc but it does not support concurrent tests under PhantomJS (the default).

Wallaby

To write concurrent acceptance tests with Wallaby, first add it as a dependency to your mix.exs:

llaby, "~> 0.6"}

Make sure to start it at the top of your test/test_helper.exs:

, _} = Application.ensure_all_started(:wallaby)

Then add the following to your test case (or to your case template):

Wallaby.DSL

p do
k = Ecto.Adapters.SQL.Sandbox.checkout(YourApp.Repo)
tadata = Phoenix.Ecto.SQL.Sandbox.metadata_for(YourApp.Repo, self())
ok, session} = Wallaby.start_session(metadata: metadata)

Wallaby currently supports PhantomJS (including concurrent tests). Support for other drivers may be added in the future.

The Phoenix <-> Ecto integration

Thanks to Elixir protocols, the integration between Phoenix and Ecto is simply a matter of implementing a handful of protocols. We provide the following implementations:

Configuration

The Plug.Exception implementations for Ecto exceptions may be disabled by including the error in the mix configuration.

ig :phoenix_ecto,
clude_ecto_exceptions_from_plug: [Ecto.NoResultsError]
License

Same license as Phoenix.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.