Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mylittlesuite/data_fixture_dart
https://github.com/mylittlesuite/data_fixture_dart
Last synced: 1 day ago
JSON representation
- Host: GitHub
- URL: https://github.com/mylittlesuite/data_fixture_dart
- Owner: MyLittleSuite
- License: mit
- Created: 2020-11-18T19:09:11.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-09T15:25:04.000Z (almost 1 year ago)
- Last Synced: 2024-01-09T16:48:36.810Z (almost 1 year ago)
- Language: Dart
- Size: 31.3 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# data_fixture_dart
![data_fixture_dart_ci](https://github.com/MyLittleSuite/data_fixture_dart/workflows/Dart/badge.svg)
[![Pub](https://img.shields.io/pub/v/data_fixture_dart.svg)](https://pub.dev/packages/data_fixture_dart)
[![License: MIT](https://img.shields.io/badge/license-MIT-purple.svg)](https://opensource.org/licenses/MIT)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/dwyl/esta/issues)Create data models easily, with no headache.
## Usage
### Basic
1. Create a new file to define the fixture factory for a model.
```dart
import 'package:data_fixture_dart/data_fixture_dart.dart';class Company {
final String name;
final List employees;Company({this.name, this.employees});
}extension CompanyFixture on Company {
static _CompanyFixtureFactory factory() => _CompanyFixtureFactory();
}class _CompanyFixtureFactory extends FixtureFactory {
@override
FixtureDefinition 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 empty(String name) => redefine(
(company) => Company(
name: name,
employees: [],
),
);
}
```2. Then you can build the model by using its factory.
```dart
// 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);
```### JSON Fixtures
A factory can create a JSON Object from a generated model.
1. First, you have to extend `JSONFixtureFactory` protocol to the model factory.
```dart
import 'package:data_fixture_dart/data_fixture_dart.dart';extension CompanyFixture on Company {
static _CompanyFixtureFactory factory() => _CompanyFixtureFactory();
}class _CompanyFixtureFactory extends JsonFixtureFactory {
@override
FixtureDefinition 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 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 empty(String name) => redefineJson(
(company) => Company(
name: name,
employees: [],
),
);
}
```2. Now you can generate the JSON Object of the model.
```dart
// 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);
```3. With `JsonFixtureFactory` you can create a JSON from an external model object.
```dart
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);
```### 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`
```dart
import 'package:data_fixture_dart/data_fixture_dart.dart';extension NewsArticleFixture on NewsArticle {
static _NewsArticleFactory factory() => _NewsArticleFactory();
}class _NewsArticleFixtureFactory extends FixtureFactory {
@override
FixtureDefinition 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 noContent() => redefine(
(newsArticle) => NewsArticle(
title: faker.lorem.sentence(),
content: null,
),
faker: Faker(
seed: Random().nextInt(9876543210),
provider: FakerDataProvider(
loremDataProvider: MyOtherCustomLoremDataProvider(),
),
),
);
}
```## 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```shell
dart test
```