active_hash 1.2.3 → 1.3.0

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.
@@ -1,119 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ActiveYaml::Base do
4
-
5
- before do
6
- ActiveYaml::Base.set_root_path File.expand_path(File.dirname(__FILE__) + "/../fixtures")
7
-
8
- class ArrayRow < ActiveYaml::Base ; end
9
- class City < ActiveYaml::Base ; end
10
- class State < ActiveYaml::Base ; end
11
- class ArrayProduct < ActiveYaml::Base ; end # Contain YAML aliases
12
- class KeyProduct < ActiveYaml::Base ; end # Contain YAML aliases
13
- end
14
-
15
- after do
16
- Object.send :remove_const, :ArrayRow
17
- Object.send :remove_const, :City
18
- Object.send :remove_const, :State
19
- end
20
-
21
- describe ".all" do
22
- context "before the file is loaded" do
23
- it "reads from the file" do
24
- State.all.should_not be_empty
25
- State.count.should > 0
26
- end
27
- end
28
- end
29
-
30
- describe ".where" do
31
- context "before the file is loaded" do
32
- it "reads from the file and filters by where statement" do
33
- State.where(:name => 'Oregon').should_not be_empty
34
- State.count.should > 0
35
- end
36
- end
37
- end
38
-
39
- describe ".delete_all" do
40
- context "when called before .all" do
41
- it "causes all to not load data" do
42
- State.delete_all
43
- State.all.should be_empty
44
- end
45
- end
46
-
47
- context "when called after .all" do
48
- it "clears out the data" do
49
- State.all.should_not be_empty
50
- State.delete_all
51
- State.all.should be_empty
52
- end
53
- end
54
- end
55
-
56
- describe ".raw_data" do
57
-
58
- it "returns the raw hash data loaded from yaml hash-formatted files" do
59
- City.raw_data.should be_kind_of(Hash)
60
- City.raw_data.keys.should include("albany", "portland")
61
- end
62
-
63
- it "returns the raw array data loaded from yaml array-formatted files" do
64
- ArrayRow.raw_data.should be_kind_of(Array)
65
- end
66
-
67
- end
68
-
69
- describe ".load_file" do
70
-
71
- describe "with array data" do
72
- it "returns an array of hashes" do
73
- ArrayRow.load_file.should be_kind_of(Array)
74
- ArrayRow.load_file.should include({"name" => "Row 1", "id" => 1})
75
- end
76
- end
77
-
78
- describe "with hash data" do
79
- it "returns an array of hashes" do
80
- City.load_file.should be_kind_of(Array)
81
- City.load_file.should include({"state" => :new_york, "name" => "Albany", "id" => 1})
82
- City.reload
83
- City.all.should include(City.new(:id => 1))
84
- end
85
- end
86
-
87
- end
88
-
89
- describe 'ID finders without reliance on a call to all, even with fields specified' do
90
-
91
- before do
92
- class City < ActiveYaml::Base
93
- fields :id, :state, :name
94
- end
95
- end
96
-
97
- it 'returns a single city based on #find' do
98
- City.find(1).name.should == 'Albany'
99
- end
100
-
101
- it 'returns a single city based on find_by_id' do
102
- City.find_by_id(1).name.should == 'Albany'
103
- end
104
-
105
- end
106
-
107
- describe 'meta programmed finders and properties for fields that exist in the YAML' do
108
-
109
- it 'should have a finder method for each property' do
110
- City.find_by_state('Oregon').should_not be_nil
111
- end
112
-
113
- it 'should have a find all method for each property' do
114
- City.find_all_by_state('Oregon').should_not be_nil
115
- end
116
-
117
- end
118
-
119
- end
@@ -1,427 +0,0 @@
1
- require 'spec_helper'
2
- require 'active_record'
3
-
4
- describe ActiveHash::Base, "associations" do
5
-
6
- before do
7
- class Country < ActiveRecord::Base
8
- establish_connection :adapter => "sqlite3", :database => ":memory:"
9
- connection.create_table(:countries, :force => true) do |t|
10
- t.string :name
11
- end
12
- extend ActiveHash::Associations::ActiveRecordExtensions
13
- end
14
-
15
- class School < ActiveRecord::Base
16
- establish_connection :adapter => "sqlite3", :database => ":memory:"
17
- connection.create_table(:schools, :force => true) do |t|
18
- t.integer :country_id
19
- t.integer :city_id
20
- end
21
- extend ActiveHash::Associations::ActiveRecordExtensions
22
- end
23
-
24
- class City < ActiveHash::Base
25
- include ActiveHash::Associations
26
- end
27
-
28
- class Author < ActiveHash::Base
29
- include ActiveHash::Associations
30
- end
31
-
32
- class SchoolStatus < ActiveHash::Base
33
- end
34
-
35
- class Book < ActiveRecord::Base
36
- establish_connection :adapter => "sqlite3", :database => ":memory:"
37
- connection.create_table(:books, :force => true) do |t|
38
- t.integer :author_id
39
- t.integer :author_code
40
- t.boolean :published
41
- end
42
-
43
- if Object.const_defined?(:ActiveModel)
44
- scope( :published, proc { where(:published => true) })
45
- else
46
- named_scope :published, {:conditions => {:published => true}}
47
- end
48
- end
49
- end
50
-
51
- after do
52
- Object.send :remove_const, :City
53
- Object.send :remove_const, :Author
54
- Object.send :remove_const, :Country
55
- Object.send :remove_const, :School
56
- Object.send :remove_const, :Book
57
- end
58
-
59
- describe "#has_many" do
60
-
61
- context "with ActiveRecord children" do
62
- context "with default options" do
63
- before do
64
- @book_1 = Book.create! :author_id => 1, :published => true
65
- @book_2 = Book.create! :author_id => 1, :published => false
66
- @book_3 = Book.create! :author_id => 2, :published => true
67
- Author.has_many :books
68
- end
69
-
70
- it "find the correct records" do
71
- author = Author.create :id => 1
72
- author.books.should == [@book_1, @book_2]
73
- end
74
-
75
- it "return a scope so that we can apply further scopes" do
76
- author = Author.create :id => 1
77
- author.books.published.should == [@book_1]
78
- end
79
- end
80
-
81
- context "with a primary_key option" do
82
- before do
83
- @book_1 = Book.create! :author_id => 1, :published => true
84
- @book_2 = Book.create! :author_id => 2, :published => false
85
- @book_3 = Book.create! :author_id => 2, :published => true
86
- Author.field :book_identifier
87
- Author.has_many :books, :primary_key => :book_identifier
88
- end
89
-
90
- it "should find the correct records" do
91
- author = Author.create :id => 1, :book_identifier => 2
92
- author.books.should == [@book_2, @book_3]
93
- end
94
-
95
- it "return a scope so that we can apply further scopes" do
96
- author = Author.create :id => 1, :book_identifier => 2
97
- author.books.published.should == [@book_3]
98
- end
99
- end
100
-
101
- context "with a foreign_key option" do
102
- before do
103
- @book_1 = Book.create! :author_code => 1, :published => true
104
- @book_2 = Book.create! :author_code => 1, :published => false
105
- @book_3 = Book.create! :author_code => 2, :published => true
106
- Author.has_many :books, :foreign_key => :author_code
107
- end
108
-
109
- it "should find the correct records" do
110
- author = Author.create :id => 1
111
- author.books.should == [@book_1, @book_2]
112
- end
113
-
114
- it "return a scope so that we can apply further scopes" do
115
- author = Author.create :id => 1
116
- author.books.published.should == [@book_1]
117
- end
118
- end
119
-
120
- it "only uses 1 query" do
121
- Author.has_many :books
122
- author = Author.create :id => 1
123
- Book.should_receive(:find_by_sql)
124
- author.books.to_a
125
- end
126
- end
127
-
128
- context "with ActiveHash children" do
129
- context "with default options" do
130
- before do
131
- Author.field :city_id
132
- @included_author_1 = Author.create :city_id => 1
133
- @included_author_2 = Author.create :city_id => 1
134
- @excluded_author = Author.create :city_id => 2
135
- end
136
-
137
- it "find the correct records" do
138
- City.has_many :authors
139
- city = City.create :id => 1
140
- city.authors.should == [@included_author_1, @included_author_2]
141
- end
142
-
143
- it "uses the correct class name when passed" do
144
- City.has_many :writers, :class_name => "Author"
145
- city = City.create :id => 1
146
- city.writers.should == [@included_author_1, @included_author_2]
147
- end
148
- end
149
-
150
- context "with a primary_key option" do
151
- before do
152
- Author.field :city_id
153
- City.field :author_identifier
154
- @author_1 = Author.create :city_id => 1
155
- @author_2 = Author.create :city_id => 10
156
- @author_3 = Author.create :city_id => 10
157
- City.has_many :authors, :primary_key => :author_identifier
158
- end
159
-
160
- it "finds the correct records" do
161
- city = City.create :id => 1, :author_identifier => 10
162
- city.authors.should == [@author_2, @author_3]
163
- end
164
- end
165
-
166
- context "with a foreign_key option" do
167
- before do
168
- Author.field :city_id
169
- Author.field :city_identifier
170
- @author_1 = Author.create :city_id => 1, :city_identifier => 10
171
- @author_2 = Author.create :city_id => 10, :city_identifier => 10
172
- @author_3 = Author.create :city_id => 10, :city_identifier => 5
173
- City.has_many :authors, :foreign_key => :city_identifier
174
- end
175
-
176
- it "finds the correct records" do
177
- city = City.create :id => 10
178
- city.authors.should == [@author_1, @author_2]
179
- end
180
- end
181
- end
182
-
183
- end
184
-
185
- describe ActiveHash::Associations::ActiveRecordExtensions do
186
-
187
- describe "#belongs_to" do
188
-
189
- if ActiveRecord::VERSION::MAJOR > 3
190
- it "doesn't interfere with AR's procs in belongs_to methods" do
191
- School.belongs_to :country, lambda{ where( ) }
192
- school = School.new
193
- country = Country.create!
194
- school.country = country
195
- school.country.should == country
196
- school.country_id.should == country.id
197
- school.save!
198
- school.reload
199
- school.reload.country_id.should == country.id
200
- end
201
- end
202
-
203
- it "sets up an ActiveRecord association for non-ActiveHash objects" do
204
- School.belongs_to :country
205
- school = School.new
206
- country = Country.create!
207
- school.country = country
208
- school.country.should == country
209
- school.country_id.should == country.id
210
- school.save!
211
- school.reload
212
- school.reload.country_id.should == country.id
213
- end
214
-
215
- it "calls through to belongs_to_active_hash if it's an ActiveHash object" do
216
- School.belongs_to :city
217
- city = City.create
218
- school = School.create :city_id => city.id
219
- school.city.should == city
220
- end
221
- end
222
-
223
- describe "#belongs_to_active_hash" do
224
- context "setting by id" do
225
- it "finds the correct records" do
226
- School.belongs_to_active_hash :city
227
- city = City.create
228
- school = School.create :city_id => city.id
229
- school.city.should == city
230
- end
231
-
232
- it "returns nil when the record does not exist" do
233
- School.belongs_to_active_hash :city
234
- school = School.create! :city_id => nil
235
- school.city.should be_nil
236
- end
237
- end
238
-
239
- context "setting by association" do
240
- it "finds the correct records" do
241
- School.belongs_to_active_hash :city
242
- city = City.create
243
- school = School.create :city => city
244
- school.city.should == city
245
- end
246
-
247
- it "is assignable by name attribute" do
248
- School.belongs_to_active_hash :city, :shortcuts => [:name]
249
- City.data = [ {:id => 1, :name => 'gothan'} ]
250
- city = City.find_by_name 'gothan'
251
- school = School.create :city_name => 'gothan'
252
- school.city.should == city
253
- school.city_name.should == 'gothan'
254
- end
255
-
256
- it "have custom shortcut" do
257
- School.belongs_to_active_hash :city, :shortcuts => :friendly_name
258
- City.data = [ {:id => 1, :friendly_name => 'Gothan City'} ]
259
- city = City.find_by_friendly_name 'Gothan City'
260
- school = School.create :city_friendly_name => 'Gothan City'
261
- school.city.should == city
262
- school.city_friendly_name.should == 'Gothan City'
263
- end
264
-
265
- it "returns nil when the record does not exist" do
266
- School.belongs_to_active_hash :city
267
- school = School.create! :city => nil
268
- school.city.should be_nil
269
- end
270
- end
271
-
272
- it "finds active record metadata for this association" do
273
- School.belongs_to_active_hash :city
274
- association = School.reflect_on_association(:city)
275
- association.should_not be_nil
276
- association.klass.name.should == City.name
277
- end
278
-
279
- it "handles classes ending with an 's'" do
280
- School.belongs_to_active_hash :school_status
281
- association = School.reflect_on_association(:school_status)
282
- association.should_not be_nil
283
- association.klass.name.should == SchoolStatus.name
284
- end
285
-
286
- it "handles custom association names" do
287
- School.belongs_to_active_hash :status, :class_name => 'SchoolStatus'
288
- association = School.reflect_on_association(:status)
289
- association.should_not be_nil
290
- association.klass.name.should == SchoolStatus.name
291
- end
292
- end
293
- end
294
-
295
- describe "#belongs_to" do
296
-
297
- context "with an ActiveRecord parent" do
298
- it "find the correct records" do
299
- City.belongs_to :country
300
- country = Country.create
301
- city = City.create :country_id => country.id
302
- city.country.should == country
303
- end
304
-
305
- it "returns nil when the record does not exist" do
306
- City.belongs_to :country
307
- city = City.create :country_id => 123
308
- city.country.should be_nil
309
- end
310
- end
311
-
312
- context "with an ActiveHash parent" do
313
- it "find the correct records" do
314
- Author.belongs_to :city
315
- city = City.create
316
- author = Author.create :city_id => city.id
317
- author.city.should == city
318
- end
319
-
320
- it "returns nil when the record does not exist" do
321
- Author.belongs_to :city
322
- author = Author.create :city_id => 123
323
- author.city.should be_nil
324
- end
325
- end
326
-
327
- describe "#parent=" do
328
- before do
329
- Author.belongs_to :city
330
- @city = City.create :id => 1
331
- end
332
-
333
- it "sets the underlying id of the parent" do
334
- author = Author.new
335
- author.city = @city
336
- author.city_id.should == @city.id
337
- end
338
-
339
- it "works from hash assignment" do
340
- author = Author.new :city => @city
341
- author.city_id.should == @city.id
342
- author.city.should == @city
343
- end
344
-
345
- it "works with nil" do
346
- author = Author.new :city => @city
347
- author.city_id.should == @city.id
348
- author.city.should == @city
349
-
350
- author.city = nil
351
- author.city_id.should be_nil
352
- author.city.should be_nil
353
- end
354
- end
355
-
356
- describe "with a different foreign key" do
357
- before do
358
- Author.belongs_to :residence, :class_name => "City", :foreign_key => "city_id"
359
- @city = City.create :id => 1
360
- end
361
-
362
- it "works" do
363
- author = Author.new
364
- author.residence = @city
365
- author.city_id.should == @city.id
366
- end
367
- end
368
-
369
- describe "with a different primary key" do
370
- before do
371
- City.field :long_identifier
372
- Author.belongs_to :city, :primary_key => "long_identifier"
373
- @city = City.create :id => 1, :long_identifier => "123"
374
- end
375
-
376
- it "works" do
377
- author = Author.new
378
- author.city = @city
379
- author.city_id.should == @city.long_identifier
380
- end
381
- end
382
- end
383
-
384
- describe "#has_one" do
385
- context "with ActiveRecord children" do
386
- before do
387
- Author.has_one :book
388
- end
389
-
390
- it "find the correct records" do
391
- book = Book.create! :author_id => 1, :published => true
392
- author = Author.create :id => 1
393
- author.book.should == book
394
- end
395
-
396
- it "returns nil when there is no record" do
397
- author = Author.create :id => 1
398
- author.book.should be_nil
399
- end
400
- end
401
-
402
- context "with ActiveHash children" do
403
- before do
404
- City.has_one :author
405
- Author.field :city_id
406
- end
407
-
408
- it "find the correct records" do
409
- city = City.create :id => 1
410
- author = Author.create :city_id => 1
411
- city.author.should == author
412
- end
413
-
414
- it "returns nil when there are no records" do
415
- city = City.create :id => 1
416
- city.author.should be_nil
417
- end
418
- end
419
- end
420
-
421
- describe "#marked_for_destruction?" do
422
- it "should return false" do
423
- City.new.marked_for_destruction?.should == false
424
- end
425
- end
426
-
427
- end