active_repository 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,348 @@
1
+ require 'spec_helper'
2
+ require 'active_repository'
3
+ require 'active_record'
4
+ require 'mongoid'
5
+
6
+ describe ActiveRepository::Base, "associations" do
7
+
8
+ before do
9
+ class Country < ActiveRecord::Base
10
+ extend ActiveRepository::Associations::ActiveRecordExtensions
11
+ establish_connection :adapter => "sqlite3", :database => ":memory:"
12
+ connection.create_table(:countries, :force => true) {}
13
+ end
14
+
15
+ class School < ActiveRecord::Base
16
+ extend ActiveRepository::Associations::ActiveRecordExtensions
17
+ establish_connection :adapter => "sqlite3", :database => ":memory:"
18
+ connection.create_table(:schools, :force => true) do |t|
19
+ t.integer :city_id
20
+ end
21
+ end
22
+
23
+ class City < ActiveRepository::Base
24
+ City.set_model_class(self)
25
+ City.set_save_in_memory(true)
26
+ end
27
+
28
+ class Author < ActiveRepository::Base
29
+ Author.set_model_class(self)
30
+ Author.set_save_in_memory(true)
31
+ end
32
+
33
+ class Book < ActiveRecord::Base
34
+ establish_connection :adapter => "sqlite3", :database => ":memory:"
35
+ connection.create_table(:books, :force => true) do |t|
36
+ t.integer :author_id
37
+ t.boolean :published
38
+ end
39
+
40
+ if Object.const_defined?(:ActiveModel)
41
+ scope :published, {:conditions => {:published => true}}
42
+ else
43
+ named_scope :published, {:conditions => {:published => true}}
44
+ end
45
+ end
46
+ end
47
+
48
+ after do
49
+ Object.send :remove_const, :City
50
+ Object.send :remove_const, :Author
51
+ Object.send :remove_const, :Country
52
+ Object.send :remove_const, :School
53
+ Object.send :remove_const, :Book
54
+ end
55
+
56
+ describe "#has_many" do
57
+
58
+ context "with ActiveRecord children" do
59
+ before do
60
+ @included_book_1 = Book.create! :author_id => 1, :published => true
61
+ @included_book_2 = Book.create! :author_id => 1, :published => false
62
+ @excluded_book = Book.create! :author_id => 2, :published => true
63
+ end
64
+
65
+ it "find the correct records" do
66
+ Author.has_many :books
67
+ author = Author.create :id => 1
68
+ author.books.should == [@included_book_1, @included_book_2]
69
+ end
70
+
71
+ it "return a scope so that we can apply further scopes" do
72
+ Author.has_many :books
73
+ author = Author.create :id => 1
74
+ author.books.published.should == [@included_book_1]
75
+ end
76
+ end
77
+
78
+ context "with ActiveHash children" do
79
+ before do
80
+ Author.field :city_id
81
+ @included_author_1 = Author.create :city_id => 1
82
+ @included_author_2 = Author.create :city_id => 1
83
+ @excluded_author = Author.create :city_id => 2
84
+ end
85
+
86
+ it "find the correct records" do
87
+ City.has_many :authors
88
+ city = City.create :id => 1
89
+ city.authors.should == [@included_author_1, @included_author_2]
90
+ end
91
+
92
+ it "uses the correct class name when passed" do
93
+ City.has_many :writers, :class_name => "Author"
94
+ city = City.create :id => 1
95
+ city.writers.should == [@included_author_1, @included_author_2]
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ describe ActiveRepository::Associations::ActiveRecordExtensions do
102
+
103
+ describe "#belongs_to_active_repository" do
104
+ context "setting by id" do
105
+ it "finds the correct records" do
106
+ School.belongs_to_active_repository :city
107
+ city = City.create
108
+ school = School.create :city_id => city.id
109
+ school.city.should == city
110
+ end
111
+
112
+ it "returns nil when the record does not exist" do
113
+ School.belongs_to_active_repository :city
114
+ school = School.create! :city_id => nil
115
+ school.city.should be_nil
116
+ end
117
+ end
118
+
119
+ context "setting by association" do
120
+ it "finds the correct records" do
121
+ School.belongs_to_active_repository :city
122
+ city = City.create
123
+ school = School.create :city => city
124
+ school.city.should == city
125
+ end
126
+
127
+ it "returns nil when the record does not exist" do
128
+ School.belongs_to_active_repository :city
129
+ school = School.create! :city => nil
130
+ school.city.should be_nil
131
+ end
132
+ end
133
+
134
+ it "finds active record metadata for this association" do
135
+ School.belongs_to_active_repository :city
136
+ association = School.reflect_on_association(:city)
137
+ association.should_not be_nil
138
+ association.klass.name.should == City.name
139
+ end
140
+ end
141
+ end
142
+
143
+ describe "#belongs_to" do
144
+
145
+ context "with an ActiveRecord parent" do
146
+ it "find the correct records" do
147
+ City.belongs_to :country
148
+ country = Country.create
149
+ city = City.create :country_id => country.id
150
+ city.country.should == country
151
+ end
152
+
153
+ it "returns nil when the record does not exist" do
154
+ City.belongs_to :country
155
+ city = City.create :country_id => 123
156
+ city.country.should be_nil
157
+ end
158
+ end
159
+
160
+ context "with an ActiveHash parent" do
161
+ it "find the correct records" do
162
+ Author.belongs_to :city
163
+ city = City.create
164
+ author = Author.create :city_id => city.id
165
+ author.city.should == city
166
+ end
167
+
168
+ it "returns nil when the record does not exist" do
169
+ Author.belongs_to :city
170
+ author = Author.create :city_id => 123
171
+ author.city.should be_nil
172
+ end
173
+ end
174
+
175
+ describe "#parent=" do
176
+ before do
177
+ Author.belongs_to :city
178
+ @city = City.create :id => 1
179
+ end
180
+
181
+ it "sets the underlying id of the parent" do
182
+ author = Author.new
183
+ author.city = @city
184
+ author.city_id.should == @city.id
185
+ end
186
+
187
+ it "works from hash assignment" do
188
+ author = Author.new :city => @city
189
+ author.city_id.should == @city.id
190
+ author.city.should == @city
191
+ end
192
+
193
+ it "works with nil" do
194
+ author = Author.new :city => @city
195
+ author.city_id.should == @city.id
196
+ author.city.should == @city
197
+
198
+ author.city = nil
199
+ author.city_id.should be_nil
200
+ author.city.should be_nil
201
+ end
202
+ end
203
+
204
+ describe "with a different foreign key" do
205
+ before do
206
+ Author.belongs_to :residence, :class_name => "City", :foreign_key => "city_id"
207
+ @city = City.create :id => 1
208
+ end
209
+
210
+ it "works" do
211
+ author = Author.new
212
+ author.residence = @city
213
+ author.city_id.should == @city.id
214
+ end
215
+ end
216
+ end
217
+
218
+ describe "#has_one" do
219
+ context "with ActiveRecord children" do
220
+ before do
221
+ Author.has_one :book
222
+ end
223
+
224
+ it "find the correct records" do
225
+ book = Book.create! :author_id => 1, :published => true
226
+ author = Author.create :id => 1
227
+ author.book.should == book
228
+ end
229
+
230
+ it "returns nil when there is no record" do
231
+ author = Author.create :id => 1
232
+ author.book.should be_nil
233
+ end
234
+ end
235
+
236
+ context "with ActiveHash children" do
237
+ before do
238
+ City.has_one :author
239
+ Author.field :city_id
240
+ end
241
+
242
+ it "find the correct records" do
243
+ city = City.create :id => 1
244
+ author = Author.create :city_id => 1
245
+ city.author.should == author
246
+ end
247
+
248
+ it "returns nil when there are no records" do
249
+ city = City.create :id => 1
250
+ city.author.should be_nil
251
+ end
252
+ end
253
+ end
254
+
255
+ describe "#marked_for_destruction?" do
256
+ it "should return false" do
257
+ City.new.marked_for_destruction?.should == false
258
+ end
259
+ end
260
+
261
+ describe "Multiple ORM" do
262
+ before do
263
+ Object.send :remove_const, :City
264
+ # Object.send :remove_const, :Author
265
+ Object.send :remove_const, :Country
266
+ # Object.send :remove_const, :School
267
+ # Object.send :remove_const, :Book
268
+
269
+ class Country < ActiveRepository::Base
270
+ Country.set_model_class(Country)
271
+ Country.set_save_in_memory(true)
272
+ has_many :states
273
+ end
274
+
275
+ class StateModel < ActiveRecord::Base
276
+ self.table_name = 'states'
277
+ establish_connection :adapter => "sqlite3", :database => ":memory:"
278
+ connection.create_table(:states) do |t|
279
+ t.integer :country_id
280
+ end
281
+ end
282
+
283
+ class State < ActiveRepository::Base
284
+ State.set_model_class(StateModel)
285
+ State.set_save_in_memory(false)
286
+ belongs_to :country
287
+ has_many :cities
288
+
289
+ fields :country_id, :city_id
290
+ end
291
+
292
+ Mongoid.load!("support/mongoid.yml", :development)
293
+
294
+ class CityModel
295
+ include Mongoid::Document
296
+ store_in collection: "countries"
297
+ field :state_id
298
+ end
299
+
300
+ class City < ActiveRepository::Base
301
+ City.set_model_class(CityModel)
302
+ City.set_save_in_memory(false)
303
+ belongs_to :state
304
+ end
305
+ end
306
+
307
+ after do
308
+ Object.send :remove_const, :CityModel
309
+ Object.send :remove_const, :State
310
+ Object.send :remove_const, :StateModel
311
+ end
312
+
313
+ context ActiveRepository do
314
+ it "relates with ActiveRecord models" do
315
+ country = Country.create
316
+ state = State.create(:country_id => country.id)
317
+
318
+ country.states.should == [state]
319
+ end
320
+ end
321
+
322
+ context ActiveRecord do
323
+ it "relates with ActiveRepository objects" do
324
+ country = Country.create
325
+ state = State.create(:country_id => country.id)
326
+
327
+ state.country.should == country
328
+ end
329
+
330
+ it "relates with Mongoid models" do
331
+ state = State.create
332
+ city = City.create(:state_id => state.id)
333
+
334
+ state.cities.should == [city]
335
+ end
336
+ end
337
+
338
+ context Mongoid do
339
+ it "relates with ActiveRecord models" do
340
+ state = State.create
341
+ city = City.create(:state_id => state.id)
342
+
343
+ city.state.should == state
344
+ end
345
+ end
346
+ end
347
+
348
+ end
@@ -2,8 +2,8 @@ require 'spec_helper'
2
2
  require 'support/shared_examples'
