148 lines
6.5 KiB
Go
148 lines
6.5 KiB
Go
package demoseed_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"Rezina-Backend/internal/demoseed"
|
|
"Rezina-Backend/internal/repository"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// Opt in with a migrated disposable test database. Never uses application DB_URL.
|
|
func TestDemoSeedAndPublicCatalogIntegration(t *testing.T) {
|
|
url := os.Getenv("DEMO_TEST_DB_URL")
|
|
if url == "" {
|
|
t.Skip("set DEMO_TEST_DB_URL to a disposable migrated PostgreSQL database")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
conn, err := pgx.Connect(ctx, url)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close(ctx)
|
|
for i := 0; i < 2; i++ {
|
|
if err := demoseed.Run(ctx, conn, demoseed.DefaultPassword, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// Migration 6 installs the same curriculum as drafts. Publication is explicit
|
|
// in this disposable test database; the seeder itself preserves authored status.
|
|
for _, stmt := range []string{
|
|
`UPDATE exam_prep.catalog_courses SET publish_status='PUBLISHED' WHERE id IN(SELECT entity_id FROM demo_seed_entities WHERE kind='course' AND seed_key LIKE 'makeup-%')`,
|
|
`UPDATE exam_prep.units SET publish_status='PUBLISHED' WHERE id IN(SELECT entity_id FROM demo_seed_entities WHERE kind='chapter' AND seed_key LIKE 'makeup-%')`,
|
|
`UPDATE exam_prep.unit_module_lessons SET publish_status='PUBLISHED' WHERE id IN(SELECT entity_id FROM demo_seed_entities WHERE kind='lesson' AND seed_key LIKE 'makeup-%')`,
|
|
`UPDATE exam_prep.lesson_practices SET publish_status='PUBLISHED' WHERE id IN(SELECT entity_id FROM demo_seed_entities WHERE kind='quiz' AND seed_key LIKE 'makeup-%')`,
|
|
`UPDATE question_sets SET status='PUBLISHED' WHERE id IN(SELECT entity_id FROM demo_seed_entities WHERE kind='question_set' AND seed_key LIKE 'makeup-%')`,
|
|
`UPDATE questions SET status='PUBLISHED' WHERE id IN(SELECT entity_id FROM demo_seed_entities WHERE kind='question' AND seed_key LIKE 'makeup-%')`,
|
|
} {
|
|
if _, err = conn.Exec(ctx, stmt); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var courses, lessons, quizzes int
|
|
if err = conn.QueryRow(ctx, `SELECT COUNT(*) FILTER(WHERE kind='course'),COUNT(*) FILTER(WHERE kind='lesson'),COUNT(*) FILTER(WHERE kind='quiz') FROM demo_seed_entities WHERE seed_key LIKE 'makeup-%' OR seed_key='draft'`).Scan(&courses, &lessons, &quizzes); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if courses != 4 || lessons != 18 || quizzes != 6 {
|
|
t.Fatalf("duplicate/missing fixtures after rerun: courses=%d lessons=%d quizzes=%d", courses, lessons, quizzes)
|
|
}
|
|
for _, a := range demoseed.Accounts {
|
|
table := "users"
|
|
if a.Audience == "staff" {
|
|
table = "team_members"
|
|
}
|
|
var hash []byte
|
|
if err = conn.QueryRow(ctx, `SELECT password FROM `+table+` WHERE email=$1`, a.Email).Scan(&hash); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = bcrypt.CompareHashAndPassword(hash, []byte(demoseed.DefaultPassword)); err != nil {
|
|
t.Errorf("login hash invalid for %s", a.Email)
|
|
}
|
|
}
|
|
pool, err := pgxpool.New(ctx, url)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer pool.Close()
|
|
store := repository.NewStore(pool)
|
|
var id, draftID int64
|
|
if err = conn.QueryRow(ctx, `SELECT entity_id FROM demo_seed_entities WHERE kind='course' AND seed_key='makeup-foundations'`).Scan(&id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = conn.QueryRow(ctx, `SELECT entity_id FROM demo_seed_entities WHERE kind='course' AND seed_key='draft'`).Scan(&draftID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
outline, err := store.PublicCatalogCourse(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if outline.ChapterCount != 2 || outline.LessonCount != 6 || outline.QuizCount != 2 || len(outline.Chapters) != 2 {
|
|
t.Fatalf("outline counts=%+v", outline.PublicCourse)
|
|
}
|
|
if len(outline.Chapters[0].Lessons) != 3 || len(outline.Chapters[0].Quizzes) != 1 || outline.Chapters[0].Quizzes[0].QuestionCount != 3 {
|
|
t.Fatalf("chapter=%+v", outline.Chapters[0])
|
|
}
|
|
if _, err = store.PublicCatalogCourse(ctx, draftID); err != pgx.ErrNoRows {
|
|
t.Fatalf("draft course accessible: %v", err)
|
|
}
|
|
page, err := store.PublicCatalogCourses(ctx, "[Demo draft]", 20, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if page.TotalCount != 0 || len(page.Courses) != 0 {
|
|
t.Fatal("draft course leaked into search")
|
|
}
|
|
// The database must contain subject-specific MCQs with one correct option.
|
|
var questionText, explanation string
|
|
var optionCount, correctCount int
|
|
err = conn.QueryRow(ctx, `SELECT q.question_text,q.explanation,COUNT(o.id),COUNT(*) FILTER(WHERE o.is_correct)
|
|
FROM demo_seed_entities d JOIN questions q ON q.id=d.entity_id JOIN question_options o ON o.question_id=q.id
|
|
WHERE d.kind='question' AND d.seed_key='makeup-foundations/chapter-1/question-1'
|
|
GROUP BY q.id`).Scan(&questionText, &explanation, &optionCount, &correctCount)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lesson := demoseed.Courses[0].Chapters[0].Lessons[0]
|
|
if questionText != lesson.Question || explanation != lesson.Explanation || optionCount != 3 || correctCount != 1 {
|
|
t.Fatal("makeup quiz payload is incomplete")
|
|
}
|
|
// Authored edits survive a subsequent seed run.
|
|
var lessonID int64
|
|
if err = conn.QueryRow(ctx, `SELECT entity_id FROM demo_seed_entities WHERE kind='lesson' AND seed_key='makeup-foundations/chapter-1/lesson-1'`).Scan(&lessonID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = conn.Exec(ctx, `UPDATE exam_prep.unit_module_lessons SET title='Instructor revised kit lesson',updated_at=NOW() WHERE id=$1`, lessonID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Exec(context.Background(), `UPDATE exam_prep.unit_module_lessons SET title=$2,updated_at=NULL WHERE id=$1`, lessonID, lesson.Title)
|
|
if err = demoseed.Run(ctx, conn, demoseed.DefaultPassword, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var preserved string
|
|
if err = conn.QueryRow(ctx, `SELECT title FROM exam_prep.unit_module_lessons WHERE id=$1`, lessonID).Scan(&preserved); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if preserved != "Instructor revised kit lesson" {
|
|
t.Fatal("seed overwrote instructor edits")
|
|
}
|
|
// A hidden chapter and all descendants must disappear from the public outline.
|
|
chapterID := outline.Chapters[0].ID
|
|
if _, err = conn.Exec(ctx, `UPDATE exam_prep.units SET publish_status='DRAFT' WHERE id=$1`, chapterID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Exec(context.Background(), `UPDATE exam_prep.units SET publish_status='PUBLISHED' WHERE id=$1`, chapterID)
|
|
outline, err = store.PublicCatalogCourse(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(outline.Chapters) != 1 || outline.LessonCount != 3 || outline.QuizCount != 1 {
|
|
t.Fatal("hidden chapter descendants leaked into preview")
|
|
}
|
|
}
|