fixture_replacement 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG.rdoc +720 -0
- data/GPL_LICENSE +674 -0
- data/MIT_LICENSE +22 -0
- data/README.rdoc +154 -0
- data/Rakefile +8 -0
- data/TODO.rdoc +22 -0
- data/VERSION +1 -0
- data/contributions.rdoc +48 -0
- data/etc/bug_reports/2007_09_28_linoj.txt +51 -0
- data/etc/google_analytics +6 -0
- data/etc/patches/2007_09_14_default_model_name_with_arguments.diff +26 -0
- data/etc/patches/2007_10_14_active_record_specs.diff +396 -0
- data/etc/patches/2007_10_14_protected_attributes.diff +26 -0
- data/etc/patches/2007_10_14_send_patch.diff +79 -0
- data/etc/patches/2007_10_14_spelling_error_in_comments.diff +13 -0
- data/etc/patches/2007_10_17_protected_attributes_second_go.diff +212 -0
- data/etc/patches/2007_10_18_README.diff +137 -0
- data/etc/patches/2007_10_19_readme_tweak.diff +12 -0
- data/etc/patches/2007_10_19_silence_migration.diff +12 -0
- data/etc/patches/2007_10_25_changed_classify_to_camelize.diff +38 -0
- data/etc/patches/2007_11_20_fixture_replacement_generator_should_not_reload_environment_or_boot.patch +13 -0
- data/etc/patches/2007_11_20_string_random_refactor_and_extension.patch +104 -0
- data/etc/patches/2007_11_20_string_random_refactor_and_extension_v2.patch +108 -0
- data/fixture_replacement.gemspec +126 -0
- data/lib/fixture_replacement.rb +16 -0
- data/lib/fixture_replacement/attribute_builder.rb +129 -0
- data/lib/fixture_replacement/class_methods.rb +45 -0
- data/lib/fixture_replacement/method_generator.rb +38 -0
- data/lib/fixture_replacement/version.rb +13 -0
- data/lib/fr.rb +1 -0
- data/philosophy_and_bugs.rdoc +128 -0
- data/rake_tasks/code_quality.rb +37 -0
- data/rake_tasks/docs.rb +59 -0
- data/rake_tasks/gem.rb +27 -0
- data/rake_tasks/specs.rb +7 -0
- data/rake_tasks/tests.rb +7 -0
- data/rake_tasks/website.rb +11 -0
- data/spec/fixture_replacement/attribute_builder_spec.rb +162 -0
- data/spec/fixture_replacement/fixture_replacement_spec.rb +96 -0
- data/spec/fixture_replacement/fixtures/classes.rb +78 -0
- data/spec/fixture_replacement/fr_spec.rb +7 -0
- data/spec/fixture_replacement/integration/attr_protected_attributes_spec.rb +71 -0
- data/spec/fixture_replacement/integration/attributes_for_with_invalid_keys_spec.rb +14 -0
- data/spec/fixture_replacement/integration/attributes_from_spec_without_block.rb +52 -0
- data/spec/fixture_replacement/integration/create_method_spec.rb +61 -0
- data/spec/fixture_replacement/integration/cyclic_dependency_spec.rb +32 -0
- data/spec/fixture_replacement/integration/default_warnings_spec.rb +29 -0
- data/spec/fixture_replacement/integration/extend_spec.rb +37 -0
- data/spec/fixture_replacement/integration/has_and_belongs_to_many_spec.rb +64 -0
- data/spec/fixture_replacement/integration/new_method_spec.rb +96 -0
- data/spec/fixture_replacement/integration/private_methods_spec.rb +43 -0
- data/spec/fixture_replacement/integration/public_methods_spec.rb +21 -0
- data/spec/fixture_replacement/integration/valid_attributes_spec.rb +41 -0
- data/spec/fixture_replacement/integration/validation_spec.rb +54 -0
- data/spec/fixture_replacement/regressions/2008_01_21_random_string_with_sti_spec.rb +71 -0
- data/spec/fixture_replacement/version_spec.rb +7 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/spec_helpers.rb +117 -0
- data/test/test_helper.rb +21 -0
- data/test/unit/user_test.rb +57 -0
- metadata +143 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
+
|
3
|
+
describe FixtureReplacement do
|
4
|
+
describe "random_string" do
|
5
|
+
before do
|
6
|
+
@fr = FixtureReplacement
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not be the same as another randomly generated string" do
|
10
|
+
@fr.random_string.should_not == @fr.random_string
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should by default be 10 characters long" do
|
14
|
+
@fr.random_string.size.should == 10
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be able to specify the length of the random string" do
|
18
|
+
@fr.random_string(100).size.should == 100
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should only generate lowercase letters" do
|
22
|
+
s = @fr.random_string(100)
|
23
|
+
s.upcase.should == s.swapcase
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "requiring the fixtures file" do
|
28
|
+
before do
|
29
|
+
FixtureReplacement.stub!(:load)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should require db/example_data" do
|
33
|
+
FixtureReplacement.stub!(:rails_root).and_return "/foo/bar"
|
34
|
+
FixtureReplacement.should_receive(:load).with("/foo/bar/db/example_data.rb")
|
35
|
+
|
36
|
+
FixtureReplacement.load_example_data
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should use the correct rails root" do
|
40
|
+
FixtureReplacement.stub!(:rails_root).and_return "/rails/root"
|
41
|
+
FixtureReplacement.should_receive(:load).with("/rails/root/db/example_data.rb")
|
42
|
+
|
43
|
+
FixtureReplacement.load_example_data
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should not blow up if the file is not found" do
|
47
|
+
FixtureReplacement.stub!(:load).and_raise LoadError
|
48
|
+
|
49
|
+
lambda {
|
50
|
+
FixtureReplacement.load_example_data
|
51
|
+
}.should_not raise_error
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "rails_root" do
|
56
|
+
def use_rails_root(rails_root, &block)
|
57
|
+
silence_warnings do
|
58
|
+
Object.const_set(:RAILS_ROOT, rails_root)
|
59
|
+
end
|
60
|
+
block.call
|
61
|
+
ensure
|
62
|
+
Object.send :remove_const, :RAILS_ROOT
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should be the RAILS_ROOT constant if given" do
|
66
|
+
use_rails_root "/rails/root" do
|
67
|
+
FR.rails_root.should == "/rails/root"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should use the correct RAILS_ROOT" do
|
72
|
+
use_rails_root "/foo/bar" do
|
73
|
+
FR.rails_root.should == "/foo/bar"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should be '.' if not defined" do
|
78
|
+
FR.rails_root.should == "."
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "reload!" do
|
83
|
+
it "should call load on the main fixture replacement file" do
|
84
|
+
file_path = File.expand_path(File.dirname(__FILE__) + "/../../lib/fixture_replacement.rb")
|
85
|
+
FixtureReplacement.should_receive(:load).with(file_path)
|
86
|
+
|
87
|
+
FixtureReplacement.reload!
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "method_added" do
|
92
|
+
it "should be private" do
|
93
|
+
FixtureReplacement.should_not respond_to(:method_added)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
unless defined?(User)
|
2
|
+
class User < ActiveRecord::Base
|
3
|
+
belongs_to :gender
|
4
|
+
end
|
5
|
+
|
6
|
+
class Member < ActiveRecord::Base; end
|
7
|
+
|
8
|
+
class Post < ActiveRecord::Base
|
9
|
+
has_many :comments
|
10
|
+
end
|
11
|
+
|
12
|
+
class Comment < ActiveRecord::Base
|
13
|
+
belongs_to :post
|
14
|
+
end
|
15
|
+
|
16
|
+
class Player < ActiveRecord::Base
|
17
|
+
end
|
18
|
+
|
19
|
+
class Alien < ActiveRecord::Base
|
20
|
+
belongs_to :gender
|
21
|
+
end
|
22
|
+
|
23
|
+
class Admin < ActiveRecord::Base
|
24
|
+
attr_protected :admin_status
|
25
|
+
end
|
26
|
+
|
27
|
+
class Gender < ActiveRecord::Base; end
|
28
|
+
class Actress < ActiveRecord::Base; end
|
29
|
+
|
30
|
+
class Item < ActiveRecord::Base
|
31
|
+
belongs_to :category
|
32
|
+
end
|
33
|
+
|
34
|
+
class Writing < Item; end
|
35
|
+
|
36
|
+
class Category < ActiveRecord::Base
|
37
|
+
has_many :items
|
38
|
+
end
|
39
|
+
|
40
|
+
class Subscriber < ActiveRecord::Base
|
41
|
+
has_and_belongs_to_many :subscriptions
|
42
|
+
end
|
43
|
+
|
44
|
+
class Subscription < ActiveRecord::Base
|
45
|
+
has_and_belongs_to_many :subscribers
|
46
|
+
end
|
47
|
+
|
48
|
+
class Event < ActiveRecord::Base
|
49
|
+
has_one :schedule
|
50
|
+
end
|
51
|
+
|
52
|
+
class Schedule < ActiveRecord::Base
|
53
|
+
belongs_to :event
|
54
|
+
end
|
55
|
+
|
56
|
+
class Foo; end
|
57
|
+
class Bar; end
|
58
|
+
|
59
|
+
class NoValidation < ActiveRecord::Base
|
60
|
+
end
|
61
|
+
|
62
|
+
class ValidateName < ActiveRecord::Base
|
63
|
+
validates_presence_of :name
|
64
|
+
end
|
65
|
+
|
66
|
+
class ValidateNameTwo < ActiveRecord::Base
|
67
|
+
validates_presence_of :name
|
68
|
+
end
|
69
|
+
|
70
|
+
class AddressWithValidCity < ActiveRecord::Base
|
71
|
+
validates_presence_of :city
|
72
|
+
end
|
73
|
+
|
74
|
+
class AddressWithValidCityAndState < ActiveRecord::Base
|
75
|
+
validates_presence_of :city
|
76
|
+
validates_presence_of :state
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../spec_helper"
|
2
|
+
|
3
|
+
module FixtureReplacement
|
4
|
+
describe "create_user with attr_protected attributes" do
|
5
|
+
before :each do
|
6
|
+
@obj = use_module do
|
7
|
+
attributes_for :admin do |u|
|
8
|
+
u.admin_status = true
|
9
|
+
u.name = "Scott"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should not complain when an apparent mass assignment has happened with default values" do
|
15
|
+
lambda {
|
16
|
+
@obj.create_admin
|
17
|
+
}.should_not raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not be a new record" do
|
21
|
+
@obj.create_admin.should_not be_a_new_record
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should have admin_status equal to the default value (when it has not been overwritten)" do
|
25
|
+
@obj.create_admin.admin_status.should == true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should have admin_status equal to the overwritten value" do
|
29
|
+
@obj.create_admin(:admin_status => false).admin_status.should be_false
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should have the other attributes assigned when the attr_value has been overwritten" do
|
33
|
+
@obj.create_admin(:admin_status => false).name.should == "Scott"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should have the other attributes assigned even when the attr_value has not been overwritten" do
|
37
|
+
@obj.create_admin.name.should == "Scott"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "new_user with attr_protected attributes" do
|
42
|
+
before :each do
|
43
|
+
@obj = use_module do |s|
|
44
|
+
attributes_for :admin do |s|
|
45
|
+
s.admin_status = true
|
46
|
+
s.name = "Scott"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return a new Admin with new_admin" do
|
52
|
+
@obj.new_admin.should be_a_kind_of(Admin)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should have admin_status equal to the default value (when it has not been overwritten)" do
|
56
|
+
@obj.new_admin.admin_status.should == true
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should have admin_status equal to the overwritten value" do
|
60
|
+
@obj.new_admin(:admin_status => false).admin_status.should be_false
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should have the other attributes assigned when the attr_value has been overwritten" do
|
64
|
+
@obj.new_admin(:admin_status => false).name.should == "Scott"
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should have the other attributes assigned even when the attr_value has not been overwritten" do
|
68
|
+
@obj.new_admin.name.should == "Scott"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../spec_helper"
|
2
|
+
|
3
|
+
module FixtureReplacement
|
4
|
+
describe "keys passed to attributes for" do
|
5
|
+
it "should raise an invalid key error when an invalid key is passed" do
|
6
|
+
lambda {
|
7
|
+
use_module do
|
8
|
+
attributes_for :user, :class_name => "foo"
|
9
|
+
end
|
10
|
+
}.should raise_error(FixtureReplacement::AttributeBuilder::InvalidKeyError,
|
11
|
+
":class_name is not a valid option to attributes_for. Valid keys are: [:from, :class]")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../spec_helper"
|
2
|
+
|
3
|
+
module FixtureReplacement
|
4
|
+
describe AttributeBuilder do
|
5
|
+
include FixtureReplacementControllerHelper
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
@obj = use_module do
|
9
|
+
attributes_for :user do |u|
|
10
|
+
u.username = random_string
|
11
|
+
u.key = random_string
|
12
|
+
end
|
13
|
+
|
14
|
+
attributes_for :scott, :from => :user
|
15
|
+
|
16
|
+
attributes_for :foo, :class => User
|
17
|
+
|
18
|
+
attributes_for :admin do |a|
|
19
|
+
a.admin_status = true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
models = "user", "foo", "scott"
|
25
|
+
|
26
|
+
models.each do |model|
|
27
|
+
it "should have the new_#{model} method as a (module) method on the module" do
|
28
|
+
@obj.should respond_to("new_#{model}")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should have the new_#{model} method as a private method in the test case" do
|
32
|
+
@obj.private_methods.should include("new_#{model}")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should have the create_#{model} method as a private method in the test case" do
|
36
|
+
@obj.private_methods.should include("create_#{model}")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should have the create_#{model} method as a (module) method on the module" do
|
40
|
+
@obj.should respond_to("create_#{model}")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should have the username as a string (for User) for new_user" do
|
45
|
+
@obj.new_user.username.class.should == String
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should have the username as a string (for User) for create_user" do
|
49
|
+
@obj.create_user.username.class.should == String
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../spec_helper"
|
2
|
+
|
3
|
+
module FixtureReplacement
|
4
|
+
describe "the create_* method" do
|
5
|
+
it "should be a user for attributes_for :user" do
|
6
|
+
obj = use_module do
|
7
|
+
attributes_for :user
|
8
|
+
end
|
9
|
+
|
10
|
+
obj.create_user.should be_a_kind_of(User)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should use the given class" do
|
14
|
+
obj = use_module do
|
15
|
+
attributes_for :foo, :class => User
|
16
|
+
end
|
17
|
+
|
18
|
+
obj.create_foo.should be_a_kind_of(User)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should find the correct class name" do
|
22
|
+
obj = use_module do
|
23
|
+
attributes_for :admin
|
24
|
+
end
|
25
|
+
|
26
|
+
obj.create_admin.should be_a_kind_of(Admin)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should over-write the User's hash with any hash given to create_user" do
|
30
|
+
obj = use_module do
|
31
|
+
attributes_for :user do |u|
|
32
|
+
u.key = "val"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
obj.create_user(:key => "other_value").key.should == "other_value"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should add any hash key-value pairs which weren't previously given in user_attributes" do
|
40
|
+
obj = use_module do
|
41
|
+
attributes_for :user do |u|
|
42
|
+
u.key = "val"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
user = obj.create_user(:other_key => "other_value")
|
47
|
+
user.key.should == "val"
|
48
|
+
user.other_key.should == "other_value"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should not be saved to the database" do
|
52
|
+
obj = use_module do
|
53
|
+
attributes_for :user do |u|
|
54
|
+
u.key = "val"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
obj.create_user.should_not be_a_new_record
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../spec_helper"
|
2
|
+
|
3
|
+
describe "cyclic dependencies" do
|
4
|
+
before do
|
5
|
+
@mod = use_module do
|
6
|
+
attributes_for :event do |e, hash|
|
7
|
+
e.schedule = new_schedule(:event => e) unless hash[:schedule]
|
8
|
+
end
|
9
|
+
|
10
|
+
attributes_for :schedule do |s, hash|
|
11
|
+
s.event = new_event(:schedule => s) unless hash[:event]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should allow them" do
|
17
|
+
@mod.new_event.should be_a_kind_of(Event)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should associate an event with a schedule" do
|
21
|
+
@mod.new_event.schedule.should be_a_kind_of(Schedule)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should associate a schedule with an event" do
|
25
|
+
@mod.new_schedule.event.should be_a_kind_of(Event)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should back associate" do
|
29
|
+
schedule = @mod.new_schedule
|
30
|
+
schedule.event.schedule.should == schedule
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
|
2
|
+
|
3
|
+
describe "default_* warnings" do
|
4
|
+
before do
|
5
|
+
@mod = use_module do
|
6
|
+
attributes_for :post
|
7
|
+
|
8
|
+
attributes_for :comment
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should warn when using default_" do
|
13
|
+
Kernel.should_receive(:warn).with("default_post has been deprecated. Please replace instances of default_post with the new_post method")
|
14
|
+
@mod.default_post
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should use the correct builder name" do
|
18
|
+
Kernel.should_receive(:warn).with("default_comment has been deprecated. Please replace instances of default_comment with the new_comment method")
|
19
|
+
@mod.default_comment
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return a new instance" do
|
23
|
+
Kernel.stub!(:warn).and_return nil
|
24
|
+
|
25
|
+
post = @mod.default_post
|
26
|
+
post.should be_a_kind_of(Post)
|
27
|
+
post.should be_a_new_record
|
28
|
+
end
|
29
|
+
end
|