acts_as_scd 0.0.1
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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +43 -0
- data/Rakefile +32 -0
- data/lib/acts_as_scd/base_class_methods.rb +101 -0
- data/lib/acts_as_scd/block_updater.rb +142 -0
- data/lib/acts_as_scd/class_methods.rb +240 -0
- data/lib/acts_as_scd/initialize.rb +114 -0
- data/lib/acts_as_scd/instance_methods.rb +105 -0
- data/lib/acts_as_scd/period.rb +135 -0
- data/lib/acts_as_scd/version.rb +3 -0
- data/lib/acts_as_scd.rb +31 -0
- data/lib/tasks/acts_as_scd_tasks.rake +4 -0
- data/test/acts_as_scd_test.rb +680 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/association.rb +9 -0
- data/test/dummy/app/models/city.rb +19 -0
- data/test/dummy/app/models/commercial_delegate.rb +9 -0
- data/test/dummy/app/models/country.rb +29 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +83 -0
- data/test/dummy/config/environments/test.rb +41 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/locales/scd.en.yml +7 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +24444 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/fixtures/cities.yml +95 -0
- data/test/fixtures/countries.yml +83 -0
- data/test/test_helper.rb +15 -0
- metadata +185 -0
@@ -0,0 +1,680 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_record/fixtures'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
5
|
+
ActiveRecord::Schema.verbose = false
|
6
|
+
|
7
|
+
# Tests data model:
|
8
|
+
# We'll have two models which represent geographical entities and are subject
|
9
|
+
# to changes over time such as modified geographical limits, entities may
|
10
|
+
# disappear or new ones come into existence (as in countries that split, etc.).
|
11
|
+
# We'll assume to such levels of geographical entities, Country and City for
|
12
|
+
# which we want to keep the historical state at any time. We'll use a simple
|
13
|
+
# 'area' field to stand for the various spatial or otherwise properties that
|
14
|
+
# would typically change between revisions.
|
15
|
+
ActiveRecord::Schema.define do
|
16
|
+
|
17
|
+
create_table :countries, :force => true do |t|
|
18
|
+
t.string :code, limit: 2
|
19
|
+
t.string :identity, limit: 2
|
20
|
+
t.integer :effective_from, default: 0
|
21
|
+
t.integer :effective_to, default: 99999999
|
22
|
+
t.string :name
|
23
|
+
t.float :area
|
24
|
+
t.integer :commercial_association_id
|
25
|
+
end
|
26
|
+
|
27
|
+
add_index :countries, :identity
|
28
|
+
add_index :countries, :effective_from
|
29
|
+
add_index :countries, :effective_to
|
30
|
+
add_index :countries, [:effective_from, :effective_to]
|
31
|
+
|
32
|
+
create_table :cities, :force => true do |t|
|
33
|
+
t.string :code, limit: 5
|
34
|
+
t.string :identity, limit: 5
|
35
|
+
t.integer :effective_from, default: 0
|
36
|
+
t.integer :effective_to, default: 99999999
|
37
|
+
t.string :name
|
38
|
+
t.float :area
|
39
|
+
t.string :country_identity, limit: 2
|
40
|
+
end
|
41
|
+
|
42
|
+
add_index :cities, :identity
|
43
|
+
add_index :cities, :effective_from
|
44
|
+
add_index :cities, :effective_to
|
45
|
+
add_index :cities, [:effective_from, :effective_to]
|
46
|
+
|
47
|
+
create_table :commercial_associations, :force => true do |t|
|
48
|
+
t.string :name
|
49
|
+
end
|
50
|
+
|
51
|
+
create_table :commercial_delegates, :force => true do |t|
|
52
|
+
t.string :name
|
53
|
+
t.string :country_identity, limit: 2
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
class ActsAsScdTest < ActiveSupport::TestCase
|
59
|
+
|
60
|
+
fixtures :all
|
61
|
+
|
62
|
+
test "Models can act as SCD" do
|
63
|
+
assert_equal countries(:caledonia), Country.find_by_identity('CL', Date.today)
|
64
|
+
assert_equal Date.new(2014,3,2), countries(:changedonia_first).effective_to_date
|
65
|
+
assert_equal Date.new(2014,3,2), countries(:changedonia_second).effective_from_date
|
66
|
+
assert_equal countries(:changedonia_third), countries(:changedonia_first).current
|
67
|
+
assert_equal countries(:changedonia_third), countries(:changedonia_second).current
|
68
|
+
assert_equal countries(:changedonia_second), countries(:changedonia_first).successor
|
69
|
+
assert_equal countries(:changedonia_third), countries(:changedonia_second).successor
|
70
|
+
end
|
71
|
+
|
72
|
+
test "Identities have iterations" do
|
73
|
+
caledonia = countries(:caledonia)
|
74
|
+
assert_equal 99999999, caledonia.effective_to
|
75
|
+
end
|
76
|
+
|
77
|
+
test "New records have identity automatically assigned and are not time-limited" do
|
78
|
+
country = Country.create!(name: 'Testing 1', code: 'T1')
|
79
|
+
assert_equal country.identity, 'T1'
|
80
|
+
assert_equal ActsAsScd::START_OF_TIME, country.effective_from
|
81
|
+
assert_equal ActsAsScd::END_OF_TIME, country.effective_to
|
82
|
+
end
|
83
|
+
|
84
|
+
test "New identities are not time-limited" do
|
85
|
+
date = Date.new(2014,03,07)
|
86
|
+
country = Country.create_identity(name: 'Testing 2', code: 'T2')
|
87
|
+
assert_equal country.identity, 'T2'
|
88
|
+
assert_equal ActsAsScd::START_OF_TIME, country.effective_from
|
89
|
+
assert_equal ActsAsScd::END_OF_TIME, country.effective_to
|
90
|
+
end
|
91
|
+
|
92
|
+
test "create identities and iterations" do
|
93
|
+
t3 = Country.create_identity(name: 'Testing 3', code: 'T3', area: 1000)
|
94
|
+
assert_equal t3.identity, 'T3'
|
95
|
+
assert_equal ActsAsScd::START_OF_TIME, t3.effective_from
|
96
|
+
assert_equal ActsAsScd::END_OF_TIME, t3.effective_to
|
97
|
+
assert_equal 'T3', t3.code
|
98
|
+
assert_equal 'Testing 3', t3.name
|
99
|
+
assert_equal 1000, t3.area
|
100
|
+
|
101
|
+
date1 = Date.new(2014,02,02)
|
102
|
+
t3_2 = Country.create_iteration('T3', { area: 2000 }, date1)
|
103
|
+
t3.reload
|
104
|
+
assert_equal 2000, t3_2.area
|
105
|
+
assert_equal t3.code, t3_2.code
|
106
|
+
assert_equal t3.name, t3_2.name
|
107
|
+
assert_equal ActsAsScd::START_OF_TIME, t3.effective_from
|
108
|
+
assert_equal date1, t3.effective_to_date
|
109
|
+
assert_equal date1, t3_2.effective_from_date
|
110
|
+
assert_equal ActsAsScd::END_OF_TIME, t3_2.effective_to
|
111
|
+
|
112
|
+
date2 = Date.new(2014,03,02)
|
113
|
+
t3_3 = Country.create_iteration('T3', { area: 3000 }, date2)
|
114
|
+
t3.reload
|
115
|
+
t3_2.reload
|
116
|
+
assert_equal 3000, t3_3.area
|
117
|
+
assert_equal t3.code, t3_3.code
|
118
|
+
assert_equal t3.name, t3_3.name
|
119
|
+
assert_equal ActsAsScd::START_OF_TIME, t3.effective_from
|
120
|
+
assert_equal date1, t3.effective_to_date
|
121
|
+
assert_equal date1, t3_2.effective_from_date
|
122
|
+
assert_equal date2, t3_2.effective_to_date
|
123
|
+
assert_equal date2, t3_3.effective_from_date
|
124
|
+
assert_equal ActsAsScd::END_OF_TIME, t3_3.effective_to
|
125
|
+
|
126
|
+
assert_equal t3_3, Country.find_by_identity('T3')
|
127
|
+
|
128
|
+
assert_equal t3_3, t3.current
|
129
|
+
assert_equal t3_3, t3.at(date2)
|
130
|
+
assert_equal t3_3, t3.at(date2+10)
|
131
|
+
assert_equal t3_2, t3.at(date2-1)
|
132
|
+
assert_equal t3_2, t3.at(date1)
|
133
|
+
assert_equal t3_2, t3.at(date1+10)
|
134
|
+
assert_equal t3, t3.at(date1-1)
|
135
|
+
|
136
|
+
assert_equal t3_3, t3_2.current
|
137
|
+
assert_equal t3_3, t3_2.at(date2)
|
138
|
+
assert_equal t3_3, t3_2.at(date2+10)
|
139
|
+
assert_equal t3_2, t3_2.at(date2-1)
|
140
|
+
|
141
|
+
assert_equal t3, t3.initial
|
142
|
+
assert_equal t3, t3_2.initial
|
143
|
+
assert_equal t3, t3_3.initial
|
144
|
+
|
145
|
+
assert_equal t3_2, t3.successor
|
146
|
+
assert_equal t3_3, t3_2.successor
|
147
|
+
assert_nil t3_3.successor
|
148
|
+
assert_equal t3_2, t3_3.antecessor
|
149
|
+
assert_equal t3, t3_2.antecessor
|
150
|
+
assert_nil t3.antecessor
|
151
|
+
assert_equal [t3, t3_2], t3_3.antecessors
|
152
|
+
assert_equal [t3], t3_2.antecessors
|
153
|
+
assert_equal [], t3.antecessors
|
154
|
+
assert_equal [t3_2, t3_3], t3.successors
|
155
|
+
assert_equal [t3_3], t3_2.successors
|
156
|
+
assert_equal [], t3_3.successors
|
157
|
+
assert_equal [t3, t3_2, t3_3], t3.history
|
158
|
+
assert_equal [t3, t3_2, t3_3], t3_2.history
|
159
|
+
assert_equal [t3, t3_2, t3_3], t3_3.history
|
160
|
+
|
161
|
+
assert_equal t3_3, t3.latest
|
162
|
+
assert_equal t3_3, t3_2.latest
|
163
|
+
assert_equal t3_3, t3_3.latest
|
164
|
+
|
165
|
+
assert_equal t3, t3_3.earliest
|
166
|
+
assert_equal t3, t3_2.earliest
|
167
|
+
assert_equal t3, t3.earliest
|
168
|
+
|
169
|
+
assert t3.ended?
|
170
|
+
assert t3_2.ended?
|
171
|
+
assert !t3_3.ended?
|
172
|
+
|
173
|
+
assert !t3.ended_at?(date1-1)
|
174
|
+
assert t3.ended_at?(date1)
|
175
|
+
assert t3.ended_at?(date1+1)
|
176
|
+
assert t3.ended_at?(date2-1)
|
177
|
+
assert t3.ended_at?(date2)
|
178
|
+
assert t3.ended_at?(date2+1)
|
179
|
+
|
180
|
+
assert !t3_2.ended_at?(date1-1)
|
181
|
+
assert !t3_2.ended_at?(date1)
|
182
|
+
assert !t3_2.ended_at?(date1+1)
|
183
|
+
assert !t3_2.ended_at?(date2-1)
|
184
|
+
assert t3_2.ended_at?(date2)
|
185
|
+
assert t3_2.ended_at?(date2+1)
|
186
|
+
|
187
|
+
assert !t3_3.ended_at?(date1-1)
|
188
|
+
assert !t3_3.ended_at?(date1)
|
189
|
+
assert !t3_3.ended_at?(date1+1)
|
190
|
+
assert !t3_3.ended_at?(date2-1)
|
191
|
+
assert !t3_3.ended_at?(date2)
|
192
|
+
assert !t3_3.ended_at?(date2+1)
|
193
|
+
|
194
|
+
assert t3.initial?
|
195
|
+
assert !t3_2.initial?
|
196
|
+
assert !t3_3.initial?
|
197
|
+
|
198
|
+
assert !t3.current?
|
199
|
+
assert !t3_2.current?
|
200
|
+
assert t3_3.current?
|
201
|
+
|
202
|
+
assert !t3.past_limited?
|
203
|
+
assert t3.future_limited?
|
204
|
+
assert t3_2.past_limited?
|
205
|
+
assert t3_2.future_limited?
|
206
|
+
assert t3_3.past_limited?
|
207
|
+
assert !t3_3.future_limited?
|
208
|
+
|
209
|
+
date3 = Date.new(2014,04,02)
|
210
|
+
# t3_2.terminate_identity(date3)
|
211
|
+
Country.terminate_identity 'T3', date3
|
212
|
+
t3.reload
|
213
|
+
t3_2.reload
|
214
|
+
t3_3.reload
|
215
|
+
assert_nil t3.current
|
216
|
+
assert_nil t3_2.current
|
217
|
+
assert_nil t3_3.current
|
218
|
+
assert_nil Country.find_by_identity('T3')
|
219
|
+
|
220
|
+
assert_nil t3.at(date3+1)
|
221
|
+
assert_nil t3.at(date3)
|
222
|
+
assert_equal t3_3, t3.at(date3-1)
|
223
|
+
assert_equal t3_3, t3.at(date2)
|
224
|
+
assert_equal t3_3, t3.at(date2+10)
|
225
|
+
assert_equal t3_2, t3.at(date2-1)
|
226
|
+
assert_equal t3_2, t3.at(date1)
|
227
|
+
assert_equal t3_2, t3.at(date1+10)
|
228
|
+
assert_equal t3, t3.at(date1-1)
|
229
|
+
|
230
|
+
assert_equal t3_3, t3_2.at(date2)
|
231
|
+
assert_equal t3_3, t3_2.at(date2)
|
232
|
+
assert_equal t3_2, t3_2.at(date2-1)
|
233
|
+
|
234
|
+
assert_equal t3, t3.initial
|
235
|
+
assert_equal t3, t3_2.initial
|
236
|
+
assert_equal t3, t3_3.initial
|
237
|
+
|
238
|
+
assert_equal t3_2, t3.successor
|
239
|
+
assert_equal t3_3, t3_2.successor
|
240
|
+
assert_nil t3_3.successor
|
241
|
+
assert_equal t3_2, t3_3.antecessor
|
242
|
+
assert_equal t3, t3_2.antecessor
|
243
|
+
assert_nil t3.antecessor
|
244
|
+
assert_equal [t3, t3_2], t3_3.antecessors
|
245
|
+
assert_equal [t3], t3_2.antecessors
|
246
|
+
assert_equal [], t3.antecessors
|
247
|
+
assert_equal [t3_2, t3_3], t3.successors
|
248
|
+
assert_equal [t3_3], t3_2.successors
|
249
|
+
assert_equal [], t3_3.successors
|
250
|
+
assert_equal [t3, t3_2, t3_3], t3.history
|
251
|
+
assert_equal [t3, t3_2, t3_3], t3_2.history
|
252
|
+
assert_equal [t3, t3_2, t3_3], t3_3.history
|
253
|
+
|
254
|
+
assert_equal t3_3, t3.latest
|
255
|
+
assert_equal t3_3, t3_2.latest
|
256
|
+
assert_equal t3_3, t3_3.latest
|
257
|
+
|
258
|
+
assert_equal t3, t3_3.earliest
|
259
|
+
assert_equal t3, t3_2.earliest
|
260
|
+
assert_equal t3, t3.earliest
|
261
|
+
|
262
|
+
assert t3.ended?
|
263
|
+
assert t3_2.ended?
|
264
|
+
assert t3_3.ended?
|
265
|
+
|
266
|
+
assert !t3.ended_at?(date1-1)
|
267
|
+
assert t3.ended_at?(date1)
|
268
|
+
assert t3.ended_at?(date1+1)
|
269
|
+
assert t3.ended_at?(date2-1)
|
270
|
+
assert t3.ended_at?(date2)
|
271
|
+
assert t3.ended_at?(date2+1)
|
272
|
+
|
273
|
+
assert !t3_2.ended_at?(date1-1)
|
274
|
+
assert !t3_2.ended_at?(date1)
|
275
|
+
assert !t3_2.ended_at?(date1+1)
|
276
|
+
assert !t3_2.ended_at?(date2-1)
|
277
|
+
assert t3_2.ended_at?(date2)
|
278
|
+
assert t3_2.ended_at?(date2+1)
|
279
|
+
|
280
|
+
assert !t3_3.ended_at?(date1-1)
|
281
|
+
assert !t3_3.ended_at?(date1)
|
282
|
+
assert !t3_3.ended_at?(date1+1)
|
283
|
+
assert !t3_3.ended_at?(date2-1)
|
284
|
+
assert !t3_3.ended_at?(date2)
|
285
|
+
assert !t3_3.ended_at?(date2+1)
|
286
|
+
assert !t3_3.ended_at?(date3-1)
|
287
|
+
assert t3_3.ended_at?(date3)
|
288
|
+
assert t3_3.ended_at?(date3+1)
|
289
|
+
|
290
|
+
assert t3.initial?
|
291
|
+
assert !t3_2.initial?
|
292
|
+
assert !t3_3.initial?
|
293
|
+
|
294
|
+
assert !t3.current?
|
295
|
+
assert !t3_2.current?
|
296
|
+
assert !t3_3.current?
|
297
|
+
|
298
|
+
assert !t3.past_limited?
|
299
|
+
assert t3.future_limited?
|
300
|
+
assert t3_2.past_limited?
|
301
|
+
assert t3_2.future_limited?
|
302
|
+
assert t3_3.past_limited?
|
303
|
+
assert t3_3.future_limited?
|
304
|
+
|
305
|
+
end
|
306
|
+
|
307
|
+
test "find_by_identity" do
|
308
|
+
|
309
|
+
de1 = countries(:de1)
|
310
|
+
de2 = countries(:de2)
|
311
|
+
de3 = countries(:de3)
|
312
|
+
ddr = countries(:ddr)
|
313
|
+
uk1 = countries(:uk1)
|
314
|
+
uk2 = countries(:uk2)
|
315
|
+
sco = countries(:scotland)
|
316
|
+
cal = countries(:caledonia)
|
317
|
+
|
318
|
+
assert_equal de3, Country.find_by_identity('DEU')
|
319
|
+
assert_nil Country.find_by_identity('DDR')
|
320
|
+
assert_equal uk2, Country.find_by_identity('GBR')
|
321
|
+
assert_equal sco, Country.find_by_identity('SCO')
|
322
|
+
|
323
|
+
assert_equal de3, Country.find_by_identity('DEU', Date.new(3000,1,1))
|
324
|
+
assert_equal de3, Country.find_by_identity('DEU', Date.new(2000,1,1))
|
325
|
+
assert_equal de3, Country.find_by_identity('DEU', Date.new(1990,10,3))
|
326
|
+
assert_equal de2, Country.find_by_identity('DEU', Date.new(1990,10,2))
|
327
|
+
assert_equal de2, Country.find_by_identity('DEU', Date.new(1970,1,1))
|
328
|
+
assert_equal de2, Country.find_by_identity('DEU', Date.new(1949,10,7))
|
329
|
+
assert_equal de1, Country.find_by_identity('DEU', Date.new(1949,10,6))
|
330
|
+
assert_equal de1, Country.find_by_identity('DEU', Date.new(1940,1,1))
|
331
|
+
assert_equal de1, Country.find_by_identity('DEU', Date.new(1000,1,1))
|
332
|
+
assert_equal cal, Country.find_by_identity('CL', Date.new(3000,1,1))
|
333
|
+
assert_equal de3, Country.find_by_identity('DEU', Date.new(2000,1,1))
|
334
|
+
assert_equal de3, Country.find_by_identity('DEU', Date.new(1990,10,3))
|
335
|
+
assert_equal de2, Country.find_by_identity('DEU', Date.new(1990,10,2))
|
336
|
+
assert_equal de2, Country.find_by_identity('DEU', Date.new(1970,1,1))
|
337
|
+
assert_equal de2, Country.find_by_identity('DEU', Date.new(1949,10,7))
|
338
|
+
assert_equal de1, Country.find_by_identity('DEU', Date.new(1949,10,6))
|
339
|
+
assert_equal de1, Country.find_by_identity('DEU', Date.new(1940,1,1))
|
340
|
+
assert_equal de1, Country.find_by_identity('DEU', Date.new(1000,1,1))
|
341
|
+
assert_nil Country.find_by_identity('DDR', Date.new(1940,1,1))
|
342
|
+
assert_nil Country.find_by_identity('DDR', Date.new(1949,10,6))
|
343
|
+
assert_equal ddr, Country.find_by_identity('DDR', Date.new(1949,10,7))
|
344
|
+
assert_equal ddr, Country.find_by_identity('DDR', Date.new(1970,1,1))
|
345
|
+
assert_equal ddr, Country.find_by_identity('DDR', Date.new(1990,10,2))
|
346
|
+
assert_nil Country.find_by_identity('DDR', Date.new(1990,10,3))
|
347
|
+
assert_nil Country.find_by_identity('DDR', Date.new(2015,1,1))
|
348
|
+
|
349
|
+
end
|
350
|
+
|
351
|
+
test "identity_exists?" do
|
352
|
+
|
353
|
+
de1 = countries(:de1)
|
354
|
+
de2 = countries(:de2)
|
355
|
+
de3 = countries(:de3)
|
356
|
+
ddr = countries(:ddr)
|
357
|
+
uk1 = countries(:uk1)
|
358
|
+
uk2 = countries(:uk2)
|
359
|
+
sco = countries(:scotland)
|
360
|
+
cal = countries(:caledonia)
|
361
|
+
|
362
|
+
assert Country.identity_exists?('DEU')
|
363
|
+
assert Country.identity_exists?('DDR')
|
364
|
+
assert Country.identity_exists?('GBR')
|
365
|
+
assert Country.identity_exists?('SCO')
|
366
|
+
|
367
|
+
assert Country.identity_exists?('DEU', Date.new(3000,1,1))
|
368
|
+
assert Country.identity_exists?('DEU', Date.new(2000,1,1))
|
369
|
+
assert Country.identity_exists?('DEU', Date.new(1990,10,3))
|
370
|
+
assert Country.identity_exists?('DEU', Date.new(1990,10,2))
|
371
|
+
assert Country.identity_exists?('DEU', Date.new(1970,1,1))
|
372
|
+
assert Country.identity_exists?('DEU', Date.new(1949,10,7))
|
373
|
+
assert Country.identity_exists?('DEU', Date.new(1949,10,6))
|
374
|
+
assert Country.identity_exists?('DEU', Date.new(1940,1,1))
|
375
|
+
assert Country.identity_exists?('DEU', Date.new(1000,1,1))
|
376
|
+
assert Country.identity_exists?('CL', Date.new(3000,1,1))
|
377
|
+
assert Country.identity_exists?('DEU', Date.new(2000,1,1))
|
378
|
+
assert Country.identity_exists?('DEU', Date.new(1990,10,3))
|
379
|
+
assert Country.identity_exists?('DEU', Date.new(1990,10,2))
|
380
|
+
assert Country.identity_exists?('DEU', Date.new(1970,1,1))
|
381
|
+
assert Country.identity_exists?('DEU', Date.new(1949,10,7))
|
382
|
+
assert Country.identity_exists?('DEU', Date.new(1949,10,6))
|
383
|
+
assert Country.identity_exists?('DEU', Date.new(1940,1,1))
|
384
|
+
assert Country.identity_exists?('DEU', Date.new(1000,1,1))
|
385
|
+
assert !Country.identity_exists?('DDR', Date.new(1940,1,1))
|
386
|
+
assert !Country.identity_exists?('DDR', Date.new(1949,10,6))
|
387
|
+
assert Country.identity_exists?('DDR', Date.new(1949,10,7))
|
388
|
+
assert Country.identity_exists?('DDR', Date.new(1970,1,1))
|
389
|
+
assert Country.identity_exists?('DDR', Date.new(1990,10,2))
|
390
|
+
assert !Country.identity_exists?('DDR', Date.new(1990,10,3))
|
391
|
+
assert !Country.identity_exists?('DDR', Date.new(2015,1,1))
|
392
|
+
|
393
|
+
end
|
394
|
+
|
395
|
+
test "Model query methods" do
|
396
|
+
|
397
|
+
de1 = countries(:de1)
|
398
|
+
de2 = countries(:de2)
|
399
|
+
de3 = countries(:de3)
|
400
|
+
ddr = countries(:ddr)
|
401
|
+
uk1 = countries(:uk1)
|
402
|
+
uk2 = countries(:uk2)
|
403
|
+
sco = countries(:scotland)
|
404
|
+
cal = countries(:caledonia)
|
405
|
+
|
406
|
+
assert_equal de3, Country.current.where(identity: 'DEU').first
|
407
|
+
assert_nil Country.current.where(identity: 'DDR').first
|
408
|
+
assert_equal uk2, Country.current.where(identity: 'GBR').first
|
409
|
+
assert_equal sco, Country.current.where(identity: 'SCO').first
|
410
|
+
assert_equal 5, Country.current.count
|
411
|
+
|
412
|
+
assert_equal de1, Country.initial.where(identity: 'DEU').first
|
413
|
+
assert_nil Country.initial.where(identity: 'DDR').first
|
414
|
+
assert_nil Country.initial.where(identity: 'SCO').first
|
415
|
+
assert_equal uk1, Country.initial.where(identity: 'GBR').first
|
416
|
+
assert_equal 4, Country.initial.count
|
417
|
+
|
418
|
+
assert_equal de1, Country.earliest_of('DEU')
|
419
|
+
assert_equal uk1, Country.earliest_of('GBR')
|
420
|
+
assert_equal ddr, Country.earliest_of('DDR')
|
421
|
+
assert_equal sco, Country.earliest_of('SCO')
|
422
|
+
assert_equal cal, Country.earliest_of('CL')
|
423
|
+
|
424
|
+
assert_equal de3, Country.at(Date.new(3000,1,1)).where(identity: 'DEU').first
|
425
|
+
assert_equal de3, Country.at(Date.new(2000,1,1)).where(identity: 'DEU').first
|
426
|
+
assert_equal de3, Country.at(Date.new(1990,10,3)).where(identity: 'DEU').first
|
427
|
+
assert_equal de2, Country.at(Date.new(1990,10,2)).where(identity: 'DEU').first
|
428
|
+
assert_equal de2, Country.at(Date.new(1970,1,1)).where(identity: 'DEU').first
|
429
|
+
assert_equal de2, Country.at(Date.new(1949,10,7)).where(identity: 'DEU').first
|
430
|
+
assert_equal de1, Country.at(Date.new(1949,10,6)).where(identity: 'DEU').first
|
431
|
+
assert_equal de1, Country.at(Date.new(1940,1,1)).where(identity: 'DEU').first
|
432
|
+
assert_equal de1, Country.at(Date.new(1000,1,1)).where(identity: 'DEU').first
|
433
|
+
assert_equal cal, Country.at(Date.new(3000,1,1)).where(identity: 'CL').first
|
434
|
+
assert_equal de3, Country.at(Date.new(2000,1,1)).where(identity: 'DEU').first
|
435
|
+
assert_equal de3, Country.at(Date.new(1990,10,3)).where(identity: 'DEU').first
|
436
|
+
assert_equal de2, Country.at(Date.new(1990,10,2)).where(identity: 'DEU').first
|
437
|
+
assert_equal de2, Country.at(Date.new(1970,1,1)).where(identity: 'DEU').first
|
438
|
+
assert_equal de2, Country.at(Date.new(1949,10,7)).where(identity: 'DEU').first
|
439
|
+
assert_equal de1, Country.at(Date.new(1949,10,6)).where(identity: 'DEU').first
|
440
|
+
assert_equal de1, Country.at(Date.new(1940,1,1)).where(identity: 'DEU').first
|
441
|
+
assert_equal de1, Country.at(Date.new(1000,1,1)).where(identity: 'DEU').first
|
442
|
+
assert_nil Country.at(Date.new(1940,1,1)).where(identity: 'DDR').first
|
443
|
+
assert_nil Country.at(Date.new(1949,10,6)).where(identity: 'DDR').first
|
444
|
+
assert_equal ddr, Country.at(Date.new(1949,10,7)).where(identity: 'DDR').first
|
445
|
+
assert_equal ddr, Country.at(Date.new(1970,1,1)).where(identity: 'DDR').first
|
446
|
+
assert_equal ddr, Country.at(Date.new(1990,10,2)).where(identity: 'DDR').first
|
447
|
+
assert_nil Country.at(Date.new(1990,10,3)).where(identity: 'DDR').first
|
448
|
+
assert_nil Country.at(Date.new(2015,1,1)).where(identity: 'DDR').first
|
449
|
+
assert_equal 4, Country.at(Date.new(1940,1,1)).count
|
450
|
+
assert_equal 4, Country.at(Date.new(1949,10,6)).count
|
451
|
+
assert_equal 5, Country.at(Date.new(1949,10,7)).count
|
452
|
+
assert_equal 5, Country.at(Date.new(1970,1,1)).count
|
453
|
+
assert_equal 5, Country.at(Date.new(1990,10,2)).count
|
454
|
+
assert_equal 4, Country.at(Date.new(1990,10,3)).count
|
455
|
+
assert_equal 4, Country.at(Date.new(2000,1,1)).count
|
456
|
+
assert_equal 4, Country.at(Date.new(2000,1,1)).count
|
457
|
+
assert_equal 4, Country.at(Date.new(2014,3,1)).count
|
458
|
+
assert_equal 4, Country.at(Date.new(2014,3,2)).count
|
459
|
+
assert_equal 4, Country.at(Date.new(2014,9,17)).count
|
460
|
+
assert_equal 5, Country.at(Date.new(2014,9,18)).count
|
461
|
+
assert_equal 5, Country.at(Date.new(2015,1,1)).count
|
462
|
+
|
463
|
+
assert_equal 6, Country.ended.count
|
464
|
+
assert_equal ddr, Country.ended.where(identity: 'DDR').first
|
465
|
+
|
466
|
+
assert_equal [de1, de2, de3], Country.all_of('DEU')
|
467
|
+
|
468
|
+
# These generate queries that are valid for PostgreSQL but not for SQLite3
|
469
|
+
# (v1, v2) IN SELECT ...
|
470
|
+
# assert_equal 1, Country.ended.latest.count
|
471
|
+
# assert_equal 1, Country.terminated.count
|
472
|
+
# assert_equal 1, Country.superseded.count
|
473
|
+
end
|
474
|
+
|
475
|
+
test "Model query methods that return objects" do
|
476
|
+
|
477
|
+
de1 = countries(:de1)
|
478
|
+
de2 = countries(:de2)
|
479
|
+
de3 = countries(:de3)
|
480
|
+
ddr = countries(:ddr)
|
481
|
+
uk1 = countries(:uk1)
|
482
|
+
uk2 = countries(:uk2)
|
483
|
+
sco = countries(:scotland)
|
484
|
+
cal = countries(:caledonia)
|
485
|
+
|
486
|
+
assert_equal de3, Country.latest_of('DEU')
|
487
|
+
assert_equal ddr, Country.latest_of('DDR')
|
488
|
+
assert_equal uk2, Country.latest_of('GBR')
|
489
|
+
assert_equal sco, Country.latest_of('SCO')
|
490
|
+
|
491
|
+
assert_equal de1, Country.earliest_of('DEU')
|
492
|
+
assert_equal ddr, Country.earliest_of('DDR')
|
493
|
+
assert_equal uk1, Country.earliest_of('GBR')
|
494
|
+
assert_equal sco, Country.earliest_of('SCO')
|
495
|
+
|
496
|
+
c = Country.scoped
|
497
|
+
|
498
|
+
assert_equal %w(CG CL DDR DEU GBR SCO), Country.ordered_identities
|
499
|
+
assert_equal %w(CG CL DEU GBR SCO), Country.current.ordered_identities
|
500
|
+
assert_equal %w(CG CL DEU GBR SCO), Country.at(Date.new(2015,1,1)).ordered_identities
|
501
|
+
assert_equal %w(CG CL DEU GBR SCO), Country.at(Date.new(2014,9,18)).ordered_identities
|
502
|
+
assert_equal %w(CG CL DEU GBR), Country.at(Date.new(2014,9,17)).ordered_identities
|
503
|
+
assert_equal %w(CG CL DEU GBR), Country.at(Date.new(2014,3,2)).ordered_identities
|
504
|
+
assert_equal %w(CG CL DEU GBR), Country.at(Date.new(2014,3,1)).ordered_identities
|
505
|
+
assert_equal %w(CG CL DEU GBR), Country.at(Date.new(1990,10,3)).ordered_identities
|
506
|
+
assert_equal %w(CG CL DDR DEU GBR), Country.at(Date.new(1990,10,2)).ordered_identities
|
507
|
+
assert_equal %w(CG CL DDR DEU GBR), Country.at(Date.new(1970,1,1)).ordered_identities
|
508
|
+
assert_equal %w(CG CL DDR DEU GBR), Country.at(Date.new(1949,10,7)).ordered_identities
|
509
|
+
assert_equal %w(CG CL DEU GBR), Country.at(Date.new(1949,10,6)).ordered_identities
|
510
|
+
assert_equal %w(CG CL DEU GBR), Country.at(Date.new(1940,1,1)).ordered_identities
|
511
|
+
|
512
|
+
assert_equal %w(CG CL DDR DEU GBR SCO), Country.ordered_identities
|
513
|
+
assert_equal %w(CG CL DEU GBR SCO), Country.current.ordered_identities
|
514
|
+
assert_equal %w(CG CL DEU GBR SCO), Country.identities_at(Date.new(2015,1,1)).sort
|
515
|
+
assert_equal %w(CG CL DEU GBR SCO), Country.identities_at(Date.new(2014,9,18)).sort
|
516
|
+
assert_equal %w(CG CL DEU GBR), Country.identities_at(Date.new(2014,9,17)).sort
|
517
|
+
assert_equal %w(CG CL DEU GBR), Country.identities_at(Date.new(2014,3,2)).sort
|
518
|
+
assert_equal %w(CG CL DEU GBR), Country.identities_at(Date.new(2014,3,1)).sort
|
519
|
+
assert_equal %w(CG CL DEU GBR), Country.identities_at(Date.new(1990,10,3)).sort
|
520
|
+
assert_equal %w(CG CL DDR DEU GBR), Country.identities_at(Date.new(1990,10,2)).sort
|
521
|
+
assert_equal %w(CG CL DDR DEU GBR), Country.identities_at(Date.new(1970,1,1)).sort
|
522
|
+
assert_equal %w(CG CL DDR DEU GBR), Country.identities_at(Date.new(1949,10,7)).sort
|
523
|
+
assert_equal %w(CG CL DEU GBR), Country.identities_at(Date.new(1949,10,6)).sort
|
524
|
+
assert_equal %w(CG CL DEU GBR), Country.identities_at(Date.new(1940,1,1)).sort
|
525
|
+
|
526
|
+
assert_equal %w(CG CL DEU GBR SCO), Country.current_identities.sort
|
527
|
+
|
528
|
+
assert_equal [ActsAsScd::Period[0, 99999999]],
|
529
|
+
Country.where(identity: 'CL').effective_periods
|
530
|
+
assert_equal [ActsAsScd::Period[19491007, 19901003]],
|
531
|
+
Country.where(identity: 'DDR').effective_periods
|
532
|
+
assert_equal [ActsAsScd::Period[20140918, 99999999]],
|
533
|
+
Country.where(identity: 'SCO').effective_periods
|
534
|
+
assert_equal [ActsAsScd::Period[0, 20140918], ActsAsScd::Period[20140918, 99999999]],
|
535
|
+
Country.where(identity: 'GBR').effective_periods
|
536
|
+
assert_equal [ActsAsScd::Period[0, 19491007], ActsAsScd::Period[19491007, 19901003], ActsAsScd::Period[19901003, 99999999]],
|
537
|
+
Country.where(identity: 'DEU').effective_periods
|
538
|
+
assert_equal [[0,19491007], [0, 20140302], [0, 20140918], [0, 99999999], [19491007, 19901003], [19901003, 99999999],
|
539
|
+
[20140302, 20140507], [20140507, 99999999], [20140918, 99999999]].map{|p| ActsAsScd::Period[*p]},
|
540
|
+
Country.effective_periods
|
541
|
+
|
542
|
+
end
|
543
|
+
|
544
|
+
test "has_many_iterations_through_identity association" do
|
545
|
+
|
546
|
+
de1 = countries(:de1)
|
547
|
+
de2 = countries(:de2)
|
548
|
+
de3 = countries(:de3)
|
549
|
+
ddr = countries(:ddr)
|
550
|
+
uk1 = countries(:uk1)
|
551
|
+
uk2 = countries(:uk2)
|
552
|
+
sco = countries(:scotland)
|
553
|
+
cal = countries(:caledonia)
|
554
|
+
|
555
|
+
# current citites
|
556
|
+
assert_equal [:berlin3, :hamburg, :leipzig_3].map{|c| cities(c)},
|
557
|
+
de3.cities.order('code')
|
558
|
+
|
559
|
+
# all iterations
|
560
|
+
assert_equal [:berlin1, :berlin2, :berlin3, :hamburg, :leipzig_1, :leipzig_3].map{|c| cities(c)},
|
561
|
+
de3.city_iterations.order('code, effective_from')
|
562
|
+
assert_equal [:berlin1, :berlin2, :berlin3, :hamburg, :leipzig_1, :leipzig_3].map{|c| cities(c)},
|
563
|
+
de2.city_iterations.order('code, effective_from')
|
564
|
+
assert_equal [:berlin1, :berlin2, :berlin3, :hamburg, :leipzig_1, :leipzig_3].map{|c| cities(c)},
|
565
|
+
de1.city_iterations.order('code, effective_from')
|
566
|
+
assert_equal [:e_berlin, :leipzig_2].map{|c| cities(c)},
|
567
|
+
ddr.city_iterations.order('code, effective_from')
|
568
|
+
|
569
|
+
assert_equal [:berlin1, :hamburg, :leipzig_1].map{|c| cities(c)},
|
570
|
+
de3.cities_at(Date.new(1949,10,6)).order('code')
|
571
|
+
assert_equal [:berlin2, :hamburg].map{|c| cities(c)},
|
572
|
+
de3.cities_at(Date.new(1949,10,7)).order('code')
|
573
|
+
assert_equal [:berlin2, :hamburg].map{|c| cities(c)},
|
574
|
+
de3.cities_at(Date.new(1990,10,2)).order('code')
|
575
|
+
assert_equal [:berlin3, :hamburg, :leipzig_3].map{|c| cities(c)},
|
576
|
+
de3.cities_at(Date.new(1990,10,3)).order('code')
|
577
|
+
|
578
|
+
assert_equal %w(BER HAM LEI), de3.city_identities.sort
|
579
|
+
assert_equal %w(BER HAM LEI), de3.city_identities_at(Date.new(1949,10,6)).sort
|
580
|
+
assert_equal %w(BER HAM), de3.city_identities_at(Date.new(1949,10,7)).sort
|
581
|
+
assert_equal %w(BER HAM), de3.city_identities_at(Date.new(1990,10,2)).sort
|
582
|
+
assert_equal %w(BER HAM LEI), de3.city_identities_at(Date.new(1990,10,3)).sort
|
583
|
+
|
584
|
+
assert_equal %w(BER HAM LEI), de3.city_current_identities.sort
|
585
|
+
assert_equal %w(BER HAM LEI), de2.city_current_identities.sort
|
586
|
+
assert_equal %w(BER HAM LEI), de1.city_current_identities.sort
|
587
|
+
|
588
|
+
# current city countries
|
589
|
+
assert_equal de3, cities(:hamburg).country
|
590
|
+
assert_equal de3, cities(:berlin1).country
|
591
|
+
assert_equal de3, cities(:berlin2).country
|
592
|
+
assert_equal de3, cities(:berlin3).country
|
593
|
+
assert_equal de3, cities(:leipzig_1).country
|
594
|
+
assert_equal de3, cities(:leipzig_3).country
|
595
|
+
assert_nil cities(:leipzig_2).country
|
596
|
+
|
597
|
+
assert_equal de1, cities(:hamburg).country_at(Date.new(1949,10,6))
|
598
|
+
assert_equal de2, cities(:hamburg).country_at(Date.new(1949,10,7))
|
599
|
+
assert_equal de2, cities(:hamburg).country_at(Date.new(1990,10,2))
|
600
|
+
assert_equal de3, cities(:hamburg).country_at(Date.new(1990,10,3))
|
601
|
+
|
602
|
+
assert_equal de1, cities(:berlin1).country_at(Date.new(1949,10,6))
|
603
|
+
assert_equal de2, cities(:berlin1).country_at(Date.new(1949,10,7))
|
604
|
+
assert_equal de2, cities(:berlin1).country_at(Date.new(1990,10,2))
|
605
|
+
assert_equal de3, cities(:berlin1).country_at(Date.new(1990,10,3))
|
606
|
+
|
607
|
+
assert_equal de1, cities(:berlin2).country_at(Date.new(1949,10,6))
|
608
|
+
assert_equal de2, cities(:berlin2).country_at(Date.new(1949,10,7))
|
609
|
+
assert_equal de2, cities(:berlin2).country_at(Date.new(1990,10,2))
|
610
|
+
assert_equal de3, cities(:berlin2).country_at(Date.new(1990,10,3))
|
611
|
+
|
612
|
+
assert_equal de1, cities(:berlin3).country_at(Date.new(1949,10,6))
|
613
|
+
assert_equal de2, cities(:berlin3).country_at(Date.new(1949,10,7))
|
614
|
+
assert_equal de2, cities(:berlin3).country_at(Date.new(1990,10,2))
|
615
|
+
assert_equal de3, cities(:berlin3).country_at(Date.new(1990,10,3))
|
616
|
+
|
617
|
+
assert_nil cities(:e_berlin).country_at(Date.new(1949,10,6))
|
618
|
+
assert_equal ddr, cities(:e_berlin).country_at(Date.new(1949,10,7))
|
619
|
+
assert_equal ddr, cities(:e_berlin).country_at(Date.new(1990,10,2))
|
620
|
+
assert_nil cities(:e_berlin).country_at(Date.new(1990,10,3))
|
621
|
+
|
622
|
+
assert_equal de1, cities(:leipzig_1).country_at(Date.new(1949,10,6))
|
623
|
+
assert_equal de2, cities(:leipzig_1).country_at(Date.new(1949,10,7)) # may cause surprise
|
624
|
+
assert_equal de2, cities(:leipzig_1).country_at(Date.new(1990,10,2)) # may cause surprise
|
625
|
+
assert_equal de3, cities(:leipzig_1).country_at(Date.new(1990,10,3))
|
626
|
+
|
627
|
+
assert_nil cities(:leipzig_2).country_at(Date.new(1949,10,6)) # may cause surprise
|
628
|
+
assert_equal ddr, cities(:leipzig_2).country_at(Date.new(1949,10,7))
|
629
|
+
assert_equal ddr, cities(:leipzig_2).country_at(Date.new(1990,10,2))
|
630
|
+
assert_nil cities(:leipzig_2).country_at(Date.new(1990,10,3)) # may cause surprise
|
631
|
+
|
632
|
+
assert_equal de1, cities(:leipzig_3).country_at(Date.new(1949,10,6))
|
633
|
+
assert_equal de2, cities(:leipzig_3).country_at(Date.new(1949,10,7)) # may cause surprise
|
634
|
+
assert_equal de2, cities(:leipzig_3).country_at(Date.new(1990,10,2)) # may cause surprise
|
635
|
+
assert_equal de3, cities(:leipzig_3).country_at(Date.new(1990,10,3))
|
636
|
+
|
637
|
+
# To avoid surprises, cities iterations should also be selected at the evaluation date:
|
638
|
+
date1 = Date.new(1949,10,7)
|
639
|
+
date2 = Date.new(1990,10,3)
|
640
|
+
assert_equal de1, cities(:leipzig_1).at(date1-1).country_at(date1-1)
|
641
|
+
assert_equal ddr, cities(:leipzig_1).at(date1).country_at(date1)
|
642
|
+
assert_equal ddr, cities(:leipzig_1).at(date2-1).country_at(date2-1)
|
643
|
+
assert_equal de3, cities(:leipzig_1).at(date2).country_at(date2)
|
644
|
+
assert_equal de1, cities(:leipzig_2).at(date1-1).country_at(date1-1)
|
645
|
+
assert_equal ddr, cities(:leipzig_2).at(date1).country_at(date1)
|
646
|
+
assert_equal ddr, cities(:leipzig_2).at(date2-1).country_at(date2-1)
|
647
|
+
assert_equal de3, cities(:leipzig_2).at(date2).country_at(date2)
|
648
|
+
assert_equal de1, cities(:leipzig_3).at(date1-1).country_at(date1-1)
|
649
|
+
assert_equal ddr, cities(:leipzig_3).at(date1).country_at(date1)
|
650
|
+
assert_equal ddr, cities(:leipzig_3).at(date2-1).country_at(date2-1)
|
651
|
+
assert_equal de3, cities(:leipzig_3).at(date2).country_at(date2)
|
652
|
+
|
653
|
+
end
|
654
|
+
|
655
|
+
test "query methods applied to associations" do
|
656
|
+
assert_equal [:berlin3, :hamburg, :leipzig_3].map{|c| cities(c)},
|
657
|
+
countries(:de3).city_iterations.current.order('code')
|
658
|
+
assert_equal [:berlin1, :hamburg, :leipzig_1].map{|c| cities(c)},
|
659
|
+
countries(:de3).city_iterations.at(Date.new(1949,10,6)).order('code')
|
660
|
+
assert_equal [:berlin2, :hamburg].map{|c| cities(c)},
|
661
|
+
countries(:de3).city_iterations.at(Date.new(1949,10,7)).order('code')
|
662
|
+
assert_equal [:berlin2, :hamburg].map{|c| cities(c)},
|
663
|
+
countries(:de3).city_iterations.at(Date.new(1990,10,2)).order('code')
|
664
|
+
assert_equal [:berlin3, :hamburg, :leipzig_3].map{|c| cities(c)},
|
665
|
+
countries(:de3).city_iterations.at(Date.new(1990,10,3)).order('code')
|
666
|
+
|
667
|
+
assert_equal %w(BER HAM LEI),
|
668
|
+
countries(:de3).city_iterations.ordered_identities
|
669
|
+
assert_equal %w(BER HAM LEI),
|
670
|
+
countries(:de3).city_iterations.at(Date.new(1949,10,6)).ordered_identities
|
671
|
+
assert_equal %w(BER HAM),
|
672
|
+
countries(:de3).city_iterations.at(Date.new(1949,10,7)).ordered_identities
|
673
|
+
assert_equal %w(BER HAM),
|
674
|
+
countries(:de3).city_iterations.at(Date.new(1990,10,2)).ordered_identities
|
675
|
+
assert_equal %w(BER HAM LEI),
|
676
|
+
countries(:de3).city_iterations.at(Date.new(1990,10,3)).ordered_identities
|
677
|
+
|
678
|
+
end
|
679
|
+
|
680
|
+
end
|