Faker
Generate fake values for your tests.
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()
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
- Switching language
- Fake data
- Date
- Identifiers
- Text
- Physical persons
- Other
- Extending/Custom fake data
### Switching language
module test
import khalyomede.faker { Faker }
fn test_it_switches_language() {
mut fake := Faker{lang: .en}
// ...
}
Boolean
module test
import khalyomede.faker { Faker }
fn test_it_returns_a_value() {
mut fake := Faker{}
terms_of_use_accepted := fake.boolean()
// ...
}
First name
module test
import khalyomede.faker { Faker }
fn test_it_generates_first_name() {
mut fake := Faker{}
first_name := fake.first_name()
// ...
}
Future date
module test
import khalyomede.faker { Faker }
fn test_it_generates_future_date() {
mut fake := Faker{}
meeting_date := fake.future_date()
// ...
}
Last name
module test
import khalyomede.faker { Faker }
fn test_it_generates_last_name() {
mut fake := Faker{}
last_name := fake.last_name()
// ...
}
Past date
module test
import khalyomede.faker { Faker }
fn test_it_generates_past_date() {
mut fake := Faker{}
payment_date := fake.past_date()
}
### 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)
// ...
}
Sentence
module test
import khalyomede.faker { Faker }
fn test_it_generates_sentence() {
mut fake := Faker{}
book_excerpt := fake.sentence()
}
UUID v4
module test
import khalyomede.faker { Faker }
fn test_it_returns_url() {
mut fake := Faker{}
payment_id := fake.uuid_v4()
// ...
}
Word
module test
import khalyomede.faker { Faker }
fn test_it_generates_word() {
mut fake := Faker{}
book_category := fake.word()
// ...
}
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()
}
Q&A
- Why is the fake variable mutable?
- Why can't I use this module to compile my program to a single binary?
Why is the fake variable mutable?
The
Fake{}
For example, calling 10 times
fake.first_name()
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.
We could improve the performance by using a
__global
v -enable-globals test .
Let me know in an issue if you think this is a good idea.
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
!string
panic()
Open question