Skip to content

Commit

Permalink
test: add test for isUniqueConstraintViolation
Browse files Browse the repository at this point in the history
  • Loading branch information
clD11 committed Feb 13, 2025
1 parent b6edd89 commit 1fcbf2b
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions services/wallet/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"
uuid "github.com/satori/go.uuid"
should "github.com/stretchr/testify/assert"
must "github.com/stretchr/testify/require"
Expand Down Expand Up @@ -436,3 +437,58 @@ func setupDBI() (*sqlx.DB, error) {

return pg.RawDB(), nil
}

func TestIsUniqueConstraintViolation(t *testing.T) {
type tcGiven struct {
err error
}

type tcExpected struct {
result bool
}

type testCase struct {
name string
given tcGiven
exp tcExpected
}

tests := []testCase{
{
name: "not_pq_error",
given: tcGiven{
err: model.Error("not_pq_error"),
},
},

{
name: "not_constraint_error",
given: tcGiven{
err: &pq.Error{
Code: "0",
},
},
},

{
name: "constraint_error",
given: tcGiven{
err: &pq.Error{
Code: "23505",
},
},
exp: tcExpected{
result: true,
},
},
}

for i := range tests {
tc := tests[i]

t.Run(tc.name, func(t *testing.T) {
actual := isUniqueConstraintViolation(tc.given.err)
should.Equal(t, tc.exp.result, actual)
})
}
}

0 comments on commit 1fcbf2b

Please sign in to comment.