2022-02-16 12:30:49 +00:00
|
|
|
package initialise
|
|
|
|
|
|
|
|
import (
|
2024-01-04 16:12:20 +00:00
|
|
|
"context"
|
2022-02-16 12:30:49 +00:00
|
|
|
"database/sql"
|
2023-10-19 10:19:10 +00:00
|
|
|
"database/sql/driver"
|
2022-02-16 12:30:49 +00:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_verifyEvents(t *testing.T) {
|
2022-08-31 07:52:43 +00:00
|
|
|
err := ReadStmts("cockroach") //TODO: check all dialects
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to read stmts: %v", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2022-02-16 12:30:49 +00:00
|
|
|
type args struct {
|
|
|
|
db db
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
targetErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "unable to begin",
|
|
|
|
args: args{
|
|
|
|
db: prepareDB(t,
|
|
|
|
expectBegin(sql.ErrConnDone),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
targetErr: sql.ErrConnDone,
|
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
{
|
|
|
|
name: "events already exists",
|
|
|
|
args: args{
|
|
|
|
db: prepareDB(t,
|
|
|
|
expectBegin(nil),
|
|
|
|
expectQuery(
|
|
|
|
"SELECT count(*) FROM information_schema.tables WHERE table_schema = 'eventstore' AND table_name like 'events%'",
|
|
|
|
nil,
|
|
|
|
[]string{"count"},
|
|
|
|
[][]driver.Value{
|
|
|
|
{1},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
expectCommit(nil),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "events and events2 already exists",
|
|
|
|
args: args{
|
|
|
|
db: prepareDB(t,
|
|
|
|
expectBegin(nil),
|
|
|
|
expectQuery(
|
|
|
|
"SELECT count(*) FROM information_schema.tables WHERE table_schema = 'eventstore' AND table_name like 'events%'",
|
|
|
|
nil,
|
|
|
|
[]string{"count"},
|
|
|
|
[][]driver.Value{
|
|
|
|
{2},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
expectCommit(nil),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
2022-02-16 12:30:49 +00:00
|
|
|
{
|
|
|
|
name: "create table fails",
|
|
|
|
args: args{
|
|
|
|
db: prepareDB(t,
|
|
|
|
expectBegin(nil),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectQuery(
|
|
|
|
"SELECT count(*) FROM information_schema.tables WHERE table_schema = 'eventstore' AND table_name like 'events%'",
|
|
|
|
nil,
|
|
|
|
[]string{"count"},
|
|
|
|
[][]driver.Value{
|
|
|
|
{0},
|
|
|
|
},
|
|
|
|
),
|
2022-02-16 12:30:49 +00:00
|
|
|
expectExec(createEventsStmt, sql.ErrNoRows),
|
|
|
|
expectRollback(nil),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
targetErr: sql.ErrNoRows,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "correct",
|
|
|
|
args: args{
|
|
|
|
db: prepareDB(t,
|
|
|
|
expectBegin(nil),
|
2023-10-19 10:19:10 +00:00
|
|
|
expectQuery(
|
|
|
|
"SELECT count(*) FROM information_schema.tables WHERE table_schema = 'eventstore' AND table_name like 'events%'",
|
|
|
|
nil,
|
|
|
|
[]string{"count"},
|
|
|
|
[][]driver.Value{
|
|
|
|
{0},
|
|
|
|
},
|
|
|
|
),
|
2022-02-16 12:30:49 +00:00
|
|
|
expectExec(createEventsStmt, nil),
|
|
|
|
expectCommit(nil),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
targetErr: nil,
|
|
|
|
},
|
|
|
|
}
|
2022-08-31 07:52:43 +00:00
|
|
|
|
2022-02-16 12:30:49 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-12-04 13:51:40 +00:00
|
|
|
conn, err := tt.args.db.db.Conn(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := createEvents(context.Background(), conn); !errors.Is(err, tt.targetErr) {
|
2022-02-16 12:30:49 +00:00
|
|
|
t.Errorf("createEvents() error = %v, want: %v", err, tt.targetErr)
|
|
|
|
}
|
|
|
|
if err := tt.args.db.mock.ExpectationsWereMet(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 06:55:09 +00:00
|
|
|
|
|
|
|
func Test_verifyEncryptionKeys(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
db db
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
targetErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "unable to begin",
|
|
|
|
args: args{
|
|
|
|
db: prepareDB(t,
|
|
|
|
expectBegin(sql.ErrConnDone),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
targetErr: sql.ErrConnDone,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "create table fails",
|
|
|
|
args: args{
|
|
|
|
db: prepareDB(t,
|
|
|
|
expectBegin(nil),
|
|
|
|
expectExec(createEncryptionKeysStmt, sql.ErrNoRows),
|
|
|
|
expectRollback(nil),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
targetErr: sql.ErrNoRows,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "correct",
|
|
|
|
args: args{
|
|
|
|
db: prepareDB(t,
|
|
|
|
expectBegin(nil),
|
|
|
|
expectExec(createEncryptionKeysStmt, nil),
|
|
|
|
expectCommit(nil),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
targetErr: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-01-04 16:12:20 +00:00
|
|
|
if err := createEncryptionKeys(context.Background(), tt.args.db.db); !errors.Is(err, tt.targetErr) {
|
2022-03-14 06:55:09 +00:00
|
|
|
t.Errorf("createEvents() error = %v, want: %v", err, tt.targetErr)
|
|
|
|
}
|
|
|
|
if err := tt.args.db.mock.ExpectationsWereMet(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|