Skip to content

Commit

Permalink
Merge pull request #5 from imdrasil/0.1.4_release
Browse files Browse the repository at this point in the history
Fix Jennifer association related bug
  • Loading branch information
imdrasil committed Sep 5, 2018
2 parents 22dfda9 + 7603f9c commit 0041e48
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 513 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -9,6 +9,6 @@ env:
- DB=postgres DB_USER=postgres DB_PASSWORD=""
before_script:
- psql -c 'create database factory_test;' -U postgres
# - crystal ./spec/support/sam.cr -- db:migrate
- crystal ./spec/support/sam.cr -- db:migrate

script: make seq
12 changes: 6 additions & 6 deletions shard.yml
@@ -1,19 +1,19 @@
name: factory
version: 0.1.3
version: 0.1.4

authors:
- Roman Kalnytskyi <moranibaca@gmail.com>

crystal: 0.24.1
crystal: 0.26.1

license: MIT

development_dependencies:
minitest:
github: ysbaddaden/minitest.cr
version: "~> 0.4.0"
# TODO: uncomment after resolving issue with migrating jennifer to 0.24.1 crystal
# jennifer:
# github: imdrasil/jennifer.cr
# version: "~> 0.3.1"
jennifer:
github: imdrasil/jennifer.cr
branch: "master"
pg:
github: will/crystal-pg
29 changes: 13 additions & 16 deletions spec/factory/base_spec.cr
Expand Up @@ -76,11 +76,11 @@ describe Factory::Base do

describe ".attributes" do
it "returns hash with element of attributes type" do
expect(TestFactory.attributes.class).must_equal(Hash(String, String | Int32 | Float64 | Array(Int32)))
expect(TestFactory.attributes).must_be_instance_of(Hash(String, String | Int32 | Float64 | Array(Int32)))
end

it "returns hash with automatically generated type" do
expect(HumanFactory.attributes.class).must_equal(Hash(String, String))
expect(HumanFactory.attributes).must_be_instance_of(Hash(String, String))
end

it "includes only attrs" do
Expand Down Expand Up @@ -110,14 +110,9 @@ describe Factory::Base do
end

describe ".build_attributes" do
it "adds given parameters to attributes" do
hash = SecondTestFactory.build_attributes({"f1" => "f1"})
expect(hash["f1"]).must_equal("f1")
hash = SecondTestFactory.build_attributes({:f1 => "f1"})
expect(hash["f1"]).must_equal("f1")
hash = SecondTestFactory.build_attributes({f1: "f1"})
expect(hash["f1"]).must_equal("f1")
end
it { expect(SecondTestFactory.build_attributes({"f1" => "f1"})["v1"]).must_equal("v1") }
it { expect(SecondTestFactory.build_attributes({:f1 => "v1"})["f1"]).must_equal("v1") }
it { expect(SecondTestFactory.build_attributes({f1: "v1"})["f1"]).must_equal("v1") }

it "adds trait's attributes" do
hash = SecondTestFactory.build_attributes({} of String => String, ["nested"])
Expand All @@ -131,16 +126,18 @@ describe Factory::Base do
end

describe ".make_assigns" do
@object : Test?

let(:object) { Factory.build_test }

it "assigns to given object" do
obj = Factory.build_test
SecondTestFactory.make_assigns(obj, [] of String)
expect(obj.f3).must_equal(0.64)
SecondTestFactory.make_assigns(object, [] of String)
expect(object.f3).must_equal(0.64)
end

it "assigns traits to given object" do
obj = Factory.build_test
SecondTestFactory.make_assigns(obj, ["nested"])
expect(obj.f2).must_equal(-2)
SecondTestFactory.make_assigns(object, ["nested"])
expect(object.f2).must_equal(-2)
end
end

Expand Down
268 changes: 134 additions & 134 deletions spec/factory/jennifer/base_spec.cr
@@ -1,136 +1,136 @@
require "../../spec_helper"

# describe Factory::Jennifer::Base do
# let(:described_class) { Factory::Jennifer::Base }

# before do
# ::Jennifer::Adapter.adapter.begin_transaction
# end

# after do
# ::Jennifer::Adapter.adapter.rollback_transaction
# end

# describe "%association" do
# it "uses factory's defined association" do
# film = Factory.create_custom_film([:bad, :hit])
# expect(film.author.nil?).wont_equal(true)
# expect(film.author!.name).must_match(/Author \d*$/)
# end

# it "uses trait's author if it is given" do
# film = Factory.create_fiction_film([:with_special_author])
# expect(film.author.nil?).wont_equal(true)
# expect(film.author!.name).must_equal("Special Author")
# end

