ot-browser/cmd/ot-browser/storage_test.go
Felix Niederwanger d534856fb6
Introduce storage module
Add a storage module to store all received geopoints into a sqlite3
database.
2024-02-17 15:10:40 +01:00

64 lines
1.2 KiB
Go

package main
import (
"fmt"
"os"
"testing"
"time"
)
const DATABASE = ":memory:"
func TestMain(m *testing.M) {
if _, err := os.Stat(DATABASE); err == nil {
fmt.Fprintf(os.Stderr, "error: test database exists already\n")
os.Exit(1)
}
// Run tests
ret := m.Run()
if DATABASE != ":memory:" {
os.Remove(DATABASE) // Ignore errors
}
os.Exit(ret)
}
func TestLocation(t *testing.T) {
var ref Location
stor, err := CreateDatabase(DATABASE)
if err != nil {
panic(err)
}
device1 := "test1"
t0 := time.Now().Unix()
for i := 0; i < 360; i++ {
ref.Timestamp = t0 + int64(i) // Never let it be zerom otherwise the check below will fail!
ref.Lon = float32(i - 180)
ref.Lat = float32((-90 + i) % 180)
ref.Alt = float32(-100 + i*2)
ref.Acc = float32(i % 2 * 10)
ref.Velocity = float32(i / 1000.0)
ref.Bearing = 0
if err := stor.InsertLocation(device1, ref); err != nil {
t.Errorf("InsertLocation failed: %s", err)
return
}
loc, err := stor.GetLastLocation(device1)
if err != nil {
t.Errorf("GetLastLocation failed: %s", err)
return
}
if loc.Timestamp == 0 {
t.Error("GetLastLocation returns an empty result", err)
return
}
if loc != ref {
t.Errorf("Fetched location doesn't match reference")
return
}
}
}