diff --git a/badger/.gitignore b/badger/.gitignore new file mode 100644 index 000000000..2630c3407 --- /dev/null +++ b/badger/.gitignore @@ -0,0 +1,3 @@ +fiber.badger/ +fiber.config.badger/ +fiber.with_options.badger/ diff --git a/badger/config.go b/badger/config.go index 32338c0c4..24e3d89ee 100644 --- a/badger/config.go +++ b/badger/config.go @@ -68,7 +68,10 @@ func configDefault(config ...Config) Config { cfg.GCInterval = ConfigDefault.GCInterval } overrideLogger := false - if cfg.BadgerOptions.Dir == defaultDatabase && cfg.BadgerOptions.Dir != cfg.Database { + // Detecting if no default Badger option was given + // Also detects when a default badger option is given with a custom database name + if cfg.BadgerOptions.ValueLogFileSize <= 0 || cfg.BadgerOptions.Dir == "" || cfg.BadgerOptions.ValueDir == "" || + (cfg.BadgerOptions.Dir == defaultDatabase && cfg.BadgerOptions.Dir != cfg.Database) { cfg.BadgerOptions = badger.DefaultOptions(cfg.Database) overrideLogger = true } diff --git a/badger/config_test.go b/badger/config_test.go new file mode 100644 index 000000000..85258436a --- /dev/null +++ b/badger/config_test.go @@ -0,0 +1,36 @@ +package badger + +import ( + "github.com/dgraph-io/badger/v3" + "github.com/gofiber/utils" + "testing" +) + +func assertRecoveryPanic(t *testing.T) { + err := recover() + utils.AssertEqual(t, nil, err) +} + +func Test_Badger_Only_Name(t *testing.T) { + defer assertRecoveryPanic(t) + testDB := New(Config{ + Database: "fiber.config.badger", + UseLogger: false, + }) + utils.AssertEqual(t, nil, testDB.Close()) +} + +func Test_Badger_Options(t *testing.T) { + defer assertRecoveryPanic(t) + testDB := New(Config{ + BadgerOptions: badger.DefaultOptions("fiber.with_options.badger"), + UseLogger: false, + }) + utils.AssertEqual(t, nil, testDB.Close()) +} + +func Test_Empty_Config(t *testing.T) { + defer assertRecoveryPanic(t) + testDB := New(Config{}) + utils.AssertEqual(t, nil, testDB.Close()) +}