feat(assessment): add table families that pick a variant row by seed

This commit is contained in:
shira committed 2026-10-11 08:19:17 +03:00
1 parent f1147b1885
commit 5e1db37746
16 files changed
+1023 -56

No files matched your search

@@ -17,11 +17,11 @@ import (
"lincodex/backend/internal/usecase/assessment"
)
// choiceBankFamilies returns the families of a bank that offers the choice
// answer format next to the integer one: a choice family whose distractors
// come from error models, and the first demo family. The choice family reuses
// the rubric and the identity of the first demo family.
func choiceBankFamilies(t *testing.T, base assessment.BundleFamily) []assessment.BundleFamily {
// choiceTableBankFamilies returns the families of a bank that offers every new
// answer format: a choice family whose distractors come from error models, a
// table family with integer answers, and a table family with choice answers.
// They reuse the rubric and the identity of the first demo family.
func choiceTableBankFamilies(t *testing.T, base assessment.BundleFamily) []assessment.BundleFamily {
t.Helper()
choice := base
choice.Key = "choice-lru-hits"
@@ -33,7 +33,32 @@ func choiceBankFamilies(t *testing.T, base assessment.BundleFamily) []assessment
StatementTemplate: "A cache of capacity {{.capacity}} drops its least recently used key when it is full. Keys are read in this order: {{.sequence}}. How many reads find their key in the cache?",
}
choice.AnswerKey = domain.AssessmentAnswerKey{AnswerFormat: domain.AnswerFormatChoice, ReferenceSolution: "Simulate the cache."}
return []assessment.BundleFamily{choice, base}
integerTable := base
integerTable.Key = "table-integer-limits"
integerTable.SeedGenerator = domain.AssessmentSeedGenerator{
StatementTemplate: "A pool opens {{.connections}} connections at most and each request holds one for {{.seconds}} seconds. How many requests per minute can it serve at most?",
Table: &domain.AssessmentVariantTable{Rows: []domain.AssessmentVariantRow{
{Values: map[string]string{"connections": "10", "seconds": "2"}, Answer: "300"},
{Values: map[string]string{"connections": "4", "seconds": "3"}, Answer: "80"},
{Values: map[string]string{"connections": "6", "seconds": "5"}, Answer: "72"},
{Values: map[string]string{"connections": "20", "seconds": "1"}, Answer: "1200"},
}},
}
integerTable.AnswerKey = domain.AssessmentAnswerKey{AnswerFormat: domain.AnswerFormatInteger, ReferenceSolution: "Multiply by sixty and divide by the hold time."}
choiceTable := base
choiceTable.Key = "table-choice-status"
choiceTable.SeedGenerator = domain.AssessmentSeedGenerator{
StatementTemplate: "Which status code does an API return for {{.situation}}?",
Table: &domain.AssessmentVariantTable{Rows: []domain.AssessmentVariantRow{
{Values: map[string]string{"situation": "a resource that does not exist"}, Answer: "404", Distractors: []string{"400", "410", "500"}},
{Values: map[string]string{"situation": "a call without credentials"}, Answer: "401", Distractors: []string{"403", "407", "511"}},
{Values: map[string]string{"situation": "an update on a stale version"}, Answer: "412", Distractors: []string{"409", "422", "428"}},
}},
}
choiceTable.AnswerKey = domain.AssessmentAnswerKey{AnswerFormat: domain.AnswerFormatChoice, ReferenceSolution: "Match the situation to the status code."}
return []assessment.BundleFamily{choice, integerTable, choiceTable}
}
// allowedItemKeys are the fields that a candidate item may carry per format.
@@ -42,7 +67,7 @@ var allowedItemContentKeys = map[string][]string{
"choice": {"format", "multiple", "options"},
}
func TestAssessmentChoiceFamiliesAreServedAndGradedOverHTTP(t *testing.T) {
func TestAssessmentChoiceAndTableFamiliesAreServedAndGradedOverHTTP(t *testing.T) {
env := bootstrap(t, postgresService, redisService)
ctx, cancel := context.WithTimeout(t.Context(), assessmentAttemptHTTPTimeout)
defer cancel()
@@ -53,9 +78,18 @@ func TestAssessmentChoiceFamiliesAreServedAndGradedOverHTTP(t *testing.T) {
harness := newAssessmentAttemptHTTPHarness(ctx, t, &isolatedEnv)
source := loadDemoBundleFile(t)
source.Families = choiceBankFamilies(t, source.Families[0])
source.Families = choiceTableBankFamilies(t, source.Families[0])
seedAssessmentAttemptHTTPBankFrom(ctx, t, harness.pool, nil, source)
// The bank holds both kinds of generator: the table families are stored
// with hash scheme 2 and the algorithmic family with scheme 1.
for scheme, want := range map[int]bool{1: true, 2: true} {
var stored int
if err := harness.pool.QueryRow(ctx, `SELECT count(*) FROM assessment_families WHERE hash_scheme = $1`, scheme).Scan(&stored); err != nil || (stored > 0) != want {
t.Fatalf("families with hash scheme %d = %d, %v, want some", scheme, stored, err)
}
}
passwordHash, err := harness.credentials.HashNewPassword(ctx, sessionHTTPTestPass)
if err != nil {
t.Fatalf("HashNewPassword() error = %v", err)