2026-04-06 15:48:37 +05:30
|
|
|
// Copyright IBM Corp. 2020, 2026
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2021-07-30 08:45:40 -04:00
|
|
|
package dbtest_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-10-12 10:33:41 -07:00
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/db/common"
|
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/testing/dbtest"
|
2021-07-30 08:45:40 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestStartUsingTemplate(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
dialect string
|
|
|
|
|
options []dbtest.Option
|
|
|
|
|
err error
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
"PostgresNoOptions",
|
|
|
|
|
dbtest.Postgres,
|
|
|
|
|
[]dbtest.Option{},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"PostgresTemplate1",
|
|
|
|
|
dbtest.Postgres,
|
|
|
|
|
[]dbtest.Option{
|
|
|
|
|
dbtest.WithTemplate(dbtest.Template1),
|
|
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"PostgresUnsupportedTemplate",
|
|
|
|
|
dbtest.Postgres,
|
|
|
|
|
[]dbtest.Option{
|
|
|
|
|
dbtest.WithTemplate("unsupportedTemplate"),
|
|
|
|
|
},
|
|
|
|
|
errors.New("unsupported database template: unsupportedTemplate"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"UnsupportedDialect",
|
|
|
|
|
"unsupportedDialect",
|
|
|
|
|
[]dbtest.Option{},
|
|
|
|
|
errors.New("unsupported dialect: unsupportedDialect"),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
c, u, _, err := dbtest.StartUsingTemplate(tt.dialect, tt.options...)
|
|
|
|
|
|
|
|
|
|
if tt.err != nil {
|
|
|
|
|
require.EqualError(t, err, tt.err.Error())
|
|
|
|
|
require.NoError(t, c())
|
|
|
|
|
} else {
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2021-09-30 09:17:27 -04:00
|
|
|
db, err := common.SqlOpen(tt.dialect, u)
|
2021-07-30 08:45:40 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NoError(t, db.Ping())
|
|
|
|
|
db.Close()
|
|
|
|
|
|
|
|
|
|
require.NoError(t, c())
|
|
|
|
|
|
|
|
|
|
// ensure that database is gone after calling c
|
2021-09-30 09:17:27 -04:00
|
|
|
db, _ = common.SqlOpen(tt.dialect, u)
|
2021-07-30 08:45:40 -04:00
|
|
|
require.Error(t, db.Ping())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|