blob: ae70c2c3dd3e8484e1a84f80b1c8405de3cb71e7 [file] [log] [blame]
Russ Cox09576202024-07-08 13:22:03 -04001// 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
5package storage
6
7import (
8 "testing"
9
10 "golang.org/x/oscar/internal/testutil"
11)
12
13func TestMemDB(t *testing.T) {
Jonathan Amsterdamfc0f8862024-07-19 14:27:06 -040014 db := MemDB()
15 TestDB(t, db)
16 TestDBLock(t, db)
Russ Cox09576202024-07-08 13:22:03 -040017}
18
19func TestMemVectorDB(t *testing.T) {
20 db := MemDB()
21 TestVectorDB(t, func() VectorDB { return MemVectorDB(db, testutil.Slogger(t), "") })
22}
23
24type maybeDB struct {
25 DB
26 maybe bool
27}
28
29type maybeBatch struct {
30 db *maybeDB
31 Batch
32}
33
34func (db *maybeDB) Batch() Batch {
35 return &maybeBatch{db: db, Batch: db.DB.Batch()}
36}
37
38func (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.
44func 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}