factory_json 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46bd43eb84be63ce213d2aa07324182523b194c0
4
- data.tar.gz: 0ebcafa553e503bb5bb140426e7e95ef55e721b0
3
+ metadata.gz: 1fdcd55cfe7a41a601a71bde443711f27310bc4d
4
+ data.tar.gz: b456e844dc4cbe95b3d976757672d57cd6e85785
5
5
  SHA512:
6
- metadata.gz: 21478e944982ce49bf27bb74d1a9d48aa914ea60693bf05f13cc2708090ae2c3a68f36eec295bee2e13691f00e251f1efaa8007b0d15c215fc697b9fb5db4ab0
7
- data.tar.gz: f41687a7e44449836e34aa69924098f23743cf33db5d00ac74972c98de6dd946f8cd0086ec97625dd8e8c3a7fdadc416d2605d46baf63379adf0de9fef13c3a4
6
+ metadata.gz: ec268ccc71b6ed66ad2c6f199f32d3f624a50153e1529bc05e7c121ae1a989efd3ab7ee53f75346b0134c01728f9af6a5fd67c7677e3ad3818121b4514b10851
7
+ data.tar.gz: 72123089a8efd9c9720509dc4b8c4030a7d116851bb707de998c883984a9ec12a1ea89f55c3fa788acfbfad1fe28edcb68c9d08326b6da30fac805ae0ac90a36
data/Gemfile CHANGED
@@ -2,3 +2,11 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in factory_json.gemspec
4
4
  gemspec
5
+
6
+ gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
7
+ gem 'jdbc-sqlite3', platforms: :jruby
8
+
9
+ gem 'sqlite3', '~> 1.3.10', platforms: :ruby
10
+
11
+ gem "pry"
12
+ gem "pry-byebug"
data/README.md CHANGED
@@ -28,7 +28,16 @@ Tired of creating json fixtures somewhere in your app and keeping them up to dat
28
28
  ```ruby
29
29
  # spec/factories/data_structures.rb
30
30
  FactoryGirl.define do
31
- factory :entry_relation, class: 'Hash' do
31
+ factory :entry, class: Hash do
32
+ name "This one is special"
33
+ location "The place where you can always see a Sunrise"
34
+
35
+ after(:build) do |entry|
36
+ entry[:relations] = FactoryGirl.build_list(entry_relation, 3, :with_address)
37
+ end
38
+ end
39
+
40
+ factory :entry_relation, class: Hash do
32
41
  type "RELATED"
33
42
  label "Apartment:"
34
43
  name_summary "Mr Darth Vader"
@@ -54,9 +63,45 @@ entry_relation = FactoryGirl.json(:entry_relation, :with_address) # to return st
54
63
  You have a factory that you'd also like to use as a JSON fixture? Use common usage patterns (to_json) with our fixtures now! Not only can you get JSON from your factories, but of course also Hashes. All you need to do is to define to_json or to_hash (since to_json is based on to_hash) on your model.
55
64
 
56
65
  ```ruby
57
- # spec/factories/users.rb
66
+ # app/models.rb
67
+ class User
68
+
69
+ has_one :profile
70
+
71
+ def to_hash
72
+ {
73
+ name: self.name,
74
+ email: self.email,
75
+ profile: profile.to_hash
76
+ }.to_json
77
+ end
78
+
79
+ def to_json(*args)
80
+ to_hash.to_json(*args)
81
+ end
82
+ end
83
+
84
+ class Profile
85
+
86
+ belongs_to :user
87
+
88
+ def to_hash
89
+ {
90
+ age: self.age,
91
+ hair_color: self.hair_color,
92
+ child_dream: self.child_dream,
93
+ i_will_start_running: "tomorrow"
94
+ }
95
+ end
96
+
97
+ def to_json(*args)
98
+ to_hash.to_json(*args)
99
+ end
100
+ end
101
+
102
+ # spec/factories.rb
58
103
  FactoryGirl.define do
59
- factory :user, class: 'User' do
104
+ factory :user do
60
105
  name "User 1"
61
106
  email "test@example.com"
62
107
 
@@ -64,17 +109,11 @@ FactoryGirl.define do
64
109
  association :profile
65
110
  end
66
111
  end
67
- end
68
-
69
- # app/models/user.rb
70
- class User
71
112
 
72
- def to_json
73
- {
74
- name: self.name,
75
- email: self.email,
76
- profile: profile.to_hash
77
- }.to_json
113
+ factory :profile do
114
+ age "18 years"
115
+ hair_color "blue"
116
+ child_dream "To become a Jedi Knight"
78
117
  end
79
118
  end
