Skip to content

Commit

Permalink
add test for ts serialization in db
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-m committed Nov 10, 2023
1 parent b2b517a commit cbbca44
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func buildDSN(path string, readOnly bool) string {
return path[:len(path)-1]
}

// Open creates a new SQLite database, opens an existing one.
// Initializes with the schema if new.
// Open creates a new SQLite database or opens an existing one.
// Will run the schema/migration script.
func Open(path string) (*sqlx.DB, error) {
db, err := sqlx.Open(driver, buildDSN(path, false))
if err != nil {
Expand Down
58 changes: 58 additions & 0 deletions internal/pkg/db/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,64 @@ func Test_Train_Queries(t *testing.T) {
assert.True(t, ok)
}

func mustParseTime(s string) time.Time {
t, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
panic(err)
}
return t
}

var (
t0 = mustParseTime("2023-06-10T16:20:58.805488032+02:00")
t1 = mustParseTime("2023-06-10T16:21:05.982318734+02:00")
t2 = mustParseTime("2023-11-10T12:57:45.897342187+01:00")
t3 = mustParseTime("2023-11-10T13:57:49.289348781+00:00")
)

func Test_Train_TimesstampDBSerialization(t *testing.T) {
tmp := t.TempDir()
dbpath := filepath.Join(tmp, "test.db")
db, err := Open(dbpath)
assert.NoError(t, err)
assert.NotNil(t, db)
defer db.Close()

// insert
id, err := InsertTrain(db, stitch.Train{
StartTS: t0,
EndTS: t1,
}, "testimgpath", "gif")
assert.NoError(t, err)
assert.Greater(t, id, int64(0))

// check
var results []struct {
StartTS string `db:"start_ts"`
EndTS string `db:"end_ts"`
}
err = db.Select(&results, "SELECT start_ts, end_ts FROM trains ORDER BY id DESC")
assert.NoError(t, err)
assert.Len(t, results, 1)
assert.Equal(t, "2023-06-10T16:20:58.805488032+02:00", results[0].StartTS)
assert.Equal(t, "2023-06-10T16:21:05.982318734+02:00", results[0].EndTS)

// another round
id, err = InsertTrain(db, stitch.Train{
StartTS: t2,
EndTS: t3,
}, "testimgpath2", "gif2")
assert.NoError(t, err)
assert.Greater(t, id, int64(0))

results = nil
err = db.Select(&results, "SELECT start_ts, end_ts FROM trains ORDER BY id DESC")
assert.NoError(t, err)
assert.Len(t, results, 2)
assert.Equal(t, "2023-11-10T12:57:45.897342187+01:00", results[0].StartTS)
assert.Equal(t, "2023-11-10T13:57:49.289348781Z", results[0].EndTS)
}

func Test_Temperature_Queries(t *testing.T) {
tmp := t.TempDir()
dbpath := filepath.Join(tmp, "test.db")
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ CREATE TABLE IF NOT EXISTS temperatures (
timestamp DATETIME NOT NULL UNIQUE,
temp_deg_c DOUBLE NOT NULL
);

-- Note: this SQL script will be run every time the database file is opened.
-- Any contents thus need to be idempotent (IF NOT EXISTS etc).

0 comments on commit cbbca44

Please sign in to comment.