Skip to content

Fake data

Basic usage

You can generate fake data, like person names, random integers, ... Useful for tests.

v
import khalyomede.mantis.http { create_app, App, Response, Request, Status }
import khalyomede.mantis.http.route
import khalyomede.mantis.test { expect, Fake }

const fake := Fake{}

fn test_it_returns_200_for_home_page() {
  content := fake.sentence()

  app := create_app(
    routes: [
      route.get(path: "/", callback: fn [content] (app App) !Response {
        return app.response.html(content: content)
      })
    ]
    request: Request{
      method: .get
      path: "/"
    }
  )

  res := app.render()

  expect(res.status).to_be_equal_to(Status.ok)
  expect(res.content).to_be_equal_to(content)
}

Custom data

The Fake struct data are called "generators". See the available generators section for a complete list.

You can create your own like this.

v
import khalyomede.mantis.test { expect }
import rand

struct Payment {}

fn (payment Payment) status() string {
  return rand.element(["pending", "validated", "rejected", "cancelled"]) or { "pending" }
}

struct Fake {
  test.Fake
  payment Payment
}

const fake := Fake{}

fn test_it_can_generate_payment_status() {
  status := fake.payment.status()

  expect(status).to_not_be_empty()
}

Available generators

Company name

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  company_name := fake.company.name() // "Google"
}

Country code alpha 2

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  code := fake.country.code.alpha2() // "us"
}

Country code alpha 2

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  code := fake.country.code.alpha3() // "usa"
}

Database primary key

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  key := fake.database.primary_key() // "129"
}

Email

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  email := fake.email() // "john.doe@example.com"
}

Integer between

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  number := fake.integer.between(1, 20) // 12
}

Internet base url

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  base_url := fake.internet.base_url() // "https://example.com"
}

Internet domain

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  domain := fake.internet.domain() // "example"
}

Internet protocol

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  protocol := fake.internet.protocol() // "https"
}

Internet top level domain

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  tld := fake.internet.top_level_domain() // "com"
}

Internet port

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  port := fake.internet.port() // 3306
}

Lang

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  lang := fake.lang() // "en"
}

Negative integer

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  number := fake.negative_integer() // -125
}

Person first name

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  name := fake.person.first_name() // "John"
}

Person last name

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  name := fake.person.last_name() // "Doe"
}

Person name

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  name := fake.person.name() // "John Doe"
}

Sentence

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  sentence := fake.sentence() // "The meeting has been rescheduled."
}

UUID v4

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  uuid := fake.uuid.v4() // "4c9c5f06-ea31-4548-a200-82ab7fe14017"
}

Word

v
module main

import khalyomede.mantis.test { Fake }

fn main() {
  fake := Fake{}

  uuid := fake.word() // "eloquent"
}