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.
- Document public functions
- Handle conflicts when creating documents
- Auto namespacing on document ids
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.
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 (c) 2014 Duilio Ruggiero. Code released under the MIT license.