test: Implement db unit tests

This commit is contained in:
Eugen Eisler 2024-08-21 00:50:19 +02:00
parent e01d355d1b
commit 1bafde09b6
6 changed files with 192 additions and 0 deletions

View File

@ -9,6 +9,23 @@ on:
branches: ["main"]
jobs:
test:
name: Run tests
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: ./go.mod
- name: Run tests
run: go test -v ./...
build:
name: Build binaries for Windows, macOS, and Linux
runs-on: ${{ matrix.os }}

29
db/contexts_test.go Normal file
View File

@ -0,0 +1,29 @@
package db
import (
"os"
"path/filepath"
"testing"
)
func TestContexts_GetContext(t *testing.T) {
dir := t.TempDir()
contexts := &Contexts{
Storage: &Storage{Dir: dir},
}
contextName := "testContext"
contextPath := filepath.Join(dir, contextName)
contextContent := "test content"
err := os.WriteFile(contextPath, []byte(contextContent), 0644)
if err != nil {
t.Fatalf("failed to write context file: %v", err)
}
context, err := contexts.GetContext(contextName)
if err != nil {
t.Fatalf("failed to get context: %v", err)
}
expectedContext := &Context{Name: contextName, Content: contextContent}
if *context != *expectedContext {
t.Errorf("expected %v, got %v", expectedContext, context)
}
}

55
db/db_test.go Normal file
View File

@ -0,0 +1,55 @@
package db
import (
"os"
"testing"
)
func TestDb_Configure(t *testing.T) {
dir := t.TempDir()
db := NewDb(dir)
err := db.Configure()
if err == nil {
t.Fatalf("db is configured, but must not be at empty dir: %v", dir)
}
if db.IsEnvFileExists() {
t.Fatalf("db file exists, but must not be at empty dir: %v", dir)
}
err = db.SaveEnv("")
if err != nil {
t.Fatalf("db can't save env for empty conf.: %v", err)
}
err = db.Configure()
if err != nil {
t.Fatalf("db is not configured, but shall be after save: %v", err)
}
}
func TestDb_LoadEnvFile(t *testing.T) {
dir := t.TempDir()
db := NewDb(dir)
content := "KEY=VALUE\n"
err := os.WriteFile(db.EnvFilePath, []byte(content), 0644)
if err != nil {
t.Fatalf("failed to write .env file: %v", err)
}
err = db.LoadEnvFile()
if err != nil {
t.Errorf("failed to load .env file: %v", err)
}
}
func TestDb_SaveEnv(t *testing.T) {
dir := t.TempDir()
db := NewDb(dir)
content := "KEY=VALUE\n"
err := db.SaveEnv(content)
if err != nil {
t.Errorf("failed to save .env file: %v", err)
}
if _, err := os.Stat(db.EnvFilePath); os.IsNotExist(err) {
t.Errorf("expected .env file to be saved")
}
}

1
db/patterns_test.go Normal file
View File

@ -0,0 +1 @@
package db

38
db/sessions_test.go Normal file
View File

@ -0,0 +1,38 @@
package db
import (
"testing"
"github.com/danielmiessler/fabric/common"
)
func TestSessions_GetOrCreateSession(t *testing.T) {
dir := t.TempDir()
sessions := &Sessions{
Storage: &Storage{Dir: dir, FileExtension: ".json"},
}
sessionName := "testSession"
session, err := sessions.GetOrCreateSession(sessionName)
if err != nil {
t.Fatalf("failed to get or create session: %v", err)
}
if session.Name != sessionName {
t.Errorf("expected session name %v, got %v", sessionName, session.Name)
}
}
func TestSessions_SaveSession(t *testing.T) {
dir := t.TempDir()
sessions := &Sessions{
Storage: &Storage{Dir: dir, FileExtension: ".json"},
}
sessionName := "testSession"
session := &Session{Name: sessionName, Messages: []*common.Message{{Content: "message1"}}}
err := sessions.SaveSession(session)
if err != nil {
t.Fatalf("failed to save session: %v", err)
}
if !sessions.Exists(sessionName) {
t.Errorf("expected session to be saved")
}
}

52
db/storage_test.go Normal file
View File

@ -0,0 +1,52 @@
package db
import (
"testing"
)
func TestStorage_SaveAndLoad(t *testing.T) {
dir := t.TempDir()
storage := &Storage{Dir: dir}
name := "test"
content := []byte("test content")
if err := storage.Save(name, content); err != nil {
t.Fatalf("failed to save content: %v", err)
}
loadedContent, err := storage.Load(name)
if err != nil {
t.Fatalf("failed to load content: %v", err)
}
if string(loadedContent) != string(content) {
t.Errorf("expected %v, got %v", string(content), string(loadedContent))
}
}
func TestStorage_Exists(t *testing.T) {
dir := t.TempDir()
storage := &Storage{Dir: dir}
name := "test"
if storage.Exists(name) {
t.Errorf("expected file to not exist")
}
if err := storage.Save(name, []byte("test content")); err != nil {
t.Fatalf("failed to save content: %v", err)
}
if !storage.Exists(name) {
t.Errorf("expected file to exist")
}
}
func TestStorage_Delete(t *testing.T) {
dir := t.TempDir()
storage := &Storage{Dir: dir}
name := "test"
if err := storage.Save(name, []byte("test content")); err != nil {
t.Fatalf("failed to save content: %v", err)
}
if err := storage.Delete(name); err != nil {
t.Fatalf("failed to delete content: %v", err)
}
if storage.Exists(name) {
t.Errorf("expected file to be deleted")
}
}