Repository

Faker

Generate fake values for your tests.

module test

import khalyomede.faker { Faker }

fn test_it_generates_email() {
  mut fake := Faker{}

  email := fake.email()

  // ...
}

Summary

About

I created this package to avoid having to hard code testing values, like UUIDs, words, first names, ... When testing my features.

Features

  • Provide various functions to get semi-real values
  • Supports the following languages:
    • English
  • Consumes the least virtual memory possible
  • Ensures maximum uniqueness by keeping track of already picked values

Disclaimer

Please do not use this library in production or when compiling V to a single binary.

This library uses heavy .txt files as source of data, and they are not embeded in the final compiled binary (primarily because they would make your executable much more heavy).

Plus, for simplicity, the package immediately calls panic() for each error encountered, which is unsuitable for production.

Use this library solely within your "*_test.v" file.

Installation

Using V installer

In your root folder, open a terminal and run this command:

v install khalyomede.faker

Manual installation

  • Locate your V modules folder (usually in "/$USER/.vmodules", so if you're using the root user, "/root/.vmodules")
  • Create a folder named "khalyomede"
  • Inside "khalyomed" folder, create a folder named "faker"
  • Copy the entire content of this repositor in the "$USER/.vmodules/khalyomede/faker" repository

Examples

Base URL

module test

import khalyomede.faker { Faker }

fn test_it_generates_base_url() {
  mut fake := Faker{}

  base_url := fake.base_url()
}

Back to examples

Boolean

module test

import khalyomede.faker { Faker }

fn test_it_returns_a_value() {
  mut fake := Faker{}

  terms_of_use_accepted := fake.boolean()

  // ...
}

Back to examples

City

module test

import khalyomede.faker { Faker }

fn test_it_generates_city() {
  mut fake := Faker{}

  city := fake.city()

  // ...
}

Back to examples

Company name

module test

import khalyomede.faker { Faker }

fn test_it_generates_company_name() {
  mut fake := Faker{}

  company := fake.company_name()

  // ...
}

Back to examples

Country code

module test

import khalyomede.faker { Faker }

fn test_it_generates_country_codes() {
  mut fake := Faker{}

  code2 := fake.country_code(format: .alpha_2)
  code3 := fake.country_code(format: .alpha_3)

  // ...
}

Country name

module test

import khalyomede.faker { Faker }

fn test_it_generates_country_name() {
  mut fake := Faker{}

  name := fake.country_name()

  // ...
}

Back to examples

Currency code

module test

import khalyomede.faker { Faker }

fn test_it_generates_currency_code() {
  mut fake := Faker{}

  code := fake.currency_code()

  // ...
}

Back to examples

Currency symbol

module test

import khalyomede.faker { Faker }

fn test_it_generates_currency_symbol() {
  mut fake := Faker{}

  symbol := fake.currency_symbol()

  // ...
}

Back to examples

EAN-13

module test

import khalyomede.faker { Faker }

fn test_it_generates_ean_13() {
  mut fake := Faker{}

  code := fake.ean_13()

  // ...
}

Back to examples

Email

module test

import khalyomede.faker { Faker }

fn test_it_generates_email() {
  mut fake := Faker{}

  email := fake.email()

  // ...
}

Back to examples

f32

module test

import khalyomede.faker { Faker }

fn test_it_generates_f32() {
  mut fake := Faker{}

  number := fake.f32()

  // ...
}

Back to examples

f32 between

module test

import khalyomede.faker { Faker }

fn test_it_generates_f32_between_range() {
  mut fake := Faker{}

  number := fake.f32_between(min: -35.546, max: 106.71)

  // ...
}

Back to examples

f64

module test

import khalyomede.faker { Faker }

fn test_it_generates_f64() {
  mut fake := Faker{}

  number := fake.f64()

  // ...
}

Back to examples

f64 between

module test

import khalyomede.faker { Faker }

fn test_it_generates_f64_between_range() {
  mut fake := Faker{}

  number := fake.f64_between(min: -35_000.546, max: 106_000.71)

  // ...
}

Back to examples

First name

module test

import khalyomede.faker { Faker }

fn test_it_generates_first_name() {
  mut fake := Faker{}

  first_name := fake.first_name()

  // ...
}

Back to examples

Future date

module test

import khalyomede.faker { Faker }

fn test_it_generates_future_date() {
  fake := Faker{}

  meeting_date := fake.future_date()

  // ...
}

Back to examples

i16

module test

import khalyomede.faker { Faker }

fn test_it_generates_i16() {
  fake := Faker{}

  number := fake.i16()

  // ...
}

Back to examples

i16 between

module test

import khalyomede.faker { Faker }

fn test_it_generates_i16_between_range() {
  fake := Faker{}

  number := fake.i16_between(min: -121, max: 595)

  // ...
}

Back to examples

i32

module test

import khalyomede.faker { Faker }

fn test_it_generates_i32() {
  fake := Faker{}

  number := fake.i32()

  // ...
}

Back to examples

i32 between

module test

import khalyomede.faker { Faker }

fn test_it_generates_i32_between_range() {
  fake := Faker{}

  number := fake.i32_between(min: -1_229, max: 5_941)

  // ...
}

Back to examples

i64

module test

import khalyomede.faker { Faker }

fn test_it_generates_i64() {
  fake := Faker{}

  number := fake.i64()

  // ...
}

Back to examples

i64 between

module test

import khalyomede.faker { Faker }

fn test_it_generates_i64_between_range() {
  fake := Faker{}

  number := fake.i64_between(min: -12_500, max: 59_000)

  // ...
}

Back to examples

i8

module test

import khalyomede.faker { Faker }

fn test_it_generates_i8() {
  fake := Faker{}

  number := fake.i8()

  // ...
}

Back to examples

i8 between

module test

import khalyomede.faker { Faker }

fn test_it_generates_i8_between_range() {
  fake := Faker{}

  number := fake.i8_between(min: -12, max: 59)

  // ...
}

Back to examples

IP V4

module test

import khalyomede.faker { Faker }

fn test_it_generates_ip_v4() {
  mut fake := Faker{}

  ip := fake.ip_v4()

  // ...
}

Back to examples

IP V6

module test

import khalyomede.faker { Faker }

fn test_it_generates_ip_v6() {
  mut fake := Faker{}

  ip := fake.ip_v6()

  // ...
}

Back to examples

Job title

module test

import khalyomede.faker { Faker }

fn test_it_generates_job_title() {
  mut fake := Faker{}

  job := fake.job_title()

  // ...
}

Back to examples

Hex color

module test

import khalyomede.faker { Faker }

fn test_it_generates_hex_color() {
  mut fake := Faker{}

  color := fake.hex_color()

  // ...
}

Back to examples

Last name

module test

import khalyomede.faker { Faker }

fn test_it_generates_last_name() {
  mut fake := Faker{}

  last_name := fake.last_name()

  // ...
}

Back to examples

Latitude

module test

import khalyomede.faker { Faker }

fn test_it_generates_latitude() {
  mut fake := Faker{}

  latitude := fake.latitude()

  // ...
}

Back to examples

Longitude

module test

import khalyomede.faker { Faker }

fn test_it_generates_longitude() {
  mut fake := Faker{}

  longitude := fake.longitude()

  // ...
}

Back to examples

MAC address

module test

import khalyomede.faker { Faker }

fn test_it_generates_mac_address() {
  mut fake := Faker{}

  mac_address := fake.mac_address()

  // ...
}

Back to examples

Past date

module test

import khalyomede.faker { Faker }

fn test_it_generates_past_date() {
  fake := Faker{}

  payment_date := fake.past_date()
}

Back to examples

### Random element

module test

import khalyomede.faker { Faker }

fn test_it_generates_random_fruit() {
  mut fake := Faker{}

  fruits := ["banana", "kiwi", "apple"]

  fruit := fake.random_element(fruits)

  // ...
}

Back to examples

Sentence

module test

import khalyomede.faker { Faker }

fn test_it_generates_sentence() {
  mut fake := Faker{}

  book_excerpt := fake.sentence()
}

Back to examples

Street

The different with street name is street is prefixed with the street number.

module test

import khalyomede.faker { Faker }

fn test_it_generates_street() {
  mut fake := Faker{}

  street := fake.street()

  // ...
}

Back to examples

Street name

The different with street is street name is not prefixed with the street number.

module test

import khalyomede.faker { Faker }

fn test_it_generates_street_name() {
  mut fake := Faker{}

  street_name := fake.street_name()

  // ...
}

Back to examples

Top level domain

module test

import khalyomede.faker { Faker }

fn test_it_generates_top_level_domain() {
  mut fake := Faker{}

  top_level_domain := fake.top_level_domain()

  // ...
}

Back to examples

u16

module test

import faker { Faker }

fn test_it_generates_u16() {
  mut fake := Faker{}

  number := fake.u16()

  // ...
}

Back to examples

u16 between

module test

import faker { Faker }

fn test_it_generates_u16_between_two_numbers() {
  mut fake := Faker{}

  number := fake.u16_between(min: 1, max: 65_535)

  // ...
}

Back to examples

u32

module test

import faker { Faker }

fn test_it_generates_u32() {
  mut fake := Faker{}

  number := fake.u32()

  // ...
}

Back to examples

u32 between

module test

import faker { Faker }

fn test_it_generates_u32_between_two_numbers() {
  mut fake := Faker{}

  number := fake.u32_between(min: 12_500, max: 55_000)

  // ...
}

Back to examples

u64

module test

import faker { Faker }

fn test_it_generates_u64() {
  mut fake := Faker{}

  number := fake.u64()

  // ...
}

Back to examples

u64 between

module test

import faker { Faker }

fn test_it_generates_u64_between_two_numbers() {
  mut fake := Faker{}

  number := fake.u64_between(min: 1, max: 1_000_000)

  // ...
}

Back to examples

u8

module test

import faker { Faker }

fn test_it_generates_u8() {
  mut fake := Faker{}

  number := fake.u8()

  // ...
}

Back to examples

u8 between

module test

import faker { Faker }

fn test_it_generates_u8_between_two_numbers() {
  mut fake := Faker{}

  number := fake.u8_between(min: 12, max: 50)

  // ...
}

Back to examples

User agent

module test

import khalyomede.faker { Faker }

fn test_it_generates_user_agent() {
  mut fake := Faker{}

  user_agent := fake.user_agent()

  // ...
}

Back to examples

UUID v4

module test

import khalyomede.faker { Faker }

fn test_it_returns_url() {
  fake := Faker{}

  payment_id := fake.uuid_v4()

  // ...
}

Back to examples

Visa credit card number

module test

import khalyomede.faker { Faker }

fn test_it_generates_visa_credit_card_number() {
  mut fake := Faker{}

  credit_card := fake.visa_credit_card_number()

  // ...
}

Back to examples

Word

module test

import khalyomede.faker { Faker }

fn test_it_generates_word() {
  mut fake := Faker{}

  book_category := fake.word()

  // ...
}

Back to examples

Zip code

module test

import khalyomede.faker { Faker }

fn test_it_generates_zip_code() {
  mut fake := Faker{}

  zip_code := fake.zip_code()

  // ...
}

Back to examples

### Switching language

From instanciation

module test

import khalyomede.faker { Faker }

fn test_it_switches_language() {
  mut fake := Faker{lang: .en}

  // ...
}

In between tests

module test

import khalyomede.faker { Faker }

fn test_it_switches_language_after_generating_data() {
  mut fake := Faker{lang: .en}

  book_excerpt := fake.sentence()

  fake.using_lang(.en)

  // ...
}

Back to examples

Extending/Custom fake data

Using struct inheritance

module test

import khalyomede.faker { Faker }

struct CustomFaker {
  Faker
}

fn (mut custom_faker CustomFaker) fruit() string {
  return custom_faker.random_element(["banana", "orange", "apple"])
}

fn test_it_generates_fruit() {
  mut fake := CustomFaker{}

  fruit := fake.fruit()

  // ...
}

Using a type alias

module test

import khalyomede.faker { Faker }

type CustomFaker = Faker

fn (custom_faker CustomFaker) fruit() string {
  return custom_faker.random_element(["banana", "orange", "apple"])
}

fn test_it_generates_fruit() {
  mut fake := CustomFaker{}

  fruit := fake.fruit()
}

Back to examples

Deterministic same values (seed)

Same random values across different tests

module test

import khalyomede.faker { Faker }

fn test_it_generates_the_same_word_as_second_test() {
  mut fake := Faker{}

  fake.using_seed(36)

  word := fake.word() // same as second test word
}

fn test_it_generates_the_same_word_as_first_test() {
  mut fake := Faker{}

  fake.using_seed(36)

  word := fake.word() // same as first test word
}

Clearing the seed afterward

module test

import khalyomede.faker { Faker }

fn test_it_generates_the_same_word_as_second_test() {
  mut fake := Faker{}

  fake.using_seed(36)

  first_word := fake.word() // same as second test's first word

  fake.clear_seed()

  second_word := fake.word() // Different than second test's second word
}

fn test_it_generates_the_same_word_as_first_test() {
  mut fake := Faker{}

  fake.using_seed(36)

  first_word := fake.word() // same as first test's first word

  fake.clear_seed()

  second_word := fake.word() // Different than first test's second word
}

Back to examples

Q&A

Why is the fake variable mutable?

The Fake{} instance will use caching to improve subsequent call to the same function.

For example, calling 10 times fake.first_name() will read the total number of first names once, then it will hold the number of lines in cache.

This is to help pick a random index in the range [0, number of first names].

This also prevents to store a constant with the number of fake first names in the code. Note that this caching mecanism is shared across all your fake := Faker{} instances, which limits testing slowdown to the minimum on large test suites.

Why can't I use this module to compile my program to a single binary?

Faker is a struct that takes its random values for a subset of its method from files.

This is done to help running tests on low RAM devices such as CI from Github Actions.

On a big test suite, your virtual memory consumption will be the least possible since each call to Faker method will parse the file it needs to read the data from line by line, holding a single line in memory (instead of mounting the whole 8kb+ file in memory).

Another reason is that to return plain values without error handling ( string instead of !string for example), this package immediately calls panic() (for example if the text file used as data source is unreadable etc...).

Open question : Do you have this need? Should we move to embedding all fake values in V file (in source code)? Let me know your uses cases by opening an issue.

Back to summary

About

Generate fake values for your tests.

0
32
last Sep 24

Author

khalyomede