active_repository 0.2.7 → 0.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.
@@ -4,16 +4,13 @@ require 'active_record'
4
4
  require 'mongoid'
5
5
 
6
6
  describe ActiveRepository::Base, "associations" do
7
-
8
7
  before do
9
8
  class Country < ActiveRecord::Base
10
- extend ActiveRepository::Associations::ActiveRecordExtensions
11
9
  establish_connection :adapter => "sqlite3", :database => ":memory:"
12
10
  connection.create_table(:countries, :force => true) {}
13
11
  end
14
12
 
15
13
  class School < ActiveRecord::Base
16
- extend ActiveRepository::Associations::ActiveRecordExtensions
17
14
  establish_connection :adapter => "sqlite3", :database => ":memory:"
18
15
  connection.create_table(:schools, :force => true) do |t|
19
16
  t.integer :city_id
@@ -34,9 +31,9 @@ describe ActiveRepository::Base, "associations" do
34
31
  end
35
32
 
36
33
  if Object.const_defined?(:ActiveModel)
37
- scope :published, {:conditions => {:published => true}}
34
+ scope :published, -> { where(published: true) }
38
35
  else
39
- named_scope :published, {:conditions => {:published => true}}
36
+ named_scope :published, -> { where(:published => true) }
40
37
  end
41
38
  end
42
39
  end
@@ -82,60 +79,18 @@ describe ActiveRepository::Base, "associations" do
82
79
  it "find the correct records" do
83
80
  City.has_many :authors
84
81
  city = City.create :id => 1
85
- city.authors.should == [@included_author_1, @included_author_2]
82
+ city.authors.all.should == [@included_author_1, @included_author_2]
86
83
  end
87
84
 
88
85
  it "uses the correct class name when passed" do
89
86
  City.has_many :writers, :class_name => "Author"
90
87
  city = City.create :id => 1
91
- city.writers.should == [@included_author_1, @included_author_2]
88
+ city.writers.all.should == [@included_author_1, @included_author_2]
92
89
  end
93
90
  end
94
91
 
95
92
  end
96
93
 
97
- describe ActiveRepository::Associations::ActiveRecordExtensions do
98
-
99
- describe "#belongs_to_active_repository" do
100
- context "setting by id" do
101
- it "finds the correct records" do
102
- School.belongs_to_active_repository :city
103
- city = City.create
104
- school = School.create :city_id => city.id
105
- school.city.should == city
106
- end
107
-
108
- it "returns nil when the record does not exist" do
109
- School.belongs_to_active_repository :city
110
- school = School.create! :city_id => nil
111
- school.city.should be_nil
112
- end
113
- end
114
-
115
- context "setting by association" do
116
- it "finds the correct records" do
117
- School.belongs_to_active_repository :city
118
- city = City.create
119
- school = School.create :city => city
120
- school.city.should == city
121
- end
122
-
123
- it "returns nil when the record does not exist" do
124
- School.belongs_to_active_repository :city
125
- school = School.create! :city => nil
126
- school.city.should be_nil
127
- end
128
- end
129
-
130
- it "finds active record metadata for this association" do
131
- School.belongs_to_active_repository :city
132
- association = School.reflect_on_association(:city)
133
- association.should_not be_nil
134
- association.klass.name.should == City.name
135
- end
136
- end
137
- end
138
-
139
94
  describe "#belongs_to" do
140
95
 
141
96
  context "with an ActiveRecord parent" do
@@ -297,6 +252,9 @@ describe ActiveRepository::Base, "associations" do
297
252
  end
298
253
 
299
254
  after do
255
+ City.delete_all
256
+ State.delete_all
257
+
300
258
  Object.send :remove_const, :CityModel
301
259
  Object.send :remove_const, :State
302
260
  Object.send :remove_const, :StateModel
@@ -307,7 +265,7 @@ describe ActiveRepository::Base, "associations" do
307
265
  country = Country.create
308
266
  state = State.create(:country_id => country.id)
309
267
 
310
- country.states.should == [state]
268
+ country.states.all.should == [state]
311
269
  end
312
270
  end
313
271
 
@@ -323,7 +281,8 @@ describe ActiveRepository::Base, "associations" do
323
281
  state = State.create
324
282
  city = City.create(:state_id => state.id)
