-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split tests setup into db and client files
- Loading branch information
Showing
4 changed files
with
269 additions
and
256 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package tests | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sourcenetwork/defradb/net" | ||
"github.com/sourcenetwork/defradb/tests/clients" | ||
"github.com/sourcenetwork/defradb/tests/clients/cli" | ||
"github.com/sourcenetwork/defradb/tests/clients/http" | ||
) | ||
|
||
// setupClient returns the client implementation for the current | ||
// testing state. The client type on the test state is used to | ||
// select the client implementation to use. | ||
func setupClient(s *state, node *net.Node) (impl clients.Client, err error) { | ||
switch s.clientType { | ||
case httpClientType: | ||
impl, err = http.NewWrapper(node) | ||
|
||
case cliClientType: | ||
impl = cli.NewWrapper(node) | ||
|
||
case goClientType: | ||
impl = node | ||
|
||
default: | ||
err = fmt.Errorf("invalid client type: %v", s.dbt) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return | ||
} |
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2023 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
badger "github.com/dgraph-io/badger/v4" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
badgerds "github.com/sourcenetwork/defradb/datastore/badger/v4" | ||
"github.com/sourcenetwork/defradb/datastore/memory" | ||
"github.com/sourcenetwork/defradb/db" | ||
changeDetector "github.com/sourcenetwork/defradb/tests/change_detector" | ||
) | ||
|
||
func NewBadgerMemoryDB(ctx context.Context, dbopts ...db.Option) (client.DB, error) { | ||
opts := badgerds.Options{ | ||
Options: badger.DefaultOptions("").WithInMemory(true), | ||
} | ||
rootstore, err := badgerds.NewDatastore("", &opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
db, err := db.NewDB(ctx, rootstore, dbopts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return db, nil | ||
} | ||
|
||
func NewInMemoryDB(ctx context.Context, dbopts ...db.Option) (client.DB, error) { | ||
db, err := db.NewDB(ctx, memory.NewDatastore(ctx), dbopts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return db, nil | ||
} | ||
|
||
func NewBadgerFileDB(ctx context.Context, t testing.TB, dbopts ...db.Option) (client.DB, string, error) { | ||
var dbPath string | ||
switch { | ||
case databaseDir != "": | ||
// restarting database | ||
dbPath = databaseDir | ||
|
||
case changeDetector.Enabled: | ||
// change detector | ||
dbPath = changeDetector.DatabaseDir(t) | ||
|
||
default: | ||
// default test case | ||
dbPath = t.TempDir() | ||
} | ||
|
||
opts := &badgerds.Options{ | ||
Options: badger.DefaultOptions(dbPath), | ||
} | ||
rootstore, err := badgerds.NewDatastore(dbPath, opts) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
db, err := db.NewDB(ctx, rootstore, dbopts...) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
return db, dbPath, err | ||
} | ||
|
||
// setupDatabase returns the database implementation for the current | ||
// testing state. The database type on the test state is used to | ||
// select the datastore implementation to use. | ||
func setupDatabase(s *state) (impl client.DB, path string, err error) { | ||
dbopts := []db.Option{ | ||
db.WithUpdateEvents(), | ||
db.WithLensPoolSize(lensPoolSize), | ||
} | ||
|
||
switch s.dbt { | ||
case badgerIMType: | ||
impl, err = NewBadgerMemoryDB(s.ctx, dbopts...) | ||
|
||
case badgerFileType: | ||
impl, path, err = NewBadgerFileDB(s.ctx, s.t, dbopts...) | ||
|
||
case defraIMType: | ||
impl, err = NewInMemoryDB(s.ctx, dbopts...) | ||
|
||
default: | ||
err = fmt.Errorf("invalid database type: %v", s.dbt) | ||
} | ||
|
||
if err != nil { | ||
return nil, "", err | ||
} | ||
return | ||
} |
Oops, something went wrong.