Files
browser/plugins/examples/scum-server-plugin/companion/sqlite_source_test.go
T

81 lines
3.2 KiB
Go

package companion
import (
"context"
"database/sql"
"path/filepath"
"testing"
)
func TestSCUMSQLiteSourceReadsRawCoordinates(t *testing.T) {
databaseFile := filepath.Join(t.TempDir(), "SCUM.db")
db, err := sql.Open("sqlite", databaseFile)
if err != nil {
t.Fatalf("open sqlite fixture: %v", err)
}
defer db.Close()
for _, statement := range []string{
`CREATE TABLE user (id TEXT PRIMARY KEY)`,
`CREATE TABLE user_profile (id INTEGER PRIMARY KEY, user_id TEXT NOT NULL, prisoner_id INTEGER NOT NULL)`,
`CREATE TABLE prisoner (id INTEGER PRIMARY KEY, last_save_time INTEGER NOT NULL)`,
`CREATE TABLE prisoner_entity (prisoner_id INTEGER NOT NULL, entity_id INTEGER NOT NULL)`,
`CREATE TABLE entity (id INTEGER PRIMARY KEY, class TEXT, location_x REAL NOT NULL, location_y REAL NOT NULL, location_z REAL NOT NULL)`,
`CREATE TABLE vehicle_spawner (vehicle_entity_id INTEGER PRIMARY KEY, vehicle_alias TEXT, vehicle_last_access_time INTEGER NOT NULL, is_vehicle_functional INTEGER NOT NULL)`,
`INSERT INTO user (id) VALUES ('76561198000000001')`,
`INSERT INTO prisoner (id, last_save_time) VALUES (2001, 1788146999)`,
`INSERT INTO user_profile (id, user_id, prisoner_id) VALUES (1001, '76561198000000001', 2001)`,
`INSERT INTO entity (id, class, location_x, location_y, location_z) VALUES (3001, 'BP_Prisoner_C', 123.25, -456.5, 7.75)`,
`INSERT INTO prisoner_entity (prisoner_id, entity_id) VALUES (2001, 3001)`,
`INSERT INTO entity (id, class, location_x, location_y, location_z) VALUES (4001, 'BPC_Laika_C', -10.5, 20.25, 0)`,
`INSERT INTO vehicle_spawner (vehicle_entity_id, vehicle_alias, vehicle_last_access_time, is_vehicle_functional) VALUES (4001, 'Laika', 1788146988, 1)`,
} {
if _, err := db.Exec(statement); err != nil {
t.Fatalf("exec sqlite fixture statement %q: %v", statement, err)
}
}
source, err := NewSCUMSQLiteSource(db)
if err != nil {
t.Fatalf("create sqlite source: %v", err)
}
positions, err := source.ReadPositionRows(context.Background(), 10)
if err != nil {
t.Fatalf("read positions: %v", err)
}
vehicles, err := source.ReadVehicleRows(context.Background(), 10)
if err != nil {
t.Fatalf("read vehicles: %v", err)
}
player := rowByText(t, positions, "subjectType", "player")
vehiclePosition := rowByText(t, positions, "subjectType", "vehicle")
vehicle := rowByText(t, vehicles, "vehicleId", "4001")
assertNumber(t, player["x"], 123.25)
assertNumber(t, player["y"], -456.5)
assertNumber(t, player["z"], 7.75)
assertNumber(t, vehiclePosition["x"], -10.5)
assertNumber(t, vehiclePosition["y"], 20.25)
assertNumber(t, vehicle["x"], -10.5)
assertNumber(t, vehicle["y"], 20.25)
if vehicle["className"] != "BPC_Laika_C" || vehicle["label"] != "Laika" {
t.Fatalf("vehicle metadata changed: %+v", vehicle)
}
}
func rowByText(t *testing.T, rows []map[string]any, key string, value string) map[string]any {
t.Helper()
for _, row := range rows {
if textFromRow(row[key]) == value {
return row
}
}
t.Fatalf("missing row where %s=%s: %+v", key, value, rows)
return nil
}
func assertNumber(t *testing.T, value any, expected float64) {
t.Helper()
actual, ok := numberFromRow(value)
if !ok || actual != expected {
t.Fatalf("number = %v, want %v", value, expected)
}
}