Skip to content

Commit

Permalink
Add unit tests for persistent DB
Browse files Browse the repository at this point in the history
Doesn't cover much of the persistent DB creation yet, but it's a start
  • Loading branch information
philippgille committed Mar 17, 2024
1 parent 5262f53 commit dd0f31d
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,70 @@ package chromem

import (
"context"
"math/rand"
"os"
"path/filepath"
"reflect"
"slices"
"testing"
)

func TestNewPersistentDB(t *testing.T) {
t.Run("Create directory", func(t *testing.T) {
randString := randomString(rand.New(rand.NewSource(rand.Int63())), 10)
path := filepath.Join(os.TempDir(), randString)
defer os.RemoveAll(path)

// Path shouldn't exist yet
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatal("expected path to not exist, got", err)
}

db, err := NewPersistentDB(path)
if err != nil {
t.Fatal("expected no error, got", err)
}
if db == nil {
t.Fatal("expected DB, got nil")
}

// Path should exist now
if _, err := os.Stat(path); err != nil {
t.Fatal("expected path to exist, got", err)
}
})
t.Run("Existing directory", func(t *testing.T) {
path, err := os.MkdirTemp(os.TempDir(), "")
if err != nil {
t.Fatal("couldn't create temp dir:", err)
}
defer os.RemoveAll(path)

db, err := NewPersistentDB(path)
if err != nil {
t.Fatal("expected no error, got", err)
}
if db == nil {
t.Fatal("expected DB, got nil")
}
})
}

func TestNewPersistentDB_Errors(t *testing.T) {
t.Run("Path is an existing file", func(t *testing.T) {
f, err := os.CreateTemp(os.TempDir(), "")
if err != nil {
t.Fatal("couldn't create temp file:", err)
}
defer os.RemoveAll(f.Name())

_, err = NewPersistentDB(f.Name())
if err == nil {
t.Fatal("expected error, got nil")
}
})
}

func TestDB_CreateCollection(t *testing.T) {
// Values in the collection
name := "test"
Expand Down

0 comments on commit dd0f31d

Please sign in to comment.