- Run 'make format' to ensure all Go code follows standard formatting - Maintains consistent code style across the entire codebase - No functional changes, only whitespace and formatting improvements
177 lines
4.6 KiB
Go
177 lines
4.6 KiB
Go
package database
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDataLoader_Creation(t *testing.T) {
|
|
db, cleanup := setupTestDatabase(t)
|
|
defer cleanup()
|
|
|
|
loader := NewDataLoader(db.GetConnection())
|
|
if loader == nil {
|
|
t.Fatal("NewDataLoader returned nil")
|
|
}
|
|
}
|
|
|
|
func TestDataLoader_LoadOpenFlightsAirlines(t *testing.T) {
|
|
db, cleanup := setupTestDatabase(t)
|
|
defer cleanup()
|
|
|
|
loader := NewDataLoader(db.GetConnection())
|
|
|
|
// Create a test data source for OpenFlights Airlines
|
|
source := DataSource{
|
|
Name: "OpenFlights Airlines Test",
|
|
License: "ODbL 1.0",
|
|
URL: "https://raw.githubusercontent.com/jpatokal/openflights/master/data/airlines.dat",
|
|
Format: "openflights",
|
|
Version: "2024-test",
|
|
}
|
|
|
|
result, err := loader.LoadDataSource(source)
|
|
if err != nil {
|
|
// Network issues in tests are acceptable
|
|
if strings.Contains(err.Error(), "connection") ||
|
|
strings.Contains(err.Error(), "timeout") ||
|
|
strings.Contains(err.Error(), "no such host") {
|
|
t.Skipf("Skipping network test due to connectivity issue: %v", err)
|
|
}
|
|
t.Fatal("LoadDataSource failed:", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Expected load result, got nil")
|
|
}
|
|
|
|
t.Logf("Loaded airlines: Total=%d, New=%d, Errors=%d, Duration=%v",
|
|
result.RecordsTotal, result.RecordsNew, result.RecordsError, result.Duration)
|
|
|
|
// Verify some data was processed
|
|
if result.RecordsTotal == 0 {
|
|
t.Error("No records were processed")
|
|
}
|
|
}
|
|
|
|
func TestDataLoader_LoadOurAirports(t *testing.T) {
|
|
db, cleanup := setupTestDatabase(t)
|
|
defer cleanup()
|
|
|
|
loader := NewDataLoader(db.GetConnection())
|
|
|
|
// Create a test data source for OurAirports
|
|
source := DataSource{
|
|
Name: "OurAirports Test",
|
|
License: "CC0 1.0",
|
|
URL: "https://davidmegginson.github.io/ourairports-data/airports.csv",
|
|
Format: "ourairports",
|
|
Version: "2024-test",
|
|
}
|
|
|
|
result, err := loader.LoadDataSource(source)
|
|
if err != nil {
|
|
// Network issues in tests are acceptable
|
|
if strings.Contains(err.Error(), "connection") ||
|
|
strings.Contains(err.Error(), "timeout") ||
|
|
strings.Contains(err.Error(), "no such host") {
|
|
t.Skipf("Skipping network test due to connectivity issue: %v", err)
|
|
}
|
|
t.Fatal("LoadDataSource failed:", err)
|
|
}
|
|
|
|
if result != nil {
|
|
t.Logf("Loaded airports: Total=%d, New=%d, Errors=%d, Duration=%v",
|
|
result.RecordsTotal, result.RecordsNew, result.RecordsError, result.Duration)
|
|
}
|
|
}
|
|
|
|
func TestDataLoader_GetLoadedDataSources(t *testing.T) {
|
|
db, cleanup := setupTestDatabase(t)
|
|
defer cleanup()
|
|
|
|
loader := NewDataLoader(db.GetConnection())
|
|
|
|
sources, err := loader.GetLoadedDataSources()
|
|
if err != nil {
|
|
t.Fatal("GetLoadedDataSources failed:", err)
|
|
}
|
|
|
|
// Initially should be empty or minimal
|
|
t.Logf("Found %d loaded data sources", len(sources))
|
|
}
|
|
|
|
func TestDataLoader_ClearDataSource(t *testing.T) {
|
|
db, cleanup := setupTestDatabase(t)
|
|
defer cleanup()
|
|
|
|
loader := NewDataLoader(db.GetConnection())
|
|
|
|
// Test clearing a non-existent source (should not error)
|
|
err := loader.ClearDataSource("nonexistent")
|
|
if err != nil {
|
|
t.Error("ClearDataSource should not error on nonexistent source:", err)
|
|
}
|
|
}
|
|
|
|
func TestDataSource_Struct(t *testing.T) {
|
|
source := DataSource{
|
|
Name: "Test Source",
|
|
License: "Test License",
|
|
URL: "https://example.com/data.csv",
|
|
RequiresConsent: false,
|
|
UserAcceptedLicense: true,
|
|
Format: "csv",
|
|
Version: "1.0",
|
|
}
|
|
|
|
// Test that all fields are accessible
|
|
if source.Name != "Test Source" {
|
|
t.Error("Name field not preserved")
|
|
}
|
|
if source.License != "Test License" {
|
|
t.Error("License field not preserved")
|
|
}
|
|
if source.URL != "https://example.com/data.csv" {
|
|
t.Error("URL field not preserved")
|
|
}
|
|
if source.RequiresConsent != false {
|
|
t.Error("RequiresConsent field not preserved")
|
|
}
|
|
if source.UserAcceptedLicense != true {
|
|
t.Error("UserAcceptedLicense field not preserved")
|
|
}
|
|
if source.Format != "csv" {
|
|
t.Error("Format field not preserved")
|
|
}
|
|
if source.Version != "1.0" {
|
|
t.Error("Version field not preserved")
|
|
}
|
|
}
|
|
|
|
func TestLoadResult_Struct(t *testing.T) {
|
|
result := LoadResult{
|
|
Source: "Test Source",
|
|
RecordsTotal: 100,
|
|
RecordsNew: 80,
|
|
RecordsError: 5,
|
|
Errors: []string{"error1", "error2"},
|
|
}
|
|
|
|
// Test that all fields are accessible
|
|
if result.Source != "Test Source" {
|
|
t.Error("Source field not preserved")
|
|
}
|
|
if result.RecordsTotal != 100 {
|
|
t.Error("RecordsTotal field not preserved")
|
|
}
|
|
if result.RecordsNew != 80 {
|
|
t.Error("RecordsNew field not preserved")
|
|
}
|
|
if result.RecordsError != 5 {
|
|
t.Error("RecordsError field not preserved")
|
|
}
|
|
if len(result.Errors) != 2 {
|
|
t.Error("Errors field not preserved")
|
|
}
|
|
}
|