3
3
 
4
4
  require 'active_repository'
5
- require "active_record"
6
- require "mongoid"
5
+ require 'active_record'
6
+ require 'mongoid'
7
7
 
8
8
  describe ActiveRepository, "Base" do
9
9
 
@@ -120,4 +120,70 @@ describe ActiveRepository, "Base" do
120
120
  it_behaves_like '.transaction'
121
121
  it_behaves_like '.delete_all'
122
122
  end
123
+
124
+ context "mongoid" do
125
+ before do
126
+ Country.fields :name, :monarch, :language
127
+
128
+ Mongoid.load!("support/mongoid.yml", :development)
129
+
130
+ class CountryModel
131
+ include Mongoid::Document
132
+
133
+ store_in collection: "countries"
134
+
135
+ field :name
136
+ field :monarch
137
+ field :language
138
+ field :_id, type: Integer, default: -> { CountryModel.last ? CountryModel.last.id + 1 : 1 }
139
+ field :updated_at
140
+ field :created_at
141
+ end
142
+
143
+ Country.set_model_class(CountryModel)
144
+ Country.set_save_in_memory(false)
145
+
146
+ Country.delete_all
147
+
148
+ Country.create(:id => 1, :name => "US", :language => 'English')
149
+ Country.create(:id => 2, :name => "Canada", :language => 'English', :monarch => "The Crown of England")
150
+ Country.create(:id => 3, :name => "Mexico", :language => 'Spanish')
151
+ Country.create(:id => 4, :name => "UK", :language => 'English', :monarch => "The Crown of England")
152
+ Country.create(:id => 5, :name => "Brazil")
153
+ end
154
+
155
+ after do
156
+ Object.send :remove_const, :CountryModel
157
+ end
158
+
159
+ it_behaves_like '.update_attributes'
160
+ it_behaves_like '.all'
161
+ it_behaves_like '.where'
162
+ it_behaves_like '.exists?'
163
+ it_behaves_like '.count'
164
+ it_behaves_like '.first'
165
+ it_behaves_like '.last'
166
+ it_behaves_like '.find'
167
+ it_behaves_like '.find_by_id'
168
+ it_behaves_like 'custom finders'
169
+ it_behaves_like '#method_missing'
170
+ it_behaves_like '#attributes'
171
+ it_behaves_like 'reader_methods'
172
+ it_behaves_like 'interrogator methods'
173
+ it_behaves_like '#id'
174
+ it_behaves_like '#quoted_id'
175
+ it_behaves_like '#to_param'
176
+ it_behaves_like '#persisted?'
177
+ it_behaves_like '#eql?'
178
+ it_behaves_like '#=='
179
+ it_behaves_like '#hash'
180
+ it_behaves_like '#readonly?'
181
+ it_behaves_like '#cache_key'
182
+ it_behaves_like '#save'
183
+ it_behaves_like '.create'
184
+ it_behaves_like '#valid?'
185
+ it_behaves_like '#new_record?'
186
+ it_behaves_like '.transaction'
187
+ it_behaves_like '.delete_all'
188
+ end
123
189
  end
