myronmarston-factory_data_preloader 0.4.1 → 0.4.2
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.
- data/VERSION.yml +1 -1
- data/lib/factory_data_preloader/factory_data.rb +5 -0
- data/test/factory_data_test.rb +42 -32
- metadata +1 -1
data/VERSION.yml
CHANGED
@@ -3,6 +3,7 @@ require 'ostruct'
|
|
3
3
|
module FactoryDataPreloader
|
4
4
|
class PreloaderAlreadyDefinedError < StandardError; end
|
5
5
|
class PreloadedRecordNotFound < StandardError; end
|
6
|
+
class DefinedPreloaderNotRunError < StandardError; end
|
6
7
|
|
7
8
|
class FactoryData
|
8
9
|
@@preloaded_cache = nil
|
@@ -80,6 +81,10 @@ module FactoryDataPreloader
|
|
80
81
|
private
|
81
82
|
|
82
83
|
def get_record(type, model_class, key)
|
84
|
+
if @@preloaded_cache[type].nil?
|
85
|
+
raise DefinedPreloaderNotRunError, "The :#{type} preloader has never been run. Did you forget to add the 'preload_factory_data :#{type}' declaration to your test case? You'll need this at the top of your test case class if you want to use the factory data defined by this preloader."
|
86
|
+
end
|
87
|
+
|
83
88
|
@@single_test_cache[type] ||= {}
|
84
89
|
@@single_test_cache[type][key] ||= begin
|
85
90
|
record = model_class.find_by_id(@@preloaded_cache[type][key])
|
data/test/factory_data_test.rb
CHANGED
@@ -1,36 +1,36 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper'
|
2
2
|
|
3
|
-
class FactoryDataTest < Test::Unit::TestCase
|
3
|
+
class FactoryDataTest < Test::Unit::TestCase
|
4
4
|
def setup
|
5
5
|
FactoryDataPreloader.reset!
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
context 'Calling FactoryData.preload(:users)' do
|
9
9
|
setup do
|
10
10
|
FactoryData.preload(:users) do |data|
|
11
11
|
data[:thom] = User.create(:first_name => 'Thom', :last_name => 'York')
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
should_not_change 'User.count'
|
16
16
|
should_change "FactoryData.methods.include?('users')", :from => false, :to => true
|
17
|
-
|
17
|
+
|
18
18
|
should 'not allow it to be called again' do
|
19
19
|
assert_raise FactoryDataPreloader::PreloaderAlreadyDefinedError do
|
20
20
|
FactoryData.preload(:users)
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
context 'when there was a previous user record in the database' do
|
25
25
|
setup { User.create(:first_name => 'Barack', :last_name => 'Obama') }
|
26
|
-
|
26
|
+
|
27
27
|
context 'and calling FactoryData.delete_preload_data!' do
|
28
28
|
setup { FactoryData.delete_preload_data! }
|
29
29
|
should_change 'User.count', :to => 0
|
30
|
-
|
30
|
+
|
31
31
|
context 'and there is another record in the database' do
|
32
32
|
setup { User.create(:first_name => 'George', :last_name => 'Bush') }
|
33
|
-
|
33
|
+
|
34
34
|
context 'and FactoryData.delete_preload_data! is called again' do
|
35
35
|
setup { FactoryData.delete_preload_data! }
|
36
36
|
should_not_change 'User.count'
|
@@ -38,36 +38,36 @@ class FactoryDataTest < Test::Unit::TestCase
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
context 'and later calling FactoryData.preload_data!' do
|
43
43
|
setup { FactoryData.preload_data! }
|
44
|
-
|
44
|
+
|
45
45
|
should_change 'User.count', :by => 1
|
46
|
-
|
46
|
+
|
47
47
|
context 'and later calling FactoryData.users(key)' do
|
48
48
|
setup { @user = FactoryData.users(:thom) }
|
49
|
-
|
49
|
+
|
50
50
|
should 'retrieve the correct user' do
|
51
51
|
assert_equal 'Thom', @user.first_name
|
52
52
|
assert_equal 'York', @user.last_name
|
53
53
|
assert !@user.new_record?
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
should 'raise the appropriate error for a non-existant key' do
|
57
57
|
assert_raise FactoryDataPreloader::PreloadedRecordNotFound do
|
58
58
|
FactoryData.users(:not_a_user)
|
59
59
|
end
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
should 'cache the record so as not to use User.find more than necessary' do
|
63
63
|
User.expects(:find).never
|
64
64
|
user2 = FactoryData.users(:thom)
|
65
65
|
assert_equal @user.object_id, user2.object_id
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
context 'and later calling FactoryData.reset_cache!' do
|
69
69
|
setup { FactoryData.reset_cache! }
|
70
|
-
|
70
|
+
|
71
71
|
should 'reload the record from the database the next time FactoryData.users(key) is called' do
|
72
72
|
User.expects(:find).once.returns(@user)
|
73
73
|
FactoryData.users(:thom)
|
@@ -76,42 +76,42 @@ class FactoryDataTest < Test::Unit::TestCase
|
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
context 'Preloading a record that has not been saved' do
|
81
81
|
setup do
|
82
82
|
@unsaved_user = User.new(:first_name => 'George', :last_name => 'Washington')
|
83
83
|
assert @unsaved_user.new_record?
|
84
|
-
|
84
|
+
|
85
85
|
FactoryData.preload(:users) do |data|
|
86
86
|
data[:george] = @unsaved_user
|
87
87
|
end
|
88
88
|
end
|
89
|
-
|
89
|
+
|
90
90
|
should 'save the record wen preload_data! is called' do
|
91
91
|
FactoryData.preload_data!
|
92
92
|
assert !@unsaved_user.new_record?
|
93
93
|
end
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
context 'Preloading a record that cannot be saved to the database' do
|
97
97
|
setup do
|
98
98
|
@invalid_user = User.new(:first_name => 'Bob')
|
99
99
|
assert !@invalid_user.valid?
|
100
|
-
|
100
|
+
|
101
101
|
FactoryData.preload(:users) do |data|
|
102
102
|
data[:bob] = @invalid_user
|
103
103
|
end
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
should 'print an appropriate error message when preload_data! is called' do
|
107
107
|
out, err = OutputCapturer.capture do
|
108
108
|
FactoryData.preload_data!
|
109
109
|
end
|
110
|
-
|
110
|
+
|
111
111
|
assert_match /Error preloading factory data\.\s+User :bob could not be saved\.\s+Errors:\s+last_name can't be blank/im, out
|
112
112
|
end
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
context 'Preloading with an explicit :model_class option' do
|
116
116
|
setup do
|
117
117
|
FactoryData.preload(:posts, :model_class => User) do |data|
|
@@ -119,36 +119,46 @@ class FactoryDataTest < Test::Unit::TestCase
|
|
119
119
|
end
|
120
120
|
FactoryData.preload_data!
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
should 'use the passed model_class rather than inferring the class from the symbol' do
|
124
124
|
assert_equal User, FactoryData.posts(:george).class
|
125
125
|
end
|
126
126
|
end
|
127
|
-
|
127
|
+
|
128
128
|
context 'Preloading multiple record types, with dependencies' do
|
129
129
|
setup do
|
130
130
|
FactoryData.preload(:comments, :depends_on => [:users, :posts]) do |data|
|
131
131
|
data[:woohoo] = FactoryData.users(:john).comments.create(:post => FactoryData.posts(:tour), :comment => "I can't wait!")
|
132
132
|
end
|
133
|
-
|
133
|
+
|
134
134
|
FactoryData.preload(:posts, :depends_on => :users) do |data|
|
135
135
|
data[:tour] = FactoryData.users(:thom).posts.create(:title => 'Tour!', :body => 'Radiohead will tour soon.')
|
136
136
|
end
|
137
|
-
|
137
|
+
|
138
138
|
FactoryData.preload(:users) do |data|
|
139
139
|
data[:thom] = User.create(:first_name => 'Thom', :last_name => 'York')
|
140
140
|
data[:john] = User.create(:first_name => 'John', :last_name => 'Doe')
|
141
141
|
end
|
142
|
-
|
142
|
+
end
|
143
|
+
|
144
|
+
should "raise the appropriate error when a developer tries to access a record that wasn't preloaded" do
|
145
|
+
FactoryDataPreloader.preload_all = false
|
146
|
+
FactoryDataPreloader.preload_types << :users
|
143
147
|
FactoryData.preload_data!
|
148
|
+
|
149
|
+
assert FactoryData.users(:thom)
|
150
|
+
assert_raise FactoryDataPreloader::DefinedPreloaderNotRunError do
|
151
|
+
FactoryData.posts(:tour)
|
152
|
+
end
|
144
153
|
end
|
145
|
-
|
154
|
+
|
146
155
|
should 'preload them in the proper order, allowing you to use the dependencies' do
|
156
|
+
FactoryData.preload_data!
|
147
157
|
assert_equal 'Thom', FactoryData.users(:thom).first_name
|
148
158
|
assert_equal 'John', FactoryData.users(:john).first_name
|
149
|
-
|
159
|
+
|
150
160
|
assert_equal FactoryData.users(:thom), FactoryData.posts(:tour).user
|
151
|
-
|
161
|
+
|
152
162
|
assert_equal FactoryData.users(:john), FactoryData.comments(:woohoo).user
|
153
163
|
assert_equal FactoryData.posts(:tour), FactoryData.comments(:woohoo).post
|
154
164
|
end
|