Skip to main content

Test Fixtures

Generate a deterministic set of fake user records using the Faker provider and range + map.

Source

examples/test-fixtures/main.rank
/// Generates deterministic test fixture data using the Faker provider.
/// Shows Rand::seed for reproducibility, range + map to produce N records,
/// and typed schemas to validate the generated shape.

use faker::{ Generate, UUID, Person, Int }
use faker::types::{ Spec }
use std::list::{ range }
use std::collections::{ map }

User = Object {
id: string,
name: string,
age: number,
}

seed = 42
count = 10

spec: Spec<User> = {
id: UUID {},
name: Person { sex: `female` },
age: Int { min: 18, max: 65 },
}

users: [User] = range(count)
|> map(|_, ctx| Generate<User> {
spec,
seed,
itemKey: ctx.index,
locale: `en_US`,
})

pub main = || {
count: count,
users: users,
}

Output

{
"count": 10,
"users": [
{ "id": "a1b2c3d4-...", "name": "Alice Johnson", "age": 34 },
{ "id": "e5f6a7b8-...", "name": "Maria Garcia", "age": 27 },
...
]
}

The output is identical on every run because seed and itemKey together fully determine each generated value.

Key concepts

  • Spec<T> — a typed descriptor that pairs each field of T with a Faker generator. The compiler checks that every field is covered.
  • Generate<T> — evaluates a Spec<T> with a seed and item key. Different itemKey values produce different records; the same key always produces the same record.
  • range(count) |> map — generates indices [0, 1, ..., count-1] and maps each to a Generate call, passing ctx.index as the itemKey.
  • ctx parameter — the second argument to map callbacks is an iterator context with index, size, isFirst, and isLast.

Run it

rank examples/test-fixtures

Requires the faker provider package. See the Faker Provider docs for installation.