# it "uses given overrides for factory" do
# film = Factory.create_fiction_film([:with_special_author])
# expect(film.author!.name).must_equal("Special Author")
# end

# it "uses parent association if current factory has no" do
# film = Factory.create_fiction_film
# expect(film.author.nil?).must_equal(false)
# end

# it "creates object without association if there is no one" do
# film = Factory.create_film
# expect(film.author.nil?).must_equal(true)
# end
# end

# describe "%factory_creators" do
# it "defines all create methods on module level" do
# expect(Factory.create_custom_film.new_record?).must_equal(false)
# expect(Factory.create_custom_film(name: "New film").new_record?).must_equal(false)
# expect(Factory.create_custom_film({:name => "New"}).new_record?).must_equal(false)
# expect(Factory.create_custom_film([:bad]).new_record?).must_equal(false)
# expect(Factory.create_custom_film([:bad], name: "new").new_record?).must_equal(false)
# expect(Factory.create_custom_film([:bad], {:name => "new"}).new_record?).must_equal(false)
# expect(Factory.create_custom_film(1)[0].new_record?).must_equal(false)
# expect(Factory.create_custom_film(1, name: "asd")[0].new_record?).must_equal(false)
# expect(Factory.create_custom_film(1, [:bad])[0].new_record?).must_equal(false)
# expect(Factory.create_custom_film(1, {:name => "asd"})[0].new_record?).must_equal(false)
# expect(Factory.create_custom_film(1, [:bad], name: "asd")[0].new_record?).must_equal(false)
# expect(Factory.create_custom_film(1, [:bad], {:name => "asd"})[0].new_record?).must_equal(false)
# end
# end

# describe "%before_create" do
# it "calls before create" do
# film = CustomFilmFactory.create
# expect(film.name).must_match(/before/)
# film.reload
# expect(film.name).must_match(/before/)
# end
# end

# describe "%after_create" do
# it "calls callback after creating" do
# film = CustomFilmFactory.create
# expect(film.name).must_match(/after$/)
# film.reload
# expect(film.name).wont_match(/after$/)
# end
# end

# describe ".create" do
# it "accepts hash attributes" do
# film = FilmFactory.create({:name => "Custom"})
# expect(film.name).must_equal("Custom")
# expect(film.new_record?).must_equal(false)
# end

# it "accepts traits" do
# film = FilmFactory.create([:hit, :bad])
# expect(film.rating).must_equal(0)
# expect(film.name).must_match(/Best Film/)
# expect(film.new_record?).must_equal(false)
# end

# it "accepts traits and attributes" do
# film = FilmFactory.create([:hit, :bad], {:budget => 10.0f32})
# expect(film.rating).must_equal(0)
# expect(film.name).must_match(/Best Film/)
# expect(film.budget).must_equal(10.0f32)
# expect(film.new_record?).must_equal(false)
# end

# it "all model callbacks during creating" do
# film = FilmFactory.create
# expect(film.before_create).must_equal(true)
# expect(film.before_save).must_equal(true)
# expect(film.after_initialize).must_equal(true)
# end

# describe "ancestor factory" do
# it "accepts no arguments" do
# film = CustomFilmFactory.create
# expect(film.name).must_match(/Custom Film \d*/)
# expect(film.new_record?).must_equal(false)
# end

# it "accepts hash attributes" do
# film = CustomFilmFactory.create({:name => "Custom"})
# expect(film.name).must_equal("Custombeforeafter")
# expect(film.new_record?).must_equal(false)
# end

# it "accepts traits" do
# film = CustomFilmFactory.create([:hit, :bad])
# expect(film.rating).must_equal(0)
# expect(film.name).must_match(/Best Film/)
# expect(film.new_record?).must_equal(false)
# end

# it "accepts traits and attributes" do
# film = CustomFilmFactory.create([:hit, :bad], {:budget => 10.0f32})
# expect(film.rating).must_equal(0)
# expect(film.name).must_match(/Best Film/)
# expect(film.budget).must_equal(10.0f32)
# expect(film.new_record?).must_equal(false)
# end
# end
# end
# end
describe Factory::Jennifer::Base do
let(:described_class) { Factory::Jennifer::Base }

before do
::Jennifer::Adapter.adapter.begin_transaction
end

after do
::Jennifer::Adapter.adapter.rollback_transaction
end

describe "%association" do
it "uses factory's defined association" do
film = Factory.create_custom_film([:bad, :hit])
expect(film.author.nil?).wont_equal(true)
expect(film.author!.name).must_match(/Author \d*$/)
end

it "uses trait's author if it is given" do
film = Factory.create_fiction_film([:with_special_author])
expect(film.author.nil?).wont_equal(true)
expect(film.author!.name).must_equal("Special Author")
end

it "uses given overrides for factory" do
film = Factory.create_fiction_film([:with_special_author])
expect(film.author!.name).must_equal("Special Author")
end

it "uses parent association if current factory has no" do
film = Factory.create_fiction_film
expect(film.author.nil?).must_equal(false)
end

it "creates object without association if there is no one" do
film = Factory.create_film
expect(film.author.nil?).must_equal(true)
end
end

describe "%factory_creators" do
it "defines all create methods on module level" do
expect(Factory.create_custom_film.new_record?).must_equal(false)
expect(Factory.create_custom_film(name: "New film").new_record?).must_equal(false)
expect(Factory.create_custom_film({:name => "New"}).new_record?).must_equal(false)
expect(Factory.create_custom_film([:bad]).new_record?).must_equal(false)
expect(Factory.create_custom_film([:bad], name: "new").new_record?).must_equal(false)
expect(Factory.create_custom_film([:bad], {:name => "new"}).new_record?).must_equal(false)
expect(Factory.create_custom_film(1)[0].new_record?).must_equal(false)
expect(Factory.create_custom_film(1, name: "asd")[0].new_record?).must_equal(false)
expect(Factory.create_custom_film(1, [:bad])[0].new_record?).must_equal(false)
expect(Factory.create_custom_film(1, {:name => "asd"})[0].new_record?).must_equal(false)
expect(Factory.create_custom_film(1, [:bad], name: "asd")[0].new_record?).must_equal(false)
expect(Factory.create_custom_film(1, [:bad], {:name => "asd"})[0].new_record?).must_equal(false)
end
end

describe "%before_create" do
it "calls before create" do
film = CustomFilmFactory.create
expect(film.name).must_match(/before/)
film.reload
expect(film.name).must_match(/before/)
end
end

describe "%after_create" do
it "calls callback after creating" do
film = CustomFilmFactory.create
expect(film.name).must_match(/after$/)
film.reload
expect(film.name).wont_match(/after$/)
end
end

describe ".create" do
it "accepts hash attributes" do
film = FilmFactory.create({:name => "Custom"})
expect(film.name).must_equal("Custom")
expect(film.new_record?).must_equal(false)
end

it "accepts traits" do
film = FilmFactory.create([:hit, :bad])
expect(film.rating).must_equal(0)
expect(film.name).must_match(/Best Film/)
expect(film.new_record?).must_equal(false)
end

it "accepts traits and attributes" do
film = FilmFactory.create([:hit, :bad], {:budget => 10.0f32})
expect(film.rating).must_equal(0)
expect(film.name).must_match(/Best Film/)
expect(film.budget).must_equal(10.0f32)
expect(film.new_record?).must_equal(false)
end

it "all model callbacks during creating" do
film = FilmFactory.create
expect(film.before_create).must_equal(true)
expect(film.before_save).must_equal(true)
expect(film.after_initialize).must_equal(true)
end

describe "ancestor factory" do
it "accepts no arguments" do
film = CustomFilmFactory.create
expect(film.name).must_match(/Custom Film \d*/)
expect(film.new_record?).must_equal(false)
end

it "accepts hash attributes" do
film = CustomFilmFactory.create({:name => "Custom"})
expect(film.name).must_equal("Custombeforeafter")
expect(film.new_record?).must_equal(false)
end

it "accepts traits" do
film = CustomFilmFactory.create([:hit, :bad])
expect(film.rating).must_equal(0)
expect(film.name).must_match(/Best Film/)
expect(film.new_record?).must_equal(false)
end

it "accepts traits and attributes" do
film = CustomFilmFactory.create([:hit, :bad], {:budget => 10.0f32})
expect(film.rating).must_equal(0)
expect(film.name).must_match(/Best Film/)
expect(film.budget).must_equal(10.0f32)
expect(film.new_record?).must_equal(false)
end
end
end
end
8 changes: 4 additions & 4 deletions spec/spec_helper.cr
@@ -1,6 +1,6 @@
# require "./support/config"
require "minitest/autorun"
require "./support/config"
require "../src/factory"
# require "../src/factory/jennifer"
# require "./support/models"
require "../src/factory/jennifer"
require "./support/models"
require "./support/factories"
require "minitest/autorun"
2 changes: 1 addition & 1 deletion spec/support/config.cr
@@ -1,5 +1,5 @@
require "jennifer/adapter/postgres"
require "jennifer"
require "jennifer/adapter/postgres"

::Jennifer::Config.configure do |conf|
conf.logger.level = Logger::ERROR
Expand Down

0 comments on commit 0041e48

Please sign in to comment.