feat: add the quizzes:import command for JSON quiz files
This commit is contained in:
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