Charlatan - Expressive fixtures generator
Relying on brianvoe/gofakeit, Charlatan allows you to create a ton of fixtures/fake data for use while developing or testing your project.
It is inspired by nelmio/alice.It gives you a few essential tools to make it very easy to generate complex data in a readable and easy to edit way,
so that everyone on your team can tweak the fixtures if needed.
Table of content
- Installation
- Example
- Complete Reference
- Handling Relations
- Data generator
Installation
First, get it:
go get -u github.com/msales/charlatan
Internal Postgres test database service.
Use internal postgres database fixture loader and rebuilder for ease of use
cfg := postgres_test.Config{
Host: "localhost",
Port: 5432,
User: "user",
Password: "pass",
MainDirectory: "project-root",
FixturesPath: "/fixtures"
ProjectPath: "/app",
Migrations: yourMigrations,
OverrideFunc: postgres_test.GithubActionsOverride, // use this when you use Github Actions tests.
}
postgres_test.Initialize(cfg) // initialize template and test database
postgres_test.RebuildDatabase() // clear database and rebuild with fresh data from fixtures.
db := postgres_test.TestDB() // connect to test database
db := postgres_test.TemplateDB() // connect to template database
Recommended usage.
Create file in another package that will wrap postgres_test.RebuildDatabase()
. E.g. test/functional.go
This way you assure test and template database initialization for every functional test that uses test database builder.
func init() {
postgres_test.Initialize(createConfig())
}
// RebuildDatabase is wrapper around charlatan's internal rebuilder so init will always happen.
func RebuildDatabase() {
postgres_test.RebuildDatabase()
}
Example
Here is a complete example of a declaration:
user:
user_tpl (template):
first_name: '<FirstName()>'
last_name: '<LastName()>'
pseudo: '<UserName()>'
password: '<Words(2,true)>'
email : '<Email()>'
admin_1:
first_name: 'William'
last_name: 'Wallace'
pseudo: 'WW'
password: 'freedommmmmmm'
email : 'freedom@gouv.co.uk'
isAdmin: true
admin_{2..5} (extends user_tpl):
isAdmin: true
user_{bob,harry,george} (extends user_tpl):
isAdmin: false
You can then load them easily with:
charlatan load --fixtures ./fixtures --user=<your_db_user> --dbname=<your_dbname> --pass=<your_db_pass>
Compatible databases
- MySQL / MariaDB
- PostgreSQL (in progress)