chrono_model 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +2 -1
- data/lib/chrono_model/time_machine.rb +1 -1
- data/lib/chrono_model/time_machine/history_model.rb +4 -0
- data/lib/chrono_model/version.rb +1 -1
- data/spec/chrono_model/adapter/base_spec.rb +157 -0
- data/spec/chrono_model/adapter/ddl_spec.rb +243 -0
- data/spec/chrono_model/adapter/indexes_spec.rb +72 -0
- data/spec/chrono_model/adapter/migrations_spec.rb +312 -0
- data/spec/chrono_model/history_models_spec.rb +32 -0
- data/spec/chrono_model/time_machine/as_of_spec.rb +188 -0
- data/spec/chrono_model/time_machine/changes_spec.rb +50 -0
- data/spec/chrono_model/{adapter → time_machine}/counter_cache_race_spec.rb +2 -2
- data/spec/chrono_model/time_machine/default_scope_spec.rb +37 -0
- data/spec/chrono_model/time_machine/history_spec.rb +104 -0
- data/spec/chrono_model/time_machine/keep_cool_spec.rb +27 -0
- data/spec/chrono_model/time_machine/manipulations_spec.rb +84 -0
- data/spec/chrono_model/time_machine/model_identification_spec.rb +46 -0
- data/spec/chrono_model/time_machine/sequence_spec.rb +74 -0
- data/spec/chrono_model/time_machine/sti_spec.rb +100 -0
- data/spec/chrono_model/{time_query_spec.rb → time_machine/time_query_spec.rb} +22 -5
- data/spec/chrono_model/time_machine/timeline_spec.rb +63 -0
- data/spec/chrono_model/time_machine/timestamps_spec.rb +43 -0
- data/spec/chrono_model/time_machine/transactions_spec.rb +69 -0
- data/spec/support/adapter/helpers.rb +53 -0
- data/spec/support/adapter/structure.rb +44 -0
- data/spec/support/time_machine/helpers.rb +47 -0
- data/spec/support/time_machine/structure.rb +111 -0
- metadata +48 -14
- data/spec/chrono_model/adapter/sti_bug_spec.rb +0 -49
- data/spec/chrono_model/adapter_spec.rb +0 -788
- data/spec/chrono_model/time_machine_spec.rb +0 -749
- data/spec/support/helpers.rb +0 -198
data/spec/support/helpers.rb
DELETED
@@ -1,198 +0,0 @@
|
|
1
|
-
module ChronoTest::Helpers
|
2
|
-
|
3
|
-
module Adapter
|
4
|
-
def self.included(base)
|
5
|
-
base.extend DSL
|
6
|
-
base.instance_eval do
|
7
|
-
delegate :adapter, :to => ChronoTest
|
8
|
-
delegate :columns, :table, :pk_type, :to => DSL
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
module DSL
|
13
|
-
def with_temporal_table(&block)
|
14
|
-
context ':temporal => true' do
|
15
|
-
before(:all) { adapter.create_table(table, :temporal => true, &DSL.columns) }
|
16
|
-
after(:all) { adapter.drop_table table }
|
17
|
-
|
18
|
-
instance_eval(&block)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def with_plain_table(&block)
|
23
|
-
context ':temporal => false' do
|
24
|
-
before(:all) { adapter.create_table(table, :temporal => false, &DSL.columns) }
|
25
|
-
after(:all) { adapter.drop_table table }
|
26
|
-
|
27
|
-
instance_eval(&block)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.table(table = nil)
|
32
|
-
@table = table if table
|
33
|
-
@table
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.columns(&block)
|
37
|
-
@columns = block.call if block
|
38
|
-
@columns
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.pk_type
|
42
|
-
@pk_type ||= if ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1
|
43
|
-
'bigint'
|
44
|
-
else
|
45
|
-
'integer'
|
46
|
-
end
|
47
|
-
end
|
48
|
-
delegate :columns, :table, :pk_type, :to => self
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
module TimeMachine
|
53
|
-
def self.included(base)
|
54
|
-
base.extend(DSL)
|
55
|
-
base.extend(self)
|
56
|
-
end
|
57
|
-
|
58
|
-
module DSL
|
59
|
-
@@schema_setup = false
|
60
|
-
|
61
|
-
def setup_schema!
|
62
|
-
return if @@schema_setup
|
63
|
-
@@schema_setup = true
|
64
|
-
|
65
|
-
# Set up database structure
|
66
|
-
#
|
67
|
-
adapter.create_table 'foos', :temporal => true do |t|
|
68
|
-
t.string :name
|
69
|
-
t.integer :fooity
|
70
|
-
end
|
71
|
-
|
72
|
-
adapter.create_table 'bars', :temporal => true do |t|
|
73
|
-
t.string :name
|
74
|
-
t.references :foo
|
75
|
-
end
|
76
|
-
|
77
|
-
adapter.create_table 'sub_bars', :temporal => true do |t|
|
78
|
-
t.string :name
|
79
|
-
t.references :bar
|
80
|
-
end
|
81
|
-
|
82
|
-
adapter.create_table 'bazs' do |t|
|
83
|
-
t.string :name
|
84
|
-
t.references :bar
|
85
|
-
end
|
86
|
-
|
87
|
-
adapter.create_table 'defoos', :temporal => true do |t|
|
88
|
-
t.string :name
|
89
|
-
t.boolean :active
|
90
|
-
end
|
91
|
-
|
92
|
-
adapter.create_table 'elements', :temporal => true do |t|
|
93
|
-
t.string :title
|
94
|
-
t.string :type
|
95
|
-
end
|
96
|
-
|
97
|
-
adapter.create_table 'plains' do |t|
|
98
|
-
t.string :foo
|
99
|
-
end
|
100
|
-
|
101
|
-
adapter.create_table 'events' do |t|
|
102
|
-
t.string :name
|
103
|
-
t.daterange :interval
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
Models = lambda {
|
108
|
-
class ::Foo < ActiveRecord::Base
|
109
|
-
include ChronoModel::TimeMachine
|
110
|
-
|
111
|
-
has_many :bars
|
112
|
-
has_many :sub_bars, :through => :bars
|
113
|
-
end
|
114
|
-
|
115
|
-
class ::Bar < ActiveRecord::Base
|
116
|
-
include ChronoModel::TimeMachine
|
117
|
-
|
118
|
-
belongs_to :foo
|
119
|
-
has_many :sub_bars
|
120
|
-
has_one :baz
|
121
|
-
|
122
|
-
has_timeline :with => :foo
|
123
|
-
end
|
124
|
-
|
125
|
-
class ::SubBar < ActiveRecord::Base
|
126
|
-
include ChronoModel::TimeMachine
|
127
|
-
|
128
|
-
belongs_to :bar
|
129
|
-
|
130
|
-
has_timeline :with => :bar
|
131
|
-
end
|
132
|
-
|
133
|
-
class ::Baz < ActiveRecord::Base
|
134
|
-
include ChronoModel::TimeGate
|
135
|
-
|
136
|
-
belongs_to :bar
|
137
|
-
|
138
|
-
has_timeline :with => :bar
|
139
|
-
end
|
140
|
-
|
141
|
-
class ::Defoo < ActiveRecord::Base
|
142
|
-
include ChronoModel::TimeMachine
|
143
|
-
|
144
|
-
default_scope proc { where(:active => true) }
|
145
|
-
end
|
146
|
-
|
147
|
-
# STI case (https://github.com/ifad/chronomodel/issues/5)
|
148
|
-
class ::Element < ActiveRecord::Base
|
149
|
-
include ChronoModel::TimeMachine
|
150
|
-
end
|
151
|
-
|
152
|
-
class ::Publication < Element
|
153
|
-
end
|
154
|
-
|
155
|
-
class ::Plain < ActiveRecord::Base
|
156
|
-
end
|
157
|
-
|
158
|
-
class ::Event < ActiveRecord::Base
|
159
|
-
extend ChronoModel::TimeMachine::TimeQuery
|
160
|
-
end
|
161
|
-
}
|
162
|
-
|
163
|
-
@@models_defined = false
|
164
|
-
def define_models!
|
165
|
-
return if @@models_defined
|
166
|
-
@@models_defined = true
|
167
|
-
|
168
|
-
Models.call
|
169
|
-
end
|
170
|
-
|
171
|
-
def adapter
|
172
|
-
ChronoTest.connection
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
# If a context object is given, evaluates the given
|
177
|
-
# block in its instance context, then defines a `ts`
|
178
|
-
# on it, backed by an Array, and adds the current
|
179
|
-
# database timestamp to it.
|
180
|
-
#
|
181
|
-
# If a context object is not given, the block is
|
182
|
-
# evaluated in the current context and the above
|
183
|
-
# mangling is done on the blocks' return value.
|
184
|
-
#
|
185
|
-
def ts_eval(ctx = nil, &block)
|
186
|
-
ret = (ctx || self).instance_eval(&block)
|
187
|
-
(ctx || ret).tap do |obj|
|
188
|
-
obj.singleton_class.instance_eval do
|
189
|
-
define_method(:ts) { @_ts ||= [] }
|
190
|
-
end unless obj.methods.include?(:ts)
|
191
|
-
|
192
|
-
now = ChronoTest.connection.select_value('select now()::timestamp') + 'Z'
|
193
|
-
obj.ts.push(Time.parse(now))
|
194
|
-
end
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
end
|