2023-06-30 16:51:06 +00:00
|
|
|
package db
|
2023-06-30 12:41:05 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-28 07:07:28 +00:00
|
|
|
"fmt"
|
2023-06-30 12:41:05 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2023-07-03 18:56:42 +00:00
|
|
|
"time"
|
2023-06-30 12:41:05 +00:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/kschamplin/gotelem/skylab"
|
|
|
|
)
|
|
|
|
|
|
|
|
// helper func to get a random bus event with random data.
|
|
|
|
func GetRandomBusEvent() skylab.BusEvent {
|
2023-07-03 18:56:42 +00:00
|
|
|
data := skylab.WsrVelocity{
|
|
|
|
MotorVelocity: 1.0,
|
|
|
|
VehicleVelocity: 4.0,
|
|
|
|
}
|
|
|
|
ev := skylab.BusEvent{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Data: &data,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ev
|
2023-06-30 12:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTelemDb(t *testing.T) {
|
|
|
|
|
|
|
|
var tdb *TelemDb
|
|
|
|
|
|
|
|
t.Run("test opening database", func(t *testing.T) {
|
|
|
|
var err error
|
2024-02-28 07:07:28 +00:00
|
|
|
// we use the underlying raw database to avoid the options.
|
|
|
|
tdb, err = openRawDb("file::memory:?cache=shared")
|
2023-06-30 12:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not open db: %v", err)
|
|
|
|
}
|
|
|
|
tdb.db.Ping()
|
2024-02-28 07:07:28 +00:00
|
|
|
res, _ := tdb.db.Query("SELECT name FROM sqlite_master WHERE type='table'")
|
|
|
|
|
|
|
|
var table string
|
|
|
|
|
|
|
|
for res.Next() {
|
|
|
|
res.Scan(&table)
|
|
|
|
fmt.Println(table)
|
|
|
|
}
|
2023-06-30 12:41:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test inserting bus event", func(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
events []skylab.BusEvent
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "add no packet",
|
|
|
|
args: args{
|
|
|
|
events: []skylab.BusEvent{},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add single packet",
|
|
|
|
args: args{
|
2023-07-04 05:50:43 +00:00
|
|
|
events: []skylab.BusEvent{GetRandomBusEvent()},
|
2023-06-30 12:41:05 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-02-28 07:07:28 +00:00
|
|
|
if _, err := tdb.AddEvents(tt.args.events...); (err != nil) != tt.wantErr {
|
2023-06-30 12:41:05 +00:00
|
|
|
t.Errorf("TelemDb.AddEvents() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
type fields struct {
|
|
|
|
db *sqlx.DB
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
limit int
|
|
|
|
where []QueryFrag
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantEvents []skylab.BusEvent
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
// TODO: Add test cases.
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tdb := &TelemDb{
|
|
|
|
db: tt.fields.db,
|
|
|
|
}
|
|
|
|
gotEvents, err := tdb.GetEvents(tt.args.limit, tt.args.where...)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("TelemDb.GetEvents() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotEvents, tt.wantEvents) {
|
|
|
|
t.Errorf("TelemDb.GetEvents() = %v, want %v", gotEvents, tt.wantEvents)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|