57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package main
|
|
|
|
import "testing"
|
|
|
|
func TestCalculateEpisodeNumbers(t *testing.T) {
|
|
db := openDatabaseSource(":memory:")
|
|
|
|
// Ensure the DB is empty
|
|
_, err := db.Exec("delete from videos")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
_, err = db.Exec("insert into videos (title, url, year, episode, run) values ('Video A', 'a', 2025, 1, 1), ('Video B', 'b', 2025, null, 2), ('Old Video', 'c', 2020, 100, 1)")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
err = CalculateEpisodeNumbers(db)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
rows, err := db.Query("select title, episode from videos where year = 2025")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
var title string
|
|
var episode int
|
|
rows.Next()
|
|
err = rows.Scan(&title, &episode)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if title != "Video A" {
|
|
t.Errorf("Title is %s, expected Video A", title)
|
|
}
|
|
if episode != 1 {
|
|
t.Errorf("Episode is %d, expected 1", episode)
|
|
}
|
|
|
|
rows.Next()
|
|
err = rows.Scan(&title, &episode)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if title != "Video B" {
|
|
t.Errorf("Title is %s, expected Video B", title)
|
|
}
|
|
if episode != 2 {
|
|
t.Errorf("Episode is %d, expected 2", episode)
|
|
}
|
|
}
|