-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(database): use agnostic engine (#873)
* feat(database): add agnostic engine * feat(database): add config.Validate() * feat(database): add engine.Close() * feat(database): add engine.Driver() * chore: minor fixes * feat(database): add engine.Ping() * feat(database): add engine.NewResources() * refactor(database): use agnostic engine * feat(database): add FromCLIContext() * chore: use database.FromCLIContext() * chore(database): remove unused code * chore: misc updates * chore: fix typos --------- Co-authored-by: Easton Crupper <[email protected]>
- Loading branch information
Showing
10 changed files
with
256 additions
and
535 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,152 @@ | ||
// Copyright (c) 2022 Target Brands, Inc. All rights reserved. | ||
// Copyright (c) 2023 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package database | ||
|
||
import ( | ||
"flag" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-vela/server/database/sqlite" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func TestDatabase_FromContext(t *testing.T) { | ||
// setup types | ||
want, _ := sqlite.NewTest() | ||
_postgres, _ := testPostgres(t) | ||
defer _postgres.Close() | ||
|
||
defer func() { _sql, _ := want.Sqlite.DB(); _sql.Close() }() | ||
|
||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
context, _ := gin.CreateTestContext(nil) | ||
context.Set(key, want) | ||
|
||
// run test | ||
got := FromContext(context) | ||
|
||
if got != want { | ||
t.Errorf("FromContext is %v, want %v", got, want) | ||
ctx, _ := gin.CreateTestContext(nil) | ||
ctx.Set(key, _postgres) | ||
|
||
typeCtx, _ := gin.CreateTestContext(nil) | ||
typeCtx.Set(key, nil) | ||
|
||
nilCtx, _ := gin.CreateTestContext(nil) | ||
nilCtx.Set(key, nil) | ||
|
||
// setup tests | ||
tests := []struct { | ||
name string | ||
context *gin.Context | ||
want Interface | ||
}{ | ||
{ | ||
name: "success", | ||
context: ctx, | ||
want: _postgres, | ||
}, | ||
{ | ||
name: "failure with nil", | ||
context: nilCtx, | ||
want: nil, | ||
}, | ||
{ | ||
name: "failure with wrong type", | ||
context: typeCtx, | ||
want: nil, | ||
}, | ||
} | ||
} | ||
|
||
func TestDatabase_FromContext_Bad(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
context, _ := gin.CreateTestContext(nil) | ||
context.Set(key, nil) | ||
|
||
// run test | ||
got := FromContext(context) | ||
|
||
if got != nil { | ||
t.Errorf("FromContext is %v, want nil", got) | ||
// run tests | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
got := FromContext(test.context) | ||
if !reflect.DeepEqual(got, test.want) { | ||
t.Errorf("FromContext for %s is %v, want %v", test.name, got, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDatabase_FromContext_WrongType(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
func TestDatabase_ToContext(t *testing.T) { | ||
context, _ := gin.CreateTestContext(nil) | ||
context.Set(key, 1) | ||
|
||
// run test | ||
got := FromContext(context) | ||
|
||
if got != nil { | ||
t.Errorf("FromContext is %v, want nil", got) | ||
_postgres, _ := testPostgres(t) | ||
defer _postgres.Close() | ||
|
||
_sqlite := testSqlite(t) | ||
defer _sqlite.Close() | ||
|
||
// setup tests | ||
tests := []struct { | ||
name string | ||
database *engine | ||
want *engine | ||
}{ | ||
{ | ||
name: "success with postgres", | ||
database: _postgres, | ||
want: _postgres, | ||
}, | ||
{ | ||
name: "success with sqlite3", | ||
database: _sqlite, | ||
want: _sqlite, | ||
}, | ||
} | ||
} | ||
|
||
func TestDatabase_FromContext_Empty(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
context, _ := gin.CreateTestContext(nil) | ||
|
||
// run test | ||
got := FromContext(context) | ||
// run tests | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
ToContext(context, test.want) | ||
|
||
if got != nil { | ||
t.Errorf("FromContext is %v, want nil", got) | ||
got := context.Value(key) | ||
if !reflect.DeepEqual(got, test.want) { | ||
t.Errorf("ToContext for %s is %v, want %v", test.name, got, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDatabase_ToContext(t *testing.T) { | ||
// setup types | ||
want, _ := sqlite.NewTest() | ||
func TestDatabase_FromCLIContext(t *testing.T) { | ||
flags := flag.NewFlagSet("test", 0) | ||
flags.String("database.driver", "sqlite3", "doc") | ||
flags.String("database.addr", "file::memory:?cache=shared", "doc") | ||
flags.Int("database.compression.level", 3, "doc") | ||
flags.Duration("database.connection.life", 10*time.Second, "doc") | ||
flags.Int("database.connection.idle", 5, "doc") | ||
flags.Int("database.connection.open", 20, "doc") | ||
flags.String("database.encryption.key", "A1B2C3D4E5G6H7I8J9K0LMNOPQRSTUVW", "doc") | ||
flags.Bool("database.skip_creation", true, "doc") | ||
|
||
// setup tests | ||
tests := []struct { | ||
name string | ||
failure bool | ||
context *cli.Context | ||
}{ | ||
{ | ||
name: "success", | ||
failure: false, | ||
context: cli.NewContext(&cli.App{Name: "vela"}, flags, nil), | ||
}, | ||
{ | ||
name: "failure", | ||
failure: true, | ||
context: cli.NewContext(&cli.App{Name: "vela"}, flag.NewFlagSet("test", 0), nil), | ||
}, | ||
} | ||
|
||
defer func() { _sql, _ := want.Sqlite.DB(); _sql.Close() }() | ||
// run tests | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
_, err := FromCLIContext(test.context) | ||
|
||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
context, _ := gin.CreateTestContext(nil) | ||
ToContext(context, want) | ||
if test.failure { | ||
if err == nil { | ||
t.Errorf("FromCLIContext for %s should have returned err", test.name) | ||
} | ||
|
||
// run test | ||
got := context.Value(key) | ||
return | ||
} | ||
|
||
if got != want { | ||
t.Errorf("ToContext is %v, want %v", got, want) | ||
if err != nil { | ||
t.Errorf("FromCLIContext for %s returned err: %v", test.name, err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.