80
119
  ```
data/factory_json.gemspec CHANGED
@@ -18,10 +18,15 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = '~> 2.0'
22
+
21
23
  spec.add_dependency "factory_girl"
22
24
 
23
25
  spec.add_development_dependency "bundler", "~> 1.6"
24
26
  spec.add_development_dependency "rake"
25
27
  spec.add_development_dependency "rspec"
26
- spec.add_development_dependency "mocha", ">= 0.12.8"
28
+ spec.add_development_dependency "mocha", "~> 0.14.0"
29
+ spec.add_development_dependency "bourne"
30
+
31
+ spec.add_development_dependency "activerecord", ">= 3.0.0"
27
32
  end
@@ -0,0 +1,20 @@
1
+ module FactoryGirl
2
+ module AttributeAssignerWithHashSupport
3
+
4
+ def object
5
+ @evaluator.instance = build_class_instance
6
+ build_class_instance.tap do |instance|
7
+ attributes_to_set_on_instance.each do |attribute|
8
+ if instance.respond_to?("#{attribute}=")
9
+ instance.public_send("#{attribute}=", get(attribute))
10
+ elsif instance.respond_to?("[]=")
11
+ instance[attribute] = get(attribute)
12
+ else
13
+ raise NoMethodError.new("undefined method `#{attribute}=' for #{instance}")
14
+ end
15
+ @attribute_names_assigned << attribute
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,21 +1,20 @@
1
1
  module FactoryGirl
2
2
  module Strategy
3
3
  class AsJSON
4
- def association(runner)
5
- runner.run(:null)
4
+ def initialize
5
+ @strategy = FactoryGirl.strategy_by_name(:build).new
6
6
  end
7
7
 
8
- def result(evaluation)
9
- source =
10
- build_class_is_hash?(evaluation) ? evaluation.hash : evaluation.object
11
-
12
- ::JSON.parse source.to_json
8
+ def association(runner)
9
+ runner.instance_variable_set :@strategy, :build
10
+ runner.run
13
11
  end
14
12
 
15
- private
13
+ def result(evaluation)
14
+ result = @strategy.result(evaluation)
15
+ evaluation.notify(:before_json, result)
16
16
 
17
- def build_class_is_hash?(evaluation)
18
- evaluation.instance_variable_get(:@attribute_assigner).instance_variable_get(:@build_class) == Hash
17
+ ::JSON.parse result.to_json
19
18
  end
20
19
  end
21
20
  end
@@ -1,21 +1,20 @@
1
1
  module FactoryGirl
2
2
  module Strategy
3
3
  class JSON
4
+ def initialize
5
+ @strategy = FactoryGirl.strategy_by_name(:build).new
6
+ end
7
+
4
8
  def association(runner)
5
- runner.run(:null)
9
+ runner.instance_variable_set :@strategy, :build
10
+ runner.run
6
11
  end
7
12
 
8
13
  def result(evaluation)
9
- source =
10
- build_class_is_hash?(evaluation) ? evaluation.hash : evaluation.object
11
-
12
- source.to_json
13
- end
14
-
15
- private
14
+ result = @strategy.result(evaluation)
15
+ evaluation.notify(:before_json, result)
16
16
 
17
- def build_class_is_hash?(evaluation)
18
- evaluation.instance_variable_get(:@attribute_assigner).instance_variable_get(:@build_class) == Hash
17
+ result.to_json
19
18
  end
20
19
  end
21
20
  end
data/lib/factory_json.rb CHANGED
@@ -1,7 +1,13 @@
1
+ require 'json'
2
+ require "factory_girl"
3
+
1
4
  require "factory_json/version"
2
5
 
3
6
  require "factory_girl/strategy/json"
4
7
  require "factory_girl/strategy/as_json"
8
+ require "factory_girl/attribute_assigner_with_hash_support"
5
9
 
6
10
  FactoryGirl.register_strategy(:json, FactoryGirl::Strategy::JSON)
7
11
  FactoryGirl.register_strategy(:as_json, FactoryGirl::Strategy::AsJSON)
12
+
13
+ FactoryGirl::AttributeAssigner.prepend FactoryGirl::AttributeAssignerWithHashSupport
@@ -1,3 +1,3 @@
1
1
  module FactoryJson
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,17 +1,14 @@
1
-
2
1
  require 'spec_helper'
3
2
 
4
3
  describe FactoryGirl::Strategy::AsJSON do
5
- it_should_behave_like "strategy without association support"
4
+ it_should_behave_like "strategy with association support", :build
5
+ it_should_behave_like "strategy with callbacks", {}, :after_build, :before_json
6
+ it_should_behave_like "strategy with strategy: :build", :build
6
7
 
7
8
  context "Object source" do
8
9
  let(:result) { { "name" => "John Doe", "gender" => "Male", "admin" => false } }
9
10
  let(:object) { stub("user", to_json: result.to_json, ) }
10
- let(:evaluation) { stub("evaluation", object: object) }
11
-
12
- before(:each) do
13
- subject.stubs(:build_class_is_hash?).returns(false)
14
- end
11
+ let(:evaluation) { stub("evaluation", object: object, notify: true) }
15
12
 
16
13
  it "pipes hash through JSON lib" do
17
14
  JSON.expects(:parse).with(kind_of(String))
@@ -32,12 +29,8 @@ describe FactoryGirl::Strategy::AsJSON do
32
29
 
33
30
  context "Hash source" do
34
31
  let(:result) { { "name" => "John Doe", "gender" => "Male", "admin" => false } }
35
- let(:hash) { { name: "John Doe", gender: "Male", admin: false } }
36
- let(:evaluation) { stub("evaluation", hash: hash) }
37
-
38
- before(:each) do
39
- subject.stubs(:build_class_is_hash?).returns(true)
40
- end
32
+ let(:hash) { { name: "John Doe", gender: "Male", admin: false } }
33
+ let(:evaluation) { stub("evaluation", object: hash, notify: true) }
41
34
 
42
35
  it "pipes hash through JSON lib" do
43
36
  JSON.expects(:parse).with(kind_of(String))
@@ -1,17 +1,14 @@
1
-
2
1
  require 'spec_helper'
3
2
 
4
3
  describe FactoryGirl::Strategy::JSON do
5
- it_should_behave_like "strategy without association support"
4
+ it_should_behave_like "strategy with association support", :build
5
+ it_should_behave_like "strategy with callbacks", "{}", :after_build, :before_json
6
+ it_should_behave_like "strategy with strategy: :build", :build
6
7
 
7
8
  context "Object source" do
8
9
  let(:result) { { "name" => "John Doe", "gender" => "Male", "admin" => false }.to_json }
9
10
  let(:object) { stub("user", to_json: result) }
10
- let(:evaluation) { stub("evaluation", object: object) }
11
-
12
- before(:each) do
13
- subject.stubs(:build_class_is_hash?).returns(false)
14
- end
11
+ let(:evaluation) { stub("evaluation", object: object, notify: true) }
15
12
 
16
13
  it "returns to_hash from the evaluation for an object" do
17
14
  expect(subject.result(evaluation)).to eq(result)
@@ -27,11 +24,7 @@ describe FactoryGirl::Strategy::JSON do
27
24
  context "Hash source" do
28
25
  let(:result) { { "name" => "John Doe", "gender" => "Male", "admin" => false }.to_json }
29
26
  let(:hash) { { name: "John Doe", gender: "Male", admin: false } }
30
- let(:evaluation) { stub("evaluation", hash: hash) }
31
-
32
- before(:each) do
33
- subject.stubs(:build_class_is_hash?).returns(true)
34
- end
27
+ let(:evaluation) { stub("evaluation", object: hash, notify: true) }
35
28
 
36
29
  it "returns the hash from the evaluation" do
37
30
  expect(subject.result(evaluation)).to eq result
data/spec/spec_helper.rb CHANGED
@@ -3,14 +3,22 @@ $LOAD_PATH << File.join(File.dirname(__FILE__))
3
3
 
4
4
  require 'rubygems'
5
5
  require 'rspec'
6
- require 'json'
6
+ require 'rspec/autorun'
7
7
  require 'mocha/api'
8
+ require 'bourne'
8
9
 
9
10
  require 'factory_girl'
10
11
  require 'factory_json'
11
12
 
12
13
  spec = Gem::Specification.find_by_name('factory_girl')
13
- require File.join(spec.gem_dir, 'spec', 'support', 'shared_examples', 'strategy')
14
+
15
+ files = File.join(spec.gem_dir, 'spec', 'support', 'macros', '**', '*.rb')
16
+ Dir[files].each { |f| require File.expand_path(f) }
17
+
18
+ files = File.join(spec.gem_dir, 'spec', 'support', 'matchers', '**', '*.rb')
19
+ Dir[files].each { |f| require File.expand_path(f) }
20
+
21
+ require "support/shared_examples/strategy"
14
22
 
15
23
  RSpec.configure do |config|
16
24
  config.expect_with :rspec do |expectations|
@@ -18,6 +26,7 @@ RSpec.configure do |config|
18
26
  end
19
27
 
20
28
  config.mock_framework = :mocha
29
+ config.include DeclarationMatchers
21
30
 
22
31
  config.after do
23
32
  FactoryGirl.reload
