Skip to content

Commit

Permalink
split tests setup into db and client files
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Oct 13, 2023
1 parent 25139aa commit 580f016
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 256 deletions.
44 changes: 44 additions & 0 deletions tests/integration/client.go
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
}
107 changes: 107 additions & 0 deletions tests/integration/db.go
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
}
Loading

0 comments on commit 580f016

Please sign in to comment.