|
| 1 | +package badgerd |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/dgraph-io/badger/v3" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +type testLogger struct{} |
| 11 | + |
| 12 | +func (testLogger) Errorf(string, ...any) {} |
| 13 | +func (testLogger) Warningf(string, ...any) {} |
| 14 | +func (testLogger) Infof(string, ...any) {} |
| 15 | +func (testLogger) Debugf(string, ...any) {} |
| 16 | +func (testLogger) Tracef(string, ...any) {} |
| 17 | + |
| 18 | +func TestWithPathTable(t *testing.T) { |
| 19 | + o := options{} |
| 20 | + WithPath(":memory:")(&o) |
| 21 | + assert.True(t, o.inMemory) |
| 22 | + assert.Equal(t, "", o.path) |
| 23 | + |
| 24 | + o = options{} |
| 25 | + WithPath("/tmp/pdb")(&o) |
| 26 | + assert.False(t, o.inMemory) |
| 27 | + assert.Equal(t, "/tmp/pdb", o.path) |
| 28 | +} |
| 29 | + |
| 30 | +func TestWithInMemoryTable(t *testing.T) { |
| 31 | + o := options{path: "/tmp/pdb"} |
| 32 | + WithInMemory(true)(&o) |
| 33 | + assert.True(t, o.inMemory) |
| 34 | + assert.Equal(t, "", o.path) |
| 35 | + |
| 36 | + WithInMemory(false)(&o) |
| 37 | + assert.False(t, o.inMemory) |
| 38 | + assert.Equal(t, "", o.path) |
| 39 | +} |
| 40 | + |
| 41 | +func TestOptionsBuildAppliesBadgerOptionsFunc(t *testing.T) { |
| 42 | + o := options{inMemory: true, logger: testLogger{}} |
| 43 | + WithBadgerOptionsFunc(func(opts badger.Options) badger.Options { |
| 44 | + opts.SyncWrites = false |
| 45 | + opts.NumMemtables = 7 |
| 46 | + return opts |
| 47 | + })(&o) |
| 48 | + |
| 49 | + b := o.build() |
| 50 | + assert.True(t, b.InMemory) |
| 51 | + assert.False(t, b.SyncWrites) |
| 52 | + assert.Equal(t, 7, b.NumMemtables) |
| 53 | +} |
0 commit comments