This can be used in tests to get access to a controller that behaves just like a dev controller but doesn't require the normal Command bits (UI, etc). This can be enhanced over time as needed; for now it's pretty simple but it's good enough to be used for API tests. A simple project create/read API test is added as well, which serves to show the programmatic model (as well as test those functions at the API level).
106 lines
1.9 KiB
Go
106 lines
1.9 KiB
Go
package db
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestOpen(t *testing.T) {
|
|
cleanup, url, _, err := StartDbInDocker("postgres")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cleanup()
|
|
type args struct {
|
|
dbType DbType
|
|
connectionUrl string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "valid",
|
|
args: args{
|
|
dbType: Postgres,
|
|
connectionUrl: url,
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "invalid",
|
|
args: args{
|
|
dbType: Postgres,
|
|
connectionUrl: "",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := Open(tt.args.dbType, tt.args.connectionUrl)
|
|
defer func() {
|
|
if err == nil {
|
|
got.Close()
|
|
}
|
|
}()
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Open() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if tt.wantErr && got != nil {
|
|
t.Error("Open() wanted error and got != nil")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMigrate(t *testing.T) {
|
|
cleanup, url, _, err := StartDbInDocker("postgres")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cleanup()
|
|
type args struct {
|
|
connectionUrl string
|
|
migrationsDirectory string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "valid",
|
|
args: args{
|
|
connectionUrl: url,
|
|
migrationsDirectory: "migrations/postgres",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "bad-url",
|
|
args: args{
|
|
connectionUrl: "",
|
|
migrationsDirectory: "migrations/postgres",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "bad-dir",
|
|
args: args{
|
|
connectionUrl: url,
|
|
migrationsDirectory: "",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := Migrate(tt.args.connectionUrl, tt.args.migrationsDirectory); (err != nil) != tt.wantErr {
|
|
t.Errorf("Migrate() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|