Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andreadelfante/DataFixture
Creation of data model easily, with no headache.
https://github.com/andreadelfante/DataFixture
cocoapods database fake fixture ios library model pods realm realmswift seeders swift
Last synced: about 1 month ago
JSON representation
Creation of data model easily, with no headache.
- Host: GitHub
- URL: https://github.com/andreadelfante/DataFixture
- Owner: andreadelfante
- License: mit
- Created: 2020-01-27T12:51:30.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-10-06T09:08:54.000Z (over 2 years ago)
- Last Synced: 2024-11-18T03:40:55.794Z (about 2 months ago)
- Topics: cocoapods, database, fake, fixture, ios, library, model, pods, realm, realmswift, seeders, swift
- Language: Swift
- Homepage:
- Size: 1.04 MB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - DataFixture - Creation of data model easily, with no headache. (Testing / Other Testing)
- awesome-ios-star - DataFixture - Creation of data model easily, with no headache. (Testing / Other Testing)
README
# DataFixture
![SwiftEssentialsKit CI](https://github.com/andreadelfante/DataFixture/workflows/DataFixture%20CI/badge.svg)
[![Version](https://img.shields.io/cocoapods/v/DataFixture.svg?style=flat)](https://cocoapods.org/pods/DataFixture)
[![License](https://img.shields.io/cocoapods/l/DataFixture.svg?style=flat)](https://cocoapods.org/pods/DataFixture)
[![Platform](https://img.shields.io/cocoapods/p/DataFixture.svg?style=flat)](https://cocoapods.org/pods/DataFixture)
[![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. DataFixture is a convenient way to generate new data for testing / seeding your Realm Database.
## Installation
### Cocoapods
> CocoaPods 0.39.0+ is required to build this libraryTo install DataFixture, simply add in your Podfile `pod 'DataFixture'` and run `pod install`
## Usage
### Basic
1. Create a new file to define the fixture factory for a model.
```swift
import DataFixtureextension Company: FixtureFactoryable {
static var factory: CompanyFixtureFactory {
return CompanyFixtureFactory()
}
}struct CompanyFixtureFactory: FixtureFactory {
typealias Model = Company
func definition() -> FixtureDefinition {
define { (faker) in
Company(
name: faker.company.name(),
employees: Person.factory.make(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.
func empty(name: String) -> FixtureDefinition {
redefine { (company) in
company.name = name
company.employees = []
}
}
}
```2. Then you can build the model by using its factory.
```swift
// Create a single object of type Company.
Company.factory.make()
// Create a single object of type Company with no employees.
Company.factory.empty(name: "EmptyCompany").make()// Create 10 objects of type Company.
Company.factory.make(10)
// Create 10 objects of type Company with no employees.
Company.factory.empty(name: "EmptyCompany").make(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.
```swift
import DataFixtureextension Company: FixtureFactoryable {
static var factory: CompanyFixtureFactory {
return CompanyFixtureFactory()
}
}struct CompanyFixtureFactory: JSONFixtureFactory {
typealias Model = Company
func definition() -> FixtureDefinition {
define { (faker) in
Company(
name: faker.company.name(),
employees: Person.factory.make(5)
)
}
}
// This function define the json definition, using the default definition (function `definition()`).
func jsonDefinition() -> JSONFixtureDefinition {
defineJSON { (company) -> [String : Any] in
[
"name": company.name,
"employees": Person.factory.makeJSON(from: company.employees)
]
}
}
// If you need to generate the JSON Object of an empty company, change the return type to `JSONFixtureDefinition`
func empty(name: String) -> JSONFixtureDefinition { // Previously `FixtureDefinition`
redefine { (company) in
company.name = name
company.employees = []
}
}
}
```2. Now you can generate the JSON Object of the model.
```swift
// Create a single JSON object of type Company.
Company.factory.makeJSON()
// Create a single JSON object of type Company with no employees.
Company.factory.empty(name: "EmptyCompany").makeJSON()// Create a JSON Array of 10 objects of type Company.
Company.factory.makeJSON(10)
// Create a JSON Array of 10 objects of type Company with no employees.
Company.factory.empty(name: "EmptyCompany").makeJSON(10)// Create a Company object with its relative JSON object.
Company.factory.makeWithJSON()
// Create 10 Company object with its relative JSON objects.
Company.factory.makeWithJSON(10)
```3. With `JSONFixtureFactory` you can create a JSON from an external model object.
```swift
let company = Company.factory.make()
let JSONObject = Company.factory.makeJSON(from: company)let companies = Company.factory.make(3)
let JSONArray = Company.factory.makeJSON(from: companies)
```## RealmSeeder
This submodule can seed some data easily in [Realm](https://github.com/realm/realm-cocoa) Database, using Seeder.
First of all, define it in your Podfile `pod 'DataFixture/RealmSeeder'`. Then create a new `struct` to define a RealmSeeder.
```swift
import DataFixturestruct ExampleSeeder: RealmSeeder {
func run(realm: Realm) throws {
// Put here your database population
realm.add(Person(firstName: "Luke"), update: .all) // You can simply create an object and then add in Realm instance.
try Dog.factory.create(10, in: realm) // You can easily create 10 fake dogs and then add in Realm instance.
try realm.seed(AnotherSeeder.self, AnotherAnotherSeeder.self) // To call another seed, please use this function to automatic handling transactions.
}
}
```To run the `ExampleSeeder` just call the **seed** function on a Realm instance. This function automatically starts a transaction if needed.
```swift
try realm.seed(ExampleSeeder.self)
```## Documentation
Click [here](https://andreadelfante.github.io/DataFixture/index.html) to read the complete DataFixture API documentation.## Contributing
DataFixture 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.