ar_attr_lazy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,359 @@
1
+ require 'helper'
2
+
3
+ # what about STI? (do the lazy attributes carry over?)
4
+
5
+ Protest.context "for a model that has lazy attributes" do
6
+ global_setup do
7
+ load File.dirname(__FILE__) + '/setup_migration.rb'
8
+ load File.dirname(__FILE__) + '/setup_tables_for_not_using.rb'
9
+ load File.dirname(__FILE__) + '/setup_tables_for_using.rb'
10
+ Account.make! do |account|
11
+ User.make!(:account => account) do |user|
12
+ Avatar.make!(:user => user)
13
+ Post.make!(:author => user) do |post|
14
+ Comment.make!(:post => post)
15
+ post.tags << Tag.make
16
+ post.categories << Category.make
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ global_teardown do
23
+ ObjectSpace.each_object(Class) do |klass|
24
+ Object.remove_class(klass) if klass < ActiveRecord::Base
25
+ end
26
+ end
27
+
28
+ def regex(str)
29
+ Regexp.new(Regexp.escape(str))
30
+ end
31
+
32
+ context "with no associations involved" do
33
+ test "find selects non-lazy attributes only by default" do
34
+ lambda { Post.find(:first) }.should query(
35
+ regex(%|SELECT "posts"."id","posts"."type","posts"."author_id","posts"."title","posts"."permalink" FROM "posts"|)
36
+ )
37
+ end
38
+ test "accessing a lazy attribute selects that attribute only" do
39
+ post = Post.find(:first)
40
+ lambda { post.body }.should query(
41
+ regex(%|SELECT "posts"."id","posts"."body" FROM "posts"|)
42
+ )
43
+ end
44
+ test "find still honors an explicit select option" do
45
+ lambda { Post.find(:first, :select => "title, permalink") }.should query(
46
+ regex(%|SELECT title, permalink FROM "posts"|)
47
+ )
48
+ end
49
+ test "find still honors a select option in a parent scope" do
50
+ lambda {
51
+ Post.send(:with_scope, :find => {:select => "title, permalink"}) do
52
+ Post.find(:first)
53
+ end
54
+ }.should query(
55
+ regex(%|SELECT title, permalink FROM "posts"|)
56
+ )
57
+ end
58
+ if Mcmire::ArAttrLazy.ar_version >= 2.3
59
+ test "find still honors a select option in a default scope" do
60
+ lambda { PostWithDefaultScope.find(:first) }.should query(
61
+ regex(%|SELECT title FROM "posts"|)
62
+ )
63
+ end
64
+ end
65
+ end
66
+
67
+ context "accessing a has_many association" do
68
+ before do
69
+ @post = Post.first
70
+ end
71
+ test "find selects non-lazy attributes by default" do
72
+ lambda { @post.comments.find(:first) }.should query(
73
+ regex(%|SELECT "comments"."id","comments"."type","comments"."post_id","comments"."name" FROM "comments"|)
74
+ )
75
+ end
76
+ test "find still honors an explicit select option" do
77
+ lambda { @post.comments.find(:first, :select => "name") }.should query(
78
+ regex(%|SELECT name FROM "comments"|)
79
+ )
80
+ end
81
+ test "find still honors a select option in a parent scope" do
82
+ lambda {
83
+ Comment.send(:with_scope, :find => {:select => "name"}) do
84
+ @post.comments.find(:first)
85
+ end
86
+ }.should query(
87
+ regex(%|SELECT name FROM "comments"|)
88
+ )
89
+ end
90
+ if Mcmire::ArAttrLazy.ar_version >= 2.3
91
+ test "find still honors a select option in a default scope" do
92
+ lambda { @post.comments_with_default_scope.find(:first) }.should query(
93
+ regex(%|SELECT name FROM "comments"|)
94
+ )
95
+ end
96
+ end
97
+ test "find still honors a select option in the association definition itself" do
98
+ lambda { @post.comments_with_select.find(:first) }.should query(
99
+ regex(%|SELECT name FROM "comments"|)
100
+ )
101
+ end
102
+ end
103
+
104
+ context "accessing a belongs_to association" do
105
+ test "find selects non-lazy attributes by default" do
106
+ post = Post.first
107
+ lambda { post.author }.should query(
108
+ regex(%|SELECT "users"."id","users"."type","users"."account_id","users"."login","users"."password","users"."email","users"."name" FROM "users"|)
109
+ )
110
+ end
111
+ # can't do a find on a belongs_to, so no testing needed for that
112
+ end
113
+
114
+ context "accessing a has_one association" do
115
+ test "find selects non-lazy attributes by default" do
116
+ account = Account.first
117
+ lambda { account.user }.should query(
118
+ regex(%|SELECT "users"."id","users"."type","users"."account_id","users"."login","users"."password","users"."email","users"."name" FROM "users"|)
119
+ )
120
+ end
121
+ # can't do a find on a has_one, so no testing needed for that
122
+ end
123
+
124
+ context "accessing a has_and_belongs_to_many association" do
125
+ before do
126
+ @post = Post.first
127
+ end
128
+ test "find selects non-lazy attributes by default" do
129
+ lambda { @post.tags.find(:all) }.should query(
130
+ regex(%|SELECT "tags"."id","tags"."type","tags"."name" FROM "tags"|)
131
+ )
132
+ end
133
+ test "find still honors an explicit select option" do
134
+ lambda { @post.tags.find(:all, :select => "tags.name") }.should query(
135
+ regex(%|SELECT tags.name FROM "tags"|)
136
+ )
137
+ end
138
+ test "find still honors a select option in a parent scope" do
139
+ pending "this fails on Rails 2.3.4"
140
+ lambda {
141
+ Tag.send(:with_scope, :find => {:select => "tags.name"}) do
142
+ @post.tags.find(:all)
143
+ end
144
+ }.should query(
145
+ regex(%|SELECT tags.name FROM "tags"|)
146
+ )
147
+ end
148
+ if Mcmire::ArAttrLazy.ar_version >= 2.3
149
+ test "find still honors a select option in a default scope" do
150
+ pending "this fails on Rails 2.3.4"
151
+ lambda {
152
+ @post.tags_with_default_scope.find(:all)
153
+ }.should query(
154
+ regex(%|SELECT tags.name FROM "tags"|)
155
+ )
156
+ end
157
+ end
158
+ test "find still honors a select option in the association definition itself" do
159
+ lambda {
160
+ @post.tags_with_select.find(:all)
161
+ }.should query(
162
+ regex(%|SELECT tags.name FROM "tags"|)
163
+ )
164
+ end
165
+ end
166
+
167
+ context "accessing a has_many :through association" do
168
+ before do
169
+ @post = Post.first
170
+ end
171
+ test "find selects non-lazy attributes by default" do
172
+ lambda { @post.categories.find(:all) }.should query(
173
+ regex(%|SELECT "categories"."id","categories"."type","categories"."name" FROM "categories"|)
174
+ )
175
+ end
176
+ test "find still honors an explicit select option" do
177
+ lambda { @post.categories.find(:all, :select => "categories.name") }.should query(
178
+ regex(%|SELECT categories.name FROM "categories"|)
179
+ )
180
+ end
181
+ test "find still honors a select option in a parent scope" do
182
+ pending "this fails on Rails 2.3.4"
183
+ lambda {
184
+ Category.send(:with_scope, :find => {:select => "categories.name"}) do
185
+ @post.categories.find(:all)
186
+ end
187
+ }.should query(
188
+ regex(%|SELECT categories.name FROM "categories"|)
189
+ )
190
+ end
191
+ if Mcmire::ArAttrLazy.ar_version >= 2.3
192
+ test "find still honors a select option in a default scope" do
193
+ pending "this fails on Rails 2.3.4"
194
+ lambda {
195
+ @post.categories_with_default_scope.find(:all)
196
+ }.should query(
197
+ regex(%|SELECT categories.name FROM "categories"|)
198
+ )
199
+ end
200
+ end
201
+ test "find still honors a select option in the association definition itself" do
202
+ lambda {
203
+ @post.categories_with_select.find(:all)
204
+ }.should query(
205
+ regex(%|SELECT categories.name FROM "categories"|)
206
+ )
207
+ end
208
+ end
209
+
210
+ # has_one :through didn't work properly prior to 2.3.4 - see LH #2719
211
+ if Mcmire::ArAttrLazy.ar_version >= "2.3.4"
212
+ context "accessing a has_one :through association" do
213
+ test "find selects non-lazy attributes by default" do
214
+ account = Account.first
215
+ lambda { account.avatar }.should query(
216
+ regex(%|SELECT "avatars"."id","avatars"."type","avatars"."user_id","avatars"."filename" FROM "avatars"|)
217
+ )
218
+ end
219
+ # can't do a find on a has_one, so no testing needed for that
220
+ end
221
+ end
222
+
223
+ context "eager loading a has_many association (association preloading)" do
224
+ test "find selects non-lazy attributes by default" do
225
+ lambda {
226
+ Post.find(:first, :include => :comments)
227
+ }.should query(
228
+ regex(%|SELECT "comments"."id","comments"."type","comments"."post_id","comments"."name" FROM "comments"|)
229
+ )
230
+ end
231
+ # can't test for an explicit select since that will force a table join
232
+ # can't test for a scope select since association preloading doesn't honor those
233
+ end
234
+ context "eager loading a has_many association (table join)" do
235
+ test "find selects non-lazy attributes by default" do
236
+ lambda {
237
+ Post.find(:first, :include => :comments, :order => "comments.id")
238
+ }.should query(
239
+ regex(%|SELECT "posts"."id" AS t0_r0, "posts"."type" AS t0_r1, "posts"."author_id" AS t0_r2, "posts"."title" AS t0_r3, "posts"."permalink" AS t0_r4, "comments"."id" AS t1_r0, "comments"."type" AS t1_r1, "comments"."post_id" AS t1_r2, "comments"."name" AS t1_r3 FROM "posts"|)
240
+ )
241
+ end
242
+ # can't test for an explicit select since that clashes with the table join anyway
243
+ # can't test for a scope for the same reason
244
+ end
245
+
246
+ context "eager loading a has_one association (association preloading)" do
247
+ test "find selects non-lazy attributes by default" do
248
+ lambda { Account.find(:first, :include => :user) }.should query(
249
+ regex(%|SELECT "users"."id","users"."type","users"."account_id","users"."login","users"."password","users"."email","users"."name" FROM "users"|)
250
+ )
251
+ end
252
+ # can't test for an explicit select since that will force a table join
253
+ # can't test for a scope select since association preloading doesn't honor those
254
+ end
255
+ context "eager loading a has_one association (table join)" do
256
+ test "find selects non-lazy attributes by default" do
257
+ lambda {
258
+ Account.find(:first, :include => :user, :order => "users.id")
259
+ }.should query(
260
+ regex(%|SELECT "accounts"."id" AS t0_r0, "accounts"."name" AS t0_r1, "users"."id" AS t1_r0, "users"."type" AS t1_r1, "users"."account_id" AS t1_r2, "users"."login" AS t1_r3, "users"."password" AS t1_r4, "users"."email" AS t1_r5, "users"."name" AS t1_r6 FROM "accounts"|)
261
+ )
262
+ end
263
+ # can't test for an explicit select since that clashes with the table join anyway
264
+ # can't test for a scope for the same reason
265
+ end
266
+
267
+ context "eager loading a belongs_to association (association preloading)" do
268
+ test "find selects non-lazy attributes by default" do
269
+ lambda {
270
+ Post.find(:first, :include => :author)
271
+ }.should query(
272
+ regex(%|SELECT "users"."id","users"."type","users"."account_id","users"."login","users"."password","users"."email","users"."name" FROM "users"|)
273
+ )
274
+ end
275
+ # can't test for an explicit select since that will force a table join
276
+ # can't test for a scope select since association preloading doesn't honor those
277
+ end
278
+ context "eager loading a belongs_to association (table join)" do
279
+ test "find selects non-lazy attributes by default" do
280
+ lambda {
281
+ Post.find(:first, :include => :author, :order => "users.id")
282
+ }.should query(
283
+ regex(%|SELECT "posts"."id" AS t0_r0, "posts"."type" AS t0_r1, "posts"."author_id" AS t0_r2, "posts"."title" AS t0_r3, "posts"."permalink" AS t0_r4, "users"."id" AS t1_r0, "users"."type" AS t1_r1, "users"."account_id" AS t1_r2, "users"."login" AS t1_r3, "users"."password" AS t1_r4, "users"."email" AS t1_r5, "users"."name" AS t1_r6 FROM "posts"|)
284
+ )
285
+ end
286
+ # can't test for an explicit select since that clashes with the table join anyway
287
+ # can't test for a scope for the same reason
288
+ end
289
+
290
+ context "eager loading a has_and_belongs_to_many association (association preloading)" do
291
+ test "find selects non-lazy attributes by default" do
292
+ lambda {
293
+ Post.find(:first, :include => :tags)
294
+ }.should query(
295
+ regex(%|SELECT "tags"."id","tags"."type","tags"."name", t0.post_id as the_parent_record_id FROM "tags"|)
296
+ )
297
+ end
298
+ # can't test for an explicit select since that will force a table join
299
+ # can't test for a scope select since association preloading doesn't honor those
300
+ end
301
+ context "eager loading a has_and_belongs_to_many association (table join)" do
302
+ test "find selects non-lazy attributes by default" do
303
+ lambda {
304
+ Post.find(:first, :include => :tags, :order => "tags.id")
305
+ }.should query(
306
+ regex(%|SELECT "posts"."id" AS t0_r0, "posts"."type" AS t0_r1, "posts"."author_id" AS t0_r2, "posts"."title" AS t0_r3, "posts"."permalink" AS t0_r4, "tags"."id" AS t1_r0, "tags"."type" AS t1_r1, "tags"."name" AS t1_r2 FROM "posts"|)
307
+ )
308
+ end
309
+ # can't test for an explicit select since that clashes with the table join anyway
310
+ # can't test for a scope for the same reason
311
+ end
312
+
313
+ context "eager loading a has_many :through association (association preloading)" do
314
+ test "find selects non-lazy attributes by default" do
315
+ lambda {
316
+ Post.find(:first, :include => :categories)
317
+ }.should query(
318
+ regex(%|SELECT "categories"."id","categories"."type","categories"."name" FROM "categories"|)
319
+ )
320
+ end
321
+ # can't test for an explicit select since that will force a table join
322
+ # can't test for a scope select since association preloading doesn't honor those
323
+ end
324
+ context "eager loading a has_many :through association (table join)" do
325
+ test "find selects non-lazy attributes by default" do
326
+ lambda {
327
+ Post.find(:first, :include => :categories, :order => "categories.id")
328
+ }.should query(
329
+ regex(%|SELECT "posts"."id" AS t0_r0, "posts"."type" AS t0_r1, "posts"."author_id" AS t0_r2, "posts"."title" AS t0_r3, "posts"."permalink" AS t0_r4, "categories"."id" AS t1_r0, "categories"."type" AS t1_r1, "categories"."name" AS t1_r2 FROM "posts"|)
330
+ )
331
+ end
332
+ # can't test for an explicit select since that clashes with the table join anyway
333
+ # can't test for a scope for the same reason
334
+ end
335
+
336
+ # has_one :through didn't work properly prior to 2.3.4 - see LH #2719
337
+ if Mcmire::ArAttrLazy.ar_version >= "2.3.4"
338
+ context "eager loading a has_one :through association (association preloading)" do
339
+ test "find selects non-lazy attributes by default" do
340
+ lambda { Account.find(:first, :include => :avatar) }.should query(
341
+ regex(%|SELECT "avatars"."id","avatars"."type","avatars"."user_id","avatars"."filename" FROM "avatars"|)
342
+ )
343
+ end
344
+ # can't test for an explicit select since that will force a table join
345
+ # can't test for a scope select since association preloading doesn't honor those
346
+ end
347
+ context "eager loading a has_one :through association (table join)" do
348
+ test "find selects non-lazy attributes by default" do
349
+ pending "this is failing for some reason!"
350
+ lambda {
351
+ Account.find(:first, :include => :avatar, :order => "avatars.id")
352
+ }.should query(%|SELECT "accounts"."id" AS t0_r0, "accounts"."name" AS t0_r1, "avatars"."id" AS t1_r0, "avatars"."user_id" AS t1_r1, "avatars"."filename" AS t1_r2 FROM "accounts"|)
353
+ end
354
+ # can't test for an explicit select since that clashes with the table join anyway
355
+ # can't test for a scope for the same reason
356
+ end
357
+ end
358
+
359
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_attr_lazy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elliot Winkler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-19 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - <
22
+ - !ruby/object:Gem::Version
23
+ version: "3.0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mcmire-protest
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mcmire-matchy
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: mcmire-mocha
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha-protest-integration
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ description: A little gem for Rails that provides the ability to specify attributes that will not be loaded when the record is loaded from the database, until you explicitly refer to those attributes. This is useful when you have a lot of text columns in your table; in this case lazy-loading the text attributes is a good way to lend your server a hand and cut down on database access time.
66
+ email: elliot.winkler@gmail.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README.md
74
+ files:
75
+ - .gitignore
76
+ - LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - init.rb
80
+ - lib/mcmire/ar_attr_lazy.rb
81
+ - lib/mcmire/ar_attr_lazy/association_preload_ext.rb
82
+ - lib/mcmire/ar_attr_lazy/base_ext.rb
83
+ - lib/mcmire/ar_attr_lazy/belongs_to_association_ext.rb
84
+ - lib/mcmire/ar_attr_lazy/habtm_ext.rb
85
+ - lib/mcmire/ar_attr_lazy/has_many_through_association_ext.rb
86
+ - lib/mcmire/ar_attr_lazy/join_base_ext.rb
87
+ - lib/mcmire/ar_attr_lazy/version.rb
88
+ - test/factories.rb
89
+ - test/helper.rb
90
+ - test/matchers.rb
91
+ - test/not_using_attr_lazy_test.rb
92
+ - test/setup_migration.rb
93
+ - test/setup_sti_models.rb
94
+ - test/setup_tables_for_not_using.rb
95
+ - test/setup_tables_for_using.rb
96
+ - test/using_attr_lazy_sti_test.rb
97
+ - test/using_attr_lazy_test.rb
98
+ has_rdoc: true
99
+ homepage: http://github.com/mcmire/ar_attr_lazy
100
+ licenses: []
101
+
102
+ post_install_message:
103
+ rdoc_options:
104
+ - --charset=UTF-8
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: "0"
112
+ version:
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: "0"
118
+ version:
119
+ requirements: []
120
+
121
+ rubyforge_project:
122
+ rubygems_version: 1.3.5
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Rails gem that provides lazy-loading for attributes.
126
+ test_files:
127
+ - test/factories.rb
128
+ - test/helper.rb
129
+ - test/matchers.rb
130
+ - test/not_using_attr_lazy_test.rb
131
+ - test/setup_migration.rb
132
+ - test/setup_sti_models.rb
133
+ - test/setup_tables_for_not_using.rb
134
+ - test/setup_tables_for_using.rb
135
+ - test/using_attr_lazy_sti_test.rb
136
+ - test/using_attr_lazy_test.rb