fabrication 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -53,7 +53,7 @@ class Fabricate
53
53
  }, options, &block)
54
54
  end
55
55
 
56
- def self.sequence(name, start=0, &block)
56
+ def self.sequence(name=Fabrication::Sequencer::DEFAULT, start=0, &block)
57
57
  Fabrication::Sequencer.sequence(name, start, &block)
58
58
  end
59
59
 
@@ -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
- class << self
3
+ DEFAULT = :_default
4
4
 
5
- def sequence(name, start=0)
6
- idx = sequences[name] ||= start
5
+ def self.sequence(name=DEFAULT, start=0)
6
+ idx = sequences[name] ||= start
7
7
 
8
- (block_given? ? yield(idx) : idx).tap do
9
- sequences[name] += 1
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
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -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::Base
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 "Fabricate.sequence" do
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
 
@@ -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
@@ -18,6 +18,11 @@ Fabricator(:author) do
18
18
  end
19
19
  end
20
20
 
21
+ Fabricator(:special_author, :from => :author) do
22
+ mongoid_dynamic_field 50
23
+ lazy_dynamic_field { "foo" }
24
+ end
25
+
21
26
  Fabricator(:hemingway, :from => :author) do
22
27
  name 'Ernest Hemingway'
23
28
  end
@@ -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
@@ -0,0 +1,9 @@
1
+ Fabricator(:sequencer) do
2
+ simple_iterator { sequence }
3
+ param_iterator { sequence(:param_iterator, 9) }
4
+ block_iterator { sequence(:block_iterator) { |i| "block#{i}" } }
5
+ end
6
+
7
+ Fabricator("Sequencer::Namespaced") do
8
+ iterator { sequence(:iterator) }
9
+ 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
@@ -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
 
@@ -14,4 +14,9 @@ class ParentSequelModel < Sequel::Model
14
14
  one_to_many :collection_field, :class => :ChildSequelModel, :key => :parent_sequel_model_id
15
15
 
16
16
  def persisted?; !new? end
17
+
18
+ def before_save
19
+ self.before_save_value = 11
20
+ super
21
+ end
17
22
  end
@@ -8,6 +8,7 @@ Sequel.migration do
8
8
 
9
9
  create_table :parent_sequel_models do
10
10
  primary_key :id
11
+ Integer :before_save_value
11
12
  String :dynamic_field
12
13
  String :nil_field
13
14
  Integer :number_field
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: fabrication
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.1
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-07-02 00:00:00 Z
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.0.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/sequence_spec.rb
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.7.2
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.