database: create shared database package
Extract common database functionality from api/ntpdb and monitor/ntpdb into shared common/database package: - Dynamic connector pattern with configuration loading - Configurable connection pool management (API: 25/10, Monitor: 10/5) - Optional Prometheus metrics integration - Generic transaction helpers with proper error handling - Unified interfaces compatible with SQLC-generated code Foundation for migration to eliminate ~200 lines of duplicate code.
This commit is contained in:
81
database/config_test.go
Normal file
81
database/config_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
func TestDefaultConfigOptions(t *testing.T) {
|
||||
opts := DefaultConfigOptions()
|
||||
|
||||
// Verify expected defaults for API package
|
||||
if opts.MaxOpenConns != 25 {
|
||||
t.Errorf("Expected MaxOpenConns=25, got %d", opts.MaxOpenConns)
|
||||
}
|
||||
if opts.MaxIdleConns != 10 {
|
||||
t.Errorf("Expected MaxIdleConns=10, got %d", opts.MaxIdleConns)
|
||||
}
|
||||
if opts.ConnMaxLifetime != 3*time.Minute {
|
||||
t.Errorf("Expected ConnMaxLifetime=3m, got %v", opts.ConnMaxLifetime)
|
||||
}
|
||||
if !opts.EnablePoolMonitoring {
|
||||
t.Error("Expected EnablePoolMonitoring=true")
|
||||
}
|
||||
if opts.PrometheusRegisterer != prometheus.DefaultRegisterer {
|
||||
t.Error("Expected PrometheusRegisterer to be DefaultRegisterer")
|
||||
}
|
||||
if len(opts.ConfigFiles) == 0 {
|
||||
t.Error("Expected ConfigFiles to be non-empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonitorConfigOptions(t *testing.T) {
|
||||
opts := MonitorConfigOptions()
|
||||
|
||||
// Verify expected defaults for Monitor package
|
||||
if opts.MaxOpenConns != 10 {
|
||||
t.Errorf("Expected MaxOpenConns=10, got %d", opts.MaxOpenConns)
|
||||
}
|
||||
if opts.MaxIdleConns != 5 {
|
||||
t.Errorf("Expected MaxIdleConns=5, got %d", opts.MaxIdleConns)
|
||||
}
|
||||
if opts.ConnMaxLifetime != 3*time.Minute {
|
||||
t.Errorf("Expected ConnMaxLifetime=3m, got %v", opts.ConnMaxLifetime)
|
||||
}
|
||||
if opts.EnablePoolMonitoring {
|
||||
t.Error("Expected EnablePoolMonitoring=false")
|
||||
}
|
||||
if opts.PrometheusRegisterer != nil {
|
||||
t.Error("Expected PrometheusRegisterer to be nil")
|
||||
}
|
||||
if len(opts.ConfigFiles) == 0 {
|
||||
t.Error("Expected ConfigFiles to be non-empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigStructures(t *testing.T) {
|
||||
// Test that configuration structures can be created and populated
|
||||
config := Config{
|
||||
MySQL: DBConfig{
|
||||
DSN: "user:pass@tcp(localhost:3306)/dbname",
|
||||
User: "testuser",
|
||||
Pass: "testpass",
|
||||
DBName: "testdb",
|
||||
},
|
||||
}
|
||||
|
||||
if config.MySQL.DSN == "" {
|
||||
t.Error("Expected DSN to be set")
|
||||
}
|
||||
if config.MySQL.User != "testuser" {
|
||||
t.Errorf("Expected User='testuser', got '%s'", config.MySQL.User)
|
||||
}
|
||||
if config.MySQL.Pass != "testpass" {
|
||||
t.Errorf("Expected Pass='testpass', got '%s'", config.MySQL.Pass)
|
||||
}
|
||||
if config.MySQL.DBName != "testdb" {
|
||||
t.Errorf("Expected DBName='testdb', got '%s'", config.MySQL.DBName)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user