- Introduced .editorconfig for consistent coding styles across the project. - Added .golangci.yml for Go linting configuration. - Updated AGENTS.md to clarify project structure and components. - Enhanced CONTRIBUTING.md with Makefile usage for common tasks. - Updated Dockerfiles to use Go 1.24 and improved build instructions. - Refined README.md and deployment documentation for clarity. - Added testing documentation in testing.md for backend and frontend tests. - Introduced Makefile for streamlined development commands and tasks.
94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
package app_test
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/andyleap/hnh-map/internal/app"
|
|
"github.com/andyleap/hnh-map/internal/app/store"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
func newTestDB(t *testing.T) *bbolt.DB {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
db, err := bbolt.Open(filepath.Join(dir, "test.db"), 0600, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { db.Close() })
|
|
return db
|
|
}
|
|
|
|
func TestRunMigrations_FreshDB(t *testing.T) {
|
|
db := newTestDB(t)
|
|
if err := app.RunMigrations(db); err != nil {
|
|
t.Fatalf("migrations failed on fresh DB: %v", err)
|
|
}
|
|
|
|
db.View(func(tx *bbolt.Tx) error {
|
|
b := tx.Bucket(store.BucketConfig)
|
|
if b == nil {
|
|
t.Fatal("expected config bucket after migrations")
|
|
}
|
|
v := b.Get([]byte("version"))
|
|
if v == nil {
|
|
t.Fatal("expected version key in config")
|
|
}
|
|
title := b.Get([]byte("title"))
|
|
if title == nil || string(title) != "HnH Automapper Server" {
|
|
t.Fatalf("expected default title, got %s", title)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if tx, _ := db.Begin(false); tx != nil {
|
|
if tx.Bucket(store.BucketOAuthStates) == nil {
|
|
t.Fatal("expected oauth_states bucket after migrations")
|
|
}
|
|
tx.Rollback()
|
|
}
|
|
}
|
|
|
|
func TestRunMigrations_Idempotent(t *testing.T) {
|
|
db := newTestDB(t)
|
|
|
|
if err := app.RunMigrations(db); err != nil {
|
|
t.Fatalf("first run failed: %v", err)
|
|
}
|
|
|
|
if err := app.RunMigrations(db); err != nil {
|
|
t.Fatalf("second run failed: %v", err)
|
|
}
|
|
|
|
db.View(func(tx *bbolt.Tx) error {
|
|
b := tx.Bucket(store.BucketConfig)
|
|
if b == nil {
|
|
t.Fatal("expected config bucket")
|
|
}
|
|
v := b.Get([]byte("version"))
|
|
if v == nil {
|
|
t.Fatal("expected version key")
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func TestRunMigrations_SetsVersion(t *testing.T) {
|
|
db := newTestDB(t)
|
|
if err := app.RunMigrations(db); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var version string
|
|
db.View(func(tx *bbolt.Tx) error {
|
|
b := tx.Bucket(store.BucketConfig)
|
|
version = string(b.Get([]byte("version")))
|
|
return nil
|
|
})
|
|
|
|
if version == "" || version == "0" {
|
|
t.Fatalf("expected non-zero version, got %q", version)
|
|
}
|
|
}
|