Skip to content

javierg/couch_factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Couch Factory

Build Status

Implementing factory_girl in Elixir, with support for persisting documents into a couchdb server.

Sort of inspired by factory girl elixir, but with a different API.

TODO

  • Document public functions
  • Handle conflicts when creating documents
  • Auto namespacing on document ids

usage

You will need couchdb server up and running

defp deps do
  [
    {:couch_factory, git: "git@github.com:javierg/couch_factory.git", branch: "master"}
  ]
end

Or oyu can use Hex

defp deps do
  [
    {:couch_factory, "0.1"}
  ]
end

Which will require you to add to your app dependencies couchbeam

defp deps do
  [
    {:couchbeam, git: "git://github.com/benoitc/couchbeam.git", branch: "master"},
    {:couch_factory, "0.1"}
  ]
end 

Update the applications list to include both projects:

def application do
  [applications: [:couch_factory]]
end

Add on your config/config.exs the couch_factory db config

config :couch_factory, CouchFactory.Db,
  host: "http://localhost:5984",
  db: "factory_test",
  user: "",
  password: ""

You will need to create the db manually. But probably is a good idea to add a config task for this...

Run mix deps.get in your shell.

Defining a Factory

defmodule Factory do
  use CouchFactory.Factory

  factory :user,
    _id: "user/octavio@paz.com",
    name: "Octavio Paz",
    email: "octavio@paz.com"

end

On your tests now you can do

test "will do something" do
  expected = {[{"_id", "user/octavio@paz.com"}, {"name", "Octavio Paz"}, {"email", "octavio@paz.com"}]}
  assert expected == Factory.build(:user)
end

test "this is persisted" do
  assert {:ok, user} = Factory.create(:user)
end

test "override properties" do
  expected = {[{"_id", "user/octavio@paz.com"}, {"name", "Octavio Paz"}, {"email", "octavio@war.com"}]}
  assert expected == Factory.build(:user, email: "octavio@war.com")
end

If you need a map of properties you can use

test "will return map of properties" do
  expected = %{_id: "user/octavio@paz.com", name: "Octavio Paz", email: "octavio@paz.com"}
  assert extected == Factory.properties_for(:user)
end

And you can override properties

test "will return map of properties" do
  expected = %{_id: "user/octavio@paz.com", name: "Octavio War", email: "octavio@paz.com"}
  assert extected == Factory.properties_for(:user, name: "Octavio War")
end

To create sequences you do

defmodule Factory do

  factory :user,
    name: sequence(fn(n)-> "User_#{1}" end)
   counter: sequence

This will generate a factory like

{[{"name", "User_1"}, {"counter", 1}]}

Copyright and license

Copyright (c) 2014 Duilio Ruggiero. Code released under the MIT license.

About

A Couch Factory for elixir

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages