DataFixture 2.1.0

DataFixture 2.1.0

Andrea Del Fante维护。



DataFixture

SwiftEssentialsKit CI Version License Platform contributions welcome

轻松创建数据模型,无需烦恼。DataFixture是生成测试/种子Realm数据库的新数据的便捷方法。

安装

Cocoapods

构建此库需要CocoaPods 0.39.0+

要安装DataFixture,只需在Podfile中添加pod 'DataFixture'并运行pod install

使用方法

基本使用

  1. 创建一个新文件来定义模型的测试 fixture 工厂。
import DataFixture

extension Company: FixtureFactoryable {
    static var factory: CompanyFixtureFactory {
        return CompanyFixtureFactory()
    }
}

struct CompanyFixtureFactory: FixtureFactory {
    typealias Model = Company
    
    func definition() -> FixtureDefinition<Company> {
        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<Company> {
        redefine { (company) in
            company.name = name
            company.employees = []
        }
    }
}
  1. 然后你可以通过使用其工厂来构建模型。
// 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

工厂可以创建一个 JSON 对象,该对象基于生成的模型。

  1. 首先,您需要将 JSONFixtureFactory 协议扩展到模型工厂。
import DataFixture

extension Company: FixtureFactoryable {
    static var factory: CompanyFixtureFactory {
        return CompanyFixtureFactory()
    }
}

struct CompanyFixtureFactory: JSONFixtureFactory {
    typealias Model = Company
    
    func definition() -> FixtureDefinition<Company> {
        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<Company> {
        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<Company> { // Previously `FixtureDefinition`
        redefine { (company) in
            company.name = name
            company.employees = []
        }
    }
}
  1. 现在您可以生成模型的 JSON 对象。
// 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)
  1. 使用 JSONFixtureFactory,您可以从外部模型对象创建 JSON。
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

这个子模块可以通过 Seeder 容易地种子数据到 Realm 数据库。首先,在您的 Podfile 中定义它: pod 'DataFixture/RealmSeeder'。然后创建一个新的 struct 来定义 Realm Seeder。

import DataFixture

struct 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.
    }
}

要运行 ExampleSeeder,只需在 Realm 实例上调用 seed 函数。如果需要,此函数会自动开启事务。

try realm.seed(ExampleSeeder.self)

文档

点击这里阅读完整的 DataFixture API 文档。

贡献

DataFixture 是一个开源项目,所以请随时贡献。您可以提出问题或建议的问题,通过提出包含更改的 pull 请求来提出自己的修复建议。