@@ -3,7 +3,6 @@ require 'support/sql_query_shared_examples'
3
3
 
4
4
  require 'active_repository'
5
5
  require "active_record"
6
- require "mongoid"
7
6
 
8
7
  describe ActiveRepository, "Base" do
9
8
 
@@ -30,10 +30,9 @@ shared_examples ".all" do
30
30
 
31
31
  it "populates the data correctly" do
32
32
  records = Country.all
33
- records.first.id.should == 1
34
- records.first.name.should == "US"
35
- records.last.id.should == 5
36
- records.last.name.should == "Brazil"
33
+ records.should include(Country.first)
34
+ records.should include(Country.last)
35
+ records.size.should == 5
37
36
  end
38
37
  end
39
38
 
@@ -72,12 +71,13 @@ end
72
71
 
73
72
  shared_examples ".exists?" do
74
73
  it "checks if a record exists" do
75
- id = Country.last.id + 1
74
+ id = Country.last.id
76
75
 
77
76
  Country.delete_all
78
77
  Country.exists?(id).should be_false
79
78
 
80
- country = Country.create(:id => id, :name => "France")
79
+ country = Country.create(:name => "France")
80
+ id = country.id
81
81
 
82
82
  Country.exists?(id).should be_true
83
83
  end
@@ -120,7 +120,11 @@ shared_examples ".find" do
120
120
 
