@@ -499,27 +499,17 @@ func TestBeginFromContextStartWithNoOptions(t *testing.T) {
499499 t .Errorf ("failed to begin transaction for read-write db - %s" , err )
500500 }
501501 test .withOpts = readOnly
502- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
503- if err != ErrCtxDBOptMismatch {
504- t .Error ("begin transaction should fail with an error DBOptionsMismatch" )
505- }
506- test .withOpts = readWrite
507- txn3 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
502+ txn2 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
508503 if err != nil {
509504 t .Error ("Received an error beginning transaction" )
510505 }
511- if txn3 == nil {
506+ if txn2 == nil {
512507 t .Error ("Did not receive a transaction from context" )
513508 }
514- // Case: Transaction begin is idempotent
515- if txn1 != txn3 {
509+ // Case: withOpts are ignored if txn is already open, current txn is returned
510+ if txn1 != txn2 {
516511 t .Error ("Got a different txn than was opened before" )
517512 }
518- test .txOpts = & sql.TxOptions {}
519- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
520- if err != ErrCtxTxnOptMismatch {
521- t .Error ("begin transaction should fail with an error TxnOptionsMismatch" )
522- }
523513 } else {
524514 txn1 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
525515 if err != nil {
@@ -532,54 +522,16 @@ func TestBeginFromContextStartWithNoOptions(t *testing.T) {
532522 t .Errorf ("failed to begin transaction for read-write db - %s" , err )
533523 }
534524 test .txOpts .ReadOnly = true
535- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
536- if err != ErrCtxTxnOptMismatch {
537- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
538- }
539- test .txOpts .ReadOnly = false
540- test .txOpts .Isolation = sql .LevelSerializable
541- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
542- if err != ErrCtxTxnOptMismatch {
543- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
544- }
545- test .txOpts .Isolation = sql .LevelDefault
546- test .withOpts = readOnly
547- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
548- if err != ErrCtxDBOptMismatch {
549- t .Error ("begin transaction should fail with an error DBOptionsMismatch" )
550- }
551- test .withOpts = readWrite
552- txn3 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
553- if err != nil {
554- t .Error ("Received an error beginning transaction" )
555- }
556- if txn3 == nil {
557- t .Error ("Did not receive a transaction from context" )
558- }
559- // Case: Transaction begin is idempotent
560- if txn1 != txn3 {
561- t .Error ("Got a different txn than was opened before" )
562- }
563- test .txOpts .ReadOnly = true
564- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
565- if err != ErrCtxTxnOptMismatch {
566- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
567- }
568- test .txOpts .ReadOnly = false
569- test .txOpts .Isolation = sql .LevelSerializable
570- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
571- if err != ErrCtxTxnOptMismatch {
572- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
573- }
574- txn4 , err := beginFromContextWithOptions (ctx , test .withOpts , nil )
525+ test .txOpts .Isolation = sql .LevelRepeatableRead
526+ txn2 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
575527 if err != nil {
576528 t .Error ("Received an error beginning transaction" )
577529 }
578- if txn4 == nil {
530+ if txn2 == nil {
579531 t .Error ("Did not receive a transaction from context" )
580532 }
581- // Case: Transaction begin is idempotent
582- if txn1 != txn4 {
533+ // Case: txOpts are ignored if txn is already open, current txn is returned
534+ if txn1 != txn2 {
583535 t .Error ("Got a different txn than was opened before" )
584536 }
585537 }
@@ -639,6 +591,7 @@ func TestBeginFromContextStartWithReadOnlyOptions(t *testing.T) {
639591 if err := dbROMock .ExpectationsWereMet (); err != nil {
640592 t .Errorf ("failed to begin transaction for read-only db - %s" , err )
641593 }
594+ // Case: noOptions allowed, current txn is returned
642595 test .withOpts = noOptions
643596 txn2 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
644597 if err != nil {
@@ -647,20 +600,14 @@ func TestBeginFromContextStartWithReadOnlyOptions(t *testing.T) {
647600 if txn2 == nil {
648601 t .Error ("Did not receive a transaction from context" )
649602 }
650- // Case: Transaction begin is idempotent
651603 if txn1 != txn2 {
652604 t .Error ("Got a different txn than was opened before" )
653605 }
606+ // Case: DB opts mismatch, RO -> RW not allowed
654607 test .withOpts = readWrite
655608 _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
656609 if err != ErrCtxDBOptMismatch {
657- t .Error ("begin transaction should fail with an error DBOptionsMismatch" )
658- }
659- test .withOpts = noOptions
660- test .txOpts = & sql.TxOptions {}
661- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
662- if err != ErrCtxTxnOptMismatch {
663- t .Error ("begin transaction should fail with an error TxnOptionsMismatch" )
610+ t .Error ("begin transaction should fail with an error DBOptMismatch" )
664611 }
665612 } else {
666613 _ , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
@@ -678,19 +625,8 @@ func TestBeginFromContextStartWithReadOnlyOptions(t *testing.T) {
678625 if err := dbROMock .ExpectationsWereMet (); err != nil {
679626 t .Errorf ("failed to begin transaction for read-only db - %s" , err )
680627 }
681- test .txOpts .ReadOnly = false
682- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
683- if err != ErrCtxTxnOptMismatch {
684- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
685- }
628+ // Case: txOpts read-only match, current txn is returned
686629 test .txOpts .ReadOnly = true
687- test .txOpts .Isolation = sql .LevelSerializable
688- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
689- if err != ErrCtxTxnOptMismatch {
690- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
691- }
692- test .txOpts .Isolation = sql .LevelDefault
693- test .withOpts = noOptions
694630 txn2 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
695631 if err != nil {
696632 t .Error ("Received an error beginning transaction" )
@@ -701,20 +637,18 @@ func TestBeginFromContextStartWithReadOnlyOptions(t *testing.T) {
701637 if txn1 != txn2 {
702638 t .Error ("Got a different txn than was opened before" )
703639 }
704- txn3 , err := beginFromContextWithOptions (ctx , test .withOpts , nil )
705- if err != nil {
706- t .Error ("Received an error beginning transaction" )
707- }
708- if txn3 == nil {
709- t .Error ("Did not receive a transaction from context" )
710- }
711- if txn1 != txn3 {
712- t .Error ("Got a different txn than was opened before" )
640+ // Case: txOpts mismatch, RO -> RW not allowed
641+ test .txOpts .ReadOnly = false
642+ test .txOpts .Isolation = sql .LevelSerializable
643+ _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
644+ if err != ErrCtxTxnOptMismatch {
645+ t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
713646 }
647+ // Case: DB opts mismatch, RO -> RW not allowed
714648 test .withOpts = readWrite
715649 _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
716650 if err != ErrCtxDBOptMismatch {
717- t .Error ("begin transaction should fail with an error DBOptionsMismatch " )
651+ t .Error ("begin transaction should fail with an error DBOptMismatch " )
718652 }
719653 }
720654 })
@@ -772,27 +706,57 @@ func TestBeginFromContextStartWithReadWriteOptions(t *testing.T) {
772706 if err := mock .ExpectationsWereMet (); err != nil {
773707 t .Errorf ("failed to begin transaction for read-write db - %s" , err )
774708 }
709+ // Case: DB opts mismatch, RW -> RO allowed, current txn is returned
775710 test .withOpts = readOnly
776- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
777- if err != ErrCtxDBOptMismatch {
778- t .Error ("begin transaction should fail with an error DBOptionsMismatch " )
711+ txn2 , err : = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
712+ if err != nil {
713+ t .Error ("Received an error beginning transaction " )
779714 }
780- test .withOpts = noOptions
715+ if txn2 == nil {
716+ t .Error ("Did not receive a transaction from context" )
717+ }
718+
719+ if txn1 != txn2 {
720+ t .Error ("Got a different txn than was opened before" )
721+ }
722+ // Case: txOpts mismatch, nil -> empty opts allowed, current txn is returned
723+ test .txOpts = & sql.TxOptions {}
781724 txn3 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
782725 if err != nil {
783726 t .Error ("Received an error beginning transaction" )
784727 }
785728 if txn3 == nil {
786729 t .Error ("Did not receive a transaction from context" )
787730 }
788- // Case: Transaction begin is idempotent
731+
789732 if txn1 != txn3 {
790733 t .Error ("Got a different txn than was opened before" )
791734 }
792- test .txOpts = & sql.TxOptions {}
793- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
794- if err != ErrCtxTxnOptMismatch {
795- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
735+ // Case: txOpts mismatch, nil -> RO allowed, current txn is returned
736+ test .txOpts = & sql.TxOptions {ReadOnly : true }
737+ txn4 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
738+ if err != nil {
739+ t .Error ("Received an error beginning transaction" )
740+ }
741+ if txn4 == nil {
742+ t .Error ("Did not receive a transaction from context" )
743+ }
744+
745+ if txn1 != txn4 {
746+ t .Error ("Got a different txn than was opened before" )
747+ }
748+ // Case: txOpts mismatch, nil -> RW allowed, current txn is returned
749+ test .txOpts = & sql.TxOptions {ReadOnly : false }
750+ txn5 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
751+ if err != nil {
752+ t .Error ("Received an error beginning transaction" )
753+ }
754+ if txn5 == nil {
755+ t .Error ("Did not receive a transaction from context" )
756+ }
757+
758+ if txn1 != txn5 {
759+ t .Error ("Got a different txn than was opened before" )
796760 }
797761 } else {
798762 txn1 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
@@ -805,47 +769,22 @@ func TestBeginFromContextStartWithReadWriteOptions(t *testing.T) {
805769 if err := mock .ExpectationsWereMet (); err != nil {
806770 t .Errorf ("failed to begin transaction for read-write db - %s" , err )
807771 }
772+ // Case: txOpts mismatch, RW -> RO allowed, current txn is returned
808773 test .txOpts .ReadOnly = true
809- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
810- if err != ErrCtxTxnOptMismatch {
811- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
812- }
813- test .txOpts .ReadOnly = false
814774 test .txOpts .Isolation = sql .LevelSerializable
815- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
816- if err != ErrCtxTxnOptMismatch {
817- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
818- }
819- test .txOpts .Isolation = sql .LevelDefault
820- test .withOpts = readOnly
821- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
822- if err != ErrCtxDBOptMismatch {
823- t .Error ("begin transaction should fail with an error DBOptionsMismatch" )
824- }
825- test .withOpts = noOptions
826775 txn2 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
827776 if err != nil {
828777 t .Error ("Received an error beginning transaction" )
829778 }
830779 if txn2 == nil {
831780 t .Error ("Did not receive a transaction from context" )
832781 }
833- // Case: Transaction begin is idempotent
834782 if txn1 != txn2 {
835783 t .Error ("Got a different txn than was opened before" )
836784 }
837- test .txOpts .ReadOnly = true
838- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
839- if err != ErrCtxTxnOptMismatch {
840- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
841- }
842- test .txOpts .ReadOnly = false
843- test .txOpts .Isolation = sql .LevelSerializable
844- _ , err = beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
845- if err != ErrCtxTxnOptMismatch {
846- t .Error ("begin transaction should fail with an error TxOptionsMismatch" )
847- }
848- txn3 , err := beginFromContextWithOptions (ctx , test .withOpts , nil )
785+ // Case: DB opts mismatch, RW -> RO allowed, current txn is returned
786+ test .withOpts = readOnly
787+ txn3 , err := beginFromContextWithOptions (ctx , test .withOpts , test .txOpts )
849788 if err != nil {
850789 t .Error ("Received an error beginning transaction" )
851790 }
0 commit comments