@@ -0,0 +1,65 @@
1
+ shared_examples_for "strategy with association support" do |factory_girl_strategy_name|
2
+ let(:factory) { stub("associate_factory") }
3
+
4
+ def association_named(name, strategy, overrides)
5
+ runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides])
6
+ subject.association(runner)
7
+ end
8
+
9
+ before do
10
+ FactoryGirl.stubs(factory_by_name: factory)
11
+ factory.stubs(:compile)
12
+ factory.stubs(:run)
13
+ end
14
+
15
+ it "runs the factory with the correct overrides" do
16
+ association_named(:author, factory_girl_strategy_name, great: "value")
17
+ expect(factory).to have_received(:run).with(factory_girl_strategy_name, great: "value")
18
+ end
19
+
20
+ it "finds the factory with the correct factory name" do
21
+ association_named(:author, factory_girl_strategy_name, great: "value")
22
+ expect(FactoryGirl).to have_received(:factory_by_name).with(:author)
23
+ end
24
+ end
25
+
26
+ shared_examples_for "strategy with strategy: :build" do |factory_girl_strategy_name|
27
+ let(:factory) { stub("associate_factory") }
28
+
29
+ def association_named(name, overrides)
30
+ runner = FactoryGirl::FactoryRunner.new(name, overrides[:strategy], [overrides.except(:strategy)])
31
+ subject.association(runner)
32
+ end
33
+
34
+ before do
35
+ FactoryGirl.stubs(factory_by_name: factory)
36
+ factory.stubs(:compile)
37
+ factory.stubs(:run)
38
+ end
39
+
40
+ it "runs the factory with the correct overrides" do
41
+ association_named(:author, strategy: :build, great: "value")
42
+ expect(factory).to have_received(:run).with(factory_girl_strategy_name, { great: "value" })
43
+ end
44
+
45
+ it "finds the factory with the correct factory name" do
46
+ association_named(:author, strategy: :build, great: "value")
47
+ expect(FactoryGirl).to have_received(:factory_by_name).with(:author)
48
+ end
49
+ end
50
+
51
+ shared_examples_for "strategy with callbacks" do |result_instance, *callback_names|
52
+ let(:object) {{}}
53
+ let(:evaluation) { stub("evaluation", object: object, notify: true, create: nil) }
54
+
55
+ it "runs the callbacks #{callback_names} with the evaluation's object" do
56
+ subject.result(evaluation)
57
+ callback_names.each do |name|
58
+ expect(evaluation).to have_received(:notify).with(name, evaluation.object)
59
+ end
60
+ end
61
+
62
+ it "returns the object from the evaluation" do
63
+ expect(subject.result(evaluation)).to eq result_instance
64
+ end
65
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - E-Max
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-13 00:00:00.000000000 Z
11
+ date: 2015-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: factory_girl
@@ -69,17 +69,45 @@ dependencies:
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mocha
71
71
  requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.14.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.14.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: bourne
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
72
93
  requirements:
73
94
  - - ">="
74
95
  - !ruby/object:Gem::Version
75
- version: 0.12.8
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 3.0.0
76
104
  type: :development
77
105
  prerelease: false
78
106
  version_requirements: !ruby/object:Gem::Requirement
79
107
  requirements:
80
108
  - - ">="
81
109
  - !ruby/object:Gem::Version
82
- version: 0.12.8
110
+ version: 3.0.0
83
111
  description: FactoryGirl can now handle JSON representation for an object.
84
112
  email:
85
113
  - max.zab87@gmail.com
@@ -94,6 +122,7 @@ files:
94
122
  - README.md
95
123
  - Rakefile
96
124
  - factory_json.gemspec
125
+ - lib/factory_girl/attribute_assigner_with_hash_support.rb
97
126
  - lib/factory_girl/strategy/as_json.rb
98
127
  - lib/factory_girl/strategy/json.rb
99
128
  - lib/factory_json.rb
@@ -101,6 +130,7 @@ files:
101
130
  - spec/factory_girl/strategy/as_json_spec.rb
102
131
  - spec/factory_girl/strategy/json_spec.rb
103
132
  - spec/spec_helper.rb
133
+ - spec/support/shared_examples/strategy.rb
104
134
  homepage: https://github.com/local-ch/factory_json
105
135
  licenses:
106
136
  - MIT
@@ -111,9 +141,9 @@ require_paths:
111
141
  - lib
112
142
  required_ruby_version: !ruby/object:Gem::Requirement
113
143
  requirements:
114
- - - ">="
144
+ - - "~>"
115
145
  - !ruby/object:Gem::Version
116
- version: '0'
146
+ version: '2.0'
117
147
  required_rubygems_version: !ruby/object:Gem::Requirement
118
148
  requirements:
119
149
  - - ">="
@@ -129,3 +159,4 @@ test_files:
129
159
  - spec/factory_girl/strategy/as_json_spec.rb
130
160
  - spec/factory_girl/strategy/json_spec.rb
131
161
  - spec/spec_helper.rb
162
+ - spec/support/shared_examples/strategy.rb