121
121
  context "with :all" do
122
122
  it "returns all records" do
123
- Country.find(:all).should == [Country.find(1), Country.find(2), Country.find(3), Country.find(4), Country.find(5)]
123
+ records = Country.find(:all)
124
+
125
+ records.should include(Country.first)
126
+ records.should include(Country.last)
127
+ records.size.should == 5
124
128
  end
125
129
  end
126
130
 
@@ -446,9 +450,13 @@ shared_examples "#cache_key" do
446
450
  end
447
451
 
448
452
  it 'should use the record\'s updated_at if present' do
449
- country = Country.create(:id => 6, :name => "foo")
453
+ country = Country.create(:name => "foo")
450
454
 
451
- Country.first.cache_key.should == "countries/6-#{country.updated_at.to_s(:number)}"
455
+ id = Country.last.id
456
+
457
+ date_string = country.updated_at.nil? ? "" : "-#{country.updated_at.to_s(:number)}"
458
+
459
+ Country.first.cache_key.should == "countries/#{id}#{date_string}"
452
460
  end
453
461
 
454
462
  it 'should use "new" instead of the id for a new record' do
@@ -467,7 +475,16 @@ shared_examples "#save" do
467
475
  country = Country.new :id => 1, :name => "foo", :monarch => nil, :language => nil
