0 purchases
data fixture dart
data_fixture_dart #
Create data models easily, with no headache.
Usage #
Basic #
Create a new file to define the fixture factory for a model.
import 'package:data_fixture_dart/data_fixture_dart.dart';
class Company {
final String name;
final List<Person> employees;
Company({this.name, this.employees});
}
extension CompanyFixture on Company {
static _CompanyFixtureFactory factory() => _CompanyFixtureFactory();
}
class _CompanyFixtureFactory extends FixtureFactory<Company> {
@override
FixtureDefinition<Company> definition() => define(
(faker) => Company(
name: faker.company.name(),
employees: PersonFixture.factory().makeMany(5),
),
);
// If you need to override a model field, simply define a function that returns a `FixtureDefinition`.
// To redefine the default definition, you must use the `redefine` function.
FixtureDefinition<Company> empty(String name) => redefine(
(company) => Company(
name: name,
employees: [],
),
);
}
copied to clipboard
Then you can build the model by using its factory.
// Create a single object of type Company.
CompanyFixture.factory().makeSingle();
// Create a single object of type Company with no employees.
CompanyFixture.factory().empty("EmptyCompany").make();
// Create 10 objects of type Company.
CompanyFixture.factory().makeMany(10);
// Create 10 objects of type Company with no employees.
CompanyFixture.factory().empty("EmptyCompany").makeMany(10);
copied to clipboard
JSON Fixtures #
A factory can create a JSON Object from a generated model.
First, you have to extend JSONFixtureFactory protocol to the model factory.
import 'package:data_fixture_dart/data_fixture_dart.dart';
extension CompanyFixture on Company {
static _CompanyFixtureFactory factory() => _CompanyFixtureFactory();
}
class _CompanyFixtureFactory extends JsonFixtureFactory<Company> {
@override
FixtureDefinition<Company> definition() => define(
(faker) => Company(
name: faker.company.name(),
employees: PersonFixture.factory().makeMany(5),
),
);
// This function define the json definition, using the default definition (function `definition()`).
@override
JsonFixtureDefinition<Company> jsonDefinition() => defineJson(
(company) => {
"name": company.name,
"employees":
PersonFixture.factory().makeJsonArrayFromMany(company.employees),
},
);
// If you need to generate the JSON Object of an empty company, change the return type to `JSONFixtureDefinition`
// Previously the return was `FixtureDefinition`.
JsonFixtureDefinition<Company> empty(String name) => redefineJson(
(company) => Company(
name: name,
employees: [],
),
);
}
copied to clipboard
Now you can generate the JSON Object of the model.
// Create a single JSON object of type Company.
CompanyFixture.factory().makeJsonObject();
// Create a single JSON object of type Company with no employees.
CompanyFixture.factory().empty("EmptyCompany").makeJsonObject();
// Create a JSON Array of 10 objects of type Company.
CompanyFixture.factory().makeJsonArray(10)
// Create a JSON Array of 10 objects of type Company with no employees.
CompanyFixture.factory().empty("EmptyCompany").makeJsonArray(10);
// Create a Company object with its relative JSON object.
CompanyFixture.factory().makeSingleWithJsonObject();
// Create 10 Company object with its relative JSON objects.
CompanyFixture.factory().makeManyWithJsonArray(10);
copied to clipboard
With JsonFixtureFactory you can create a JSON from an external model object.
final company = CompanyFixture.factory.makeSingle();
final JSONObject = CompanyFixture.factory.makeJsonObjectFromSingle(from: company);
final companies = CompanyFixture.factory.makeMany(3);
final JSONArray = CompanyFixture.factory.makeJsonArrayFromMany(from: companies);
copied to clipboard
Using a custom Faker instance #
Sometimes you want your Faker to have maybe a custom seed or custom provider.
In that case you can simply pass a custom Faker instance to either define or redefine
import 'package:data_fixture_dart/data_fixture_dart.dart';
extension NewsArticleFixture on NewsArticle {
static _NewsArticleFactory factory() => _NewsArticleFactory();
}
class _NewsArticleFixtureFactory extends FixtureFactory<NewsArticle> {
@override
FixtureDefinition<NewsArticle> definition() => define(
(Faker faker) => NewsArticle(
title: faker.lorem.sentence(),
content: faker.lorem.sentences(3).join(' '),
),
faker: Faker(
seed: Random().nextInt(1234567890),
provider: FakerDataProvider(
loremDataProvider: MyCustomLoremDataProvider(),
),
),
);
FixtureDefinition<Company> noContent() => redefine(
(newsArticle) => NewsArticle(
title: faker.lorem.sentence(),
content: null,
),
faker: Faker(
seed: Random().nextInt(9876543210),
provider: FakerDataProvider(
loremDataProvider: MyOtherCustomLoremDataProvider(),
),
),
);
}
copied to clipboard
Contributing #
data_fixture_dart is an open source project, so feel free to contribute.
You can open an issue for problems or suggestions, and you can propose your own fixes by opening a pull request with the changes.
Testing #
In order to test the package run this command
dart test
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.