fixture_replacement 3.0.1

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.
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,37 @@
1
+ require File.dirname(__FILE__) + "/../../spec_helper"
2
+
3
+ module FixtureReplacement
4
+ describe "extending an object" do
5
+ it "should not create the create_* method in every instance of the class" do
6
+ mod = Module.new do
7
+ extend FixtureReplacement::ClassMethods
8
+
9
+ attributes_for :user do |x|
10
+ x.first_name = "Scott"
11
+ end
12
+ end
13
+
14
+ o1 = Object.new
15
+ o1.extend mod
16
+
17
+ Object.new.should_not respond_to(:create_user)
18
+ end
19
+ end
20
+
21
+ describe "including an object" do
22
+ it "should include methods into instances of the class" do
23
+ mod = Module.new do
24
+ extend FixtureReplacement::ClassMethods
25
+
26
+ attributes_for :user do |x|
27
+ x.first_name = "Scott"
28
+ end
29
+ end
30
+
31
+ klass = Class.new { include mod }
32
+ obj = klass.new
33
+
34
+ obj.should respond_to(:create_user)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,64 @@
1
+ require File.dirname(__FILE__) + "/../../spec_helper"
2
+
3
+ module FixtureReplacement
4
+ describe "HasAndBelongsToMany Associations" do
5
+ before :each do
6
+ @obj = use_module do
7
+ attributes_for :subscriber do |s|
8
+ s.first_name = "Scott"
9
+ s.subscriptions = [new_subscription]
10
+ end
11
+
12
+ attributes_for :subscription do |s|
13
+ s.name = "The New York Times"
14
+ end
15
+
16
+ attributes_for :subscriber_with_two_subscriptions, :from => :subscriber, :class => Subscriber do |s|
17
+ s.subscriptions = [new_harpers_subscription, new_ny_times_subscription]
18
+ end
19
+
20
+ attributes_for :harpers_subscription, :class => Subscription do |s|
21
+ s.name = "Harper's Magazine"
22
+ end
23
+
24
+ attributes_for :ny_times_subscription, :from => :subscription, :class => Subscription
25
+ end
26
+ end
27
+
28
+ it "should have the fixture create_subscriber" do
29
+ @obj.should respond_to(:create_subscriber)
30
+ end
31
+
32
+ it "should have the fixture create_subscription" do
33
+ @obj.should respond_to(:create_subscription)
34
+ end
35
+
36
+ it "should be able to create a new subscriber" do
37
+ lambda {
38
+ @obj.create_subscriber
39
+ }.should_not raise_error
40
+ end
41
+
42
+ it "should have the subscriber with the default subscription" do
43
+ subscriber = @obj.create_subscriber
44
+ subscriber.should have(1).subscription
45
+ subscriber.subscriptions.first.name.should == "The New York Times"
46
+ end
47
+
48
+ it "should be able to create a subscriber with two subscriptions (inline)" do
49
+ subscription_one = @obj.create_harpers_subscription
50
+ subscription_two = @obj.create_ny_times_subscription
51
+
52
+ subscriptions = [subscription_one, subscription_two]
53
+
54
+ subscriber = @obj.create_subscriber(:subscriptions => subscriptions)
55
+
56
+ subscriber.subscriptions.should == subscriptions
57
+ end
58
+
59
+ it "should be able to create a subscriber with two subscriptions, from the fixtures" do
60
+ subscriber = @obj.create_subscriber_with_two_subscriptions
61
+ subscriber.should have(2).subscriptions
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,96 @@
1
+ require File.dirname(__FILE__) + "/../../spec_helper"
2
+
3
+ module FixtureReplacement
4
+ describe "the new_* 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.new_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.new_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.new_admin.should be_a_kind_of(Admin)
27
+ end
28
+
29
+ it "should return a new object with the keys given in user_attributes" do
30
+ obj = use_module do
31
+ attributes_for :user do |u|
32
+ u.key = "val"
33
+ end
34
+ end
35
+
36
+ obj.new_user.key.should == "val"
37
+ end
38
+
39
+ it "should over-write the User's hash with any hash given to new_user" do
40
+ obj = use_module do
41
+ attributes_for :user do |u|
42
+ u.key = "val"
43
+ end
44
+ end
45
+
46
+ obj.new_user(:key => "other_value").key.should == "other_value"
47
+ end
48
+
49
+ it "should add any hash key-value pairs which weren't previously given in user_attributes" do
50
+ obj = use_module do
51
+ attributes_for :user do |u|
52
+ u.key = "val"
53
+ end
54
+ end
55
+
56
+ user = obj.new_user(:other_key => "other_value")
57
+ user.key.should == "val"
58
+ user.other_key.should == "other_value"
59
+ end
60
+
61
+ it "should not be saved to the database" do
62
+ obj = use_module do
63
+ attributes_for :user do |u|
64
+ u.key = "val"
65
+ end
66
+ end
67
+
68
+ obj.new_user.should be_a_new_record
69
+ end
70
+
71
+ it "should be able to be saved to the database" do
72
+ obj = use_module do
73
+ attributes_for :user do |u|
74
+ u.key = "val"
75
+ end
76
+ end
77
+
78
+ lambda {
79
+ obj.new_user.save!
80
+ }.should_not raise_error
81
+ end
82
+
83
+ it "should yield the object inside the block" do
84
+ obj = nil
85
+
86
+ mod = use_module do
87
+ attributes_for :user do |u|
88
+ obj = u
89
+ end
90
+ end
91
+
92
+ mod.new_user # trigger the block
93
+ obj.should be_a_kind_of(User)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + "/../../spec_helper"
2
+
3
+ module FixtureReplacement
4
+ describe "With a public, user defined method" do
5
+ before do
6
+ @obj = use_module do
7
+ attributes_for :user_with_a_public_method, :class => User do |u|
8
+ u.key = a_public_method
9
+ end
10
+
11
+ def a_public_method
12
+ :public
13
+ end
14
+ end
15
+ end
16
+
17
+ it "should be able to call it" do
18
+ user = @obj.new_user_with_a_public_method
19
+ user.key.should == :public
20
+ end
21
+ end
22
+
23
+ describe "With a private, user defined method" do
24
+ before do
25
+ @obj = use_module do
26
+ attributes_for :user_with_a_private_method, :class => User do |u|
27
+ u.key = a_private_method
28
+ end
29
+
30
+ private
31
+
32
+ def a_private_method
33
+ :private
34
+ end
35
+ end
36
+ end
37
+
38
+ it "should be able to call it" do
39
+ user = @obj.new_user_with_a_private_method
40
+ user.key.should == :private
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + "/../../spec_helper"
2
+
3
+ module FixtureReplacement
4
+ describe "including methods" do
5
+ it "should have the create_user method as public" do
6
+ obj = use_module do
7
+ attributes_for :user
8
+ end
9
+
10
+ obj.public_methods.should include("create_user")
11
+ end
12
+
13
+ it "should respond_to? create_user" do
14
+ obj = use_module do
15
+ attributes_for :user
16
+ end
17
+
18
+ obj.respond_to?(:create_user).should be_true
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + "/../../spec_helper"
2
+
3
+ module FixtureReplacement
4
+ describe "valid_*_attributes" do
5
+ it "should generate the method valid_user_attributes when attributes_for is specified" do
6
+ obj = use_module do
7
+ attributes_for :user
8
+ end
9
+
10
+ obj.should respond_to(:valid_user_attributes)
11
+ end
12
+
13
+ it "should generate the method valid_member_attributes when attributes_for is specified" do
14
+ obj = use_module do
15
+ attributes_for :member
16
+ end
17
+
18
+ obj.should respond_to(:valid_member_attributes)
19
+ end
20
+
21
+ it "should have the attributes given as a hash" do
22
+ obj = use_module do
23
+ attributes_for :user do |x|
24
+ x.key = 17
25
+ end
26
+ end
27
+
28
+ obj.valid_user_attributes.should include({ "key" => 17 })
29
+ end
30
+
31
+ it "should have the attributes given" do
32
+ obj = use_module do
33
+ attributes_for :user do |x|
34
+ x.key = 18
35
+ end
36
+ end
37
+
38
+ obj.valid_user_attributes.should include({ "key" => 18 })
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ module FixtureReplacement
4
+ describe "validations" do
5
+ it "should not raise with a record that has no validations" do
6
+ fr = mock_fr_module do
7
+ attributes_for :no_validation do
8
+ end
9
+ end
10
+
11
+ lambda {
12
+ fr.validate!
13
+ }.should_not raise_error
14
+ end
15
+
16
+ it "should raise an error when the record is not valid" do
17
+ fr = mock_fr_module do
18
+ attributes_for :validate_name
19
+ end
20
+
21
+ lambda {
22
+ fr.validate!
23
+ }.should raise_error
24
+ end
25
+
26
+ it "should not raise if the record is valid" do
27
+ fr = mock_fr_module do
28
+ attributes_for :validate_name do |n|
29
+ n.name = "Scott"
30
+ end
31
+ end
32
+
33
+ lambda {
34
+ fr.validate!
35
+ }.should_not raise_error
36
+ end
37
+
38
+ it "should not raise if first loaded with an invalid record, and later a valid one after reloading" do
39
+ fr = mock_fr_module do
40
+ attributes_for :validate_name
41
+ end
42
+
43
+ fr.reload!
44
+
45
+ fr.attributes_for :validate_name do |n|
46
+ n.name = "Scott"
47
+ end
48
+
49
+ lambda {
50
+ fr.validate!
51
+ }.should_not raise_error
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,71 @@
1
+ #
2
+ # Bug Report:
3
+ #
4
+ #
5
+ # [#17249] String.random in a STI base class works, but doesn't work with inherited classes
6
+ # Date:
7
+ # 2008-01-20 14:01 Priority:
8
+ # 3
9
+ # Submitted By:
10
+ # andy watts (andywatts) Assigned To:
11
+ # Nobody (None)
12
+ # Category:
13
+ # Console (script/console) State:
14
+ # Open
15
+ # Summary:
16
+ # String.random in a STI base class works, but doesn't work with inherited classes
17
+ #
18
+ # Detailed description
19
+ #
20
+ # Love the plugin, but seem to have found a bug with the latest FixtureReplacemnt2.
21
+ #
22
+ # There appears to be a problem when a base STI class in example_data has a random string.
23
+ # The derived STI classes do not get a fresh random string with each call.
24
+ #
25
+ # Eg.
26
+ # Given the below example_data.rb, repeated new_user/create_user will work fine.
27
+ # Each call creating a new object with a new random string.
28
+ #
29
+ # However the STI classes do not work as expected...
30
+ # new_player/create_player will always return an object with the same random string
31
+ #
32
+ #
33
+ # attributes_for :user do |u|
34
+ # u.first_name = "First_name_" + String.random
35
+ # u.email = "#{u.first_name}@aaa.com"
36
+ # end
37
+ #
38
+ # attributes_for :player, :from => :user, :class => Player
39
+ #
40
+ #
41
+ # Thanks
42
+ # Andy
43
+ #
44
+ require File.dirname(__FILE__) + "/../../spec_helper"
45
+
46
+ module FixtureReplacement
47
+ describe "random_string" do
48
+ before :each do
49
+ @obj = use_module do
50
+ attributes_for :user do |u|
51
+ u.key = "foo"
52
+ u.username = random_string(55)
53
+ end
54
+
55
+ attributes_for :player, :class => Player, :from => :user
56
+ end
57
+ end
58
+
59
+ it "should have a different string for each instance in the base class" do
60
+ user1 = @obj.create_user
61
+ user2 = @obj.create_user
62
+ user1.username.should_not == user2.username
63
+ end
64
+
65
+ it "should have a different string for each instance in the inherited class (with STI)" do
66
+ player1 = @obj.create_player
67
+ player2 = @obj.create_player
68
+ player1.username.should_not == player2.username
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + "/../spec_helper"
2
+
3
+ describe FixtureReplacement do
4
+ it "should be at version 3.0.1" do
5
+ FixtureReplacement::VERSION.should == "3.0.1"
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ --color