@@ -52,7 +52,7 @@ def around_save_callback
5252 end
5353 end
5454
55- it 'persists changes' do
55+ it 'persists changes in already presisted model ' do
5656 obj = klass . create! ( name : 'Alex' )
5757
5858 described_class . execute do |txn |
@@ -316,7 +316,7 @@ def around_save_callback
316316 expect ( klass . find ( obj_deleted . id ) . name ) . to eql 'Alex [Updated]'
317317 end
318318
319- it 'is marked as changed when the transaction rolled back' do
319+ it 'keeps a model as changed when the transaction rolled back' do
320320 obj = klass . create! ( name : 'Alex' )
321321
322322 expect {
@@ -545,6 +545,10 @@ def around_update_callback
545545end
546546
547547describe Dynamoid ::TransactionWrite , '#update_attributes!' do
548+ # The only difference in specs structure between
549+ # #update_attributes and #update_attributes! is missing a section
550+ # for callbacks here
551+
548552 let ( :klass ) do
549553 new_class do
550554 field :name
@@ -603,6 +607,98 @@ def around_update_callback
603607 end
604608 end
605609
610+ describe 'primary key validation' do
611+ context 'simple primary key' do
612+ it 'requires partition key to be specified' do
613+ obj = klass . create! ( name : 'Alex' )
614+ obj . id = nil
615+
616+ expect {
617+ described_class . execute do |txn |
618+ txn . update_attributes! obj , name : 'Alex [Updated]'
619+ end
620+ } . to raise_exception ( Dynamoid ::Errors ::MissingHashKey )
621+ end
622+ end
623+
624+ context 'composite key' do
625+ it 'requires partition key to be specified' do
626+ obj = klass_with_composite_key . create! ( name : 'Alex' , age : 3 )
627+ obj . id = nil
628+
629+ expect {
630+ described_class . execute do |txn |
631+ txn . update_attributes! obj , name : 'Alex [Updated]'
632+ end
633+ } . to raise_exception ( Dynamoid ::Errors ::MissingHashKey )
634+ end
635+
636+ it 'requires sort key to be specified' do
637+ obj = klass_with_composite_key . create! ( name : 'Alex' , age : 3 )
638+ obj . age = nil
639+
640+ expect {
641+ described_class . execute do |txn |
642+ txn . update_attributes! obj , name : 'Alex [Updated]'
643+ end
644+ } . to raise_exception ( Dynamoid ::Errors ::MissingRangeKey )
645+ end
646+ end
647+ end
648+
649+ describe 'timestamps' do
650+ it 'sets updated_at if Config.timestamps=true' , config : { timestamps : true } do
651+ obj = klass . create!
652+
653+ travel 1 . hour do
654+ time_now = Time . now
655+
656+ described_class . execute do |txn |
657+ txn . update_attributes! obj , name : 'Alex [Updated]'
658+ end
659+
660+ obj . reload
661+ expect ( obj . updated_at . to_i ) . to eql time_now . to_i
662+ end
663+ end
664+
665+ it 'uses provided values of created_at and updated_at if Config.timestamps=true' , config : { timestamps : true } do
666+ obj = klass . create!
667+
668+ travel 1 . hour do
669+ created_at = updated_at = Time . now
670+
671+ described_class . execute do |txn |
672+ txn . update_attributes! obj , created_at : created_at , updated_at : updated_at
673+ end
674+
675+ obj . reload
676+ expect ( obj . created_at . to_i ) . to eql created_at . to_i
677+ expect ( obj . updated_at . to_i ) . to eql updated_at . to_i
678+ end
679+ end
680+
681+ it 'does not raise error if Config.timestamps=false' , config : { timestamps : false } do
682+ obj = klass . create!
683+
684+ expect {
685+ described_class . execute do |txn |
686+ txn . update_attributes! obj , name : 'Alex [Updated]'
687+ end
688+ } . not_to raise_error
689+ end
690+
691+ it 'does not raise error if no changes and Config.timestamps=false' , config : { timestamps : false } do
692+ obj = klass . create!
693+
694+ expect {
695+ described_class . execute do |txn |
696+ txn . update_attributes! obj , { }
697+ end
698+ } . not_to raise_error
699+ end
700+ end
701+
606702 describe 'validation' do
607703 before do
608704 klass_with_validation . create_table
@@ -710,6 +806,19 @@ def around_update_callback
710806 expect ( klass . find ( obj_deleted . id ) . name ) . to eql 'Alex [Updated]'
711807 end
712808
809+ it 'keeps a model as changed when the transaction rolled back' do
810+ obj = klass . create! ( name : 'Alex' )
811+
812+ expect {
813+ described_class . execute do |txn |
814+ txn . update_attributes! obj , name : 'Alex [Updated]'
815+ raise 'trigger rollback'
816+ end
817+ } . to raise_error ( 'trigger rollback' )
818+
819+ expect ( obj ) . to be_changed
820+ end
821+
713822 it 'uses dumped value of partition key to update item' do
714823 klass = new_class ( partition_key : { name : :published_on , type : :date } ) do
715824 field :name
0 commit comments