test: Add comprehensive test suite for database functionality
- Add unit tests for database operations and optimization - Test external data source loading and caching - Add callsign manager functionality tests - Create test helpers for database testing utilities - Ensure database reliability and performance validation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
0f16748224
commit
5733209251
5 changed files with 955 additions and 0 deletions
268
internal/database/manager_callsign_test.go
Normal file
268
internal/database/manager_callsign_test.go
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCallsignManager_Creation(t *testing.T) {
|
||||
db, cleanup := setupTestDatabase(t)
|
||||
defer cleanup()
|
||||
|
||||
manager := NewCallsignManager(db.GetConnection())
|
||||
if manager == nil {
|
||||
t.Fatal("NewCallsignManager returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallsignManager_ParseCallsign(t *testing.T) {
|
||||
db, cleanup := setupTestDatabase(t)
|
||||
defer cleanup()
|
||||
|
||||
manager := NewCallsignManager(db.GetConnection())
|
||||
|
||||
testCases := []struct {
|
||||
callsign string
|
||||
expectedValid bool
|
||||
expectedAirline string
|
||||
expectedFlight string
|
||||
}{
|
||||
{"UAL123", true, "UAL", "123"},
|
||||
{"BA4567", true, "BA", "4567"},
|
||||
{"AFR89", true, "AFR", "89"},
|
||||
{"N123AB", false, "", ""}, // Aircraft registration, not callsign
|
||||
{"INVALID", false, "", ""}, // No numbers
|
||||
{"123", false, "", ""}, // Only numbers
|
||||
{"A", false, "", ""}, // Too short
|
||||
{"", false, "", ""}, // Empty
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := manager.ParseCallsign(tc.callsign)
|
||||
if result.IsValid != tc.expectedValid {
|
||||
t.Errorf("ParseCallsign(%s): expected valid=%v, got %v",
|
||||
tc.callsign, tc.expectedValid, result.IsValid)
|
||||
}
|
||||
if result.IsValid && result.AirlineCode != tc.expectedAirline {
|
||||
t.Errorf("ParseCallsign(%s): expected airline=%s, got %s",
|
||||
tc.callsign, tc.expectedAirline, result.AirlineCode)
|
||||
}
|
||||
if result.IsValid && result.FlightNumber != tc.expectedFlight {
|
||||
t.Errorf("ParseCallsign(%s): expected flight=%s, got %s",
|
||||
tc.callsign, tc.expectedFlight, result.FlightNumber)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallsignManager_GetCallsignInfo(t *testing.T) {
|
||||
db, cleanup := setupTestDatabase(t)
|
||||
defer cleanup()
|
||||
|
||||
manager := NewCallsignManager(db.GetConnection())
|
||||
|
||||
// Insert test airline data
|
||||
conn := db.GetConnection()
|
||||
_, err := conn.Exec(`
|
||||
INSERT INTO airlines (id, name, alias, iata_code, icao_code, callsign, country, active, data_source)
|
||||
VALUES (1, 'Test Airways', 'Test', 'TA', 'TST', 'TESTAIR', 'United States', 1, 'test')
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to insert test data:", err)
|
||||
}
|
||||
|
||||
// Test valid callsign
|
||||
info, err := manager.GetCallsignInfo("TST123")
|
||||
if err != nil {
|
||||
t.Fatal("GetCallsignInfo failed:", err)
|
||||
}
|
||||
|
||||
if info == nil {
|
||||
t.Fatal("Expected callsign info, got nil")
|
||||
}
|
||||
|
||||
if info.OriginalCallsign != "TST123" {
|
||||
t.Errorf("Expected callsign TST123, got %s", info.OriginalCallsign)
|
||||
}
|
||||
if info.AirlineCode != "TST" {
|
||||
t.Errorf("Expected airline code TST, got %s", info.AirlineCode)
|
||||
}
|
||||
if info.FlightNumber != "123" {
|
||||
t.Errorf("Expected flight number 123, got %s", info.FlightNumber)
|
||||
}
|
||||
if info.AirlineName != "Test Airways" {
|
||||
t.Errorf("Expected airline name 'Test Airways', got %s", info.AirlineName)
|
||||
}
|
||||
if info.AirlineCountry != "United States" {
|
||||
t.Errorf("Expected airline country 'United States', got %s", info.AirlineCountry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallsignManager_GetCallsignInfo_InvalidCallsign(t *testing.T) {
|
||||
db, cleanup := setupTestDatabase(t)
|
||||
defer cleanup()
|
||||
|
||||
manager := NewCallsignManager(db.GetConnection())
|
||||
|
||||
// Test with invalid callsign format
|
||||
info, err := manager.GetCallsignInfo("INVALID")
|
||||
if err != nil {
|
||||
t.Error("GetCallsignInfo should not error on invalid format:", err)
|
||||
}
|
||||
if info == nil {
|
||||
t.Fatal("Expected info structure even for invalid callsign")
|
||||
}
|
||||
if info.IsValid {
|
||||
t.Error("Invalid callsign should not be marked as valid")
|
||||
}
|
||||
|
||||
// Test with unknown airline
|
||||
info, err = manager.GetCallsignInfo("UNK123")
|
||||
if err != nil {
|
||||
t.Error("GetCallsignInfo should not error on unknown airline:", err)
|
||||
}
|
||||
if info == nil {
|
||||
t.Fatal("Expected info structure for unknown airline")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallsignManager_GetCallsignInfo_EmptyCallsign(t *testing.T) {
|
||||
db, cleanup := setupTestDatabase(t)
|
||||
defer cleanup()
|
||||
|
||||
manager := NewCallsignManager(db.GetConnection())
|
||||
|
||||
// Test with empty callsign
|
||||
info, err := manager.GetCallsignInfo("")
|
||||
if err == nil {
|
||||
t.Error("GetCallsignInfo should error on empty callsign")
|
||||
}
|
||||
if info != nil {
|
||||
t.Error("Expected nil info for empty callsign")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallsignManager_ClearExpiredCache(t *testing.T) {
|
||||
db, cleanup := setupTestDatabase(t)
|
||||
defer cleanup()
|
||||
|
||||
manager := NewCallsignManager(db.GetConnection())
|
||||
|
||||
err := manager.ClearExpiredCache()
|
||||
if err != nil {
|
||||
t.Error("ClearExpiredCache should not error:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallsignManager_GetCacheStats(t *testing.T) {
|
||||
db, cleanup := setupTestDatabase(t)
|
||||
defer cleanup()
|
||||
|
||||
manager := NewCallsignManager(db.GetConnection())
|
||||
|
||||
stats, err := manager.GetCacheStats()
|
||||
if err != nil {
|
||||
t.Error("GetCacheStats should not error:", err)
|
||||
}
|
||||
|
||||
if stats == nil {
|
||||
t.Error("Expected cache stats, got nil")
|
||||
}
|
||||
|
||||
t.Logf("Cache stats: %+v", stats)
|
||||
}
|
||||
|
||||
func TestCallsignManager_SearchAirlines(t *testing.T) {
|
||||
db, cleanup := setupTestDatabase(t)
|
||||
defer cleanup()
|
||||
|
||||
manager := NewCallsignManager(db.GetConnection())
|
||||
|
||||
// Insert test airline data
|
||||
conn := db.GetConnection()
|
||||
_, err := conn.Exec(`
|
||||
INSERT INTO airlines (id, name, alias, iata_code, icao_code, callsign, country, active, data_source)
|
||||
VALUES (1, 'Test Airways', 'Test', 'TA', 'TST', 'TESTAIR', 'United States', 1, 'test'),
|
||||
(2, 'Another Airline', 'Another', 'AA', 'ANO', 'ANOTHER', 'Canada', 1, 'test')
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to insert test data:", err)
|
||||
}
|
||||
|
||||
// Search for airlines
|
||||
airlines, err := manager.SearchAirlines("Test")
|
||||
if err != nil {
|
||||
t.Fatal("SearchAirlines failed:", err)
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, airline := range airlines {
|
||||
if airline.Name == "Test Airways" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("Expected to find Test Airways in search results")
|
||||
}
|
||||
|
||||
t.Logf("Found %d airlines matching 'Test'", len(airlines))
|
||||
}
|
||||
|
||||
func TestCallsignManager_GetAirlinesByCountry(t *testing.T) {
|
||||
db, cleanup := setupTestDatabase(t)
|
||||
defer cleanup()
|
||||
|
||||
manager := NewCallsignManager(db.GetConnection())
|
||||
|
||||
// Insert test airline data
|
||||
conn := db.GetConnection()
|
||||
_, err := conn.Exec(`
|
||||
INSERT INTO airlines (id, name, alias, iata_code, icao_code, callsign, country, active, data_source)
|
||||
VALUES (1, 'US Airways', 'US', 'US', 'USA', 'USAIR', 'United States', 1, 'test'),
|
||||
(2, 'Canada Air', 'CA', 'CA', 'CAN', 'CANAIR', 'Canada', 1, 'test')
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal("Failed to insert test data:", err)
|
||||
}
|
||||
|
||||
// Get airlines by country
|
||||
airlines, err := manager.GetAirlinesByCountry("United States")
|
||||
if err != nil {
|
||||
t.Fatal("GetAirlinesByCountry failed:", err)
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, airline := range airlines {
|
||||
if airline.Name == "US Airways" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("Expected to find US Airways for United States")
|
||||
}
|
||||
|
||||
t.Logf("Found %d airlines in United States", len(airlines))
|
||||
}
|
||||
|
||||
func TestCallsignParseResult_Struct(t *testing.T) {
|
||||
result := &CallsignParseResult{
|
||||
OriginalCallsign: "UAL123",
|
||||
AirlineCode: "UAL",
|
||||
FlightNumber: "123",
|
||||
IsValid: true,
|
||||
}
|
||||
|
||||
// Test that all fields are accessible
|
||||
if result.OriginalCallsign != "UAL123" {
|
||||
t.Error("OriginalCallsign field not preserved")
|
||||
}
|
||||
if result.AirlineCode != "UAL" {
|
||||
t.Error("AirlineCode field not preserved")
|
||||
}
|
||||
if result.FlightNumber != "123" {
|
||||
t.Error("FlightNumber field not preserved")
|
||||
}
|
||||
if !result.IsValid {
|
||||
t.Error("IsValid field not preserved")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue