forked from couchbase/couchbase-ruby-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crud_test.rb
892 lines (644 loc) · 27.8 KB
/
crud_test.rb
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# Copyright 2020-2021 Couchbase, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require_relative "test_helper"
require "couchbase/raw_binary_transcoder"
module Couchbase
class CrudTest < Minitest::Test
include TestUtilities
def setup
connect
@bucket = @cluster.bucket(env.bucket)
@collection = @bucket.default_collection
end
def teardown
disconnect
end
def test_create_documents
doc_id = uniq_id(:foo)
document = {"value" => 42}
@collection.upsert(doc_id, document)
res = @collection.get(doc_id)
assert_equal document, res.content
end
def test_removes_documents
doc_id = uniq_id(:foo)
document = {"value" => 42}
@collection.upsert(doc_id, document)
@collection.remove(doc_id)
assert_raises(Couchbase::Error::DocumentNotFound) do
@collection.get(doc_id)
end
end
def test_reads_from_replica
doc_id = uniq_id(:foo)
document = {"value" => 42}
options =
if env.jenkins?
Options::Upsert(persist_to: :active, replicate_to: :one)
else
Options::Upsert(persist_to: :active)
end
@collection.upsert(doc_id, document, options)
res = @collection.get_any_replica(doc_id)
assert_equal document, res.content
assert_respond_to res, :replica?
res = @collection.get_all_replicas(doc_id)
refute_empty res
res.each do |entry|
assert_equal document, entry.content
assert_respond_to entry, :replica?
end
end
def test_reads_from_replica_does_not_exist
doc_id = uniq_id(:foo)
assert_raises(Couchbase::Error::DocumentIrretrievable) do
@collection.get_any_replica(doc_id)
end
if env.protostellar?
assert_empty @collection.get_all_replicas(doc_id)
else
assert_raises(Couchbase::Error::DocumentNotFound) do
@collection.get_all_replicas(doc_id)
end
end
end
def test_touch_sets_expiration
document = {"value" => 42}
doc_id = uniq_id(:foo)
@collection.upsert(doc_id, document)
@collection.touch(doc_id, 1)
time_travel(2)
assert_raises(Couchbase::Error::DocumentNotFound, "Document \"#{doc_id}\" should be expired") do
@collection.get(doc_id)
end
end
def test_get_can_also_set_expiration
document = {"value" => 42}
doc_id = uniq_id(:foo)
@collection.upsert(doc_id, document)
res = @collection.get_and_touch(doc_id, 1)
assert_equal 42, res.content["value"]
time_travel(2)
assert_raises(Couchbase::Error::DocumentNotFound) do
@collection.get(doc_id)
end
end
def test_exists_allows_to_check_document_existence
doc_id = uniq_id(:foo)
res = @collection.exists(doc_id)
refute_predicate res, :exists?
document = {"value" => 42}
res = @collection.upsert(doc_id, document)
cas = res.cas
res = @collection.exists(doc_id)
assert_predicate res, :exists?
assert_equal cas, res.cas
@collection.remove(doc_id)
res = @collection.exists(doc_id)
refute_predicate res, :exists?
end
def test_get_and_lock_protects_document_from_mutations
doc_id = uniq_id(:foo)
document = {"value" => 42}
@collection.upsert(doc_id, document)
res = @collection.get_and_lock(doc_id, 5)
cas = res.cas
document["value"] += 1
assert_raises(Couchbase::Error::Timeout) do
@collection.upsert(doc_id, document)
end
@collection.unlock(doc_id, cas)
@collection.upsert(doc_id, document)
end
def test_insert_fails_when_document_exists_already
doc_id = uniq_id(:foo)
document = {"value" => 42}
@collection.insert(doc_id, document)
assert_raises(Couchbase::Error::DocumentExists) do
@collection.insert(doc_id, document)
end
end
def test_replace_fails_when_document_does_not_exist_yet
doc_id = uniq_id(:foo)
document = {"value" => 42}
assert_raises(Couchbase::Error::DocumentNotFound) do
@collection.replace(doc_id, document)
end
@collection.upsert(doc_id, document)
document["value"] = 43
@collection.replace(doc_id, document)
res = @collection.get(doc_id)
assert_equal 43, res.content["value"]
end
def test_replace_supports_optimistic_locking
doc_id = uniq_id(:foo)
document = {"value" => 42}
res = @collection.upsert(doc_id, document)
cas = res.cas
options = Collection::ReplaceOptions.new
options.cas = cas + 1 # incorrect CAS
document["value"] = 43
assert_raises(Couchbase::Error::CasMismatch) do
@collection.replace(doc_id, document, options)
end
options.cas = cas # correct CAS
@collection.replace(doc_id, document, options)
res = @collection.get(doc_id)
assert_equal 43, res.content["value"]
end
def test_increments_and_decrements_existing_binary_document
doc_id = uniq_id(:foo)
document = "42"
options = Collection::UpsertOptions.new
options.transcoder = Couchbase::RawBinaryTranscoder.new
@collection.upsert(doc_id, document, options)
res = @collection.binary.increment(doc_id)
assert_equal 43, res.content
options = Collection::GetOptions.new
options.transcoder = Couchbase::RawBinaryTranscoder.new
res = @collection.get(doc_id, options)
assert_equal "43", res.content
res = @collection.binary.decrement(doc_id)
assert_equal 42, res.content
options = Collection::GetOptions.new
options.transcoder = Couchbase::RawBinaryTranscoder.new
res = @collection.get(doc_id, options)
assert_equal "42", res.content
end
def test_fails_to_increment_and_decrement_missing_document
doc_id = uniq_id(:foo)
assert_raises(Couchbase::Error::DocumentNotFound) do
@collection.binary.increment(doc_id)
end
assert_raises(Couchbase::Error::DocumentNotFound) do
@collection.binary.decrement(doc_id)
end
end
def test_increment_and_decrement_can_initialize_document
doc_id = uniq_id(:foo)
options = BinaryCollection::IncrementOptions.new
options.initial = 42
res = @collection.binary.increment(doc_id, options)
assert_equal 42, res.content
res = @collection.binary.increment(doc_id, options)
assert_equal 43, res.content
options = Collection::GetOptions.new
options.transcoder = Couchbase::RawBinaryTranscoder.new
res = @collection.get(doc_id, options)
assert_equal "43", res.content
doc_id = uniq_id(:bar)
options = BinaryCollection::DecrementOptions.new
options.initial = 142
res = @collection.binary.decrement(doc_id, options)
assert_equal 142, res.content
res = @collection.binary.decrement(doc_id, options)
assert_equal 141, res.content
options = Collection::GetOptions.new
options.transcoder = Couchbase::RawBinaryTranscoder.new
res = @collection.get(doc_id, options)
assert_equal "141", res.content
end
def test_increment_and_decrement_can_use_custom_delta
doc_id = uniq_id(:foo)
options = BinaryCollection::IncrementOptions.new
options.initial = 42
options.delta = 50
res = @collection.binary.increment(doc_id, options)
assert_equal 42, res.content
res = @collection.binary.increment(doc_id, options)
assert_equal 92, res.content
options = Collection::GetOptions.new
options.transcoder = Couchbase::RawBinaryTranscoder.new
res = @collection.get(doc_id, options)
assert_equal "92", res.content
doc_id = uniq_id(:bar)
options = BinaryCollection::DecrementOptions.new
options.initial = 142
options.delta = 20
res = @collection.binary.decrement(doc_id, options)
assert_equal 142, res.content
res = @collection.binary.decrement(doc_id, options)
assert_equal 122, res.content
options = Collection::GetOptions.new
options.transcoder = Couchbase::RawBinaryTranscoder.new
res = @collection.get(doc_id, options)
assert_equal "122", res.content
end
def test_insert_bigger_document_and_get_full
doc_id = uniq_id(:project_doc)
person = load_json_test_dataset("projection_doc")
res = @collection.upsert(doc_id, person)
assert_kind_of Integer, res.cas
refute_equal 0, res.cas
cas = res.cas
res = @collection.get(doc_id)
assert_equal cas, res.cas
assert_equal person, res.content
end
def test_error_not_existent
doc_id = uniq_id(:does_not_exist)
assert_raises(Couchbase::Error::DocumentNotFound) do
@collection.get(doc_id)
end
end
def test_error_double_insert
doc_id = uniq_id(:does_not_exist)
@collection.insert(doc_id, "test")
assert_raises(Couchbase::Error::DocumentExists) do
@collection.insert(doc_id, "test")
end
end
def test_error_insert_get_with_expiration
doc_id = uniq_id(:expiry_doc)
doc = load_json_test_dataset("beer_sample_single")
options = Collection::InsertOptions.new
options.expiry = 10
res = @collection.insert(doc_id, doc, options)
refute_equal 0, res.cas
options = Collection::GetOptions.new
options.with_expiry = true
res = @collection.get(doc_id, options)
assert_equal doc, res.content
assert_kind_of Time, res.expiry_time
now = Time.now
assert_operator res.expiry_time, :>=, now, "now: #{now} (#{now.to_i}), expiry_time: #{res.expiry_time} (#{res.expiry_time.to_i})"
end
def test_expiry_option_as_time_instance
doc_id = uniq_id(:expiry_doc)
doc = load_json_test_dataset("beer_sample_single")
today = Time.now.round
tomorrow = today + (24 * 60 * 60) # add one day
res = @collection.insert(doc_id, doc, Options::Insert(expiry: tomorrow))
refute_equal 0, res.cas
res = @collection.get(doc_id, Options::Get(with_expiry: true))
assert_equal doc, res.content
assert_equal tomorrow, res.expiry_time
end
def test_integer_expiry_of_40_days_remains_relative
doc_id = uniq_id(:expiry_doc)
doc = load_json_test_dataset("beer_sample_single")
forty_days_from_today = 40 * 24 * 60 * 60
res = @collection.insert(doc_id, doc, Options::Insert(expiry: forty_days_from_today))
refute_equal 0, res.cas
res = @collection.get(doc_id, Options::Get(with_expiry: true))
assert_equal doc, res.content
assert_kind_of Time, res.expiry_time
# check that expiration time is not greater than 2 seconds to the expected
assert_in_delta (Time.now + forty_days_from_today).to_f, res.expiry_time.to_f, 2
end
def test_insert_get_projection
doc_id = uniq_id(:project_doc)
person = load_json_test_dataset("projection_doc")
res = @collection.upsert(doc_id, person)
refute_equal 0, res.cas
test_cases = [
{name: "string", project: "name",
expected: {"name" => person["name"]}},
{name: "int", project: "age",
expected: {"age" => person["age"]}},
{name: "array", project: "animals",
expected: {"animals" => person["animals"]}},
{name: "array-index1", project: "animals[0]",
expected: {"animals" => [person["animals"][0]]}},
{name: "array-index2", project: "animals[1]",
expected: {"animals" => [person["animals"][1]]}},
{name: "array-index3", project: "animals[2]",
expected: {"animals" => [person["animals"][2]]}},
{name: "full-object-field", project: "attributes",
expected: {"attributes" => person["attributes"]}},
{name: "nested-object-field1", project: "attributes.hair",
expected: {"attributes" => {"hair" => person["attributes"]["hair"]}}},
{name: "nested-object-field2", project: "attributes.dimensions",
expected: {"attributes" => {"dimensions" => person["attributes"]["dimensions"]}}},
{name: "nested-object-field3", project: "attributes.dimensions.height",
expected: {"attributes" => {"dimensions" => {"height" => person["attributes"]["dimensions"]["height"]}}}},
{name: "nested-object-field4", project: "attributes.dimensions.weight",
expected: {"attributes" => {"dimensions" => {"weight" => person["attributes"]["dimensions"]["weight"]}}}},
{name: "nested-object-field5", project: "attributes.hobbies",
expected: {"attributes" => {"hobbies" => person["attributes"]["hobbies"]}}},
{name: "nested-array-object-field1", project: "attributes.hobbies[0].type",
expected: {"attributes" => {"hobbies" => [{"type" => person["attributes"]["hobbies"][0]["type"]}]}}},
{name: "nested-array-object-field2", project: "attributes.hobbies[1].type",
expected: {"attributes" => {"hobbies" => [{"type" => person["attributes"]["hobbies"][1]["type"]}]}}},
{name: "nested-array-object-field3", project: "attributes.hobbies[0].name",
expected: {"attributes" => {"hobbies" => [{"name" => person["attributes"]["hobbies"][0]["name"]}]}}},
{name: "nested-array-object-field4", project: "attributes.hobbies[1].name",
expected: {"attributes" => {"hobbies" => [{"name" => person["attributes"]["hobbies"][1]["name"]}]}}},
{name: "nested-array-object-field5", project: "attributes.hobbies[1].details",
expected: {"attributes" => {"hobbies" => [{"details" => person["attributes"]["hobbies"][1]["details"]}]}}},
{name: "nested-array-object-nested-field1", project: "attributes.hobbies[1].details.location",
expected: {"attributes" => {"hobbies" => [
{"details" => {"location" => person["attributes"]["hobbies"][1]["details"]["location"]}},
]}}},
{name: "nested-array-object-nested-nested-field1", project: "attributes.hobbies[1].details.location.lat",
expected: {"attributes" => {"hobbies" => [
{"details" => {"location" => {"lat" => person["attributes"]["hobbies"][1]["details"]["location"]["lat"]}}},
]}}},
{name: "nested-array-object-nested-nested-field2", project: "attributes.hobbies[1].details.location.long",
expected: {"attributes" => {"hobbies" => [
{"details" => {"location" => {"long" => person["attributes"]["hobbies"][1]["details"]["location"]["long"]}}},
]}}},
]
unless use_caves?
test_cases |=
[
{name: "array-of-arrays-object", project: "tracking.locations[1][1].lat",
expected: {"tracking" => {"locations" => [[{"lat" => person["tracking"]["locations"][1][1]["lat"]}]]}}},
{name: "array-of-arrays-native", project: "tracking.raw[1][1]",
expected: {"tracking" => {"raw" => [[person["tracking"]["raw"][1][1]]]}}},
]
end
test_cases.each do |test_case|
options = Collection::GetOptions.new
options.project(test_case[:project])
res = @collection.get(doc_id, options)
assert_equal(test_case[:expected], res.content,
"unexpected content for case #{test_case[:name]} with projections #{test_case[:project].inspect}")
end
end
def test_projection_few_paths
doc_id = uniq_id(:project_doc)
person = load_json_test_dataset("projection_doc")
res = @collection.upsert(doc_id, person)
refute_equal 0, res.cas
test_cases = [
{name: "simple", project: %w[name age animals],
expected: {"name" => person["name"], "age" => person["age"], "animals" => person["animals"]}},
]
test_cases << if env.protostellar?
{name: "array entries", project: %w[animals[1] animals[0]],
expected: {"animals" => [person["animals"][0], person["animals"][1]]}}
else
# In protostellar the order in the original document is preserved by design
{name: "array entries", project: %w[animals[1] animals[0]],
expected: {"animals" => [person["animals"][1], person["animals"][0]]}}
end
test_cases.each do |test_case|
options = Collection::GetOptions.new
options.project(*test_case[:project])
res = @collection.get(doc_id, options)
assert_equal(test_case[:expected], res.content,
"unexpected content for case #{test_case[:name]} with projections #{test_case[:project].inspect}")
end
end
def test_projection_preserve_array_indexes
skip("#{name}: The #{Couchbase::Protostellar::NAME} protocol does not support the preserve_array_indexes option") if env.protostellar?
doc_id = uniq_id(:project_doc)
person = load_json_test_dataset("projection_doc")
res = @collection.upsert(doc_id, person)
refute_equal 0, res.cas
test_cases = [
{name: "array entries", project: %w[animals[1] animals[0]],
expected: {"animals" => [person["animals"][0], person["animals"][1]]}},
{name: "with inner array", project: %w[attributes.hobbies[1].details.location.lat],
expected: {"attributes" => {"hobbies" => [
nil,
{
"details" => {
"location" => {
"lat" => person["attributes"]["hobbies"][1]["details"]["location"]["lat"],
},
},
},
]}}},
]
test_cases.each do |test_case|
options = Collection::GetOptions.new
options.project(*test_case[:project])
options.preserve_array_indexes = true
res = @collection.get(doc_id, options)
assert_equal(test_case[:expected], res.content,
"unexpected content for case #{test_case[:name]} with projections #{test_case[:project].inspect}")
end
end
def test_insert_get_projection_18_fields
doc_id = uniq_id(:project_too_many_fields)
doc = (1..18).each_with_object({}) do |n, obj|
obj["field#{n}"] = n
end
res = @collection.insert(doc_id, doc)
refute_equal 0, res.cas
options = Collection::GetOptions.new
options.project((1..17).map { |n| "field#{n}" })
res = @collection.get(doc_id, options)
expected = (1..17).each_with_object({}) do |n, obj|
obj["field#{n}"] = n
end
assert_equal(expected, res.content, "expected result do not include field18")
end
def test_upsert_get_projection_16_fields_and_expiry
doc_id = uniq_id(:project_too_many_fields)
doc = (1..18).each_with_object({}) do |n, obj|
obj["field#{n}"] = n
end
options = Collection::UpsertOptions.new
options.expiry = 60
res = @collection.upsert(doc_id, doc, options)
refute_equal 0, res.cas
options = Collection::GetOptions.new
options.project((1..16).map { |n| "field#{n}" })
options.with_expiry = true
res = @collection.get(doc_id, options)
expected = (1..16).each_with_object({}) do |n, obj|
obj["field#{n}"] = n
end
assert_equal(expected, res.content, "expected result do not include field17, field18")
assert_kind_of(Time, res.expiry_time)
assert_operator(res.expiry_time, :>, Time.now)
end
def test_upsert_get_projection_missing_path
doc_id = uniq_id(:project_doc)
person = load_json_test_dataset("projection_doc")
res = @collection.upsert(doc_id, person)
refute_equal 0, res.cas
options = Collection::GetOptions.new
options.project("this_field_does_not_exist")
res = @collection.get(doc_id, options)
assert_empty(res.content)
options = Collection::GetOptions.new
options.project("this_field_does_not_exist", "age", "attributes.hair")
res = @collection.get(doc_id, options)
assert_equal({"age" => 26, "attributes" => {"hair" => "brown"}}, res.content)
end
def __wait_for_collections_manifest(uid)
hits = 5 # make sure that the manifest has distributed well enough
while hits.positive?
backend = @cluster.instance_variable_get(:@backend)
manifest = backend.collections_manifest_get(@bucket.name, 10_000)
if manifest[:uid] < uid
time_travel(0.1)
next
end
hits -= 1
end
Couchbase::JsonTranscoder.new
end
# Following test tests that if a collection is deleted and recreated midway through a set of operations then the
# operations will still succeed due to the cid being refreshed under the hood.
def test_collection_retry
skip("#{name}: __wait_for_collections_manifest cannot be used with #{Couchbase::Protostellar::NAME}") if env.protostellar?
skip("#{name}: The server does not support collections (#{env.server_version})") unless env.server_version.supports_collections?
doc_id = uniq_id(:test_collection_retry)
doc = load_json_test_dataset("beer_sample_single")
collection_name = uniq_id(:collection).delete(".")[0, 30]
manager = @bucket.collections
spec = Management::CollectionSpec.new
spec.scope_name = "_default"
spec.name = collection_name
ns_uid = manager.create_collection(spec)
__wait_for_collections_manifest(ns_uid)
collection = @bucket.collection(collection_name)
# make sure we've connected to the collection
options = Collection::UpsertOptions.new
options.timeout = 15_000
res = collection.upsert(doc_id, doc, options)
refute_equal 0, res.cas
# the following delete and create will recreate a collection with the same name but a different collection ID.
ns_uid = manager.drop_collection(spec)
__wait_for_collections_manifest(ns_uid)
ns_uid = manager.create_collection(spec)
__wait_for_collections_manifest(ns_uid)
# we've wiped the collection so we need to recreate this doc
# we know that this operation can take a bit longer than normal as collections take time to come online
options = Collection::UpsertOptions.new
options.timeout = 15_000
res = collection.upsert(doc_id, doc, options)
refute_equal 0, res.cas
res = collection.get(doc_id)
assert_equal doc, res.content
end
def test_append
doc_id = uniq_id(:append)
res = @collection.upsert(doc_id, "foo", Options::Upsert(transcoder: nil))
refute_equal 0, res.cas
res = @collection.binary.append(doc_id, "bar")
refute_equal 0, res.cas
res = @collection.get(doc_id, Options::Get(transcoder: nil))
assert_equal "foobar", res.content
end
def test_prepend
doc_id = uniq_id(:append)
res = @collection.upsert(doc_id, "foo", Options::Upsert(transcoder: nil))
refute_equal 0, res.cas
res = @collection.binary.prepend(doc_id, "bar")
refute_equal 0, res.cas
res = @collection.get(doc_id, Options::Get(transcoder: nil))
assert_equal "barfoo", res.content
end
def test_append_with_durability
doc_id = uniq_id(:append)
res = @collection.upsert(doc_id, "foo", Options::Upsert(transcoder: nil))
refute_equal 0, res.cas
res = @collection.binary.append(doc_id, "bar", Options::Append(durability_level: :majority))
refute_equal 0, res.cas
res = @collection.get(doc_id, Options::Get(transcoder: nil))
assert_equal "foobar", res.content
end
def test_prepend_with_durability
doc_id = uniq_id(:append)
res = @collection.upsert(doc_id, "foo", Options::Upsert(transcoder: nil))
refute_equal 0, res.cas
res = @collection.binary.prepend(doc_id, "bar", Options::Prepend(durability_level: :majority))
refute_equal 0, res.cas
res = @collection.get(doc_id, Options::Get(transcoder: nil))
assert_equal "barfoo", res.content
end
def test_multi_ops
skip("#{name}: The #{Couchbase::Protostellar::NAME} protocol does not support multi ops") if env.protostellar?
doc_id1 = uniq_id(:foo)
doc_id2 = uniq_id(:bar)
res = @collection.upsert_multi([
[doc_id1, {"foo" => 32}],
[doc_id2, {"bar" => "bar42"}],
])
assert_kind_of Array, res
assert_equal 2, res.size
assert_nil res[0].error
assert_nil res[1].error
assert_equal res[0].id, doc_id1
assert_equal res[1].id, doc_id2
cas2 = res[1].cas
res = @collection.get_multi([doc_id1, doc_id2, uniq_id(:does_not_exist)])
assert_kind_of Array, res
assert_equal 3, res.size
assert_nil res[0].error
assert_nil res[1].error
assert_equal res[0].id, doc_id1
assert_equal res[1].id, doc_id2
assert_kind_of Error::DocumentNotFound, res[2].error
assert_equal({"foo" => 32}, res[0].content)
assert_equal({"bar" => "bar42"}, res[1].content)
assert_equal(cas2, res[1].cas)
res = @collection.remove_multi([doc_id1, [doc_id2, cas2]])
assert_kind_of Array, res
assert_equal 2, res.size
assert_nil res[0].error
assert_nil res[1].error
assert_equal res[0].id, doc_id1
assert_equal res[1].id, doc_id2
res = @collection.get_multi([doc_id1, doc_id2])
assert_kind_of Array, res
assert_equal 2, res.size
assert_kind_of Error::DocumentNotFound, res[0].error
assert_kind_of Error::DocumentNotFound, res[1].error
assert_equal res[0].id, doc_id1
assert_equal res[1].id, doc_id2
end
def test_massive_multi_ops
skip("#{name}: The #{Couchbase::Protostellar::NAME} protocol does not support multi ops") if env.protostellar?
# github often chokes on such a big batches (and raises Error::Timeout)
num_keys = ENV.fetch("TEST_MASSIVE_MULTI_OPS_NUM_KEYS", nil) || 1_000
keys = (0..num_keys).map { |idx| uniq_id("key_#{idx}") }
res = @collection.upsert_multi(keys.map { |k| [k, {"value" => k}] })
assert_kind_of Array, res
assert_equal keys.size, res.size
res.each_with_index do |r, i|
assert_nil r.error, "upsert for #{keys[i].inspect} must not fail"
end
res = @collection.get_multi(keys)
assert_kind_of Array, res
assert_equal keys.size, res.size
res.each_with_index do |r, i|
assert_nil r.error, "upsert for #{keys[i].inspect} must not fail"
assert_equal keys[i], r.content["value"]
end
end
def test_preserve_expiry
skip("#{name}: CAVES does not support preserve expiry") if use_caves?
unless env.server_version.supports_preserve_expiry?
skip("#{name}: The server does not support preserve expiry (#{env.server_version})")
end
doc_id = uniq_id(:foo)
res = @collection.upsert(doc_id, {answer: 42}, Options::Upsert(expiry: 1))
old_cas = res.cas
refute_equal 0, old_cas
res = @collection.get(doc_id, Options::Get(with_expiry: true))
old_expiry = res.expiry_time
res = @collection.upsert(doc_id, {answer: 43}, Options::Upsert(expiry: 100, preserve_expiry: true))
refute_equal old_cas, res.cas
res = @collection.get(doc_id, Options::Get(with_expiry: true))
assert_equal old_expiry, res.expiry_time
time_travel(2)
assert_raises(Couchbase::Error::DocumentNotFound) do
@collection.get(doc_id)
end
end
def test_legacy_durability
doc_id = uniq_id(:foo)
res = @collection.upsert(doc_id, {answer: 42}, Options::Upsert(persist_to: :active))
old_cas = res.cas
refute_equal 0, old_cas
end
end
end