468
476
  country.persist.should be_true
469
477
  country.reload
470
- Country.all.should == [country]
478
+
479
+ countries_attributes = Country.all.map(&:attributes)
480
+ countries_attributes.delete(:created_at)
481
+ countries_attributes.delete(:updated_at)
482
+
483
+ expected_attributes = [country].map(&:attributes)
484
+ expected_attributes.delete(:created_at)
485
+ expected_attributes.delete(:updated_at)
486
+
487
+ countries_attributes.should == expected_attributes
471
488
  end
472
489
  end
473
490
 
@@ -479,16 +496,31 @@ shared_examples ".create" do
479
496
 
480
497
  it "works with no args" do
481
498
  Country.all.should be_empty
482
- country = Country.create :id => 6
483
- country.id.should == 6
499
+ country = Country.create
500
+
501
+ country.id.should == Country.last.id
484
502
  end
485
503
 
486
504
  it "adds the new object to the data collection" do
487
505
  Country.all.should be_empty
488
- country = Country.create :id => 6, :name => "foo"
489
- country.id.should == 6
506
+ country = Country.create :name => "foo"
507
+ country.id.should == Country.last.id
490
508
  country.name.should == "foo"
491
- Country.all.should == [country]
509
+
510
+ countries_attributes = Country.all.map(&:attributes)
511
+ expected_attributes = [country].map(&:attributes)
512
+
513
+ countries_attributes.each do |a|
514
+ a.delete(:created_at)
515
+ a.delete(:updated_at)
516
+ end
517
+
518
+ expected_attributes.each do |a|
519
+ a.delete(:created_at)
520
+ a.delete(:updated_at)
521
+ end
522
+
523
+ countries_attributes.should == expected_attributes
492
524
  end
493
525
 
494
526
  it "adds an auto-incrementing id if the id is nil" do
@@ -509,11 +541,24 @@ shared_examples ".create" do
509
541
 
510
542
  it "adds the new object to the data collection" do
511
543
  Country.all.should be_empty
512
- country = Country.create :id => 6, :name => "foo"
513
- country.id.should == 6
544
+ country = Country.create :name => "foo"
545
+ country.id.should == Country.last.id
514
546
  country.name.should == "foo"
515
547
 
516
- Country.all.should == [country]
548
+ countries_attributes = Country.all.map(&:attributes)
549
+ expected_attributes = [country].map(&:attributes)
550
+
551
+ countries_attributes.each do |a|
552
+ a.delete(:created_at)
553
+ a.delete(:updated_at)
554
+ end
555
+
556
+ expected_attributes.each do |a|
557
+ a.delete(:created_at)
558
+ a.delete(:updated_at)
559
+ end
560
+
561
+ countries_attributes.should == expected_attributes
517
562
  end
518
563
 
519
564
  it "updates count" do
@@ -575,7 +620,21 @@ shared_examples ".delete_all" do
575
620
  it "clears out all record" do
576
621
  country1 = Country.create
577
622
  country2 = Country.create
578
- Country.all.should == [country1, country2]
623
+
624
+ countries_attributes = Country.all.map(&:attributes)
625
+ expected_attributes = [country1, country2].map(&:attributes)
626
+
627
+ countries_attributes.each do |a|
628
+ a.delete(:created_at)
629
+ a.delete(:updated_at)
630
+ end
631
+
632
+ expected_attributes.each do |a|
633
+ a.delete(:created_at)
634
+ a.delete(:updated_at)
635
+ end
636
+
637
+ countries_attributes.should == expected_attributes
579
638
  Country.delete_all
580
639
  Country.all.should be_empty
581
640
  end
@@ -0,0 +1,6 @@
1
+ development:
2
+ sessions:
3
+ default:
4
+ database: active_repository
5
+ hosts:
6
+ - localhost:27017