fixture_replacement 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +2 -0
  2. data/CHANGELOG.rdoc +720 -0
  3. data/GPL_LICENSE +674 -0
  4. data/MIT_LICENSE +22 -0
  5. data/README.rdoc +154 -0
  6. data/Rakefile +8 -0
  7. data/TODO.rdoc +22 -0
  8. data/VERSION +1 -0
  9. data/contributions.rdoc +48 -0
  10. data/etc/bug_reports/2007_09_28_linoj.txt +51 -0
  11. data/etc/google_analytics +6 -0
  12. data/etc/patches/2007_09_14_default_model_name_with_arguments.diff +26 -0
  13. data/etc/patches/2007_10_14_active_record_specs.diff +396 -0
  14. data/etc/patches/2007_10_14_protected_attributes.diff +26 -0
  15. data/etc/patches/2007_10_14_send_patch.diff +79 -0
  16. data/etc/patches/2007_10_14_spelling_error_in_comments.diff +13 -0
  17. data/etc/patches/2007_10_17_protected_attributes_second_go.diff +212 -0
  18. data/etc/patches/2007_10_18_README.diff +137 -0
  19. data/etc/patches/2007_10_19_readme_tweak.diff +12 -0
  20. data/etc/patches/2007_10_19_silence_migration.diff +12 -0
  21. data/etc/patches/2007_10_25_changed_classify_to_camelize.diff +38 -0
  22. data/etc/patches/2007_11_20_fixture_replacement_generator_should_not_reload_environment_or_boot.patch +13 -0
  23. data/etc/patches/2007_11_20_string_random_refactor_and_extension.patch +104 -0
  24. data/etc/patches/2007_11_20_string_random_refactor_and_extension_v2.patch +108 -0
  25. data/fixture_replacement.gemspec +126 -0
  26. data/lib/fixture_replacement.rb +16 -0
  27. data/lib/fixture_replacement/attribute_builder.rb +129 -0
  28. data/lib/fixture_replacement/class_methods.rb +45 -0
  29. data/lib/fixture_replacement/method_generator.rb +38 -0
  30. data/lib/fixture_replacement/version.rb +13 -0
  31. data/lib/fr.rb +1 -0
  32. data/philosophy_and_bugs.rdoc +128 -0
  33. data/rake_tasks/code_quality.rb +37 -0
  34. data/rake_tasks/docs.rb +59 -0
  35. data/rake_tasks/gem.rb +27 -0
  36. data/rake_tasks/specs.rb +7 -0
  37. data/rake_tasks/tests.rb +7 -0
  38. data/rake_tasks/website.rb +11 -0
  39. data/spec/fixture_replacement/attribute_builder_spec.rb +162 -0
  40. data/spec/fixture_replacement/fixture_replacement_spec.rb +96 -0
  41. data/spec/fixture_replacement/fixtures/classes.rb +78 -0
  42. data/spec/fixture_replacement/fr_spec.rb +7 -0
  43. data/spec/fixture_replacement/integration/attr_protected_attributes_spec.rb +71 -0
  44. data/spec/fixture_replacement/integration/attributes_for_with_invalid_keys_spec.rb +14 -0
  45. data/spec/fixture_replacement/integration/attributes_from_spec_without_block.rb +52 -0
  46. data/spec/fixture_replacement/integration/create_method_spec.rb +61 -0
  47. data/spec/fixture_replacement/integration/cyclic_dependency_spec.rb +32 -0
  48. data/spec/fixture_replacement/integration/default_warnings_spec.rb +29 -0
  49. data/spec/fixture_replacement/integration/extend_spec.rb +37 -0
  50. data/spec/fixture_replacement/integration/has_and_belongs_to_many_spec.rb +64 -0
  51. data/spec/fixture_replacement/integration/new_method_spec.rb +96 -0
  52. data/spec/fixture_replacement/integration/private_methods_spec.rb +43 -0
  53. data/spec/fixture_replacement/integration/public_methods_spec.rb +21 -0
  54. data/spec/fixture_replacement/integration/valid_attributes_spec.rb +41 -0
  55. data/spec/fixture_replacement/integration/validation_spec.rb +54 -0
  56. data/spec/fixture_replacement/regressions/2008_01_21_random_string_with_sti_spec.rb +71 -0
  57. data/spec/fixture_replacement/version_spec.rb +7 -0
  58. data/spec/spec.opts +1 -0
  59. data/spec/spec_helper.rb +16 -0
  60. data/spec/spec_helpers.rb +117 -0
  61. data/test/test_helper.rb +21 -0
  62. data/test/unit/user_test.rb +57 -0
  63. metadata +143 -0
