forked from go-gorm/postgres
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: foreign key violated error (go-gorm#193)
* feat: foreign key violated error * ci: fixed ci workflow to be triggered for all pull requests * test: fixed typo --------- Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
- Loading branch information
Showing
5 changed files
with
54 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package postgres | ||
|
||
import ( | ||
"errors" | ||
"github.com/jackc/pgx/v5/pgconn" | ||
"gorm.io/gorm" | ||
"testing" | ||
) | ||
|
||
func TestDialector_Translate(t *testing.T) { | ||
type fields struct { | ||
Config *Config | ||
} | ||
type args struct { | ||
err error | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want error | ||
}{ | ||
{ | ||
name: "it should return ErrDuplicatedKey error if the status code is 23505", | ||
args: args{err: &pgconn.PgError{Code: "23505"}}, | ||
want: gorm.ErrDuplicatedKey, | ||
}, | ||
{ | ||
name: "it should return ErrForeignKeyViolated error if the status code is 23503", | ||
args: args{err: &pgconn.PgError{Code: "23503"}}, | ||
want: gorm.ErrForeignKeyViolated, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dialector := Dialector{ | ||
Config: tt.fields.Config, | ||
} | ||
if err := dialector.Translate(tt.args.err); !errors.Is(err, tt.want) { | ||
t.Errorf("Translate() expected error = %v, got error %v", err, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters