fabrication 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/fabrication.rb +1 -1
- data/lib/fabrication/generator/mongoid.rb +21 -0
- data/lib/fabrication/schematic.rb +2 -1
- data/lib/fabrication/sequencer.rb +8 -10
- data/lib/fabrication/version.rb +1 -1
- data/spec/fabrication/schematic_spec.rb +1 -1
- data/spec/fabrication/{sequence_spec.rb → sequencer_spec.rb} +10 -1
- data/spec/fabrication_spec.rb +12 -0
- data/spec/fabricators/mongoid_fabricator.rb +5 -0
- data/spec/fabricators/plain_old_ruby_object_fabricator.rb +0 -10
- data/spec/fabricators/sequencer_fabricator.rb +9 -0
- data/spec/support/active_record.rb +5 -0
- data/spec/support/mongoid.rb +6 -1
- data/spec/support/plain_old_ruby_objects.rb +7 -0
- data/spec/support/sequel.rb +5 -0
- data/spec/support/sequel_migrations/001_create_tables.rb +1 -0
- metadata +7 -5
data/lib/fabrication.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
class Fabrication::Generator::Mongoid < Fabrication::Generator::Base
|
2
|
+
def self.supports?(klass)
|
3
|
+
defined?(Mongoid) && klass.ancestors.include?(Mongoid::Document)
|
4
|
+
end
|
5
|
+
|
6
|
+
def assign(method_name, options, raw_value=nil)
|
7
|
+
if options.has_key?(:count)
|
8
|
+
value = (1..options[:count]).map do |i|
|
9
|
+
block_given? ? yield(instance, i) : raw_value
|
10
|
+
end
|
11
|
+
else
|
12
|
+
value = block_given? ? yield(instance) : raw_value
|
13
|
+
end
|
14
|
+
|
15
|
+
if instance.respond_to?("#{method_name}=")
|
16
|
+
instance.send("#{method_name}=", value)
|
17
|
+
else
|
18
|
+
instance[method_name] = value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -3,6 +3,7 @@ class Fabrication::Schematic
|
|
3
3
|
GENERATORS = [
|
4
4
|
Fabrication::Generator::ActiveRecord,
|
5
5
|
Fabrication::Generator::Sequel,
|
6
|
+
Fabrication::Generator::Mongoid,
|
6
7
|
Fabrication::Generator::Base
|
7
8
|
]
|
8
9
|
|
@@ -104,7 +105,7 @@ class Fabrication::Schematic
|
|
104
105
|
method_name
|
105
106
|
end
|
106
107
|
|
107
|
-
def sequence(name, start=0, &block)
|
108
|
+
def sequence(name=Fabrication::Sequencer::DEFAULT, start=0, &block)
|
108
109
|
name = "#{self.klass.to_s.downcase.gsub(/::/, '_')}_#{name}"
|
109
110
|
Fabrication::Sequencer.sequence(name, start, &block)
|
110
111
|
end
|
@@ -1,19 +1,17 @@
|
|
1
1
|
class Fabrication::Sequencer
|
2
2
|
|
3
|
-
|
3
|
+
DEFAULT = :_default
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
def self.sequence(name=DEFAULT, start=0)
|
6
|
+
idx = sequences[name] ||= start
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def sequences
|
14
|
-
@sequences ||= {}
|
8
|
+
(block_given? ? yield(idx) : idx).tap do
|
9
|
+
sequences[name] += 1
|
15
10
|
end
|
11
|
+
end
|
16
12
|
|
13
|
+
def self.sequences
|
14
|
+
@sequences ||= {}
|
17
15
|
end
|
18
16
|
|
19
17
|
end
|
data/lib/fabrication/version.rb
CHANGED
@@ -18,7 +18,7 @@ describe Fabrication::Schematic do
|
|
18
18
|
end
|
19
19
|
context "for a mongoid object" do
|
20
20
|
it "uses the base generator" do
|
21
|
-
Fabrication::Schematic.new(Author).generator.should == Fabrication::Generator::
|
21
|
+
Fabrication::Schematic.new(Author).generator.should == Fabrication::Generator::Mongoid
|
22
22
|
end
|
23
23
|
end
|
24
24
|
context "for a sequel object" do
|
@@ -1,6 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Fabrication::Sequencer do
|
4
|
+
|
5
|
+
context 'with no arguments' do
|
6
|
+
subject { Fabrication::Sequencer.sequence }
|
7
|
+
|
8
|
+
it { should == 0 }
|
9
|
+
it 'creates a default sequencer' do
|
10
|
+
Fabrication::Sequencer.sequences[:_default].should == 1
|
11
|
+
end
|
12
|
+
end
|
4
13
|
|
5
14
|
context 'with only a name' do
|
6
15
|
|
data/spec/fabrication_spec.rb
CHANGED
@@ -10,6 +10,10 @@ shared_examples 'something fabricatable' do
|
|
10
10
|
its(:string_field) { should == 'content' }
|
11
11
|
end
|
12
12
|
|
13
|
+
context 'model callbacks are fired' do
|
14
|
+
its(:before_save_value) { should == 11 }
|
15
|
+
end
|
16
|
+
|
13
17
|
context 'overriding at fabricate time' do
|
14
18
|
subject do
|
15
19
|
Fabricate(
|
@@ -206,6 +210,14 @@ describe Fabrication do
|
|
206
210
|
it 'generates four books' do
|
207
211
|
author.books.map(&:title).should == (1..4).map { |i| "book title #{i}" }
|
208
212
|
end
|
213
|
+
|
214
|
+
it "sets dynamic fields" do
|
215
|
+
Fabricate(:special_author).mongoid_dynamic_field.should == 50
|
216
|
+
end
|
217
|
+
|
218
|
+
it "sets lazy dynamic fields" do
|
219
|
+
Fabricate(:special_author).lazy_dynamic_field.should == "foo"
|
220
|
+
end
|
209
221
|
end
|
210
222
|
|
211
223
|
context 'with multiple callbacks' do
|
@@ -49,13 +49,3 @@ end
|
|
49
49
|
Fabricator("Something::Amazing") do
|
50
50
|
stuff "cool"
|
51
51
|
end
|
52
|
-
|
53
|
-
Fabricator(:sequencer) do
|
54
|
-
simple_iterator { sequence(:simple_iterator) }
|
55
|
-
param_iterator { sequence(:param_iterator, 9) }
|
56
|
-
block_iterator { sequence(:block_iterator) { |i| "block#{i}" } }
|
57
|
-
end
|
58
|
-
|
59
|
-
Fabricator("Sequencer::Namespaced") do
|
60
|
-
iterator { sequence(:iterator) }
|
61
|
-
end
|
@@ -16,6 +16,7 @@ class TestMigration < ActiveRecord::Migration
|
|
16
16
|
end
|
17
17
|
|
18
18
|
create_table :parent_active_record_models, :force => true do |t|
|
19
|
+
t.column :before_save_value, :integer
|
19
20
|
t.column :dynamic_field, :string
|
20
21
|
t.column :nil_field, :string
|
21
22
|
t.column :number_field, :integer
|
@@ -47,6 +48,10 @@ end
|
|
47
48
|
|
48
49
|
class ParentActiveRecordModel < ActiveRecord::Base
|
49
50
|
has_many :collection_field, :class_name => 'ChildActiveRecordModel'
|
51
|
+
|
52
|
+
before_save do
|
53
|
+
self.before_save_value = 11
|
54
|
+
end
|
50
55
|
end
|
51
56
|
|
52
57
|
class Company < ActiveRecord::Base
|
data/spec/support/mongoid.rb
CHANGED
@@ -20,12 +20,17 @@ end
|
|
20
20
|
class ParentMongoidDocument
|
21
21
|
include Mongoid::Document
|
22
22
|
|
23
|
+
field :before_save_value
|
23
24
|
field :dynamic_field
|
24
25
|
field :nil_field
|
25
26
|
field :number_field
|
26
27
|
field :string_field
|
27
28
|
|
28
|
-
references_many :collection_field, :class_name => 'ChildMongoidDocument'
|
29
|
+
references_many :collection_field, :class_name => 'ChildMongoidDocument', :inverse_of => :parent
|
30
|
+
|
31
|
+
before_save do
|
32
|
+
self.before_save_value = 11
|
33
|
+
end
|
29
34
|
end
|
30
35
|
|
31
36
|
class Author
|
@@ -1,11 +1,18 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
1
3
|
class ParentRubyObject
|
2
4
|
attr_accessor \
|
5
|
+
:before_save_value,
|
3
6
|
:collection_field,
|
4
7
|
:dynamic_field,
|
5
8
|
:nil_field,
|
6
9
|
:number_field,
|
7
10
|
:string_field
|
8
11
|
|
12
|
+
def initialize
|
13
|
+
self.before_save_value = 11
|
14
|
+
end
|
15
|
+
|
9
16
|
def persisted?; true end
|
10
17
|
end
|
11
18
|
|
data/spec/support/sequel.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: fabrication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0
|
5
|
+
version: 1.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Paul Elliott
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-08-26 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -86,7 +86,7 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - ~>
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 2.
|
89
|
+
version: 2.1.3
|
90
90
|
type: :development
|
91
91
|
version_requirements: *id007
|
92
92
|
- !ruby/object:Gem::Dependency
|
@@ -151,6 +151,7 @@ files:
|
|
151
151
|
- lib/fabrication/fabricator.rb
|
152
152
|
- lib/fabrication/generator/active_record.rb
|
153
153
|
- lib/fabrication/generator/base.rb
|
154
|
+
- lib/fabrication/generator/mongoid.rb
|
154
155
|
- lib/fabrication/generator/sequel.rb
|
155
156
|
- lib/fabrication/schematic.rb
|
156
157
|
- lib/fabrication/sequencer.rb
|
@@ -167,7 +168,7 @@ files:
|
|
167
168
|
- spec/fabrication/generator/active_record_spec.rb
|
168
169
|
- spec/fabrication/generator/base_spec.rb
|
169
170
|
- spec/fabrication/schematic_spec.rb
|
170
|
-
- spec/fabrication/
|
171
|
+
- spec/fabrication/sequencer_spec.rb
|
171
172
|
- spec/fabrication/support_spec.rb
|
172
173
|
- spec/fabrication_spec.rb
|
173
174
|
- spec/fabricators/active_record_fabricator.rb
|
@@ -175,6 +176,7 @@ files:
|
|
175
176
|
- spec/fabricators/mongoid_fabricator.rb
|
176
177
|
- spec/fabricators/plain_old_ruby_object_fabricator.rb
|
177
178
|
- spec/fabricators/sequel_fabricator.rb
|
179
|
+
- spec/fabricators/sequencer_fabricator.rb
|
178
180
|
- spec/spec_helper.rb
|
179
181
|
- spec/support/active_record.rb
|
180
182
|
- spec/support/mongoid.rb
|
@@ -205,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
205
207
|
requirements: []
|
206
208
|
|
207
209
|
rubyforge_project:
|
208
|
-
rubygems_version: 1.
|
210
|
+
rubygems_version: 1.8.6
|
209
211
|
signing_key:
|
210
212
|
specification_version: 3
|
211
213
|
summary: Fabrication provides a robust solution for test object generation.
|