cleaner tests i thought
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"local/dndex/config"
|
||||
"local/dndex/storage/entity"
|
||||
"os"
|
||||
"regexp"
|
||||
@@ -20,9 +19,8 @@ type BoltDB struct {
|
||||
db *bolt.DB
|
||||
}
|
||||
|
||||
func NewBoltDB() *BoltDB {
|
||||
config := config.New()
|
||||
db, err := bolt.Open(config.DBURI, os.ModePerm, &bolt.Options{
|
||||
func NewBoltDB(path string) *BoltDB {
|
||||
db, err := bolt.Open(path, os.ModePerm, &bolt.Options{
|
||||
Timeout: time.Second * 3,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -364,8 +364,7 @@ func tempBoltDB(t *testing.T) (*BoltDB, func()) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
os.Args = []string{"a", "-dburi", f.Name()}
|
||||
bdb := NewBoltDB()
|
||||
bdb := NewBoltDB(f.Name())
|
||||
fillBoltDB(t, bdb)
|
||||
return bdb, func() {
|
||||
bdb.db.Close()
|
||||
|
||||
@@ -15,12 +15,15 @@ type Driver interface {
|
||||
Delete(context.Context, string, interface{}) error
|
||||
}
|
||||
|
||||
func New() Driver {
|
||||
func New(path ...string) Driver {
|
||||
if len(path) == 0 {
|
||||
path = []string{config.New().DBURI}
|
||||
}
|
||||
switch strings.ToLower(config.New().DriverType) {
|
||||
case "mongo":
|
||||
return NewMongo()
|
||||
return NewMongo(path[0])
|
||||
case "boltdb":
|
||||
return NewBoltDB()
|
||||
return NewBoltDB(path[0])
|
||||
}
|
||||
panic("unknown driver type " + strings.ToLower(config.New().DriverType))
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ type Mongo struct {
|
||||
db string
|
||||
}
|
||||
|
||||
func NewMongo() Mongo {
|
||||
opts := options.Client().ApplyURI(config.New().DBURI)
|
||||
func NewMongo(path string) Mongo {
|
||||
opts := options.Client().ApplyURI(path)
|
||||
c, err := mongo.NewClient(opts)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -3,11 +3,9 @@ package storage
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"local/dndex/config"
|
||||
"local/dndex/storage/driver"
|
||||
"local/dndex/storage/entity"
|
||||
"local/dndex/storage/operator"
|
||||
"strings"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
@@ -16,16 +14,9 @@ type Graph struct {
|
||||
driver driver.Driver
|
||||
}
|
||||
|
||||
func NewGraph() Graph {
|
||||
var d driver.Driver
|
||||
switch strings.ToLower(config.New().DriverType) {
|
||||
case "mongo":
|
||||
d = driver.NewMongo()
|
||||
case "boltdb":
|
||||
d = driver.NewBoltDB()
|
||||
}
|
||||
func NewGraph(path ...string) Graph {
|
||||
return Graph{
|
||||
driver: d,
|
||||
driver: driver.New(path...),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,9 +16,9 @@ type RateLimitedGraph struct {
|
||||
limiters *sync.Map
|
||||
}
|
||||
|
||||
func NewRateLimitedGraph() RateLimitedGraph {
|
||||
func NewRateLimitedGraph(path ...string) RateLimitedGraph {
|
||||
return RateLimitedGraph{
|
||||
g: NewGraph(),
|
||||
g: NewGraph(path...),
|
||||
rps: config.New().RPS,
|
||||
limiters: &sync.Map{},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user