From 1fcbf2b0cd93a83bae6a447009fa0ca47b2c497a Mon Sep 17 00:00:00 2001 From: clD11 <23483715+clD11@users.noreply.github.com> Date: Thu, 13 Feb 2025 12:37:21 +0000 Subject: [PATCH] test: add test for isUniqueConstraintViolation --- services/wallet/storage/storage_test.go | 56 +++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/services/wallet/storage/storage_test.go b/services/wallet/storage/storage_test.go index 6bd0df978..029142302 100644 --- a/services/wallet/storage/storage_test.go +++ b/services/wallet/storage/storage_test.go @@ -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" @@ -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) + }) + } +}