diff options
author | Joe Chen <jc@unknwon.io> | 2022-11-05 23:33:05 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-05 23:33:05 +0800 |
commit | 5fb29db2db04bc128af410867f1f602320eb5d66 (patch) | |
tree | 9d0b86702d872f8f5ab7d0691e511c52f38fde34 /internal/db/email_addresses_test.go | |
parent | b5d47b969258f3d644ad797b29901eb607f6b94f (diff) |
refactor(db): migrate methods off and delete deprecated methods from `user.go` (#7231)
Diffstat (limited to 'internal/db/email_addresses_test.go')
-rw-r--r-- | internal/db/email_addresses_test.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/internal/db/email_addresses_test.go b/internal/db/email_addresses_test.go new file mode 100644 index 00000000..b54ffbad --- /dev/null +++ b/internal/db/email_addresses_test.go @@ -0,0 +1,66 @@ +// Copyright 2022 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package db + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "gogs.io/gogs/internal/dbtest" + "gogs.io/gogs/internal/errutil" +) + +func TestEmailAddresses(t *testing.T) { + if testing.Short() { + t.Skip() + } + t.Parallel() + + tables := []interface{}{new(EmailAddress)} + db := &emailAddresses{ + DB: dbtest.NewDB(t, "emailAddresses", tables...), + } + + for _, tc := range []struct { + name string + test func(t *testing.T, db *emailAddresses) + }{ + {"GetByEmail", emailAddressesGetByEmail}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Cleanup(func() { + err := clearTables(t, db.DB, tables...) + require.NoError(t, err) + }) + tc.test(t, db) + }) + if t.Failed() { + break + } + } +} + +func emailAddressesGetByEmail(t *testing.T, db *emailAddresses) { + ctx := context.Background() + + const testEmail = "alice@example.com" + _, err := db.GetByEmail(ctx, testEmail) + wantErr := ErrEmailNotExist{ + args: errutil.Args{ + "email": testEmail, + }, + } + assert.Equal(t, wantErr, err) + + // TODO: Use EmailAddresses.Create to replace SQL hack when the method is available. + err = db.Exec(`INSERT INTO email_address (uid, email) VALUES (1, ?)`, testEmail).Error + require.NoError(t, err) + got, err := db.GetByEmail(ctx, testEmail) + require.NoError(t, err) + assert.Equal(t, testEmail, got.Email) +} |