• Stars
    star
    168
  • Rank 225,507 (Top 5 %)
  • Language
    Elixir
  • Created over 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fixtures for Elixir apps

EctoFixtures Build Status

Fixtures for Ecto

EctoFixtures is built and maintained by DockYard, contact us for expert Elixir and Phoenix consulting.

Usage

Fixture files should be stored in test/fixtures/. The format of a fixture file is as:

# test/fixtures/accounts.exs

accounts model: Account, repo: Repo do
  test do
    email "[email protected]"
    name "Brian Cardarella"
    password_hash :crypto.hash(:sha, "password")
  end
end

In your test file you can access the fixture sets with the by tagging each test with the fixtures you want to load then pattern matching on the data field for the context argument.

defmodule MyTestCase do
  use ExUnit.Case
  use EctoFixtures

  @tag fixtures: :accounts
  test "data test", %{data: data}  do
    assert data.accounts.test.email == "[email protected]"
  end
end

This data is also inserted into the database, the resulting data set returned from fixtures/1 is actually an Ecto.Model.

defmodule MyTestCase do
  use ExUnit.Case
  use EctoFixtures

  test "database data is inserted and equal to data set", %{data: data} do
    assert data.accounts.test == Repo.get(Account, accounts.test.id)
  end
end

Avoiding Inserts

If you'd like the model structs without actually inserting into the database pass insert: false to fixtures/2.

defmodule MyTestCase do
  use ExUnit.Case
  import EctoFixtures, only: [fixtures: 2]

  test "database data is inserted and equal to data set" do
    fixtures(:accounts, insert: false)

    assert length(Repo.all(Account)) == 0
  end
end

Optional Overrides

You can override the fixture data on a per-usage basis by passing a map as the 2nd argument. The maps keys must correspond with the structure of the fixture names and row names column data you wish to override.

Note: you cannot add rows with the override. It is intended only to mutate the fixture column data

defmodule MyTestCase do
  use ExUnit.Case
  import EctoFixtures, only: [fixtures: 2]

  test "data test" do
    %{accounts: accounts} = fixtures(:accounts, %{
      accounts: %{
        test: %{
          email: "[email protected]"
        }
      }
    })

    assert accounts.test.email != "[email protected]"
    assert accounts.test.email == "[email protected]"
  end
end

Associations

Associations can be made between data sets, reference the data set's name and label:

Belongs To

accounts model: Account, repo: Repo do
  brian do
    name "Brian"
  end
end

events model: Event, repo: Repo do
  one do
    name "First Event"
    account accounts.brian
  end
end

Has One

accounts model: Account, repo: Repo do
  brian do
    name "Brian"
    pet pets.boomer
  end
end

pets model: Pet, repo: Repo do
  boomer do
    name "Boomer"
  end
end

Has Many

accounts model: Account, repo: Repo do
  brian do
    name "Brian"
    events [events.one, events.two]
  end
end

events model: Event, repo: Repo do
  one do
    name "First Event"
  end
  two do
    name "Second Event"
  end
end

Between other fixtures files

Associations can also be made between fixture files:

# test/fixtures/accounts.exs
accounts model: Account, repo: Repo do
  brian do
    name "Brian"
    pet fixtures(:pets).pets.boomer
  end
end

# test/fixtures/pets.exs
pets model: Pet, repo: Repo do
  boomer do
    name "Boomer"
  end
end

Handling Foreign Key Constraints

ecto_fixtures will determine the assocation type being made and ensure that child records are always inserted after the parent record to avoid any foreign key constraint issues, regardless of the order in which the fixtures are loaded.

Inheriting Data

If you'd like to have default values inherited into other rows you can do that with the inherit option on the row defintion:

accounts model: Account, repo: Repo do
  default do
    is_admin false
  end

  brian inherits: default do
    name "Brian"
  end
end

other_accounts: Account, repo: Repo do
  stephanie inherits: accounts.default do
    name "Stephanie"
    is_admin true
  end
end

When inheriting from rows in the same group you can simply refer to the row name. When referring to rows in other groups you have to refer to the group name and table name.

You can inherit data from other fixture files as well:

# test/fixtures/accounts.exs
accounts model: Account, repo: Repo do
  default do
    is_admin false
  end

  brian inherits: default do
    name "Brian"
  end
end

# test/fixtures/other_accounts.exs
other_accounts: Account, repo: Repo do
  stephanie inherits: fixtures(:accounts).accounts.default do
    name "Stephanie"
    is_admin true
  end
end

Inherited values can be overriden by defining values on the same column.

Virtual Rows

Sometimes you may not want the row you inherit from to be inserted into the database. In this case you must set the virtual: true flag for that row:

accounts model: Account, repo: repo do
  default virtual: true do
    is_admin false
  end
end

Authors

We are very thankful for the many contributors

Versioning

This library follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this library. Please see our Contribution Guidelines on how to properly submit issues and pull requests.

Legal

DockYard, Inc. Β© 2015

@dockyard

Licensed under the MIT license

More Repositories

1

ember-composable-helpers

Composable helpers for declarative templating in Ember
JavaScript
636
star
2

elixir-mail

Build composable mail messages
Elixir
386
star
3

ember-route-action-helper

Bubble closure actions in routes
JavaScript
330
star
4

ember-in-viewport

Detect if an Ember View or Component is in the viewport @ 60FPS
JavaScript
246
star
5

ember-admin

Admin backend for ember-cli projects
JavaScript
241
star
6

ember-service-worker

A pluggable approach to Service Workers for Ember.js
JavaScript
238
star
7

flame_on

Flame Graph LiveView Component and LiveDashboard plugin
Elixir
228
star
8

ember-router-scroll

πŸ—” Scroll to top with preserved browser history scroll position.
JavaScript
204
star
9

ember-async-button

Async Button Component for Ember CLI apps
JavaScript
173
star
10

inquisitor

Composable query builder for Ecto
Elixir
170
star
11

openid_connect

Elixir
66
star
12

eslint-plugin-ember-suave

DockYard's ESLint plugin for Ember apps
JavaScript
53
star
13

courier

Elixir
53
star
14

ember-cart

Shopping cart primitives for Ember
JavaScript
53
star
15

valid_field

Elixir
48
star
16

rein

Reinforcement Learning tooling built with Nx
Elixir
42
star
17

json_api_assert

Composable assertions for JSON API payload
Elixir
38
star
18

live_view_demo

Forkable repo for entries in Phoenix Phrenzy (https://phoenixphrenzy.com/)
Elixir
35
star
19

ember-service-worker-asset-cache

JavaScript
28
star
20

svelte-inline-compile

JavaScript
27
star
21

ember-cli-custom-assertions

Add custom QUnit assertions to your ember-cli test suite
JavaScript
26
star
22

ember-app-shell

JavaScript
24
star
23

easing

Elixir
22
star
24

design-sprints

HTML
22
star
25

ember-i18n-to-intl-migrator

Migrate ember-i18n to ember-intl
JavaScript
21
star
26

ember-service-worker-index

An Ember Service Worker plugin that caches the index.html file
JavaScript
20
star
27

ember-cli-deploy-compress

Compress your assets automatically choosing the best compression available for your browser targets
JavaScript
18
star
28

laptop-install

Shell
17
star
29

narwin-pack

Package of PostCSS plugins DockYard utilizes for PostCSS based projects!
JavaScript
16
star
30

ember-maybe-in-element

Conditionally render content elsewhere using #-in-element on ember apps
JavaScript
15
star
31

ember-service-worker-cache-fallback

JavaScript
15
star
32

inquisitor_jsonapi

JSON API Matchers for Inquisitor
Elixir
14
star
33

ember-one-way-select

JavaScript
10
star
34

svelte-inline-component

Utility and vite plugin to allow to create your own inline svelte components in tests
JavaScript
9
star
35

live_view_events

A simple library to unify sending and receiving messages in LiveView components
JavaScript
8
star
36

disklavier

Elixir
8
star
37

canon

All the must-read articles and must-watch videos for the DockYard engineering team.
8
star
38

plausible_proxy

An Elixir Plug to proxy calls to Plausible through your server
Elixir
7
star
39

netcdf

Elixir NetCDF Bindings
Rust
7
star
40

ember-service-worker-cache-first

JavaScript
7
star
41

qunit-notifications

Web Notifications support for QUnit in-browser test suites
JavaScript
6
star
42

auth_test_support

Authentication and authorization test functions
Elixir
4
star
43

broccoli-json-concat

JavaScript
3
star
44

boat-tracker

Elixir
3
star
45

ember-load-css

Ember CLI wrapper for loadCSS
JavaScript
3
star
46

drive-in-privacy-policy

2
star
47

boston_elixir

LiveView Native Workshop for Boston Elixir
Elixir
2
star
48

ketch

Simple proof-of-concept web application built with Next.js, Storybook, and Firebase.
JavaScript
1
star
49

courier_web

JavaScript
1
star
50

stylelint-config-narwin

DockYard stylelint configuration
JavaScript
1
star
51

liveview_tailwind_demo

Demo showing TailWind 3 integration in a Phoenix LiveView project
Elixir
1
star
52

ember-qunit-notifications

tomster-ified qunit-notifications
1
star
53

formation

Example of Phoenix LiveView Form logic
Elixir
1
star