@@ -0,0 +1,26 @@
1
+ Index: lib/fixture_replacement/fixture_replacement.rb
2
+ ===================================================================
3
+ --- lib/fixture_replacement/fixture_replacement.rb (revision 31)
4
+ +++ lib/fixture_replacement/fixture_replacement.rb (working copy)
5
+ @@ -70,7 +70,9 @@
6
+ hash_given = args[0] || Hash.new
7
+ merged_hash = self.send(attributes_method).merge(hash_given)
8
+ evaluated_hash = Generator.merge_unevaluated_method(self, :create, merged_hash)
9
+ - obj = class_name.create!(evaluated_hash)
10
+ + obj = class_name.new
11
+ + evaluated_hash.each { |k, v| obj.update_attribute(k, v) }
12
+ + obj.save!
13
+ obj
14
+ end
15
+ end
16
+ @@ -86,7 +88,9 @@
17
+ hash_given = args[0] || Hash.new
18
+ merged_hash = self.send(attributes_method).merge(hash_given)
19
+ evaluated_hash = Generator.merge_unevaluated_method(self, :create, merged_hash)
20
+ - class_name.new(evaluated_hash)
21
+ + obj = class_name.new
22
+ + evaluated_hash.each { |k, v| obj.update_attribute(k, v) }
23
+ + obj
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,79 @@
1
+ Index: lib/fixture_replacement/fixture_replacement.rb
2
+ ===================================================================
3
+ --- lib/fixture_replacement/fixture_replacement.rb (revision 31)
4
+ +++ lib/fixture_replacement/fixture_replacement.rb (working copy)
5
+ @@ -70,7 +70,9 @@
6
+ hash_given = args[0] || Hash.new
7
+ merged_hash = self.send(attributes_method).merge(hash_given)
8
+ evaluated_hash = Generator.merge_unevaluated_method(self, :create, merged_hash)
9
+ - obj = class_name.create!(evaluated_hash)
10
+ + obj = class_name.new
11
+ + evaluated_hash.each { |k, v| obj.send("#{k}=", v) }
12
+ + obj.save!
13
+ obj
14
+ end
15
+ end
16
+ @@ -86,7 +88,9 @@
17
+ hash_given = args[0] || Hash.new
18
+ merged_hash = self.send(attributes_method).merge(hash_given)
19
+ evaluated_hash = Generator.merge_unevaluated_method(self, :create, merged_hash)
20
+ - class_name.new(evaluated_hash)
21
+ + obj = class_name.new
22
+ + evaluated_hash.each { |k, v| obj.send("#{k}=", v) }
23
+ + obj
24
+ end
25
+ end
26
+ end
27
+ Index: spec/fixture_replacement/fixture_replacement_spec.rb
28
+ ===================================================================
29
+ --- spec/fixture_replacement/fixture_replacement_spec.rb (revision 31)
30
+ +++ spec/fixture_replacement/fixture_replacement_spec.rb (working copy)
31
+ @@ -18,6 +18,14 @@
32
+
33
+ attr_reader :hash
34
+
35
+ + def method_missing symbol, *args
36
+ + if symbol.id2name =~ /\A(.+)=\z/
37
+ + @hash[$~[1].to_sym] = *args
38
+ + else
39
+ + super
40
+ + end
41
+ + end
42
+ +
43
+ def gender # this would be a has_many call in rails
44
+ 17
45
+ end
46
+ @@ -222,14 +230,14 @@
47
+ created_gender.hash.should == {:sex => "Female"}
48
+ end
49
+
50
+ - it "should call Gender.create! when the default_gender method is evaluated by default_gender" do
51
+ + it "should call Gender.save! when the default_gender method is evaluated by default_gender" do
52
+ gender = Gender.new
53
+ - Gender.should_receive(:create!).and_return gender
54
+ + Gender.should_receive(:save!).and_return gender
55
+ @instance.create_user
56
+ end
57
+
58
+ - it "should not call Gender.create! if the default_gender is overwritten by another value (say, a string)" do
59
+ - Gender.should_not_receive(:create!)
60
+ + it "should not call Gender.save! if the default_gender is overwritten by another value (say, a string)" do
61
+ + Gender.should_not_receive(:save!)
62
+ @instance.create_user(:gender => "a string")
63
+ end
64
+ end
65
+ @@ -385,12 +393,12 @@
66
+ end
67
+
68
+ it "should call Gender.save! when the default_gender method is evaluated by default_gender" do
69
+ - Gender.should_receive(:create!)
70
+ + Gender.should_receive(:save!)
71
+ @instance.new_user
72
+ end
73
+
74
+ it "should not call Gender.new if the default_gender is overwritten by another value (say, a string)" do
75
+ - Gender.should_not_receive(:create!)
76
+ + Gender.should_not_receive(:save!)
77
+ @instance.new_user(:gender => "a string")
78
+ end
79
+
@@ -0,0 +1,13 @@
1
+ Index: lib/fixture_replacement/fixture_replacement.rb
2
+ ===================================================================
3
+ --- lib/fixture_replacement/fixture_replacement.rb (revision 31)
4
+ +++ lib/fixture_replacement/fixture_replacement.rb (working copy)
5
+ @@ -22,7 +22,7 @@
6
+ end
7
+
8
+ # This uses a DelayedEvaluationProc, not a typical proc, for type checking.
9
+ - # It maybe absurd to try to store a proc in a database, but even if someone tries,
10
+ + # It may be absurd to try to store a proc in a database, but even if someone tries,
11
+ # they won't get an error from FixtureReplacement, since the error would be incredibly unclear
12
+ def merge_unevaluated_method(obj, method_for_instantiation, hash={})
13
+ hash.each do |key, value|
@@ -0,0 +1,212 @@
1
+ Index: lib/fixture_replacement/fixture_replacement.rb
2
+ ===================================================================
3
+ --- lib/fixture_replacement/fixture_replacement.rb (revision 52)
4
+ +++ lib/fixture_replacement/fixture_replacement.rb (working copy)
5
+ @@ -70,7 +70,12 @@
6
+ hash_given = args[0] || Hash.new
7
+ merged_hash = self.send(attributes_method).merge(hash_given)
8
+ evaluated_hash = Generator.merge_unevaluated_method(self, :create, merged_hash)
9
+ - obj = class_name.create!(evaluated_hash)
10
+ +
11
+ + # we are NOT doing the following, because of attr_protected:
12
+ + # obj = class_name.create!(evaluated_hash)
13
+ + obj = class_name.new
14
+ + evaluated_hash.each { |key, value| obj.send("#{key}=", value) }
15
+ + obj.save!
16
+ obj
17
+ end
18
+ end
19
+ @@ -86,7 +91,11 @@
20
+ hash_given = args[0] || Hash.new
21
+ merged_hash = self.send(attributes_method).merge(hash_given)
22
+ evaluated_hash = Generator.merge_unevaluated_method(self, :create, merged_hash)
23
+ - class_name.new(evaluated_hash)
24
+ +
25
+ + # we are also doing the following because of attr_protected:
26
+ + obj = class_name.new
27
+ + evaluated_hash.each { |key, value| obj.send("#{key}=", value) }
28
+ + obj
29
+ end
30
+ end
31
+ end
32
+ Index: spec/fixture_replacement/fixture_replacement_spec.rb
33
+ ===================================================================
34
+ --- spec/fixture_replacement/fixture_replacement_spec.rb (revision 52)
35
+ +++ spec/fixture_replacement/fixture_replacement_spec.rb (working copy)
36
+ @@ -9,9 +9,88 @@
37
+ belongs_to :gender
38
+ end
39
+
40
+ +class Admin < ActiveRecord::Base
41
+ + attr_protected :admin_status
42
+ +end
43
+ +
44
+ class Gender < ActiveRecord::Base; end
45
+
46
+ +
47
+ +
48
+ +
49
+ module FixtureReplacement
50
+ +
51
+ + describe "create_user with attr_protected attributes" do
52
+ + before :each do
53
+ + FixtureReplacement.module_eval do
54
+ + def admin_attributes
55
+ + {
56
+ + :admin_status => true,
57
+ + :name => "Scott"
58
+ + }
59
+ + end
60
+ + end
61
+ +
62
+ + @generator = Generator.new("admin")
63
+ + @generator.generate_create_method
64
+ + end
65
+ +
66
+ + it "should not complain when an apparent mass assignment has happened with default values" do
67
+ + lambda {
68
+ + create_admin
69
+ + }.should_not raise_error
70
+ + end
71
+ +
72
+ + it "should have admin_status equal to the default value (when it has not been overwritten)" do
73
+ + create_admin.admin_status.should == true
74
+ + end
75
+ +
76
+ + it "should have admin_status equal to the overwritten value" do
77
+ + create_admin(:admin_status => false).admin_status.should be_false
78
+ + end
79
+ +
80
+ + it "should have the other attributes assigned when the attr_value has been overwritten" do
81
+ + create_admin(:admin_status => false).name.should == "Scott"
82
+ + end
83
+ +
84
+ + it "should have the other attributes assigned even when the attr_value has not been overwritten" do
85
+ + create_admin.name.should == "Scott"
86
+ + end
87
+ + end
88
+ +
89
+ + describe "new_user with attr_protected attributes" do
90
+ + before :each do
91
+ + FixtureReplacement.module_eval do
92
+ + def admin_attributes
93
+ + {
94
+ + :admin_status => true,
95
+ + :name => "Scott"
96
+ + }
97
+ + end
98
+ + end
99
+ +
100
+ + @generator = Generator.new("admin")
101
+ + @generator.generate_new_method
102
+ + end
103
+ +
104
+ + it "should have admin_status equal to the default value (when it has not been overwritten)" do
105
+ + new_admin.admin_status.should == true
106
+ + end
107
+ +
108
+ + it "should have admin_status equal to the overwritten value" do
109
+ + new_admin(:admin_status => false).admin_status.should be_false
110
+ + end
111
+ +
112
+ + it "should have the other attributes assigned when the attr_value has been overwritten" do
113
+ + new_admin(:admin_status => false).name.should == "Scott"
114
+ + end
115
+ +
116
+ + it "should have the other attributes assigned even when the attr_value has not been overwritten" do
117
+ + new_admin.name.should == "Scott"
118
+ + end
119
+ + end
120
+ +
121
+ +
122
+ describe Generator, "creation" do
123
+ before :each do
124
+ @generator = Generator.new("user")
125
+ @@ -121,9 +200,14 @@
126
+ create_user.should_not be_a_new_record
127
+ end
128
+
129
+ - it "should save the user with create!" do
130
+ + it "should save the user with save!" do
131
+ @generator.generate_create_method
132
+ - User.should_receive(:create!).with({:key => "val"})
133
+ +
134
+ + @user = mock('User', :null_object => true)
135
+ + @user.stub!(:save!).and_return true
136
+ + User.stub!(:new).and_return @user
137
+ +
138
+ + @user.should_receive(:save!).with(no_args)
139
+ create_user
140
+ end
141
+
142
+ @@ -175,13 +259,20 @@
143
+ created_gender.sex.should == "Female"
144
+ end
145
+
146
+ - it "should call Gender.create! when the default_gender method is evaluated by default_gender" do
147
+ - Gender.should_receive(:create!).with({:sex => "Male"})
148
+ + it "should call save! when the default_gender method is evaluated by default_gender" do
149
+ + @gender = mock('Gender', :null_object => true)
150
+ + Gender.stub!(:new).and_return @gender
151
+ +
152
+ + @user = mock('User', :null_object => true)
153
+ + User.stub!(:new).and_return @user
154
+ + @user.stub!(:gender=).and_return @gender
155
+ +
156
+ + @gender.should_receive(:save!).with(no_args)
157
+ create_user
158
+ end
159
+
160
+ - it "should not call Gender.create! if the default_gender is overwritten by another value" do
161
+ - Gender.should_not_receive(:create!)
162
+ + it "should not call Gender.save! if the default_gender is overwritten by another value" do
163
+ + Gender.should_not_receive(:save!)
164
+ create_user(:gender => Gender.new)
165
+ end
166
+ end
167
+ @@ -324,18 +415,24 @@
168
+ new_gender.sex.should == "unknown"
169
+ end
170
+
171
+ - it "should call Gender.create! when the default_gender method is evaluated by default_gender" do
172
+ - Gender.should_receive(:create!).with({:sex => "Male"})
173
+ + it "should call Gender.save! when the default_gender method is evaluated by default_gender" do
174
+ + @gender = mock('Gender', :null_object => true)
175
+ + Gender.stub!(:new).and_return @gender
176
+ + @user = mock('User')
177
+ + @user.stub!(:gender=).and_return @gender
178
+ + User.stub!(:new).and_return @user
179
+ +
180
+ + @gender.should_receive(:save!)
181
+ new_user
182
+ end
183
+
184
+ - it "should not call Gender.create! if the default_gender is overwritten by another value" do
185
+ - Gender.should_not_receive(:create!)
186
+ + it "should not call Gender.save! if the default_gender is overwritten by another value" do
187
+ + Gender.should_not_receive(:save!)
188
+ new_user(:gender => Gender.new)
189
+ end
190
+
191
+ it "should be able to overwrite a default_* method" do
192
+ - Gender.should_not_receive(:create!).with({:sex => "Male"})
193
+ + Gender.should_not_receive(:save!)
194
+ new_user(:gender => Gender.create!(:sex => "Female"))
195
+ end
196
+ end
197
+ Index: spec/spec_helper.rb
198
+ ===================================================================
199
+ --- spec/spec_helper.rb (revision 52)
200
+ +++ spec/spec_helper.rb (working copy)
201
+ @@ -21,6 +21,11 @@
202
+ t.column :gender_id, :string
203
+ end
204
+
205
+ + create_table :admins do |t|
206
+ + t.column :admin_status, :boolean
207
+ + t.column :name, :string
208
+ + end
209
+ +
210
+ end
211
+
212
+
@@ -0,0 +1,137 @@
1
+ Index: README
2
+ ===================================================================
3
+ --- README (revision 60)
4
+ +++ README (working copy)
5
+ @@ -1,32 +1,92 @@
6
+ = FixtureReplacement
7
+
8
+ -=== How to use FixtureReplacement
9
+ +== What is FixtureReplacement
10
+
11
+ -Full Documentation is coming, when time permits. For now, watch this screencast (and forward
12
+ -through my stupidity):
13
+ +FixtureReplacement is a Rails[http://rubyonrails.org/] plugin that provides a simple way to quickly populate your test database with model objects without having to manage multiple, brittle fixture files. You can easily set up complex object graphs (with models which reference other models) and add new objects on the fly.
14
+
15
+ -http://railsnewbie.com/files/fixture_replacement_demo.mov
16
+ +Not only can FixtureReplacement make your test data easier to maintain, it can also help to make your tests and specs much more readable and intention-revealing by allowing you to omit extraneous details and focus only on the attributes that are important for a particular behaviour. It works well with both RSpec[http://rspec.rubyforge.org/] and Test::Unit[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html].
17
+
18
+ -There is also some documentation at the following link, although the screencast is still advised:
19
+ +== How to use FixtureReplacement
20
+
21
+ -http://wincent.com/knowledge-base/FixtureReplacement_cheatsheet
22
+ +=== Defining default attributes
23
+
24
+ +At the heart of FixtureReplacement is the <tt>db/example_data.rb</tt> file where you define the default attributes for each of your test models. This example shows a <tt>user_attributes</tt> method that returns the attributes for an instance of the <tt>User</tt> model:
25
+
26
+ -=== Installation
27
+ + module FixtureReplacement
28
+ + def user_attributes
29
+ + password = String.random
30
+ + {
31
+ + :value => "a value",
32
+ + :other => "other value",
33
+ + :another => String.random, # random string 10 characters long
34
+ + :one_more => String.random(15), # 15 characters long
35
+ + :password => password,
36
+ + :password_confirmation => password,
37
+ + :associated_object => default_bar # expects bar_attributes to be defined
38
+ + }
39
+ + end
40
+ + end
41
+
42
+ +Note that:
43
+ +
44
+ +- the method returns a hash of attributes
45
+ +- a String.random method is provided for attributes whose exact value isn't important; this means you can create multiple, unique model instances
46
+ +- you can perform arbitrary set-up and execute any Ruby code prior to returning the hash (as shown here where a <tt>password</tt> is generated and then used for both the <tt>:password</tt> and <tt>:password_confirmation</tt> attributes)
47
+ +- a <tt>default_modelname</tt> method is automatically provided that allows you to set up dependent model objects (in this case an instance of the <tt>Bar</tt> model)
48
+ +
49
+ +=== Available methods
50
+ +
51
+ +Based on the above definition FixtureReplacement makes the following methods available:
52
+ +
53
+ +- String.random: generates a random string as shown above
54
+ +- <tt>new_user</tt>: equivalent to <tt>User.new(user_attributes)</tt>.
55
+ +- <tt>create_user</tt>: equivalent to <tt>User.create!(user_attributes)</tt>.
56
+ +- <tt>default_user</tt>: for use inside <tt>model_attributes</tt> definitions; this basically returns a <tt>Proc</tt> object which allows the actual creation of the object to be deferred until it is actually needed: in this way unnecessary object creation is avoided until it is known for sure that a particular attribute is not going to be overridden.
57
+ +
58
+ +=== Overriding attributes
59
+ +
60
+ +Overrides of specific attributes can be performed as follows:
61
+ +
62
+ + new_user(:thing => "overridden")
63
+ + create_user(:thing => "overridden")
64
+ +
65
+ +=== Screencast
66
+ +
67
+ +Further documentation is forthcoming, but for now the following screencast provides a powerful demonstration of how FixtureReplacement can help you to set up complex, flexible object graphs with ease:
68
+ +
69
+ +http://railsnewbie.com/files/fixture_replacement_demo.mov
70
+ +
71
+ +== Installation
72
+ +
73
+ ruby script/plugin install http://thmadb.com/public_svn/plugins/fixture_replacement/
74
+
75
+ Or use externals:
76
+
77
+ ruby script/plugin install -x http://thmadb.com/public_svn/plugins/fixture_replacement/
78
+
79
+ -Run the generator if you don't have the file db/example_data.rb
80
+ +Run the generator if you don't have the file <tt>db/example_data.rb</tt>:
81
+
82
+ ruby script/generate fixture_replacement
83
+
84
+ +=== Using from within <tt>script/console</tt>
85
+
86
+ -=== Running the Specs/Tests for FixtureReplacement
87
+ + % script/console
88
+ + >> include FixtureReplacement
89
+
90
+ +=== Using from with RSpec
91
+ +
92
+ +Add the following to your <tt>spec/spec_helper.rb</tt> file:
93
+ +
94
+ + include FixtureReplacement
95
+ +
96
+ +=== Using with Test::Unit
97
+ +
98
+ +Add the following to your <tt>test/test_helper.rb</tt> file:
99
+ +
100
+ + include FixtureReplacement
101
+ +
102
+ +== Running the Specs/Tests for FixtureReplacement
103
+ +
104
+ You will need rspec (version 1.0.8 or later) to run the specs, as well as the sqlite3-ruby gem
105
+ (and sqlite3 installed):
106
+
107
+ @@ -37,16 +97,16 @@
108
+
109
+ % cd vendor/plugins/fixture_replacement
110
+
111
+ -Then run with rake
112
+ +Then run with <tt>rake<tt>
113
+
114
+ % rake
115
+
116
+ -=== Specdocs
117
+ +== Specdocs
118
+
119
+ Specdocs can be found here[http://replacefixtures.rubyforge.org/specdoc.html]
120
+
121
+
122
+ -=== Patches, Contributions:
123
+ +== Patches, Contributions:
124
+
125
+ Thanks to the following:
126
+
127
+ @@ -62,8 +122,7 @@
128
+
129
+ If you would like to change how this software works, please submit a patch with specs to scott@railsnewbie.com
130
+
131
+ +== License
132
+
133
+ -=== License
134
+ -
135
+ This software is released under the MIT License. See the license agreement
136
+ -in lib/fixture_replacement.rb
137
+ +in <tt>lib/fixture_replacement.rb</tt>