Russ Cox | 0957620 | 2024-07-08 13:22:03 -0400 | [diff] [blame] | 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package storage |
| 6 | |
| 7 | import ( |
| 8 | "testing" |
| 9 | |
| 10 | "golang.org/x/oscar/internal/testutil" |
| 11 | ) |
| 12 | |
| 13 | func TestMemDB(t *testing.T) { |
Jonathan Amsterdam | fc0f886 | 2024-07-19 14:27:06 -0400 | [diff] [blame] | 14 | db := MemDB() |
| 15 | TestDB(t, db) |
| 16 | TestDBLock(t, db) |
Russ Cox | 0957620 | 2024-07-08 13:22:03 -0400 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | func TestMemVectorDB(t *testing.T) { |
| 20 | db := MemDB() |
| 21 | TestVectorDB(t, func() VectorDB { return MemVectorDB(db, testutil.Slogger(t), "") }) |
| 22 | } |
| 23 | |
| 24 | type maybeDB struct { |
| 25 | DB |
| 26 | maybe bool |
| 27 | } |
| 28 | |
| 29 | type maybeBatch struct { |
| 30 | db *maybeDB |
| 31 | Batch |
| 32 | } |
| 33 | |
| 34 | func (db *maybeDB) Batch() Batch { |
| 35 | return &maybeBatch{db: db, Batch: db.DB.Batch()} |
| 36 | } |
| 37 | |
| 38 | func (b *maybeBatch) MaybeApply() bool { |
| 39 | return b.db.maybe |
| 40 | } |
| 41 | |
| 42 | // Test that when db.Batch.MaybeApply returns true, |
| 43 | // the memvector Batch MaybeApply applies the memvector ops. |
| 44 | func TestMemVectorBatchMaybeApply(t *testing.T) { |
| 45 | db := &maybeDB{DB: MemDB()} |
| 46 | vdb := MemVectorDB(db, testutil.Slogger(t), "") |
| 47 | b := vdb.Batch() |
| 48 | b.Set("apple3", embed("apple3")) |
| 49 | if _, ok := vdb.Get("apple3"); ok { |
| 50 | t.Errorf("Get(apple3) succeeded before batch apply") |
| 51 | } |
| 52 | b.MaybeApply() // should not apply because the db Batch does not apply |
| 53 | if _, ok := vdb.Get("apple3"); ok { |
| 54 | t.Errorf("Get(apple3) succeeded after MaybeApply that didn't apply") |
| 55 | } |
| 56 | db.maybe = true |
| 57 | b.MaybeApply() // now should apply |
| 58 | if _, ok := vdb.Get("apple3"); !ok { |
| 59 | t.Errorf("Get(apple3) failed after MaybeApply that did apply") |
| 60 | } |
| 61 | } |