|
| 1 | +package stats |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + _ "github.com/ncruces/go-sqlite3/driver" |
| 10 | + _ "github.com/ncruces/go-sqlite3/embed" |
| 11 | +) |
| 12 | + |
| 13 | +func TestOpenCreatesStatisticsIndexes(t *testing.T) { |
| 14 | + path := filepath.Join(t.TempDir(), "stats.db") |
| 15 | + legacy, err := sql.Open("sqlite3", path) |
| 16 | + if err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | + _, err = legacy.Exec(`CREATE TABLE listener_sessions ( |
| 20 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 21 | + station TEXT NOT NULL, |
| 22 | + country TEXT NOT NULL DEFAULT '', |
| 23 | + country_code TEXT NOT NULL DEFAULT '', |
| 24 | + city TEXT NOT NULL DEFAULT '', |
| 25 | + latitude REAL NOT NULL DEFAULT 0, |
| 26 | + longitude REAL NOT NULL DEFAULT 0, |
| 27 | + connected_at TEXT NOT NULL, |
| 28 | + disconnected_at TEXT NOT NULL, |
| 29 | + duration_seconds INTEGER NOT NULL |
| 30 | + )`) |
| 31 | + if err != nil { |
| 32 | + legacy.Close() |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + if _, err := legacy.Exec(`INSERT INTO listener_sessions (station, connected_at, disconnected_at, duration_seconds) VALUES ('lofi', '2026-01-01T00:00:00Z', '2026-01-01T01:00:00Z', 3600)`); err != nil { |
| 36 | + legacy.Close() |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + if err := legacy.Close(); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + |
| 43 | + db, err := Open(path) |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + defer db.Close() |
| 48 | + |
| 49 | + rows, err := db.db.Query(`SELECT name FROM sqlite_master WHERE type = 'index' ORDER BY name`) |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + defer rows.Close() |
| 54 | + |
| 55 | + want := []string{ |
| 56 | + "idx_listener_sessions_station_city", |
| 57 | + "idx_listener_sessions_station_connected_at", |
| 58 | + "idx_listener_sessions_station_country", |
| 59 | + } |
| 60 | + for _, name := range want { |
| 61 | + if !rows.Next() { |
| 62 | + t.Fatalf("missing index %q", name) |
| 63 | + } |
| 64 | + var got string |
| 65 | + if err := rows.Scan(&got); err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + if got != name { |
| 69 | + t.Errorf("index = %q, want %q", got, name) |
| 70 | + } |
| 71 | + } |
| 72 | + if rows.Next() { |
| 73 | + t.Error("found unexpected index") |
| 74 | + } |
| 75 | + if err := rows.Err(); err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + var count int |
| 79 | + if err := db.db.QueryRow(`SELECT COUNT(*) FROM listener_sessions`).Scan(&count); err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + if count != 1 { |
| 83 | + t.Errorf("session count = %d, want 1", count) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestStationStats(t *testing.T) { |
| 88 | + db, err := Open(filepath.Join(t.TempDir(), "stats.db")) |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + defer db.Close() |
| 93 | + |
| 94 | + now := time.Now().UTC() |
| 95 | + for _, session := range []Session{ |
| 96 | + { |
| 97 | + Station: "lofi", Country: "Norway", CountryCode: "NO", City: "Oslo", |
| 98 | + ConnectedAt: now.Add(-time.Hour), DisconnectedAt: now, DurationSeconds: 3600, |
| 99 | + }, |
| 100 | + { |
| 101 | + Station: "lofi", Country: "Norway", CountryCode: "NO", City: "Oslo", |
| 102 | + ConnectedAt: now.Add(-2 * time.Hour), DisconnectedAt: now.Add(-time.Hour), DurationSeconds: 1800, |
| 103 | + }, |
| 104 | + { |
| 105 | + Station: "lofi", ConnectedAt: now.Add(-48 * time.Hour), DisconnectedAt: now.Add(-47 * time.Hour), DurationSeconds: 600, |
| 106 | + }, |
| 107 | + { |
| 108 | + Station: "old", Country: "Sweden", CountryCode: "SE", City: "Stockholm", |
| 109 | + ConnectedAt: now.Add(-time.Hour), DisconnectedAt: now, DurationSeconds: 3600, |
| 110 | + }, |
| 111 | + } { |
| 112 | + if err := db.Record(session); err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + got, err := db.StationStats("lofi") |
| 118 | + if err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + if got.TotalSessions != 3 { |
| 122 | + t.Errorf("TotalSessions = %d, want 3", got.TotalSessions) |
| 123 | + } |
| 124 | + if got.TotalListenHours != 1.7 { |
| 125 | + t.Errorf("TotalListenHours = %v, want 1.7", got.TotalListenHours) |
| 126 | + } |
| 127 | + if len(got.TopCountries) != 1 || got.TopCountries[0].CountryCode != "NO" || got.TopCountries[0].Sessions != 2 { |
| 128 | + t.Errorf("TopCountries = %#v, want Norway with 2 sessions", got.TopCountries) |
| 129 | + } |
| 130 | + if len(got.TopCities) != 1 || got.TopCities[0].City != "Oslo" || got.TopCities[0].Sessions != 2 { |
| 131 | + t.Errorf("TopCities = %#v, want Oslo with 2 sessions", got.TopCities) |
| 132 | + } |
| 133 | + if len(got.Daily) != 2 { |
| 134 | + t.Errorf("Daily = %#v, want two days", got.Daily) |
| 135 | + } |
| 136 | + |
| 137 | + all, err := db.AllStats([]string{"lofi"}) |
| 138 | + if err != nil { |
| 139 | + t.Fatal(err) |
| 140 | + } |
| 141 | + if len(all) != 1 || all["lofi"].TotalSessions != 3 { |
| 142 | + t.Errorf("AllStats() = %#v, want lofi only", all) |
| 143 | + } |
| 144 | +} |
0 commit comments