76 lines
3.1 KiB
Go
76 lines
3.1 KiB
Go
package demoseed
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// This test only touches a disposable opt-in database, and rolls back its fixtures.
|
|
func TestLegacyDemoArchivalPreservesAuthoredContent(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(), 30*time.Second)
|
|
defer cancel()
|
|
conn, err := pgx.Connect(ctx, url)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close(ctx)
|
|
tx, err := conn.Begin(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
if _, err = tx.Exec(ctx, `CREATE TEMP TABLE demo_seed_entities(kind text,seed_key text,entity_id bigint,PRIMARY KEY(kind,seed_key)) ON COMMIT DROP`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
create := func(key, name, description string, owned bool) int64 {
|
|
t.Helper()
|
|
var id int64
|
|
if err := tx.QueryRow(ctx, `INSERT INTO exam_prep.catalog_courses(name,description,category,publish_status,access_tier) VALUES($1,$2,'EDUCATION','PUBLISHED','FREE') RETURNING id`, name, description).Scan(&id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if owned {
|
|
if _, err := tx.Exec(ctx, `INSERT INTO demo_seed_entities VALUES('course',$1,$2)`, key, id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return id
|
|
}
|
|
untouched := create("everyday-english", "Everyday English", "Build confidence with introductions, daily conversations, and practical vocabulary.", true)
|
|
edited := create("workplace-communication", "Workplace Communication", "Communicate clearly in meetings, write effective messages, and present your ideas.", true)
|
|
renamed := create("digital-skills", "Instructor-authored portfolio course", "Learn practical habits for working online, organizing information, and collaborating safely.", true)
|
|
unrelated := create("", "Everyday English", "Build confidence with introductions, daily conversations, and practical vocabulary.", false)
|
|
var unit, module int64
|
|
if err = tx.QueryRow(ctx, `INSERT INTO exam_prep.units(catalog_course_id,name,publish_status,access_tier) VALUES($1,'Edited chapter','PUBLISHED','FREE') RETURNING id`, edited).Scan(&unit); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = tx.QueryRow(ctx, `INSERT INTO exam_prep.unit_modules(unit_id,name,publish_status,access_tier) VALUES($1,'__chapter_lessons__','PUBLISHED','FREE') RETURNING id`, unit).Scan(&module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = tx.Exec(ctx, `INSERT INTO exam_prep.unit_module_lessons(unit_module_id,title,publish_status,access_tier,sort_order,updated_at) VALUES($1,'Instructor edit','PUBLISHED','FREE',1,NOW())`, module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fixture := seed{tx: tx, ctx: ctx}
|
|
for i := 0; i < 2; i++ {
|
|
if err = fixture.archiveLegacyCourses(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for id, want := range map[int64]string{untouched: "DRAFT", edited: "PUBLISHED", renamed: "PUBLISHED", unrelated: "PUBLISHED"} {
|
|
var got string
|
|
if err = tx.QueryRow(ctx, `SELECT publish_status FROM exam_prep.catalog_courses WHERE id=$1`, id).Scan(&got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != want {
|
|
t.Errorf("course %d: got %s want %s", id, got, want)
|
|
}
|
|
}
|
|
}
|