-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_spec.rb
More file actions
322 lines (271 loc) · 10.8 KB
/
Copy pathtype_spec.rb
File metadata and controls
322 lines (271 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
require File.expand_path("../support", __FILE__)
require "timecop"
require "active_model"
require "couchbase-orm/types"
class DateTimeWith3Decimal < CouchbaseOrm::Types::DateTime
def cast(value)
result = super(value)
result&.floor(3)
end
def serialize(value)
value&.iso8601(3)
end
end
ActiveModel::Type.register(:datetime3decimal, DateTimeWith3Decimal)
class TypeTest < CouchbaseOrm::Base
attribute :name, :string
attribute :age, :integer
attribute :size, :float
attribute :renewal_date, :date
attribute :subscribed_at, :datetime
attribute :some_time, :timestamp
attribute :precision3_time, :datetime3decimal
attribute :precision6_time, :datetime, precision: 6
attribute :created_at, :datetime, precision: 6
attribute :updated_at, :datetime, precision: 6
attribute :active, :boolean
index :age, presence: false
index :renewal_date, presence: false
index :some_time, presence: false
index :precision3_time, presence: false
end
class N1qlTypeTest < CouchbaseOrm::Base
attribute :name, :string
attribute :age, :integer
attribute :size, :float
attribute :renewal_date, :date
attribute :subscribed_at, :datetime
attribute :some_time, :timestamp
attribute :precision3_time, :datetime3decimal
attribute :active, :boolean
index_n1ql :name, validate: false
index_n1ql :age, validate: false
index_n1ql :size, validate: false
index_n1ql :active, validate: false
index_n1ql :renewal_date, validate: false
index_n1ql :some_time, validate: false
index_n1ql :subscribed_at, validate: false
index_n1ql :precision3_time, validate: false
n1ql :by_both_dates, emit_key: [:renewal_date, :subscribed_at], presence: false
end
TypeTest.ensure_design_document!
N1qlTypeTest.ensure_design_document!
describe CouchbaseOrm::Types::Timestamp do
it "should cast an integer to time" do
t = Time.at(Time.now.to_i)
expect(CouchbaseOrm::Types::Timestamp.new.cast(t.to_i)).to eq(t)
end
it "should cast an integer string to time" do
t = Time.at(Time.now.to_i)
expect(CouchbaseOrm::Types::Timestamp.new.cast(t.to_s)).to eq(t)
end
end
describe CouchbaseOrm::Types::Date do
it "should cast an string to date" do
d = Date.today
expect(CouchbaseOrm::Types::Date.new.cast(d.to_s)).to eq(d)
end
it "should serialize date to string" do
d = Date.today
expect(CouchbaseOrm::Types::Date.new.serialize(d)).to eq(d.to_s)
end
it "should get the type from the registry" do
expect(ActiveModel::Type.lookup(:date)).to eq(CouchbaseOrm::Types::Date.new)
end
end
describe CouchbaseOrm::Base do
before(:each) do
TypeTest.delete_all
N1qlTypeTest.delete_all
end
it "should be typed" do
expect(N1qlTypeTest.attribute_types["name"]).to be_a(ActiveModel::Type::String)
end
it "should be createable" do
t = TypeTest.create!
expect(t).to be_a(TypeTest)
end
it "should be able to set attributes" do
t = TypeTest.new
t.name = "joe"
t.age = 20
t.size = 1.5
t.renewal_date = Date.today
t.subscribed_at = Time.now
t.active = true
t.save!
expect(t.name).to eq("joe")
expect(t.age).to eq(20)
expect(t.size).to eq(1.5)
expect(t.renewal_date).to eq(Date.today)
expect(t.subscribed_at).to be_a(Time)
expect(t.active).to eq(true)
end
it "should be able to set attributes with a hash" do
t = TypeTest.new(name: "joe", age: 20, size: 1.5, renewal_date: Date.today, subscribed_at: Time.now, active: true)
t.save!
expect(t.name).to eq("joe")
expect(t.age).to eq(20)
expect(t.size).to eq(1.5)
expect(t.renewal_date).to eq(Date.today)
expect(t.subscribed_at).to be_a(Time)
expect(t.active).to eq(true)
end
it "should be able to be stored and retrieved" do
now = Time.now
t = TypeTest.create!(name: "joe", age: 20, size: 1.5, renewal_date: Date.today, subscribed_at: now, active: true)
t2 = TypeTest.find(t.id)
expect(t2.name).to eq("joe")
expect(t2.age).to eq(20)
expect(t2.size).to eq(1.5)
expect(t2.renewal_date).to eq(Date.today)
expect(t2.subscribed_at).to eq(now.utc.change(usec: 0))
expect(t2.active).to eq(true)
end
it "should be able to query by age" do
t = TypeTest.create!(age: 20)
_t2 = TypeTest.create!(age: 40)
expect(TypeTest.find_by_age(20)).to eq t
end
it "should be able to query by age and type cast" do
t = TypeTest.create!(age: "20")
expect(TypeTest.find_by_age(20)).to eq t
expect(TypeTest.find_by_age("20")).to eq t
end
it "should be able to query by date" do
t = TypeTest.create!(renewal_date: Date.today)
_t2 = TypeTest.create!(renewal_date: Date.today + 1)
expect(TypeTest.find_by_renewal_date(Date.today)).to eq t
end
it "should be able to query by date and type cast" do
t = TypeTest.create!(renewal_date: Date.today.to_s)
expect(TypeTest.find_by_renewal_date(Date.today)).to eq t
expect(TypeTest.find_by_renewal_date(Date.today.to_s)).to eq t
end
it "should be able to query by time" do
now = Time.now
t = TypeTest.create!(name: "t", some_time: now)
_t2 = TypeTest.create!(name: "t2", some_time: now + 1)
expect(TypeTest.find_by_some_time(now)).to eq t
end
it "should be able to query by time and type cast" do
now = Time.now
now_s = now.to_i.to_s
t = TypeTest.create!(some_time: now_s)
expect(TypeTest.find_by_some_time(now)).to eq t
expect(TypeTest.find_by_some_time(now_s)).to eq t
end
it "should be able to query by custom type" do
now = Time.now
t = TypeTest.create!(precision3_time: now)
_t2 = TypeTest.create!(precision3_time: now + 1)
expect(TypeTest.find_by_precision3_time(now)).to eq t
end
it "should be able to query by custom type and type cast" do
now = Time.now
now_s = now.utc.iso8601(3)
t = TypeTest.create!(precision3_time: now_s)
expect(TypeTest.find_by_precision3_time(now)).to eq t
expect(TypeTest.find_by_precision3_time(now_s)).to eq t
end
it "should be able to set attributes with a hash with indifferent access" do
t = TypeTest.new(ActiveSupport::HashWithIndifferentAccess.new(name: "joe", age: 20, size: 1.5, renewal_date: Date.today, subscribed_at: Time.now, active: true))
t.save!
expect(t.name).to eq("joe")
expect(t.age).to eq(20)
expect(t.size).to eq(1.5)
expect(t.renewal_date).to eq(Date.today)
expect(t.subscribed_at).to be_a(Time)
expect(t.active).to eq(true)
end
it "should be able to type cast attributes" do
t = TypeTest.new(name: "joe", age: "20", size: "1.5", renewal_date: Date.today.to_s, subscribed_at: Time.now.to_s, active: "true")
t.save!
expect(t.name).to eq("joe")
expect(t.age).to eq(20)
expect(t.size).to eq(1.5)
expect(t.renewal_date).to eq(Date.today)
expect(t.subscribed_at).to be_a(Time)
expect(t.active).to eq(true)
end
it "should be consistent with active record on failed cast" do
t = TypeTest.new(name: "joe", age: "joe", size: "joe", renewal_date: "joe", subscribed_at: "joe", active: "true")
t.save!
expect(t.age).to eq 0
expect(t.size).to eq 0.0
expect(t.renewal_date).to eq nil
expect(t.subscribed_at).to eq nil
expect(t.active).to eq true
end
it "should be able to query by name" do
t = N1qlTypeTest.create!(name: "joe")
_t2 = N1qlTypeTest.create!(name: "john")
expect(N1qlTypeTest.find_by_name("joe").to_a).to eq [t]
end
it "should be able to query by nil value" do
t = N1qlTypeTest.create!()
_t2 = N1qlTypeTest.create!(name: "john")
expect(N1qlTypeTest.find_by_name(nil).to_a).to eq [t]
end
it "should be able to query by array value" do
t = N1qlTypeTest.create!(name: "laura")
t2 = N1qlTypeTest.create!(name: "joe")
_t3 = N1qlTypeTest.create!(name: "john")
expect(N1qlTypeTest.find_by_name(["laura", "joe"]).to_a).to match_array [t, t2]
end
it "should be able to query by integer" do
t = N1qlTypeTest.create!(age: 20)
t2 = N1qlTypeTest.create!(age: 20)
_t3 = N1qlTypeTest.create!(age: 40)
expect(N1qlTypeTest.find_by_age(20).to_a).to match_array [t, t2]
end
it "should be able to query by integer and type cast" do
t = N1qlTypeTest.create!(age: "20")
expect(N1qlTypeTest.find_by_age(20).to_a).to eq [t]
expect(N1qlTypeTest.find_by_age("20").to_a).to eq [t]
end
it "should be able to query by date" do
t = N1qlTypeTest.create!(renewal_date: Date.today)
_t2 = N1qlTypeTest.create!(renewal_date: Date.today + 1)
expect(N1qlTypeTest.find_by_renewal_date(Date.today).to_a).to eq [t]
end
it "should be able to query by datetime" do
now = Time.now
t = N1qlTypeTest.create!(subscribed_at: now)
_t2 = N1qlTypeTest.create!(subscribed_at: now + 1)
expect(N1qlTypeTest.find_by_subscribed_at(now).to_a).to eq [t]
end
it "should be able to query by timestamp" do
now = Time.now
t = N1qlTypeTest.create!(some_time: now)
_t2 = N1qlTypeTest.create!(some_time: now + 1)
expect(N1qlTypeTest.find_by_some_time(now).to_a).to eq [t]
end
it "should be able to query by custom type" do
now = Time.now
t = N1qlTypeTest.create!(precision3_time: now)
_t2 = N1qlTypeTest.create!(precision3_time: now + 1)
expect(N1qlTypeTest.find_by_precision3_time(now).to_a).to eq [t]
end
it "should be able to query by boolean" do
t = N1qlTypeTest.create!(active: true)
_t2 = N1qlTypeTest.create!(active: false)
expect(N1qlTypeTest.find_by_active(true).to_a).to eq [t]
end
it "should be able to query by float" do
t = N1qlTypeTest.create!(size: 1.5)
_t2 = N1qlTypeTest.create!(size: 2.5)
expect(N1qlTypeTest.find_by_size(1.5).to_a).to eq [t]
end
it "should set datetime with precision" do
time = Time.at(1667499592.5170466123)
Timecop.freeze(time) do
test = TypeTest.create!(precision3_time: 1667499592.5170466123, some_time: 1667499592.5170466123, precision6_time: Time.now)
expect(test.created_at).to eq(time.floor(6))
expect(test.updated_at).to eq(time.floor(6))
expect(test.some_time).to eq(time.floor)
expect(test.precision3_time).to eq(time.floor(3))
expect(test.precision6_time).to eq(time.floor(6))
end
end
end