122 lines
2.9 KiB
Go
122 lines
2.9 KiB
Go
package database
|
|
|
|
import (
|
|
"testing"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
func TestNewMySQL(t *testing.T) {
|
|
// Skip this test if no MySQL test database is available
|
|
t.Skip("Skipping MySQL test - requires MySQL test database")
|
|
|
|
// Example test for when MySQL is available:
|
|
// db, err := NewMySQL("test_user:test_pass@tcp(localhost:3306)/test_db")
|
|
// if err != nil {
|
|
// t.Fatalf("Failed to create MySQL database: %v", err)
|
|
// }
|
|
// defer db.Close()
|
|
//
|
|
// // Test database type
|
|
// if db.GetType() != MySQL {
|
|
// t.Errorf("Expected MySQL database type, got %v", db.GetType())
|
|
// }
|
|
}
|
|
|
|
func TestConfigValidation(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
config Config
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "valid_mysql_config",
|
|
config: Config{
|
|
DSN: "user:password@tcp(localhost:3306)/database",
|
|
},
|
|
wantErr: false, // Will fail without actual MySQL, but config is valid
|
|
},
|
|
{
|
|
name: "empty_dsn",
|
|
config: Config{
|
|
DSN: "",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
db, _ := New(tt.config)
|
|
// We expect connection errors since we don't have a test MySQL
|
|
// but we can test that the configuration is handled properly
|
|
if db != nil {
|
|
db.Close()
|
|
}
|
|
// For now, just ensure no panics occur
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDatabaseTypeMethods(t *testing.T) {
|
|
// Test with mock config (will fail to connect but won't panic)
|
|
config := Config{
|
|
DSN: "test:test@tcp(localhost:3306)/test",
|
|
}
|
|
|
|
db, err := New(config)
|
|
if err != nil {
|
|
// Expected - no actual MySQL server
|
|
t.Logf("Expected connection error: %v", err)
|
|
return
|
|
}
|
|
defer db.Close()
|
|
|
|
if db.GetType() != MySQL {
|
|
t.Errorf("Expected MySQL type, got %v", db.GetType())
|
|
}
|
|
}
|
|
|
|
func TestDatabaseMethods(t *testing.T) {
|
|
// Skip actual database tests without MySQL
|
|
t.Skip("Skipping database method tests - requires MySQL test database")
|
|
|
|
// Example tests for when MySQL is available:
|
|
// db, err := NewMySQL("test_user:test_pass@tcp(localhost:3306)/test_db")
|
|
// if err != nil {
|
|
// t.Fatalf("Failed to create database: %v", err)
|
|
// }
|
|
// defer db.Close()
|
|
//
|
|
// // Test basic operations
|
|
// _, err = db.Exec("CREATE TEMPORARY TABLE test (id INT PRIMARY KEY, name VARCHAR(255))")
|
|
// if err != nil {
|
|
// t.Fatalf("Failed to create test table: %v", err)
|
|
// }
|
|
//
|
|
// // Test insert
|
|
// result, err := db.Exec("INSERT INTO test (id, name) VALUES (?, ?)", 1, "test_value")
|
|
// if err != nil {
|
|
// t.Fatalf("Failed to insert test data: %v", err)
|
|
// }
|
|
//
|
|
// // Test query
|
|
// rows, err := db.Query("SELECT name FROM test WHERE id = ?", 1)
|
|
// if err != nil {
|
|
// t.Fatalf("Failed to query test data: %v", err)
|
|
// }
|
|
// defer rows.Close()
|
|
//
|
|
// if !rows.Next() {
|
|
// t.Fatal("No rows returned from query")
|
|
// }
|
|
//
|
|
// var name string
|
|
// if err := rows.Scan(&name); err != nil {
|
|
// t.Fatalf("Failed to scan result: %v", err)
|
|
// }
|
|
//
|
|
// if name != "test_value" {
|
|
// t.Errorf("Expected 'test_value', got '%s'", name)
|
|
// }
|
|
} |