feat: add the bm:seed command
Validates a whole JSON file (course keys, meeting numbers, values, dates) and upserts meetings, courses, class sessions, deadlines and readings in one transaction, matched on their natural identity. Prints a created, updated and unchanged summary; --dry-run only validates. Ships a small example fixture with made-up data.
This commit is contained in:
449
classes/seed.go
Normal file
449
classes/seed.go
Normal file
@@ -0,0 +1,449 @@
|
||||
package classes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/lagoon"
|
||||
"git.golem15.com/jakub/sm-bm-plugin/models"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// SeedFile is the JSON document bm:seed reads. Every top-level array is
|
||||
// optional. Courses carry a seed-local key that class sessions, deadlines
|
||||
// and readings use to point at them; meetings are referenced by number.
|
||||
type SeedFile struct {
|
||||
Meetings []SeedMeeting `json:"meetings"`
|
||||
Courses []SeedCourse `json:"courses"`
|
||||
ClassSessions []SeedSession `json:"class_sessions"`
|
||||
Deadlines []SeedDeadline `json:"deadlines"`
|
||||
Readings []SeedReading `json:"readings"`
|
||||
}
|
||||
|
||||
// SeedMeeting is one meeting; its identity is Number.
|
||||
type SeedMeeting struct {
|
||||
Number int `json:"number"`
|
||||
StartsOn string `json:"starts_on"`
|
||||
EndsOn string `json:"ends_on"`
|
||||
Notes *string `json:"notes"`
|
||||
}
|
||||
|
||||
// SeedCourse is one course; its identity is Name, Form and Semester. Key is
|
||||
// only a reference inside the file.
|
||||
type SeedCourse struct {
|
||||
Key string `json:"key"`
|
||||
Code *string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Form string `json:"form"`
|
||||
Lecturer *string `json:"lecturer"`
|
||||
Semester int `json:"semester"`
|
||||
ECTS *float64 `json:"ects"`
|
||||
Assessment *string `json:"assessment"`
|
||||
ElectiveGroup *string `json:"elective_group"`
|
||||
Description *string `json:"description"`
|
||||
SyllabusURL *string `json:"syllabus_url"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
|
||||
// SeedSession is one class session; its identity is the course and StartsAt.
|
||||
// Course is a course key, Meeting an optional meeting number.
|
||||
type SeedSession struct {
|
||||
Course string `json:"course"`
|
||||
Meeting *int `json:"meeting"`
|
||||
StartsAt string `json:"starts_at"`
|
||||
EndsAt string `json:"ends_at"`
|
||||
Room *string `json:"room"`
|
||||
Kind *string `json:"kind"`
|
||||
IsOnline bool `json:"is_online"`
|
||||
Notes *string `json:"notes"`
|
||||
}
|
||||
|
||||
// SeedDeadline is one deadline; its identity is Title and DueAt. Course is
|
||||
// an optional course key.
|
||||
type SeedDeadline struct {
|
||||
Title string `json:"title"`
|
||||
DueAt string `json:"due_at"`
|
||||
Course *string `json:"course"`
|
||||
Kind string `json:"kind"`
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
|
||||
// SeedReading is one reading; its identity is the course and Title.
|
||||
type SeedReading struct {
|
||||
Course string `json:"course"`
|
||||
Title string `json:"title"`
|
||||
Author *string `json:"author"`
|
||||
Source *string `json:"source"`
|
||||
URL *string `json:"url"`
|
||||
IsRequired bool `json:"is_required"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
|
||||
// SeedCounts counts the rows of one kind a seed created, updated or left
|
||||
// unchanged.
|
||||
type SeedCounts struct {
|
||||
Created int
|
||||
Updated int
|
||||
Unchanged int
|
||||
}
|
||||
|
||||
func (c *SeedCounts) record(created, changed bool) {
|
||||
switch {
|
||||
case created:
|
||||
c.Created++
|
||||
case changed:
|
||||
c.Updated++
|
||||
default:
|
||||
c.Unchanged++
|
||||
}
|
||||
}
|
||||
|
||||
// SeedSummary is the outcome of a seed, per kind.
|
||||
type SeedSummary struct {
|
||||
Meetings SeedCounts
|
||||
Courses SeedCounts
|
||||
ClassSessions SeedCounts
|
||||
Deadlines SeedCounts
|
||||
Readings SeedCounts
|
||||
}
|
||||
|
||||
// Rows returns the summary as table rows: kind, created, updated, unchanged.
|
||||
func (s SeedSummary) Rows() [][]string {
|
||||
row := func(name string, c SeedCounts) []string {
|
||||
return []string{name, fmt.Sprint(c.Created), fmt.Sprint(c.Updated), fmt.Sprint(c.Unchanged)}
|
||||
}
|
||||
return [][]string{
|
||||
row("meetings", s.Meetings),
|
||||
row("courses", s.Courses),
|
||||
row("class_sessions", s.ClassSessions),
|
||||
row("deadlines", s.Deadlines),
|
||||
row("readings", s.Readings),
|
||||
}
|
||||
}
|
||||
|
||||
// ParseSeed decodes a seed document. Unknown fields are an error, so a typo
|
||||
// in a key never drops data silently.
|
||||
func ParseSeed(r io.Reader) (*SeedFile, error) {
|
||||
body, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(body))
|
||||
dec.DisallowUnknownFields()
|
||||
var f SeedFile
|
||||
if err := dec.Decode(&f); err != nil {
|
||||
return nil, fmt.Errorf("bm:seed: %w", err)
|
||||
}
|
||||
if dec.More() {
|
||||
return nil, errors.New("bm:seed: trailing data after the JSON document")
|
||||
}
|
||||
return &f, nil
|
||||
}
|
||||
|
||||
// Validate checks the whole document before anything is written: required
|
||||
// fields, allowed values, dates and times, duplicate identities and that
|
||||
// every course key and meeting number a record points at exists in the
|
||||
// file. Each error names the record.
|
||||
func (f *SeedFile) Validate() error {
|
||||
var errs []error
|
||||
add := func(format string, args ...any) { errs = append(errs, fmt.Errorf(format, args...)) }
|
||||
|
||||
meetings := map[int]bool{}
|
||||
for i, m := range f.Meetings {
|
||||
at := fmt.Sprintf("meetings[%d] (number %d)", i, m.Number)
|
||||
if m.Number < 1 {
|
||||
add("%s: number must be a positive integer", at)
|
||||
}
|
||||
if meetings[m.Number] {
|
||||
add("%s: duplicate meeting number", at)
|
||||
}
|
||||
meetings[m.Number] = true
|
||||
start, err1 := models.ParseDate(m.StartsOn)
|
||||
end, err2 := models.ParseDate(m.EndsOn)
|
||||
if err1 != nil {
|
||||
add("%s: starts_on: %v", at, err1)
|
||||
}
|
||||
if err2 != nil {
|
||||
add("%s: ends_on: %v", at, err2)
|
||||
}
|
||||
if err1 == nil && err2 == nil && end.Before(start) {
|
||||
add("%s: ends_on is before starts_on", at)
|
||||
}
|
||||
}
|
||||
|
||||
keys := map[string]bool{}
|
||||
identities := map[string]bool{}
|
||||
for i, c := range f.Courses {
|
||||
at := fmt.Sprintf("courses[%d] (key %q)", i, c.Key)
|
||||
if strings.TrimSpace(c.Key) == "" {
|
||||
add("%s: key is required", at)
|
||||
} else if keys[c.Key] {
|
||||
add("%s: duplicate course key", at)
|
||||
}
|
||||
keys[c.Key] = true
|
||||
if strings.TrimSpace(c.Name) == "" {
|
||||
add("%s: name is required", at)
|
||||
}
|
||||
if !slices.Contains(models.CourseForms, c.Form) {
|
||||
add("%s: form %q is not one of %s", at, c.Form, strings.Join(models.CourseForms, ", "))
|
||||
}
|
||||
if c.Semester < 1 || c.Semester > 4 {
|
||||
add("%s: semester must be between 1 and 4", at)
|
||||
}
|
||||
id := fmt.Sprintf("%s\x00%s\x00%d", c.Name, c.Form, c.Semester)
|
||||
if identities[id] {
|
||||
add("%s: another course has the same name, form and semester", at)
|
||||
}
|
||||
identities[id] = true
|
||||
}
|
||||
|
||||
sessions := map[string]bool{}
|
||||
for i, s := range f.ClassSessions {
|
||||
at := fmt.Sprintf("class_sessions[%d] (course %q, starts_at %s)", i, s.Course, s.StartsAt)
|
||||
if !keys[s.Course] {
|
||||
add("%s: unknown course key %q", at, s.Course)
|
||||
}
|
||||
if s.Meeting != nil && !meetings[*s.Meeting] {
|
||||
add("%s: unknown meeting number %d", at, *s.Meeting)
|
||||
}
|
||||
start, err1 := models.ParseDateTime(s.StartsAt)
|
||||
end, err2 := models.ParseDateTime(s.EndsAt)
|
||||
if err1 != nil {
|
||||
add("%s: starts_at: %v", at, err1)
|
||||
}
|
||||
if err2 != nil {
|
||||
add("%s: ends_at: %v", at, err2)
|
||||
}
|
||||
if err1 == nil && err2 == nil {
|
||||
if !end.After(start) {
|
||||
add("%s: ends_at is not after starts_at", at)
|
||||
}
|
||||
id := s.Course + "\x00" + start.UTC().String()
|
||||
if sessions[id] {
|
||||
add("%s: duplicate session of the course at this time", at)
|
||||
}
|
||||
sessions[id] = true
|
||||
}
|
||||
}
|
||||
|
||||
deadlines := map[string]bool{}
|
||||
for i, d := range f.Deadlines {
|
||||
at := fmt.Sprintf("deadlines[%d] (%q)", i, d.Title)
|
||||
if strings.TrimSpace(d.Title) == "" {
|
||||
add("%s: title is required", at)
|
||||
}
|
||||
if d.Course != nil && !keys[*d.Course] {
|
||||
add("%s: unknown course key %q", at, *d.Course)
|
||||
}
|
||||
if !slices.Contains(models.DeadlineKinds, d.Kind) {
|
||||
add("%s: kind %q is not one of %s", at, d.Kind, strings.Join(models.DeadlineKinds, ", "))
|
||||
}
|
||||
due, err := models.ParseDateTime(d.DueAt)
|
||||
if err != nil {
|
||||
add("%s: due_at: %v", at, err)
|
||||
continue
|
||||
}
|
||||
id := d.Title + "\x00" + due.UTC().String()
|
||||
if deadlines[id] {
|
||||
add("%s: duplicate deadline (same title and due_at)", at)
|
||||
}
|
||||
deadlines[id] = true
|
||||
}
|
||||
|
||||
readings := map[string]bool{}
|
||||
for i, r := range f.Readings {
|
||||
at := fmt.Sprintf("readings[%d] (course %q, %q)", i, r.Course, r.Title)
|
||||
if !keys[r.Course] {
|
||||
add("%s: unknown course key %q", at, r.Course)
|
||||
}
|
||||
if strings.TrimSpace(r.Title) == "" {
|
||||
add("%s: title is required", at)
|
||||
}
|
||||
id := r.Course + "\x00" + r.Title
|
||||
if readings[id] {
|
||||
add("%s: duplicate reading of the course", at)
|
||||
}
|
||||
readings[id] = true
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// Seed validates f and upserts it in one transaction: meetings by number,
|
||||
// courses by name, form and semester, class sessions by course and start
|
||||
// time, deadlines by title and due time, readings by course and title. Rows
|
||||
// absent from the file are left alone. On any error nothing is written.
|
||||
func Seed(ctx context.Context, db *gorm.DB, f *SeedFile) (SeedSummary, error) {
|
||||
var sum SeedSummary
|
||||
if err := f.Validate(); err != nil {
|
||||
return sum, err
|
||||
}
|
||||
err := lagoon.Transaction(ctx, db, func(ctx context.Context, tx *gorm.DB) error {
|
||||
meetingIDs := map[int]uint{}
|
||||
for _, in := range f.Meetings {
|
||||
m := models.Meeting{}
|
||||
created, changed, err := upsert(tx, &m, clause.Eq{Column: "number", Value: in.Number}, func(m *models.Meeting) {
|
||||
m.Number = in.Number
|
||||
start, _ := models.ParseDate(in.StartsOn)
|
||||
end, _ := models.ParseDate(in.EndsOn)
|
||||
m.StartsOn = models.NewDate(start)
|
||||
m.EndsOn = models.NewDate(end)
|
||||
m.Notes = nz(in.Notes)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("meeting %d: %w", in.Number, err)
|
||||
}
|
||||
sum.Meetings.record(created, changed)
|
||||
meetingIDs[in.Number] = m.ID
|
||||
}
|
||||
|
||||
courseIDs := map[string]uint{}
|
||||
for _, in := range f.Courses {
|
||||
c := models.Course{}
|
||||
where := clause.And(
|
||||
clause.Eq{Column: "name", Value: in.Name},
|
||||
clause.Eq{Column: "form", Value: in.Form},
|
||||
clause.Eq{Column: "semester", Value: in.Semester},
|
||||
)
|
||||
created, changed, err := upsert(tx, &c, where, func(c *models.Course) {
|
||||
c.Code = nz(in.Code)
|
||||
c.Name = in.Name
|
||||
c.Form = in.Form
|
||||
c.Lecturer = nz(in.Lecturer)
|
||||
c.Semester = in.Semester
|
||||
c.ECTS = in.ECTS
|
||||
c.Assessment = nz(in.Assessment)
|
||||
c.ElectiveGroup = nz(in.ElectiveGroup)
|
||||
c.Description = nz(in.Description)
|
||||
c.SyllabusURL = nz(in.SyllabusURL)
|
||||
c.SortOrder = in.SortOrder
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("course %q: %w", in.Key, err)
|
||||
}
|
||||
sum.Courses.record(created, changed)
|
||||
courseIDs[in.Key] = c.ID
|
||||
}
|
||||
|
||||
for _, in := range f.ClassSessions {
|
||||
start, _ := models.ParseDateTime(in.StartsAt)
|
||||
s := models.ClassSession{}
|
||||
where := clause.And(
|
||||
clause.Eq{Column: "course_id", Value: courseIDs[in.Course]},
|
||||
clause.Eq{Column: "starts_at", Value: start.UTC()},
|
||||
)
|
||||
created, changed, err := upsert(tx, &s, where, func(s *models.ClassSession) {
|
||||
s.CourseID = courseIDs[in.Course]
|
||||
s.MeetingID = nil
|
||||
if in.Meeting != nil {
|
||||
id := meetingIDs[*in.Meeting]
|
||||
s.MeetingID = &id
|
||||
}
|
||||
s.StartsAt = models.NewDateTime(start)
|
||||
end, _ := models.ParseDateTime(in.EndsAt)
|
||||
s.EndsAt = models.NewDateTime(end)
|
||||
s.Room = nz(in.Room)
|
||||
s.Kind = nz(in.Kind)
|
||||
s.IsOnline = in.IsOnline
|
||||
s.Notes = nz(in.Notes)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("class session %s %s: %w", in.Course, in.StartsAt, err)
|
||||
}
|
||||
sum.ClassSessions.record(created, changed)
|
||||
}
|
||||
|
||||
for _, in := range f.Deadlines {
|
||||
due, _ := models.ParseDateTime(in.DueAt)
|
||||
d := models.Deadline{}
|
||||
where := clause.And(
|
||||
clause.Eq{Column: "title", Value: in.Title},
|
||||
clause.Eq{Column: "due_at", Value: due.UTC()},
|
||||
)
|
||||
created, changed, err := upsert(tx, &d, where, func(d *models.Deadline) {
|
||||
d.Title = in.Title
|
||||
d.DueAt = models.NewDateTime(due)
|
||||
d.CourseID = nil
|
||||
if in.Course != nil {
|
||||
id := courseIDs[*in.Course]
|
||||
d.CourseID = &id
|
||||
}
|
||||
d.Kind = in.Kind
|
||||
d.Description = nz(in.Description)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("deadline %q: %w", in.Title, err)
|
||||
}
|
||||
sum.Deadlines.record(created, changed)
|
||||
}
|
||||
|
||||
for _, in := range f.Readings {
|
||||
r := models.Reading{}
|
||||
where := clause.And(
|
||||
clause.Eq{Column: "course_id", Value: courseIDs[in.Course]},
|
||||
clause.Eq{Column: "title", Value: in.Title},
|
||||
)
|
||||
created, changed, err := upsert(tx, &r, where, func(r *models.Reading) {
|
||||
r.CourseID = courseIDs[in.Course]
|
||||
r.Title = in.Title
|
||||
r.Author = nz(in.Author)
|
||||
r.Source = nz(in.Source)
|
||||
r.URL = nz(in.URL)
|
||||
r.IsRequired = in.IsRequired
|
||||
r.SortOrder = in.SortOrder
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading %q of %s: %w", in.Title, in.Course, err)
|
||||
}
|
||||
sum.Readings.record(created, changed)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return SeedSummary{}, err
|
||||
}
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
// nz returns nil for a missing or blank optional text, as the models'
|
||||
// BeforeSave hooks store it, so an unchanged row compares equal.
|
||||
func nz(s *string) *string {
|
||||
if s == nil || strings.TrimSpace(*s) == "" {
|
||||
return nil
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// upsert loads the row matching where into row, applies set and writes it:
|
||||
// an insert when there was no row, an update when set changed a column, and
|
||||
// nothing otherwise. The comparison is on the row's own fields, before and
|
||||
// after set, as JSON.
|
||||
func upsert[T any](tx *gorm.DB, row *T, where clause.Expression, set func(*T)) (created, changed bool, err error) {
|
||||
res := tx.Clauses(where).Limit(1).Find(row)
|
||||
if res.Error != nil {
|
||||
return false, false, res.Error
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
set(row)
|
||||
return true, false, tx.Create(row).Error
|
||||
}
|
||||
before, err := json.Marshal(row)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
set(row)
|
||||
after, err := json.Marshal(row)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
if bytes.Equal(before, after) {
|
||||
return false, false, nil
|
||||
}
|
||||
return false, true, tx.Save(row).Error
|
||||
}
|
||||
117
classes/seed_test.go
Normal file
117
classes/seed_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package classes_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/jakub/sm-bm-plugin/classes"
|
||||
)
|
||||
|
||||
func parse(t *testing.T, doc string) *classes.SeedFile {
|
||||
t.Helper()
|
||||
f, err := classes.ParseSeed(strings.NewReader(doc))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseSeed: %v", err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func TestExampleFixtureIsValid(t *testing.T) {
|
||||
fh, err := os.Open("../testdata/seed.example.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer fh.Close()
|
||||
f, err := classes.ParseSeed(fh)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.Validate(); err != nil {
|
||||
t.Fatalf("Validate: %v", err)
|
||||
}
|
||||
if len(f.Meetings) != 2 || len(f.Courses) != 3 || len(f.ClassSessions) != 3 || len(f.Deadlines) != 2 || len(f.Readings) != 2 {
|
||||
t.Errorf("fixture counts = %d %d %d %d %d", len(f.Meetings), len(f.Courses), len(f.ClassSessions), len(f.Deadlines), len(f.Readings))
|
||||
}
|
||||
if f.Courses[2].ElectiveGroup == nil || *f.Courses[2].ElectiveGroup != "przyklad-do-wyboru" {
|
||||
t.Errorf("elective_group = %v", f.Courses[2].ElectiveGroup)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyDocumentIsValid(t *testing.T) {
|
||||
if err := parse(t, `{}`).Validate(); err != nil {
|
||||
t.Errorf("{}: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRejectsUnknownFieldsAndTrailingData(t *testing.T) {
|
||||
for _, doc := range []string{
|
||||
`{"courses": [{"key": "a", "nmae": "typo"}]}`,
|
||||
`{"lectures": []}`,
|
||||
`{} {}`,
|
||||
`[]`,
|
||||
`{"meetings": [{"number": "one"}]}`,
|
||||
} {
|
||||
if _, err := classes.ParseSeed(strings.NewReader(doc)); err == nil {
|
||||
t.Errorf("ParseSeed(%s) accepted", doc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateNamesTheRecord(t *testing.T) {
|
||||
cases := []struct {
|
||||
doc string
|
||||
want []string
|
||||
}{
|
||||
{`{"class_sessions": [{"course": "ghost", "starts_at": "2030-10-05T08:00:00+02:00", "ends_at": "2030-10-05T09:00:00+02:00"}]}`,
|
||||
[]string{`class_sessions[0]`, `unknown course key "ghost"`}},
|
||||
{`{"courses": [{"key": "a", "name": "A", "form": "wyklad", "semester": 1}],
|
||||
"class_sessions": [{"course": "a", "meeting": 9, "starts_at": "2030-10-05T08:00:00+02:00", "ends_at": "2030-10-05T09:00:00+02:00"}]}`,
|
||||
[]string{`unknown meeting number 9`}},
|
||||
{`{"deadlines": [{"title": "X", "due_at": "2030-10-05T08:00:00+02:00", "course": "nope", "kind": "inne"}]}`,
|
||||
[]string{`deadlines[0] ("X")`, `unknown course key "nope"`}},
|
||||
{`{"readings": [{"course": "nope", "title": "Book"}]}`,
|
||||
[]string{`readings[0]`, `unknown course key "nope"`}},
|
||||
{`{"courses": [{"key": "a", "name": "A", "form": "lecture", "semester": 7}]}`,
|
||||
[]string{`form "lecture"`, `semester must be between 1 and 4`}},
|
||||
{`{"courses": [{"key": "a", "name": "A", "form": "wyklad", "semester": 1}, {"key": "a", "name": "A", "form": "wyklad", "semester": 1}]}`,
|
||||
[]string{`duplicate course key`, `same name, form and semester`}},
|
||||
{`{"meetings": [{"number": 1, "starts_on": "2030-10-06", "ends_on": "2030-10-05"}, {"number": 1, "starts_on": "05.10.2030", "ends_on": "2030-10-05"}]}`,
|
||||
[]string{`ends_on is before starts_on`, `duplicate meeting number`, `starts_on`}},
|
||||
{`{"courses": [{"key": "a", "name": "A", "form": "wyklad", "semester": 1}],
|
||||
"class_sessions": [{"course": "a", "starts_at": "2030-10-05 10:00", "ends_at": "2030-10-05 09:00"}]}`,
|
||||
[]string{`ends_at is not after starts_at`}},
|
||||
{`{"deadlines": [{"title": "", "due_at": "soon", "kind": "deadline"}]}`,
|
||||
[]string{`title is required`, `due_at`, `kind "deadline"`}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
err := parse(t, tc.doc).Validate()
|
||||
if err == nil {
|
||||
t.Errorf("Validate(%s) passed", tc.doc)
|
||||
continue
|
||||
}
|
||||
for _, want := range tc.want {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("Validate(%s) = %q, want it to mention %q", tc.doc, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedValidatesBeforeTouchingTheDatabase(t *testing.T) {
|
||||
f := parse(t, `{"readings": [{"course": "ghost", "title": "Book"}]}`)
|
||||
// A nil database would panic if Seed reached it.
|
||||
if _, err := classes.Seed(t.Context(), nil, f); err == nil || !strings.Contains(err.Error(), "ghost") {
|
||||
t.Errorf("Seed = %v, want the unknown course error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSummaryRows(t *testing.T) {
|
||||
var s classes.SeedSummary
|
||||
s.Courses.Created = 2
|
||||
s.Readings.Unchanged = 1
|
||||
rows := s.Rows()
|
||||
if len(rows) != 5 || rows[1][0] != "courses" || rows[1][1] != "2" || rows[4][3] != "1" {
|
||||
t.Errorf("Rows = %v", rows)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user