| // Copyright 2024 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package storage |
| |
| import ( |
| "testing" |
| |
| "golang.org/x/oscar/internal/testutil" |
| ) |
| |
| func TestMemDB(t *testing.T) { |
| db := MemDB() |
| TestDB(t, db) |
| TestDBLock(t, db) |
| } |
| |
| func TestMemVectorDB(t *testing.T) { |
| db := MemDB() |
| TestVectorDB(t, func() VectorDB { return MemVectorDB(db, testutil.Slogger(t), "") }) |
| } |
| |
| type maybeDB struct { |
| DB |
| maybe bool |
| } |
| |
| type maybeBatch struct { |
| db *maybeDB |
| Batch |
| } |
| |
| func (db *maybeDB) Batch() Batch { |
| return &maybeBatch{db: db, Batch: db.DB.Batch()} |
| } |
| |
| func (b *maybeBatch) MaybeApply() bool { |
| return b.db.maybe |
| } |
| |
| // Test that when db.Batch.MaybeApply returns true, |
| // the memvector Batch MaybeApply applies the memvector ops. |
| func TestMemVectorBatchMaybeApply(t *testing.T) { |
| db := &maybeDB{DB: MemDB()} |
| vdb := MemVectorDB(db, testutil.Slogger(t), "") |
| b := vdb.Batch() |
| b.Set("apple3", embed("apple3")) |
| if _, ok := vdb.Get("apple3"); ok { |
| t.Errorf("Get(apple3) succeeded before batch apply") |
| } |
| b.MaybeApply() // should not apply because the db Batch does not apply |
| if _, ok := vdb.Get("apple3"); ok { |
| t.Errorf("Get(apple3) succeeded after MaybeApply that didn't apply") |
| } |
| db.maybe = true |
| b.MaybeApply() // now should apply |
| if _, ok := vdb.Get("apple3"); !ok { |
| t.Errorf("Get(apple3) failed after MaybeApply that did apply") |
| } |
| } |