-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
persistence_spec.rb
2890 lines (2298 loc) · 82.3 KB
/
persistence_spec.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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require 'spec_helper'
describe Dynamoid::Persistence do
let(:address) { Address.new }
context 'without AWS keys' do
unless ENV['ACCESS_KEY'] && ENV['SECRET_KEY']
before do
Dynamoid.adapter.delete_table(Address.table_name) if Dynamoid.adapter.list_tables.include?(Address.table_name)
end
it 'creates a table' do
Address.create_table(table_name: Address.table_name)
expect(Dynamoid.adapter.list_tables).to include 'dynamoid_tests_addresses'
end
it 'checks if a table already exists' do
Address.create_table(table_name: Address.table_name)
expect(Address.table_exists?(Address.table_name)).to be_truthy
expect(Address.table_exists?('crazytable')).to be_falsey
end
end
end
describe '.create_table' do
let(:user_class) do
Class.new do
attr_accessor :name
def initialize(name)
self.name = name
end
def dynamoid_dump
name
end
def eql?(other)
name == other.name
end
def self.dynamoid_load(string)
new(string.to_s)
end
end
end
let(:user_class_with_type) do
Class.new do
attr_accessor :age
def initialize(age)
self.age = age
end
def dynamoid_dump
age
end
def eql?(other)
age == other.age
end
def self.dynamoid_load(string)
new(string.to_i)
end
def self.dynamoid_field_type
:number
end
end
end
describe 'partition key attribute type' do
it 'maps :string to String' do
klass = new_class(partition_key: { name: :id, type: :string })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('S')
end
it 'maps :integer to Number' do
klass = new_class(partition_key: { name: :id, type: :integer })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('N')
end
it 'maps :number to Number' do
klass = new_class(partition_key: { name: :id, type: :number })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('N')
end
describe ':datetime' do
it 'maps :datetime to Number' do
klass = new_class(partition_key: { name: :id, type: :datetime })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('N')
end
it 'maps :datetime to String if field option :store_as_string is true' do
klass = new_class(partition_key: { name: :id, type: :datetime, options: { store_as_string: true } })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('S')
end
it 'maps :datetime to Number if field option :store_as_string is false' do
klass = new_class(partition_key: { name: :id, type: :datetime, options: { store_as_string: false } })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('N')
end
context 'field option :store_as_string is nil' do
it 'maps :datetime to String if :store_datetime_as_string is true', config: { store_datetime_as_string: true } do
klass = new_class(partition_key: { name: :id, type: :datetime, options: { store_as_string: nil } })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('S')
end
it 'maps :datetime to Number if :store_datetime_as_string is false', config: { store_datetime_as_string: false } do
klass = new_class(partition_key: { name: :id, type: :datetime, options: { store_as_string: nil } })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('N')
end
end
end
describe ':date' do
it 'maps :date to Number' do
klass = new_class(partition_key: { name: :id, type: :date })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('N')
end
it 'maps :date to String if field option :store_as_string is true' do
klass = new_class(partition_key: { name: :id, type: :date, options: { store_as_string: true } })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('S')
end
it 'maps :date to Number if field option :store_as_string is false' do
klass = new_class(partition_key: { name: :id, type: :date, options: { store_as_string: false } })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('N')
end
context 'field option :store_as_string is nil' do
it 'maps :date to String if :store_date_as_string is true', config: { store_date_as_string: true } do
klass = new_class(partition_key: { name: :id, type: :date, options: { store_as_string: nil } })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('S')
end
it 'maps :date to Number if :store_date_as_string is false', config: { store_date_as_string: false } do
klass = new_class(partition_key: { name: :id, type: :date, options: { store_as_string: nil } })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('N')
end
end
end
it 'maps :serialized to String' do
klass = new_class(partition_key: { name: :id, type: :serialized })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('S')
end
describe 'custom type' do
it 'maps custom type to String by default' do
klass = new_class(partition_key: { name: :id, type: user_class })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('S')
end
it 'uses specified type if .dynamoid_field_type method declared' do
klass = new_class(partition_key: { name: :id, type: user_class_with_type })
klass.create_table
expect(raw_attribute_types(klass.table_name)['id']).to eql('N')
end
end
it 'does not support :array' do
klass = new_class(partition_key: { name: :id, type: :array })
expect { klass.create_table }.to raise_error(
Dynamoid::Errors::UnsupportedKeyType, 'array cannot be used as a type of table key attribute'
)
end
it 'does not support :set' do
klass = new_class(partition_key: { name: :id, type: :set })
expect { klass.create_table }.to raise_error(
Dynamoid::Errors::UnsupportedKeyType, 'set cannot be used as a type of table key attribute'
)
end
it 'does not support :raw' do
klass = new_class(partition_key: { name: :id, type: :raw })
expect { klass.create_table }.to raise_error(
Dynamoid::Errors::UnsupportedKeyType, 'raw cannot be used as a type of table key attribute'
)
end
it 'does not support :boolean' do
klass = new_class(partition_key: { name: :id, type: :boolean })
expect { klass.create_table }.to raise_error(
Dynamoid::Errors::UnsupportedKeyType, 'boolean cannot be used as a type of table key attribute'
)
end
end
describe 'sort key attribute type' do
it 'maps :string to String' do
klass = new_class do
range :prop, :string
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('S')
end
it 'maps :integer to Number' do
klass = new_class do
range :prop, :integer
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('N')
end
it 'maps :number to Number' do
klass = new_class do
range :prop, :number
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('N')
end
describe ':datetime' do
it 'maps :datetime to Number' do
klass = new_class do
range :prop, :datetime
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('N')
end
it 'maps :datetime to String if field option :store_as_string is true' do
klass = new_class do
range :prop, :datetime, store_as_string: true
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('S')
end
it 'maps :datetime to Number if field option :store_as_string is false' do
klass = new_class do
range :prop, :datetime, store_as_string: false
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('N')
end
context 'field option :store_as_string is nil' do
it 'maps :datetime to String if :store_datetime_as_string is true', config: { store_datetime_as_string: true } do
klass = new_class do
range :prop, :datetime, store_as_string: nil
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('S')
end
it 'maps :datetime to Number if :store_datetime_as_string is false', config: { store_datetime_as_string: false } do
klass = new_class do
range :prop, :datetime, store_as_string: nil
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('N')
end
end
end
describe ':date' do
it 'maps :date to Number' do
klass = new_class do
range :prop, :date
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('N')
end
it 'maps :date to String if field option :store_as_string is true' do
klass = new_class do
range :prop, :date, store_as_string: true
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('S')
end
it 'maps :date to Number if field option :store_as_string is false' do
klass = new_class do
range :prop, :date, store_as_string: false
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('N')
end
context 'field option :store_as_string is nil' do
it 'maps :date to String if :store_date_as_string is true', config: { store_date_as_string: true } do
klass = new_class do
range :prop, :date, store_as_string: nil
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('S')
end
it 'maps :date to Number if :store_date_as_string is false', config: { store_date_as_string: false } do
klass = new_class do
range :prop, :date, store_as_string: nil
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('N')
end
end
end
it 'maps :serialized to String' do
klass = new_class do
range :prop, :serialized
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('S')
end
describe 'custom type' do
it 'maps custom type to String by default' do
klass = new_class(sort_key_type: user_class) do |options|
range :prop, options[:sort_key_type]
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('S')
end
it 'uses specified type if .dynamoid_field_type method declared' do
klass = new_class(sort_key_type: user_class_with_type) do |options|
range :prop, options[:sort_key_type]
end
klass.create_table
expect(raw_attribute_types(klass.table_name)['prop']).to eql('N')
end
end
it 'does not support :array' do
klass = new_class do
range :prop, :array
end
expect { klass.create_table }.to raise_error(
Dynamoid::Errors::UnsupportedKeyType, 'array cannot be used as a type of table key attribute'
)
end
it 'does not support :set' do
klass = new_class do
range :prop, :set
end
expect { klass.create_table }.to raise_error(
Dynamoid::Errors::UnsupportedKeyType, 'set cannot be used as a type of table key attribute'
)
end
it 'does not support :raw' do
klass = new_class do
range :prop, :raw
end
expect { klass.create_table }.to raise_error(
Dynamoid::Errors::UnsupportedKeyType, 'raw cannot be used as a type of table key attribute'
)
end
it 'does not support :boolean' do
klass = new_class do
range :prop, :boolean
end
expect { klass.create_table }.to raise_error(
Dynamoid::Errors::UnsupportedKeyType, 'boolean cannot be used as a type of table key attribute'
)
end
end
describe 'expires (Time To Live)' do
let(:class_with_expiration) do
new_class do
table expires: { field: :ttl, after: 60 }
field :ttl, :integer
end
end
it 'sets up TTL for table' do
expect(Dynamoid.adapter).to receive(:update_time_to_live)
.with(table_name: class_with_expiration.table_name, attribute: :ttl)
.and_call_original
class_with_expiration.create_table
end
end
describe 'capacity mode' do
# when capacity mode is PROVISIONED DynamoDB returns billing_mode_summary=nil
let(:table_description) { Dynamoid.adapter.adapter.send(:describe_table, model.table_name) }
let(:billing_mode) { table_description.schema.billing_mode_summary&.billing_mode }
before do
model.create_table
end
context 'when global config option capacity_mode=on_demand', config: { capacity_mode: :on_demand } do
context 'when capacity_mode=provisioned in table' do
let(:model) do
new_class do
table capacity_mode: :provisioned
end
end
it 'creates table with provisioned capacity mode' do
expect(billing_mode).to eq nil # it means 'PROVISIONED'
end
end
context 'when capacity_mode not set in table' do
let(:model) do
new_class do
table capacity_mode: nil
end
end
it 'creates table with on-demand capacity mode' do
expect(billing_mode).to eq 'PAY_PER_REQUEST'
end
end
end
context 'when global config option capacity_mode=provisioned', config: { capacity_mode: :provisioned } do
context 'when capacity_mode=on_demand in table' do
let(:model) do
new_class do
table capacity_mode: :on_demand
end
end
it 'creates table with on-demand capacity mode' do
expect(billing_mode).to eq 'PAY_PER_REQUEST'
end
end
context 'when capacity_mode not set in table' do
let(:model) do
new_class do
table capacity_mode: nil
end
end
it 'creates table with provisioned capacity mode' do
expect(billing_mode).to eq nil # it means 'PROVISIONED'
end
end
end
context 'when global config option capacity_mode is not set', config: { capacity_mode: nil } do
let(:model) do
new_class do
table capacity_mode: nil
end
end
it 'creates table with provisioned capacity mode' do
expect(billing_mode).to eq nil # it means 'PROVISIONED'
end
end
end
end
describe 'delete_table' do
it 'deletes the table' do
Address.create_table
Address.delete_table
tables = Dynamoid.adapter.list_tables
expect(tables.include?(Address.table_name)).to be_falsey
end
end
describe 'record deletion' do
let(:klass) do
new_class do
field :city
before_destroy do |_i|
# Halting the callback chain in active record changed with Rails >= 5.0.0.beta1
# We now have to throw :abort to halt the callback chain
# See: https://github.com/rails/rails/commit/bb78af73ab7e86fd9662e8810e346b082a1ae193
if ActiveModel::VERSION::MAJOR < 5
false
else
throw :abort
end
end
end
end
describe 'destroy' do
it 'deletes an item completely' do
@user = User.create(name: 'Josh')
@user.destroy
expect(Dynamoid.adapter.read('dynamoid_tests_users', @user.id)).to be_nil
end
it 'returns false when destroy fails (due to callback)' do
a = klass.create!
expect(a.destroy).to eql false
expect(klass.first.id).to eql a.id
end
end
describe 'destroy!' do
it 'deletes the item' do
address.save!
address.destroy!
expect(Address.count).to eql 0
end
it 'raises exception when destroy fails (due to callback)' do
a = klass.create!
expect { a.destroy! }.to raise_error(Dynamoid::Errors::RecordNotDestroyed)
end
end
end
it 'has a table name' do
expect(Address.table_name).to eq 'dynamoid_tests_addresses'
end
context 'with namespace is empty' do
def reload_address
Object.send(:remove_const, 'Address')
load 'app/models/address.rb'
end
namespace = Dynamoid::Config.namespace
before do
reload_address
Dynamoid.configure do |config|
config.namespace = ''
end
end
after do
reload_address
Dynamoid.configure do |config|
config.namespace = namespace
end
end
it 'does not add a namespace prefix to table names' do
table_name = Address.table_name
expect(Dynamoid::Config.namespace).to be_empty
expect(table_name).to eq 'addresses'
end
end
it 'deletes an item completely' do
@user = User.create(name: 'Josh')
@user.destroy
expect(Dynamoid.adapter.read('dynamoid_tests_users', @user.id)).to be_nil
end
describe '.create' do
let(:klass) do
new_class do
field :city
end
end
it 'creates a new document' do
address = klass.create(city: 'Chicago')
expect(address.new_record).to eql false
expect(address.id).to be_present
address_saved = klass.find(address.id)
expect(address_saved.city).to eq('Chicago')
end
it 'creates multiple documents' do
addresses = klass.create([{ city: 'Chicago' }, { city: 'New York' }])
expect(addresses.size).to eq 2
expect(addresses).to be_all(&:persisted?)
expect(addresses[0].city).to eq 'Chicago'
expect(addresses[1].city).to eq 'New York'
end
context 'when block specified' do
it 'calls a block and passes a model as argument' do
object = klass.create(city: 'a') do |obj|
obj.city = 'b'
end
expect(object.city).to eq('b')
end
it 'calls a block and passes each model as argument if there are multiple models' do
objects = klass.create([{ city: 'a' }, { city: 'b' }]) do |obj|
obj.city = obj.city * 2
end
expect(objects[0].city).to eq('aa')
expect(objects[1].city).to eq('bb')
end
end
describe 'validation' do
let(:klass_with_validation) do
new_class do
field :name
validates :name, length: { minimum: 4 }
end
end
it 'does not save invalid model' do
obj = klass_with_validation.create(name: 'Theodor')
expect(obj).to be_persisted
obj = klass_with_validation.create(name: 'Mo')
expect(obj).not_to be_persisted
end
it 'saves valid models even if there are invalid' do
obj1, obj2 = klass_with_validation.create([{ name: 'Theodor' }, { name: 'Mo' }])
expect(obj1).to be_persisted
expect(obj2).not_to be_persisted
end
end
it 'works with a HashWithIndifferentAccess argument' do
attrs = ActiveSupport::HashWithIndifferentAccess.new(city: 'Atlanta')
obj = klass.create(attrs)
expect(obj).to be_persisted
expect(obj.city).to eq 'Atlanta'
end
it 'creates table if it does not exist' do
expect {
klass.create(city: 'Chicago')
}.to change {
tables_created.include?(klass.table_name)
}.from(false).to(true)
end
describe 'callbacks' do
it 'runs before_create callback' do
klass_with_callback = new_class do
field :name
before_create { print 'run before_create' }
end
expect do
klass_with_callback.create(name: 'Alex')
end.to output('run before_create').to_stdout
end
it 'runs after_create callback' do
klass_with_callback = new_class do
field :name
after_create { print 'run after_create' }
end
expect do
klass_with_callback.create(name: 'Alex')
end.to output('run after_create').to_stdout
end
it 'runs before_save callback' do
klass_with_callback = new_class do
field :name
before_save { print 'run before_save' }
end
expect do
klass_with_callback.create(name: 'Alex')
end.to output('run before_save').to_stdout
end
it 'runs after_save callbacks' do
klass_with_callback = new_class do
field :name
after_save { print 'run after_save' }
end
expect do
klass_with_callback.create(name: 'Alex')
end.to output('run after_save').to_stdout
end
it 'runs callback specified with method name' do
klass_with_callback = new_class do
field :name
before_create :log_message
def log_message
print 'run before_create'
end
end
expect do
klass_with_callback.create(name: 'Alex')
end.to output('run before_create').to_stdout
end
end
context 'not unique primary key' do
context 'composite key' do
let(:klass_with_composite_key) do
new_class do
range :name
end
end
it 'raises RecordNotUnique error' do
klass_with_composite_key.create(id: '10', name: 'aaa')
expect {
klass_with_composite_key.create(id: '10', name: 'aaa')
}.to raise_error(Dynamoid::Errors::RecordNotUnique)
end
end
context 'simple key' do
let(:klass_with_simple_key) do
new_class
end
it 'raises RecordNotUnique error' do
klass_with_simple_key.create(id: '10')
expect {
klass_with_simple_key.create(id: '10')
}.to raise_error(Dynamoid::Errors::RecordNotUnique)
end
end
end
describe 'timestamps' do
let(:klass) do
new_class
end
it 'sets created_at and updated_at if Config.timestamps=true', config: { timestamps: true } do
travel 1.hour do
time_now = Time.now
obj = klass.create
expect(obj.created_at.to_i).to eql(time_now.to_i)
expect(obj.updated_at.to_i).to eql(time_now.to_i)
end
end
it 'uses provided values of created_at and updated_at if Config.timestamps=true', config: { timestamps: true } do
travel 1.hour do
created_at = updated_at = Time.now
obj = klass.create(created_at: created_at, updated_at: updated_at)
expect(obj.created_at.to_i).to eql(created_at.to_i)
expect(obj.updated_at.to_i).to eql(updated_at.to_i)
end
end
it 'does not raise error if Config.timestamps=false', config: { timestamps: false } do
expect { klass.create }.not_to raise_error
end
end
end
describe '.create!' do
let(:klass) do
new_class do
field :city
end
end
context 'when block specified' do
it 'calls a block and passes a model as argument' do
object = klass.create!(city: 'a') do |obj|
obj.city = 'b'
end
expect(object.city).to eq('b')
end
it 'calls a block and passes each model as argument if there are multiple models' do
objects = klass.create!([{ city: 'a' }, { city: 'b' }]) do |obj|
obj.city = obj.city * 2
end
expect(objects[0].city).to eq('aa')
expect(objects[1].city).to eq('bb')
end
end
context 'validation' do
let(:klass_with_validation) do
new_class do
field :city
validates :city, presence: true
end
end
it 'raises DocumentNotValid error when saves invalid model' do
expect do
klass_with_validation.create!(city: nil)
end.to raise_error(Dynamoid::Errors::DocumentNotValid)
end
it 'raises DocumentNotValid error when saves multiple models and some of them are invalid' do
expect do
klass_with_validation.create!([{ city: 'Chicago' }, { city: nil }])
end.to raise_error(Dynamoid::Errors::DocumentNotValid)
end
it 'saves some valid models before raising error because of invalid model' do
klass_with_validation.create_table
expect do
begin
klass_with_validation.create!([{ city: 'Chicago' }, { city: nil }, { city: 'London' }])
rescue StandardError
nil
end
end.to change { klass_with_validation.count }.by(1)
obj = klass_with_validation.last
expect(obj.city).to eq 'Chicago'
end
end
end
describe '.update!' do
let(:document_class) do
new_class do
field :name
validates :name, presence: true, length: { minimum: 5 }
end
end
it 'loads and saves document' do
d = document_class.create(name: 'Document#1')
expect do
document_class.update!(d.id, name: '[Updated]')
end.to change { d.reload.name }.from('Document#1').to('[Updated]')
end
it 'returns updated document' do
d = document_class.create(name: 'Document#1')
d2 = document_class.update!(d.id, name: '[Updated]')
expect(d2).to be_a(document_class)
expect(d2.name).to eq '[Updated]'
end
it 'does not save invalid document' do
d = document_class.create(name: 'Document#1')
d2 = nil
expect do
d2 = document_class.update!(d.id, name: '[Up')
end.to raise_error(Dynamoid::Errors::DocumentNotValid)
expect(d2).to be_nil
end
it 'accepts range key value if document class declares it' do
klass = new_class do
field :name
range :status
end
d = klass.create(status: 'old', name: 'Document#1')
expect do
klass.update!(d.id, 'old', name: '[Updated]')
end.to change { d.reload.name }.to('[Updated]')
end
it 'dumps range key value to proper format' do
klass = new_class do
field :name
range :activated_on, :date
field :another_date, :datetime
end
d = klass.create(activated_on: '2018-01-14'.to_date, name: 'Document#1')
expect do
klass.update!(d.id, '2018-01-14'.to_date, name: '[Updated]')
end.to change { d.reload.name }.to('[Updated]')
end
it 'raises an UnknownAttribute error when adding an attribute that is not on the model' do
klass = new_class do
field :name
end
obj = klass.create(name: 'Alex')
expect {
klass.update!(obj.id, age: 26)
}.to raise_error Dynamoid::Errors::UnknownAttribute
end
describe 'timestamps' do
it 'sets updated_at if Config.timestamps=true', config: { timestamps: true } do
d = document_class.create(name: 'Document#1')
travel 1.hour do
time_now = Time.now
expect {
document_class.update!(d.id, name: '[Updated]')
}.to change { d.reload.updated_at.to_i }.to(time_now.to_i)
end
end
it 'uses provided value of updated_at if Config.timestamps=true', config: { timestamps: true } do
d = document_class.create(name: 'Document#1')
travel 1.hour do
updated_at = Time.now + 1.hour
expect {
document_class.update!(d.id, name: '[Updated]', updated_at: updated_at)
}.to change { d.reload.updated_at.to_i }.to(updated_at.to_i)
end
end
it 'does not raise error if Config.timestamps=false', config: { timestamps: false } do
doc = document_class.create(name: 'Document#1')
expect do
document_class.update!(doc.id, name: '[Updated]')
end.not_to raise_error
end
end
describe 'type casting' do
it 'uses type casted value of sort key to call UpdateItem' do
document_class_with_range = new_class do
range :count, :integer
field :title
end
obj = document_class_with_range.create(title: 'Old', count: '100')
document_class_with_range.update!(obj.id, '100', title: 'New')
expect(obj.reload.title).to eq 'New'
end
it 'type casts attributes' do
klass = new_class do
field :count, :integer
end
obj = klass.create(count: 100)
obj2 = klass.update!(obj.id, count: '101')
expect(obj2.attributes[:count]).to eql(101)
expect(raw_attributes(obj2)[:count]).to eql(101)
end
end
end