stub asses
This commit is contained in:
43
src/asses/db.go
Normal file
43
src/asses/db.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package asses
|
||||
|
||||
import (
|
||||
"context"
|
||||
"show-rss/src/db"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func Next(ctx context.Context) (time.Time, error) {
|
||||
if err := initDB(ctx); err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
type Did struct {
|
||||
Did time.Time
|
||||
}
|
||||
result, err := db.QueryOne[Did](ctx, `
|
||||
SELECT executed_at AS "Did"
|
||||
FROM "asses.executions"
|
||||
ORDER BY executed_at DESC
|
||||
LIMIT 1
|
||||
`)
|
||||
return result.Did, err
|
||||
}
|
||||
|
||||
func Record(ctx context.Context) error {
|
||||
if err := initDB(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return db.Exec(ctx, `INSERT INTO "asses.executions" (id, executed_at) VALUES ($1, $2)`, uuid.New().String(), time.Now())
|
||||
}
|
||||
|
||||
func initDB(ctx context.Context) error {
|
||||
return db.InitializeSchema(ctx, "asses", []string{
|
||||
`CREATE TABLE "asses.executions" (
|
||||
id TEXT PRIMARY KEY NOT NULL,
|
||||
executed_at TIMESTAMP NOT NULL
|
||||
)`,
|
||||
})
|
||||
}
|
||||
29
src/asses/db_test.go
Normal file
29
src/asses/db_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package asses_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"show-rss/src/asses"
|
||||
"show-rss/src/db"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNextRecord(t *testing.T) {
|
||||
ctx := db.Test(t, context.Background())
|
||||
|
||||
if v, err := asses.Next(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if zero := v.IsZero(); !zero {
|
||||
t.Fatal(v)
|
||||
}
|
||||
|
||||
if err := asses.Record(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if v, err := asses.Next(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if since := time.Since(v); since > time.Minute {
|
||||
t.Fatal(since)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user