Sequences
Why not index + 1
Numbering from ctx.Index + 1 looks reasonable while a test has one Add. Add another batch, though, and the index starts over. If a class already has a student in seat 1, the next batch would try to claim seat 1 again and violate the class-and-seat uniqueness constraint.
A sequence addresses that by keeping its position across Adds. Declare a start value and a next function once, then choose where the counter should be shared. Its state lives in each session, so one test's numbering does not affect another.
Declare, scope, derive
The Quick start numbers seats within each class. If the school also needs a roster number that continues across classes, add a separate Student.RosterNumber and scope that sequence to the school ancestor.
public static partial class Blueprints
{
// Seats restart in each class, while roster numbers continue across classes at one school.
private static readonly Sequence<int> Seats = Sequence.Of(1, n => n + 1).PerParent();
private static readonly Sequence<int> RosterNumbers = Sequence.Of(1, n => n + 1).Per<School>();
public static readonly ChildBlueprint<SchoolSeed, SchoolClass, Student> Student = Blueprint
.ChildOf<SchoolSeed, SchoolClass, Student>(
(schoolClass, ctx) =>
new()
{
Id = ctx.NewId("stu"),
DistrictId = schoolClass.DistrictId,
ClassId = schoolClass.Id,
EnrolledAtUtc = ctx.Now,
}
)
// Fill missing numbers and keep any numbers set by the test.
.Derive(s => s.Seat, Seats)
.Derive(s => s.RosterNumber, RosterNumbers)
.Derive(s => s.FullName, (s, _) => $"Student {s.Seat}");
}
Assign each sequence with the fill-only Derive(property, sequence).
Scope: one counter per…
| Scope | One counter per | Example |
|---|---|---|
.PerSession() (default) | test | a student ID number unique across districts |
.PerParent() | immediate parent | seat within a class |
.Per<TAncestor>() | nearest ancestor of that type | roster number within a school |
State is keyed by the sequence instance and the scope entity's identity. A blueprint variant using the same sequence keeps counting within that scope, as does a later Add after .Under(schoolClass).
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchools(out var schools, 2)
.Under(schools[0])
.AddClass(out _)
.AddStudents(out var first, 2) // This school’s first class uses roster numbers 1 and 2.
// Geometry shares the school counter (roster 3) and starts its own seats (seat 1).
.Under(schools[0])
.AddClass(out _, c => c.Name = "Geometry")
.AddStudent(out var geometryStudent)
// The other school starts both counters at 1.
.Under(schools[1])
.AddClass(out _)
.AddStudent(out var otherSchoolStudent)
.BuildAsync();
// Keep a separate roster counter for each school.
var nextRoster = new Dictionary<string, int> { [schools[0].Id] = 1, [schools[1].Id] = 1 };
var now = DateTimeOffset.UtcNow;
Student NewStudent(SchoolClass schoolClass, int seat) =>
new()
{
Id = $"stu_{Guid.CreateVersion7():N}",
DistrictId = schoolClass.DistrictId,
ClassId = schoolClass.Id,
Seat = seat,
RosterNumber = nextRoster[schoolClass.SchoolId]++,
FullName = $"Student {seat}",
EnrolledAtUtc = now,
};
Student[] first = [NewStudent(algebra, 1), NewStudent(algebra, 2)];
var geometryStudent = NewStudent(geometry, 1);
var otherSchoolStudent = NewStudent(otherSchoolClass, 1);
Explicit values win
Sometimes the number itself matters to the test. The sequence needs to leave that value available while assigning the surrounding defaults. Biome reserves explicit values before filling the unset ones:
- Explicit values never move the sequence. And the sequence skips any value already set explicitly in that scope, in this step or an earlier one.
- Within one step, Biome first reserves every explicit value, then fills the rest. The test below shows
[2, 1, 3, 4]: the first unset entry takes2because1and3are explicit.
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out var schoolClass)
.AddStudents(
out var students,
// Reserve seats 1 and 3 before any default draws from the sequence.
_ => { },
s => s.Seat = 1,
s => s.Seat = 3,
_ => { }
)
// This class already used seats 2, 1, 3, and 4, so the next student takes 5.
.Under(schoolClass)
.AddStudent(out var next)
.BuildAsync();
An explicit high value leaves the next generated seat at 1.
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out var schoolClass, c => c.Capacity = 100)
// Reserving seat 99 leaves the first generated seat at 1.
.AddStudent(out var assigned, s => s.Seat = 99)
.Under(schoolClass)
.AddStudent(out var generated)
.BuildAsync();
Prefer Derive(prop, sequence) over ctx.Next
ctx.Next(sequence) cannot see values the test will supply. That can create a collision. Prefer .Derive(property, sequence), which reserves explicit values before assigning defaults.Rules of thumb
- Declare the sequence above the blueprints that use it. In a static class, fields initialize top to bottom. Keep a base blueprint and its variants in the same file; across
partialfiles the initialization order is unspecified. Derives run in declaration order, so deriveFullNameafter the seat number it uses.- Variants made with
.Withshare their base blueprint's sequence. - Globally unique values such as student IDs use a
PerSessionsequence orctx.NewId. Parent-scoped sequences can generate seat 1 in two different classes. - Sequences also support non-numeric values, such as classroom row labels.
- A
nextfunction that cycles through reserved values throws, rather than hanging.
Non-numeric sequences: ranks
A seating chart can label the rows in each class A, B, and C. The next function here steps through single letters for a small chart. The same class shares its sequence across Adds.
// Start at A for each class and advance to the next letter for each row.
private static readonly Sequence<string> RowLabels = Sequence
.Of("A", label => ((char)(label[0] + 1)).ToString())
.PerParent();
public static readonly ChildBlueprint<SchoolSeed, SchoolClass, SeatingRow> SeatingRow =
Blueprint
.ChildOf<SchoolSeed, SchoolClass, SeatingRow>(
(schoolClass, ctx) =>
new()
{
Id = ctx.NewId("row"),
DistrictId = schoolClass.DistrictId,
ClassId = schoolClass.Id,
Label = null!, // Leave the required label unset so the sequence can fill it.
}
)
.Derive(row => row.Label, RowLabels); // Preserve any label the test reserves explicitly.
Label as required string with a setter on SeatingRow. The factory leaves it unset with null! so the sequence can fill it. An empty string counts as a value.await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out var school)
.AddClasses(out var classes, 2)
.AddSeatingRows(out var rows, 2) // Each class uses A and B before the next Add.
// Resume the first class at C because it already used A and B.
.Under(classes[0])
.AddSeatingRow(out var next)
// A new class starts at A with its own counter.
.Under(school)
.AddClass(out _)
.AddSeatingRow(out var siblingRow)
.BuildAsync();
See Board for a test that reserves an explicit rank.