Time and precision
Shared context clock
Reading wall-clock time separately for each row can give related entities slightly different timestamps. That difference can obscure what the test intended. Biome gives each session one clock, accessed in blueprints through ctx.Now, ctx.Ago(…), and ctx.FromNow(…).
| You pass… | ctx.Now is… |
|---|---|
| no clock | Frozen when Start is called. Every entity in the test sees the same Now. |
a TimeProvider | Read live. Advance a FakeTimeProvider during the test. |
Suppose a class accepts enrollments for five days after it opens. Add an EnrollmentClosesAtUtc property to SchoolClass and set it with ctx.FromNow(TimeSpan.FromDays(5)) in the class blueprint. That gives every enrollment test the same deadline rule. The app's EnrollmentPolicy.IsOpen compares the deadline with the current time.
FakeTimeProvider to both, then advance it to exercise the transition.// Give the setup and enrollment policy the same adjustable clock.
var time = new FakeTimeProvider(new DateTimeOffset(2026, 10, 11, 12, 0, 0, TimeSpan.Zero));
await TestBiome
.Start(out _, time: time)
.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out var algebra)
.BuildAsync();
// Advance beyond the five-day enrollment window to exercise the cutoff.
await Assert.That(EnrollmentPolicy.IsOpen(algebra, time)).IsTrue();
time.Advance(TimeSpan.FromDays(6));
await Assert.That(EnrollmentPolicy.IsOpen(algebra, time)).IsFalse();
var start = new DateTimeOffset(2026, 10, 11, 12, 0, 0, TimeSpan.Zero);
var time = new FakeTimeProvider(start);
var district = new District
{
Id = $"dist_{Guid.CreateVersion7():N}",
Name = "Riverside Unified",
TimeZoneId = "America/Chicago",
CreatedAtUtc = start,
UpdatedAtUtc = start,
};
var school = new School
{
Id = $"sch_{Guid.CreateVersion7():N}",
DistrictId = district.Id,
Name = "School 1",
Code = "S1",
CreatedAtUtc = start,
UpdatedAtUtc = start,
};
var algebra = new SchoolClass
{
Id = $"cls_{Guid.CreateVersion7():N}",
DistrictId = district.Id,
SchoolId = school.Id,
Name = "Algebra I",
Capacity = 25,
CreatedAtUtc = start,
EnrollmentClosesAtUtc = start.AddDays(5), // Apply the enrollment window to the same clock instant.
};
await Assert.That(EnrollmentPolicy.IsOpen(algebra, time)).IsTrue();
time.Advance(TimeSpan.FromDays(6));
await Assert.That(EnrollmentPolicy.IsOpen(algebra, time)).IsFalse();
Frozen by default
Without a provider, Biome captures TimeProvider.System.GetUtcNow() once at Start. Entities built a moment apart still get identical timestamps.
var biome = TestBiome.Start(out _);
biome.AddDistrict(out var first);
await Task.Delay(20); // Wall time advances while the session clock stays frozen.
await biome.AddDistrict(out var second).BuildAsync();
await Assert.That(second.CreatedAtUtc).IsEqualTo(first.CreatedAtUtc);
A live clock between Adds
Adds are eager, so a test can advance the provider between Add calls before the single save.
var instant = new DateTimeOffset(2026, 10, 11, 12, 0, 0, TimeSpan.Zero);
var time = new FakeTimeProvider(instant);
var schoolStep = TestBiome.Start(out _, time: time).AddDistrict(out _).AddSchool(out _);
schoolStep.AddTeacher(out var first);
// Advance between Adds because each teacher takes its timestamp when built.
time.Advance(TimeSpan.FromHours(2));
await schoolStep.AddTeacher(out var later).BuildAsync();
await Assert.That(first.CreatedAtUtc).IsEqualTo(instant);
await Assert.That(later.CreatedAtUtc).IsEqualTo(instant.AddHours(2));
Relative times
Use the two-parameter form with ctx.Ago or ctx.FromNow. It works with any clock.
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out _)
.AddOne(
Blueprints.Student,
out var returningStudent,
// Backdate from the same clock the test uses to check enrollment age.
(s, ctx) => s.EnrolledAtUtc = ctx.Ago(TimeSpan.FromDays(14))
)
.BuildAsync();
Precision rounding
timestamptz, so values built from those readings retain their precision when saved..NET stores 100 ns ticks. Postgres stores microseconds. An unrounded DateTimeOffset.UtcNow loses its last digit on a round trip, and an equality assertion can fail. Biome truncates every reading of "now" (frozen and test-supplied) before Ago and FromNow derive from it.
// Include seven extra ticks beyond the last whole microsecond.
var instant = new DateTimeOffset(2026, 10, 11, 12, 0, 0, TimeSpan.Zero).AddTicks(1_234_567);
var time = new FakeTimeProvider(instant);
await using var db = postgres.CreateContext();
await TestBiome.Start(db, out _, time).AddDistrict(out var district).BuildAsync();
await using var read = postgres.CreateContext();
var saved = await read.Districts.SingleAsync(d => d.Id == district.Id);
// Biome drops the seven ticks before saving, so Postgres preserves the value.
await Assert.That(district.CreatedAtUtc).IsEqualTo(instant.AddTicks(-7));
await Assert.That(saved.CreatedAtUtc).IsEqualTo(district.CreatedAtUtc);
| Option | Effect |
|---|---|
| default | 1 µs (TimeSpan.FromTicks(10)). |
options.TimePrecision(TimeSpan.FromMilliseconds(1)) | Round to a millisecond. |
options.TimePrecision(TimeSpan.Zero) | Turn rounding off. |
FakeTimeProvider for comparisons, so precision and elapsed time do not introduce an unrelated difference.