p2p-llm / db /database_test.go
arpinfidel's picture
temp
48511d8
package db
import (
"path/filepath"
"testing"
"time"
"github.com/arpinfidel/p2p-llm/config"
)
func setupTestDB(t *testing.T) (*SQLiteDB, string) {
// Create a temporary database file
tempDir := t.TempDir()
dbPath := filepath.Join(tempDir, "test.db")
// Create test config
cfg := &config.Config{
DBPath: dbPath,
}
// Initialize database
db, err := NewSQLiteDB(cfg)
if err != nil {
t.Fatalf("Failed to create test database: %v", err)
}
if err := db.Init(); err != nil {
t.Fatalf("Failed to initialize test database: %v", err)
}
// Create tables
if err := CreateTables(db); err != nil {
t.Fatalf("Failed to create tables: %v", err)
}
return db, dbPath
}
func TestAPIKeyOperations(t *testing.T) {
db, _ := setupTestDB(t)
defer db.Close()
repo := NewSQLiteAPIKeyRepository(db)
// Test CreateKey
expiresAt := time.Now().Add(24 * time.Hour)
err := repo.CreateKey("testhash", "testkey", &expiresAt)
if err != nil {
t.Errorf("CreateKey failed: %v", err)
}
// Test GetActiveKeyHash
hash, err := repo.GetActiveKeyHash()
if err != nil {
t.Errorf("GetActiveKeyHash failed: %v", err)
}
if hash != "testhash" {
t.Errorf("Expected hash 'testhash', got '%s'", hash)
}
// Test DeactivateKey
// Note: This would need to know the ID of the created key
// For a complete test, we'd need to query the ID first
// For now, we'll just verify the basic operations work
}