feat: add the quizzes:import command for JSON quiz files
This commit is contained in:
57
console/import.go
Normal file
57
console/import.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Code generated by summer make. DO NOT EDIT.
|
||||
|
||||
package console
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.golem15.com/golem15/sm-quizzes-plugin/classes"
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ImportCommand returns the quizzes:import console command. withDB runs the
|
||||
// command's work with the application's database; the plugin supplies it.
|
||||
func ImportCommand(withDB func(ctx context.Context, fn func(*gorm.DB) error) error) bonfire.Command {
|
||||
return bonfire.Command{
|
||||
Name: "quizzes:import",
|
||||
Description: "Import one quiz from a JSON file (see the plugin README for the format)",
|
||||
Args: []bonfire.Arg{{Name: "file", Description: "Path to the quiz JSON file", Required: true}},
|
||||
Flags: []bonfire.Flag{
|
||||
{Name: "replace", Description: "Replace the questions of an existing quiz with the same slug", Bare: true},
|
||||
},
|
||||
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
|
||||
file, _ := in.Argument("file")
|
||||
if file == "" {
|
||||
return fmt.Errorf("quizzes:import: a file is required")
|
||||
}
|
||||
raw, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("quizzes:import: %w", err)
|
||||
}
|
||||
doc, err := classes.ParseImport(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("quizzes:import: %s: %w", file, err)
|
||||
}
|
||||
replace, _ := in.Flag("replace")
|
||||
return withDB(ctx, func(db *gorm.DB) error {
|
||||
res, err := classes.Import(ctx, db, doc, replace == "true")
|
||||
if errors.Is(err, classes.ErrQuizExists) {
|
||||
return fmt.Errorf("quizzes:import: a quiz with the slug %q already exists; pass --replace to replace its questions", doc.Slug)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("quizzes:import: %w", err)
|
||||
}
|
||||
verb := "replaced"
|
||||
if res.Created {
|
||||
verb = "created"
|
||||
}
|
||||
out.Printf("%s quiz %s (id %d): %d questions, %d answers\n", verb, res.Quiz.Slug, res.Quiz.ID, res.Questions, res.Answers)
|
||||
return nil
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
37
console/import_test.go
Normal file
37
console/import_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package console_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/sm-quizzes-plugin/console"
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestImportCommandChecksTheFileFirst(t *testing.T) {
|
||||
opened := false
|
||||
cmd := console.ImportCommand(func(ctx context.Context, fn func(*gorm.DB) error) error {
|
||||
opened = true
|
||||
return errors.New("no database in unit tests")
|
||||
})
|
||||
if cmd.Name != "quizzes:import" || len(cmd.Args) != 1 || !cmd.Args[0].Required || cmd.Flags[0].Name != "replace" || !cmd.Flags[0].Bare {
|
||||
t.Fatalf("command = %+v", cmd)
|
||||
}
|
||||
call := func(args ...string) error {
|
||||
var out bytes.Buffer
|
||||
return bonfire.Call(context.Background(), []bonfire.Command{cmd}, "quizzes:import", args, &out)
|
||||
}
|
||||
if err := call("../testdata/missing.json"); err == nil || opened {
|
||||
t.Errorf("missing file = %v (database opened: %v)", err, opened)
|
||||
}
|
||||
if err := call("../go.mod"); err == nil || !strings.Contains(err.Error(), "invalid JSON") || opened {
|
||||
t.Errorf("non-JSON file = %v (database opened: %v)", err, opened)
|
||||
}
|
||||
if err := call("../testdata/quiz.json"); err == nil || !opened {
|
||||
t.Errorf("valid file = %v, want the database opened", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user