325
283
 
326
- state.cities.should == [city]
284
+
285
+ state.cities.all.should == [city]
327
286
  end
328
287
  end
329
288
 
@@ -29,6 +29,9 @@ describe ActiveRepository, "Base" do
29
29
  Country.create(:id => 5, :name => "Brazil")
30
30
  end
31
31
 
32
+ it_behaves_like '.constantize'
33
+ it_behaves_like '.serialized_attributes'
34
+ it_behaves_like '.update_attribute'
32
35
  it_behaves_like '.update_attributes'
33
36
  it_behaves_like '.all'
34
37
  it_behaves_like '.where'
@@ -37,7 +40,8 @@ describe ActiveRepository, "Base" do
37
40
  it_behaves_like '.first'
38
41
  it_behaves_like '.last'
39
42
  it_behaves_like '.find'
40
- it_behaves_like '.find_by_id'
43
+ it_behaves_like '.find_by'
44
+ it_behaves_like '.find_by!'
41
45
  it_behaves_like 'custom finders'
42
46
  it_behaves_like '#method_missing'
43
47
  it_behaves_like '#attributes'
@@ -58,6 +62,7 @@ describe ActiveRepository, "Base" do
58
62
  it_behaves_like '#new_record?'
59
63
  it_behaves_like '.transaction'
60
64
  it_behaves_like '.delete_all'
65
+ it_behaves_like '#delete'
61
66
  end
62
67
 
63
68
  context "active_record", :active_record do
@@ -90,6 +95,9 @@ describe ActiveRepository, "Base" do
90
95
  Object.send :remove_const, :CountryModel
91
96
  end
92
97
 
98
+ it_behaves_like '.constantize'
99
+ it_behaves_like '.serialized_attributes'
100
+ it_behaves_like '.update_attribute'
93
101
  it_behaves_like '.update_attributes'
94
102
  it_behaves_like '.all'
95
103
  it_behaves_like '.where'
@@ -98,7 +106,8 @@ describe ActiveRepository, "Base" do
98
106
  it_behaves_like '.first'
99
107
  it_behaves_like '.last'
100
108
  it_behaves_like '.find'
101
- it_behaves_like '.find_by_id'
109
+ it_behaves_like '.find_by'
110
+ it_behaves_like '.find_by!'
102
111
  it_behaves_like 'custom finders'
103
112
  it_behaves_like '#method_missing'
104
113
  it_behaves_like '#attributes'
@@ -119,6 +128,7 @@ describe ActiveRepository, "Base" do
119
128
  it_behaves_like '#new_record?'
120
129
  it_behaves_like '.transaction'
121
130
  it_behaves_like '.delete_all'
131
+ it_behaves_like '#delete'
122
132
  end
123
133
 
124
134
  context "mongoid", :mongoid do
@@ -156,6 +166,9 @@ describe ActiveRepository, "Base" do
156
166
  Object.send :remove_const, :CountryModel
157
167
  end
158
168
 
169
+ it_behaves_like '.constantize'
170
+ it_behaves_like '.serialized_attributes'
171
+ it_behaves_like '.update_attribute'
159
172
  it_behaves_like '.update_attributes'
160
173
  it_behaves_like '.all'
161
174
  it_behaves_like '.where'
@@ -164,7 +177,8 @@ describe ActiveRepository, "Base" do
164
177
  it_behaves_like '.first'
165
178
  it_behaves_like '.last'
166
179
  it_behaves_like '.find'
167
- it_behaves_like '.find_by_id'
180
+ it_behaves_like '.find_by'
181
+ it_behaves_like '.find_by!'
168
182
  it_behaves_like 'custom finders'
169
183
  it_behaves_like '#method_missing'
170
184
  it_behaves_like '#attributes'
@@ -185,5 +199,6 @@ describe ActiveRepository, "Base" do
185
199
  it_behaves_like '#new_record?'
186
200
  it_behaves_like '.transaction'
187
201
  it_behaves_like '.delete_all'
202
+ it_behaves_like '#delete'
188
203
  end
189
204
  end
@@ -0,0 +1,366 @@
1
+ require 'spec_helper'
2
+ require 'active_repository'
3
+ require 'active_repository/result_set'
4
+ require 'pry'
5
+
6
+ describe ActiveRepository::ResultSet, :result_set do
7
+ before do
8
+ class Country < ActiveRepository::Base
9
+ fields :name, :continent
10
+ end
11
+ end
12
+
13
+ describe 'initialize' do
14
+ context 'with class only' do
15
+ subject { ActiveRepository::ResultSet.new(Country) }
16
+
17
+ it 'must not be nil' do
18
+ expect(subject).not_to be_nil
19
+ end
20
+ end
21
+
22
+ context 'with class and attributes' do
23
+ subject { ActiveRepository::ResultSet.new(Country, {a: 'a'}) }
24
+
25
+ it 'must not be nil' do
26
+ expect(subject).not_to be_nil
27
+ end
28
+ end
29
+ end
30
+
31
+ describe '#count' do
32
+ after do
33
+ Country.delete_all
34
+ end
35
+
36
+ context 'without query' do
37
+ it 'returns total records in data store' do
38
+ Country.create
39
+ Country.create
40
+ Country.create
41
+ Country.create
42
+
43
+ expect(Country.count).to eq 4
44
+ end
45
+ end
46
+
47
+ context 'without query' do
48
+ it 'returns total records in data store' do
49
+ Country.create(name: 'A')
50
+ Country.create
51
+ Country.create(name: 'B')
52
+ Country.create
53
+
54
+ expect(Country.where(name: { '$ne' => nil}).count).to eq 2
55
+ end
56
+ end
57
+ end
58
+
59
+ describe '#where' do
60
+ subject { ActiveRepository::ResultSet.new(Country) }
61
+
62
+ it "returns a ResultSet object" do
63
+ expect(subject.where(name: 'Canada')).to be_a(ActiveRepository::ResultSet)
64
+ end
65
+
66
+ it 'creates another ResultSet object' do
67
+ result = subject.where(continent: 'America')
68
+
69
+ expect(result).not_to eq subject
70
+ end
71
+
72
+ context 'nested ResultSets' do
73
+ it 'returns a new ResultSet' do
74
+ result = subject.where(name: 'Canada').where(continent: 'America')
75
+
76
+ expect(result).to be_a(ActiveRepository::ResultSet)
77
+ end
78
+
79
+ it 'returns a new ResultSet' do
80
+ result = subject.where(name: 'Canada').where("continent = 'America'")
81
+
82
+ expect(result).to be_a(ActiveRepository::ResultSet)
83
+ end
84
+ end
85
+ end
86
+
87
+ describe '#and' do
88
+ it 'is a alias for #where' do
89
+ expect(described_class.instance_method(:and)).to eq described_class.instance_method(:where)
90
+ end
91
+ end
92
+
93
+ describe '#or' do
94
+ subject { ActiveRepository::ResultSet.new(Country) }
95
+
96
+ it "returns a ResultSet object" do
97
+ expect(subject.or(name: 'Canada')).to be_a(ActiveRepository::ResultSet)
98
+ end
99
+
100
+ it 'creates another ResultSet object' do
101
+ result = subject.or(continent: 'America')
102
+
103
+ expect(result).not_to eq subject
104
+ end
105
+
106
+ context 'nested ResultSets' do
107
+ it 'returns a new ResultSet' do
108
+ result = subject.or(name: 'Canada').or(continent: 'America')
109
+
110
+ expect(result).to be_a(ActiveRepository::ResultSet)
111
+ end
112
+ end
113
+ end
114
+
115
+ describe '#all' do
116
+ before do
117
+ Country.delete_all
118
+ Country.create(name: 'Canada', continent: 'America')
119
+ Country.create(name: 'Russia', continent: 'Europe')
120
+ Country.create(name: 'USA', continent: 'America')
121
+ Country.create(name: 'Brazil', continent: 'America')
122
+ end
123
+
124
+ subject { ActiveRepository::ResultSet.new(Country) }
125
+
126
+ context 'when result_set is not empty' do
127
+ context 'single ResultSet' do
128
+ it 'returns an array of objects' do
129
+ objects = subject.where(continent: 'America').all
130
+
131
+ expect(objects.class).to eq Array
132
+ end
133
+
134
+ it 'returns a collection of Countries' do
135
+ objects = subject.where(continent: 'America').all
136
+
137
+ expect(objects.map(&:class).uniq).to eq [Country]
138
+ end
139
+
140
+ it 'returns filtered objects' do
141
+ objects = subject.where(continent: 'America').all
142
+
143
+ expect(objects).to eq (Country.all - [Country.find(2)])
144
+ end
145
+ end
146
+
147
+ context 'nested ResultSets' do
148
+ it 'returns an array of objects' do
149
+ objects = subject.where(continent: 'America').where(name: 'Canada').all
150
+
151
+ expect(objects.class).to eq Array
152
+ end
153
+
154
+ it 'returns a collection of Countries' do
155
+ objects = subject.where(continent: 'America').where(name: 'Canada').all
156
+
157
+ expect(objects.map(&:class).uniq).to eq [Country]
158
+ end
159
+
160
+ it 'returns filtered objects' do
161
+ objects = subject.where(continent: 'America').where(name: 'Canada').all
162
+
163
+ expect(objects).to eq ([Country.first])
164
+ end
165
+
166
+ it 'returns filtered objects' do
167
+ objects = subject.where(continent: 'America').where("name = 'Canada'").all
168
+
169
+ expect(objects).to eq ([Country.first])
170
+ end
171
+ end
172
+ end
173
+
174
+ context 'when result_set is empty' do
175
+ it 'returns nil' do
176
+ expect(subject.where('').all).to be_empty
177
+ end
178
+ end
179
+ end
180
+
181
+ describe '#first' do
182
+ subject { ActiveRepository::ResultSet.new(Country) }
183
+
184
+ context 'when result_set is not empty' do
185
+ it 'returns first object from filtered objects' do
186
+ objects = subject.where(name: 'Russia').or(name: 'USA').first
187
+
188
+ expect(objects).to eq Country.find(2)
189
+ end
190
+ end
191
+
192
+ context 'when result_set is empty' do
193
+ it 'returns nil' do
194
+ objects = subject.where(name: 'Russia').and(name: 'USA').first
195
+
196
+ expect(objects).to be_nil
197
+ end
198
+ end
199
+ end
200
+
201
+ describe '#last' do
202
+ subject { ActiveRepository::ResultSet.new(Country) }
203
+
204
+ context 'when result_set is not empty' do
205
+ it 'returns first object from filtered objects' do
206
+ objects = subject.where(name: 'Russia').or(name: 'USA').last
207
+
208
+ expect(objects).to eq Country.find(3)
209
+ end
210
+ end
211
+
212
+ context 'when result_set is empty' do
213
+ it 'returns nil' do
214
+ objects = subject.where(name: 'Russia').and(name: 'USA').first
215
+
216
+ expect(objects).to be_nil
217
+ end
218
+ end
219
+ end
220
+
221
+ describe '#first_or_initialize' do
222
+ subject { ActiveRepository::ResultSet.new(Country) }
223
+
224
+ context 'single query' do
225
+ context 'when result_set is not empty' do
226
+ it 'returns first filtered object' do
227
+ object = subject.where(name: 'Russia').first_or_initialize
228
+
229
+ expect(object).to eq Country.find(2)
230
+ end
231
+ end
232
+
233
+ context 'when result_set is empty' do
234
+ context 'query is a Hash' do
235
+ it 'returns an object with specified attributes' do
236
+ object = subject.where(name: 'Poland').first_or_initialize
237
+
238
+ expect(object.attributes).to eq(name: 'Poland')
239
+ end
240
+ end
241
+
242
+ context 'query is not a Hash' do
243
+ it 'returns a new object with specified attributes' do
244
+ object = subject.where("name = 'Poland'").first_or_initialize
245
+
246
+ expect(object.attributes).to eq({})
247
+ end
248
+ end
249
+ end
250
+ end
251
+
252
+ context 'nested _query' do
253
+ context 'when result_set is not empty' do
254
+ it 'returns first filtered object' do
255
+ object = subject.where(name: 'Russia').and(continent: 'Europe').first_or_initialize
256
+
257
+ expect(object).to eq Country.find(2)
258
+ end
259
+ end
260
+
261
+ context 'when result_set is empty' do
262
+ context 'all queries are Hashes' do
263
+ it 'returns a new object with specified attributes' do
264
+ object = subject.where(name: 'Poland').and(continent: 'Europe').first_or_initialize
265
+
266
+ expect(object.attributes).to eq(name: 'Poland', continent: 'Europe')
267
+ end
268
+ end
269
+
270
+ context 'not all queries are Hashes' do
271
+ it 'returns a new object with all Hashes as attributes' do
272
+ object = subject.where(name: 'Poland').and("continent = 'Europe'").first_or_initialize
273
+
274
+ expect(object.attributes).to eq(name: 'Poland')
275
+ end
276
+
277
+ it 'returns a new object with all Hashes as attributes' do
278
+ object = subject.where("name = 'Poland'").and("continent = 'Europe'").first_or_initialize
279
+
280
+ expect(object.attributes).to eq({})
281
+ end
282
+ end
283
+ end
284
+ end
285
+ end
286
+
287
+ describe '#first_or_create' do
288
+ subject { ActiveRepository::ResultSet.new(Country) }
289
+
290
+ before do
291
+ Country.delete_all
292
+ Country.create(name: 'Canada', continent: 'America')
293
+ Country.create(name: 'Russia', continent: 'Europe')
294
+ Country.create(name: 'USA', continent: 'America')
295
+ Country.create(name: 'Brazil', continent: 'America')
296
+ end
297
+
298
+ context 'single query' do
299
+ context 'when result_set is not empty' do
300
+ it 'returns first filtered object' do
301
+ object = subject.where(name: 'Russia').first_or_create
302
+
303
+ expect(object).to eq Country.find(2)
304
+ end
305
+ end
306
+
307
+ context 'when result_set is empty' do
308
+ context 'query is a Hash' do
309
+ it 'returns an object with id not nil' do
310
+ object = subject.where(name: 'Poland').first_or_create
311
+
312
+ expect(object.id).to eq Country.last.id
313
+ end
314
+
315
+ it 'returns an object with specified attributes' do
316
+ object = subject.where(name: 'Poland').first_or_create
317
+
318
+ expect(object.attributes).to eq(id: 5, name: 'Poland')
319
+ end
320
+ end
321
+
322
+ context 'query is not a Hash' do
323
+ it 'returns a new object with specified attributes' do
324
+ object = subject.where("name = 'Poland'").first_or_create
325
+
326
+ expect(object.attributes).to eq(id: 5)
327
+ end
328
+ end
329
+ end
330
+ end
331
+
332
+ context 'nested _query' do
333
+ context 'when result_set is not empty' do
334
+ it 'returns first filtered object' do
335
+ object = subject.where(name: 'Russia').and(continent: 'Europe').first_or_create
336
+
337
+ expect(object).to eq Country.find(2)
338
+ end
339
+ end
340
+
341
+ context 'when result_set is empty' do
342
+ context 'all queries are Hashes' do
343
+ it 'returns a new object with specified attributes' do
344
+ object = subject.where(name: 'Poland').and(continent: 'Europe').first_or_create
345
+
346
+ expect(object.attributes).to eq(id: 5, name: 'Poland', continent: 'Europe')
347
+ end
348
+ end
349
+
350
+ context 'not all queries are Hashes' do
351
+ it 'returns a new object with all Hashes as attributes' do
352
+ object = subject.where(name: 'Poland').and("continent = 'Europe'").first_or_create
353
+
354
+ expect(object.attributes).to eq(id: 5, name: 'Poland')
355
+ end
356
+
357
+ it 'returns a new object with all Hashes as attributes' do
358
+ object = subject.where("name = 'Poland'").and("continent = 'Europe'").first_or_create
359
+
360
+ expect(object.attributes).to eq(id: 5)
361
+ end
362
+ end
363
+ end
364
+ end
365
+ end
366
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,12 @@
1
- require 'rspec'
2
- require 'rspec/autorun'
3
1
  require 'coveralls'
4
2
  Coveralls.wear!
3
+ # require 'simplecov'
4
+ # SimpleCov.start do
5
+ # add_filter "/spec/"
6
+ # end
7
+
8
+ require 'rspec'
9
+ require 'rspec/autorun'
5
10
 
6
11
  RSpec.configure do |c|
7
12
  c.treat_symbols_as_metadata_keys_with_true_values = true