From e755e2eb25fcb9752f12c01d6d4611d4fb45c27b Mon Sep 17 00:00:00 2001 From: Harminder virk Date: Thu, 4 Jun 2020 17:27:49 +0530 Subject: [PATCH] feat: allow defining custom handler for generating stub ids --- adonis-typings/factory.ts | 51 +++++++++++++++++++++++--- src/Factory/FactoryBuilder.ts | 36 +++++++++++++++++-- src/Factory/FactoryContext.ts | 3 +- src/Factory/FactoryModel.ts | 54 +++++++++++++++++++++++++--- src/Factory/Relations/Base.ts | 6 ++-- src/Factory/Relations/BelongsTo.ts | 4 +-- src/Factory/Relations/HasMany.ts | 4 +-- src/Factory/Relations/HasOne.ts | 4 +-- src/Factory/Relations/ManyToMany.ts | 4 +-- src/Factory/index.ts | 41 +++++++++++++++++++++ test-helpers/index.ts | 5 +-- test/factory/belongs-to-spec.ts | 30 ++++++++-------- test/factory/factory-builder.spec.ts | 52 ++++++++++++++------------- test/factory/factory-model.spec.ts | 23 ++++++------ test/factory/has-many.spec.ts | 34 +++++++++--------- test/factory/has-one.spec.ts | 26 +++++++------- test/factory/many-to-many.spec.ts | 30 ++++++++-------- 17 files changed, 291 insertions(+), 116 deletions(-) create mode 100644 src/Factory/index.ts diff --git a/adonis-typings/factory.ts b/adonis-typings/factory.ts index f5233874..c311e552 100644 --- a/adonis-typings/factory.ts +++ b/adonis-typings/factory.ts @@ -41,6 +41,11 @@ declare module '@ioc:Adonis/Lucid/Factory' { ctx: FactoryContextContract, ) => Promise>>> | Partial>> + /** + * Function to generate custom stub ids + */ + export type StubIdCallback = (counter: number, model: LucidRow) => any + /** * Function to initiate a model instance. It will receive the * attributes returned by the `define` method @@ -197,6 +202,17 @@ declare module '@ioc:Adonis/Lucid/Factory' { */ useCtx (ctx: FactoryContextContract): this + /** + * Make model instance without persitance. The make method + * doesn't process relationships + */ + make ( + callback?: ( + model: InstanceType, + ctx: FactoryContextContract, + ) => void + ): Promise> + /** * Create model instance and stub out the persistance * mechanism @@ -218,6 +234,18 @@ declare module '@ioc:Adonis/Lucid/Factory' { ) => void ): Promise> + /** + * Make model instance without persitance. The makeMany method + * doesn't process relationships + */ + makeMany ( + count: number, + callback?: ( + model: InstanceType, + ctx: FactoryContextContract, + ) => void + ): Promise[]> + /** * Create one or more model instances and stub * out the persistance mechanism. @@ -242,6 +270,16 @@ declare module '@ioc:Adonis/Lucid/Factory' { ): Promise[]> } + /** + * Query contract that initiates the factory builder. Since the factory builder + * API surface is small, we also proxy all of it's methods for a nicer DX + */ + export interface FactoryBuilderQueryContract< + FactoryModel extends FactoryModelContract + > extends FactoryBuilderContract { + query (): FactoryBuilderContract + } + /** * ------------------------------------------------------ * Factory model @@ -295,7 +333,7 @@ declare module '@ioc:Adonis/Lucid/Factory' { * Define before hooks. Only `create` event is invoked * during the before lifecycle */ - before (event: 'create', handler: HooksHandler): this + before (event: Exclude, handler: HooksHandler): this /** * Define after hooks. @@ -306,7 +344,7 @@ declare module '@ioc:Adonis/Lucid/Factory' { * Build model factory. This method returns the factory builder, which can be used to * execute model queries */ - build (): FactoryBuilderContract + build (): FactoryBuilderQueryContract } /** @@ -318,7 +356,7 @@ declare module '@ioc:Adonis/Lucid/Factory' { /** * Factory manager to define new factories */ - export interface FactoryManager { + export interface FactoryManagerContract { /** * Define a custom factory */ @@ -326,8 +364,13 @@ declare module '@ioc:Adonis/Lucid/Factory' { model: Model, callback: DefineCallback ): FactoryModelContract + + /** + * Define a custom callback to generate stub ids + */ + stubId (callback: StubIdCallback): void } - const Factory: FactoryManager + const Factory: FactoryManagerContract export default Factory } diff --git a/src/Factory/FactoryBuilder.ts b/src/Factory/FactoryBuilder.ts index c3f668d4..f82c7a97 100644 --- a/src/Factory/FactoryBuilder.ts +++ b/src/Factory/FactoryBuilder.ts @@ -17,8 +17,6 @@ import { import { FactoryModel } from './FactoryModel' import { FactoryContext } from './FactoryContext' -let Counter = 1 - /** * Factory builder exposes the API to create/persist factory model instances. */ @@ -195,6 +193,18 @@ export class FactoryBuilder implements FactoryBuilderContract void) { + const { modelInstance, ctx } = await this.compile(true, callback) + await this.model.hooks.exec('after', 'make', this, modelInstance, ctx) + return modelInstance + } + /** * Returns a model instance without persisting it to the database. * Relationships are still loaded and states are also applied. @@ -205,8 +215,10 @@ export class FactoryBuilder implements FactoryBuilderContract void) { const { modelInstance, ctx } = await this.compile(true, callback) await this.model.hooks.exec('after', 'make', this, modelInstance, ctx) + await this.model.hooks.exec('before', 'makeStubbed', this, modelInstance, ctx) - modelInstance[this.model.model.primaryKey] = modelInstance.$primaryKeyValue || Counter++ + const id = modelInstance.$primaryKeyValue || this.model.manager.getNextId(modelInstance) + modelInstance[this.model.model.primaryKey] = id /** * Make relationships. The relationships will be not persisted @@ -303,4 +315,22 @@ export class FactoryBuilder implements FactoryBuilderContract void, + ) { + let modelInstances: LucidRow[] = [] + + const counter = new Array(count).fill(0).map((_, i) => i) + for (let index of counter) { + this.currentIndex = index + modelInstances.push(await this.make(callback)) + } + + return modelInstances + } } diff --git a/src/Factory/FactoryContext.ts b/src/Factory/FactoryContext.ts index daef94b0..8f3fdb4f 100644 --- a/src/Factory/FactoryContext.ts +++ b/src/Factory/FactoryContext.ts @@ -7,11 +7,12 @@ * file that was distributed with this source code. */ +import faker from 'faker' import { FactoryContextContract } from '@ioc:Adonis/Lucid/Factory' import { TransactionClientContract } from '@ioc:Adonis/Lucid/Database' export class FactoryContext implements FactoryContextContract { - public faker = {} + public faker = faker constructor ( public isStubbed: boolean, diff --git a/src/Factory/FactoryModel.ts b/src/Factory/FactoryModel.ts index 7ce6fa6f..232402a4 100644 --- a/src/Factory/FactoryModel.ts +++ b/src/Factory/FactoryModel.ts @@ -20,15 +20,16 @@ import { DefineCallback, FactoryModelContract, FactoryRelationContract, + FactoryBuilderQueryContract, } from '@ioc:Adonis/Lucid/Factory' +import { FactoryManager } from './index' import { HasOne } from './Relations/HasOne' import { HasMany } from './Relations/HasMany' +import { FactoryBuilder } from './FactoryBuilder' import { BelongsTo } from './Relations/BelongsTo' import { ManyToMany } from './Relations/ManyToMany' -import { FactoryBuilder } from './FactoryBuilder' - /** * Factory model exposes the API to define a model factory with custom * states and relationships @@ -72,7 +73,11 @@ export class FactoryModel implements FactoryModelContr */ public hooks = new Hooks() - constructor (public model: Model, public define: DefineCallback) { + constructor ( + public model: Model, + public define: DefineCallback, + public manager: FactoryManager, + ) { } /** @@ -195,6 +200,47 @@ export class FactoryModel implements FactoryModelContr * used to make/create model instances */ public build () { - return new FactoryBuilder(this) + /** + * Return a build object, which proxies all of the factory builder + * method and invokes them with a fresh instance. + */ + const builder = { + model: this, + query () { + return new FactoryBuilder(this.model) + }, + apply (...args: any[]) { + return this.query().apply(...args) + }, + with (relation, ...args: any[]) { + return this.query().with(relation, ...args) + }, + merge (attributes) { + return this.query().merge(attributes) + }, + useCtx (ctx) { + return this.query().useCtx(ctx) + }, + make (callback) { + return this.query().make(callback) + }, + makeStubbed (callback) { + return this.query().makeStubbed(callback) + }, + create (callback) { + return this.query().create(callback) + }, + makeMany (count, callback) { + return this.query().makeMany(count, callback) + }, + makeStubbedMany (count, callback) { + return this.query().makeStubbedMany(count, callback) + }, + createMany (count, callback) { + return this.query().createMany(count, callback) + }, + } + + return builder as unknown as FactoryBuilderQueryContract> } } diff --git a/src/Factory/Relations/Base.ts b/src/Factory/Relations/Base.ts index c03f6e52..d85ea043 100644 --- a/src/Factory/Relations/Base.ts +++ b/src/Factory/Relations/Base.ts @@ -12,7 +12,7 @@ import { RelationCallback, FactoryModelContract, FactoryContextContract, - FactoryBuilderContract, + FactoryBuilderQueryContract, } from '@ioc:Adonis/Lucid/Factory' /** @@ -22,7 +22,7 @@ export abstract class BaseRelation { protected ctx: FactoryContextContract constructor ( - private factory: () => FactoryBuilderContract> + private factory: () => FactoryBuilderQueryContract> ) { } @@ -30,7 +30,7 @@ export abstract class BaseRelation { * Instantiates the relationship factory */ protected compile (callback?: RelationCallback) { - const factory = this.factory() + const factory = this.factory().query() if (typeof (callback) === 'function') { callback(factory) } diff --git a/src/Factory/Relations/BelongsTo.ts b/src/Factory/Relations/BelongsTo.ts index a48db64a..6da5c13b 100644 --- a/src/Factory/Relations/BelongsTo.ts +++ b/src/Factory/Relations/BelongsTo.ts @@ -12,8 +12,8 @@ import { BelongsToRelationContract } from '@ioc:Adonis/Lucid/Relations' import { RelationCallback, FactoryModelContract, - FactoryBuilderContract, FactoryRelationContract, + FactoryBuilderQueryContract, } from '@ioc:Adonis/Lucid/Factory' import { BaseRelation } from './Base' @@ -24,7 +24,7 @@ import { BaseRelation } from './Base' export class BelongsTo extends BaseRelation implements FactoryRelationContract { constructor ( public relation: BelongsToRelationContract, - factory: () => FactoryBuilderContract> + factory: () => FactoryBuilderQueryContract> ) { super(factory) this.relation.boot() diff --git a/src/Factory/Relations/HasMany.ts b/src/Factory/Relations/HasMany.ts index bf401b69..e6b22c94 100644 --- a/src/Factory/Relations/HasMany.ts +++ b/src/Factory/Relations/HasMany.ts @@ -12,8 +12,8 @@ import { HasManyRelationContract } from '@ioc:Adonis/Lucid/Relations' import { RelationCallback, FactoryModelContract, - FactoryBuilderContract, FactoryRelationContract, + FactoryBuilderQueryContract, } from '@ioc:Adonis/Lucid/Factory' import { BaseRelation } from './Base' @@ -24,7 +24,7 @@ import { BaseRelation } from './Base' export class HasMany extends BaseRelation implements FactoryRelationContract { constructor ( public relation: HasManyRelationContract, - factory: () => FactoryBuilderContract> + factory: () => FactoryBuilderQueryContract> ) { super(factory) this.relation.boot() diff --git a/src/Factory/Relations/HasOne.ts b/src/Factory/Relations/HasOne.ts index 33c303eb..93f8f850 100644 --- a/src/Factory/Relations/HasOne.ts +++ b/src/Factory/Relations/HasOne.ts @@ -12,8 +12,8 @@ import { HasOneRelationContract } from '@ioc:Adonis/Lucid/Relations' import { RelationCallback, FactoryModelContract, - FactoryBuilderContract, FactoryRelationContract, + FactoryBuilderQueryContract, } from '@ioc:Adonis/Lucid/Factory' import { BaseRelation } from './Base' @@ -24,7 +24,7 @@ import { BaseRelation } from './Base' export class HasOne extends BaseRelation implements FactoryRelationContract { constructor ( public relation: HasOneRelationContract, - factory: () => FactoryBuilderContract> + factory: () => FactoryBuilderQueryContract> ) { super(factory) this.relation.boot() diff --git a/src/Factory/Relations/ManyToMany.ts b/src/Factory/Relations/ManyToMany.ts index 128d5de2..dc958cb8 100644 --- a/src/Factory/Relations/ManyToMany.ts +++ b/src/Factory/Relations/ManyToMany.ts @@ -12,8 +12,8 @@ import { LucidModel, LucidRow, ModelObject } from '@ioc:Adonis/Lucid/Model' import { RelationCallback, FactoryModelContract, - FactoryBuilderContract, FactoryRelationContract, + FactoryBuilderQueryContract, } from '@ioc:Adonis/Lucid/Factory' import { BaseRelation } from './Base' @@ -24,7 +24,7 @@ import { BaseRelation } from './Base' export class ManyToMany extends BaseRelation implements FactoryRelationContract { constructor ( public relation: ManyToManyRelationContract, - factory: () => FactoryBuilderContract> + factory: () => FactoryBuilderQueryContract> ) { super(factory) this.relation.boot() diff --git a/src/Factory/index.ts b/src/Factory/index.ts new file mode 100644 index 00000000..ccb352a5 --- /dev/null +++ b/src/Factory/index.ts @@ -0,0 +1,41 @@ +/* + * @adonisjs/lucid + * + * (c) Harminder Virk + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +import { LucidModel, LucidRow } from '@ioc:Adonis/Lucid/Model' +import { FactoryManagerContract, DefineCallback, StubIdCallback } from '@ioc:Adonis/Lucid/Factory' +import { FactoryModel } from './FactoryModel' + +/** + * Factory manager exposes the API to register factories. + */ +export class FactoryManager implements FactoryManagerContract { + private stubCounter = 1 + private stubIdCallback: StubIdCallback = (counter) => counter + + /** + * Returns the next id + */ + public getNextId (model: LucidRow) { + return this.stubIdCallback(this.stubCounter++, model) + } + + /** + * Define a factory model + */ + public define (model: Model, callback: DefineCallback) { + return new FactoryModel(model, callback, this) + } + + /** + * Define custom callback to generate stub ids + */ + public stubId (callback: StubIdCallback) { + this.stubIdCallback = callback + } +} diff --git a/test-helpers/index.ts b/test-helpers/index.ts index 5a0ed955..68f34cf2 100644 --- a/test-helpers/index.ts +++ b/test-helpers/index.ts @@ -41,7 +41,7 @@ import { import { ApplicationContract } from '@ioc:Adonis/Core/Application' import { SchemaConstructorContract } from '@ioc:Adonis/Lucid/Schema' import { MigratorContract, MigratorOptions } from '@ioc:Adonis/Lucid/Migrator' -import { FactoryModelContract, DefineCallback } from '@ioc:Adonis/Lucid/Factory' +import { FactoryModelContract, DefineCallback, FactoryManagerContract } from '@ioc:Adonis/Lucid/Factory' import { Schema } from '../src/Schema' import { Migrator } from '../src/Migrator' @@ -384,7 +384,8 @@ export function getFactoryModel () { return FactoryModel as unknown as { new ( model: Model, - callback: DefineCallback + callback: DefineCallback, + manager: FactoryManagerContract, ): FactoryModelContract } } diff --git a/test/factory/belongs-to-spec.ts b/test/factory/belongs-to-spec.ts index cc7a3077..c9f6e046 100644 --- a/test/factory/belongs-to-spec.ts +++ b/test/factory/belongs-to-spec.ts @@ -11,6 +11,7 @@ import test from 'japa' import { BelongsTo } from '@ioc:Adonis/Lucid/Relations' +import { FactoryManager } from '../../src/Factory/index' import { column, belongsTo } from '../../src/Orm/Decorators' import { @@ -26,6 +27,7 @@ import { let db: ReturnType let BaseModel: ReturnType const FactoryModel = getFactoryModel() +const factoryManager = new FactoryManager() test.group('Factory | BelongTo | make', (group) => { group.before(async () => { @@ -74,13 +76,13 @@ test.group('Factory | BelongTo | make', (group) => { return { displayName: 'virk', } - }) + }, factoryManager) .relation('user', () => factory) .build() const factory = new FactoryModel(User, () => { return {} - }).build() + }, factoryManager).build() const profile = await profileFactory.with('user').makeStubbed() assert.exists(profile.id) @@ -123,7 +125,7 @@ test.group('Factory | BelongTo | make', (group) => { return { displayName: 'virk', } - }) + }, factoryManager) .relation('user', () => factory) .build() @@ -131,7 +133,7 @@ test.group('Factory | BelongTo | make', (group) => { return { points: 0, } - }).build() + }, factoryManager).build() const profile = await profileFactory .with('user', 1, (related) => related.merge({ points: 10 })) @@ -179,7 +181,7 @@ test.group('Factory | BelongTo | make', (group) => { return { displayName: 'virk', } - }) + }, factoryManager) .relation('user', () => factory) .build() @@ -187,7 +189,7 @@ test.group('Factory | BelongTo | make', (group) => { return { points: 0, } - }) + }, factoryManager) .after('make', (_, user) => { user.id = 100 }) @@ -253,13 +255,13 @@ test.group('Factory | BelongTo | create', (group) => { return { displayName: 'virk', } - }) + }, factoryManager) .relation('user', () => factory) .build() const factory = new FactoryModel(User, () => { return {} - }).build() + }, factoryManager).build() const profile = await profileFactory.with('user').create() @@ -297,7 +299,7 @@ test.group('Factory | BelongTo | create', (group) => { return { displayName: 'virk', } - }) + }, factoryManager) .relation('user', () => factory) .build() @@ -305,7 +307,7 @@ test.group('Factory | BelongTo | create', (group) => { return { points: 0, } - }).build() + }, factoryManager).build() const profile = await profileFactory .with('user', 1, (related) => related.merge({ points: 10 })) @@ -346,7 +348,7 @@ test.group('Factory | BelongTo | create', (group) => { return { displayName: 'virk', } - }) + }, factoryManager) .relation('user', () => factory) .build() @@ -354,7 +356,7 @@ test.group('Factory | BelongTo | create', (group) => { return { points: 0, } - }).build() + }, factoryManager).build() const profile = await profileFactory .with('user', 1, (related) => related.merge({ points: 10 })) @@ -397,7 +399,7 @@ test.group('Factory | BelongTo | create', (group) => { return { displayName: 'virk', } - }) + }, factoryManager) .relation('user', () => factory) .build() @@ -414,7 +416,7 @@ test.group('Factory | BelongTo | create', (group) => { return { username: 'virk', } - }).build() + }, factoryManager).build() try { await profileFactory.with('user').create() diff --git a/test/factory/factory-builder.spec.ts b/test/factory/factory-builder.spec.ts index 3b487780..94d6ac01 100644 --- a/test/factory/factory-builder.spec.ts +++ b/test/factory/factory-builder.spec.ts @@ -11,6 +11,7 @@ import test from 'japa' import { column } from '../../src/Orm/Decorators' +import { FactoryManager } from '../../src/Factory/index' import { FactoryContext } from '../../src/Factory/FactoryContext' import { FactoryBuilder } from '../../src/Factory/FactoryBuilder' @@ -27,6 +28,7 @@ import { let db: ReturnType let BaseModel: ReturnType const FactoryModel = getFactoryModel() +const factoryManager = new FactoryManager() test.group('Factory | Factory Builder | make', (group) => { group.before(async () => { @@ -58,7 +60,7 @@ test.group('Factory | Factory Builder | make', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .state('withPoints', (user) => user.points = 10) .build() @@ -82,7 +84,7 @@ test.group('Factory | Factory Builder | make', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .state('withPoints', (user) => { user.points += 10 }) @@ -110,7 +112,7 @@ test.group('Factory | Factory Builder | make', (group) => { return { username: 'virk', } - }).build() + }, factoryManager).build() const user = await factory.merge({ username: 'nikk' }).makeStubbed() assert.equal(user.username, 'nikk') @@ -134,7 +136,7 @@ test.group('Factory | Factory Builder | make', (group) => { return { username: 'virk', } - }) + }, factoryManager) .merge(() => {}) .build() @@ -160,7 +162,7 @@ test.group('Factory | Factory Builder | make', (group) => { return { username: 'virk', } - }) + }, factoryManager) .newUp((attributes) => { const user = new User() user.fill(attributes) @@ -193,7 +195,7 @@ test.group('Factory | Factory Builder | make', (group) => { return { username: 'virk', } - }).build() + }, factoryManager).build() const user = await factory.merge([{ username: 'nikk' }, { username: 'romain' }]).makeStubbed() assert.equal(user.username, 'nikk') @@ -219,7 +221,7 @@ test.group('Factory | Factory Builder | make', (group) => { return { username: 'virk', } - }) + }, factoryManager) .after('make', (_, user, ctx) => { assert.instanceOf(_, FactoryBuilder) assert.instanceOf(user, User) @@ -251,7 +253,7 @@ test.group('Factory | Factory Builder | make', (group) => { return { username: 'virk', } - }) + }, factoryManager) .after('makeStubbed', (_, user, ctx) => { assert.instanceOf(_, FactoryBuilder) assert.instanceOf(user, User) @@ -283,7 +285,7 @@ test.group('Factory | Factory Builder | make', (group) => { return { username: 'virk', } - }) + }, factoryManager) .after('make', (_, user, ctx) => { if (ctx.isStubbed) { user.id = 100 @@ -328,7 +330,7 @@ test.group('Factory | Factory Builder | makeMany', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .state('withPoints', (user) => user.points = 10) .build() @@ -357,7 +359,7 @@ test.group('Factory | Factory Builder | makeMany', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .state('withPoints', (user) => user.points += 10) .build() @@ -389,7 +391,7 @@ test.group('Factory | Factory Builder | makeMany', (group) => { return { username: 'virk', } - }).build() + }, factoryManager).build() const users = await factory.merge({ username: 'nikk' }).makeStubbedMany(2) assert.lengthOf(users, 2) @@ -419,7 +421,7 @@ test.group('Factory | Factory Builder | makeMany', (group) => { return { username: 'virk', } - }).build() + }, factoryManager).build() const users = await factory.merge([{ username: 'nikk' }, { username: 'romain' }]).makeStubbedMany(2) assert.lengthOf(users, 2) @@ -449,7 +451,7 @@ test.group('Factory | Factory Builder | makeMany', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .after('makeStubbed', (_, user, ctx) => { assert.instanceOf(_, FactoryBuilder) assert.instanceOf(user, User) @@ -486,7 +488,7 @@ test.group('Factory | Factory Builder | makeMany', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .after('make', (_, user, ctx) => { assert.instanceOf(_, FactoryBuilder) assert.instanceOf(user, User) @@ -538,7 +540,7 @@ test.group('Factory | Factory Builder | create', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .state('withPoints', (user) => user.points = 10) .build() @@ -561,7 +563,7 @@ test.group('Factory | Factory Builder | create', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .state('withPoints', (user) => user.points += 10) .build() @@ -586,7 +588,7 @@ test.group('Factory | Factory Builder | create', (group) => { return { username: 'virk', } - }).build() + }, factoryManager).build() const user = await factory.merge({ username: 'nikk' }).create() assert.equal(user.username, 'nikk') @@ -609,7 +611,7 @@ test.group('Factory | Factory Builder | create', (group) => { return { username: 'virk', } - }).build() + }, factoryManager).build() const user = await factory.merge([{ username: 'nikk' }, { username: 'romain' }]).create() assert.equal(user.username, 'nikk') @@ -632,7 +634,7 @@ test.group('Factory | Factory Builder | create', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .before('create', (_, user) => { assert.isFalse(user.$isPersisted) }) @@ -664,7 +666,7 @@ test.group('Factory | Factory Builder | create', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .before('create', (_, user) => { stack.push('beforeCreate') assert.isFalse(user.$isPersisted) @@ -717,7 +719,7 @@ test.group('Factory | Factory Builder | createMany', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .state('withPoints', (user) => user.points = 10) .build() @@ -743,7 +745,7 @@ test.group('Factory | Factory Builder | createMany', (group) => { const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .state('withPoints', (user) => user.points += 10) .build() @@ -772,7 +774,7 @@ test.group('Factory | Factory Builder | createMany', (group) => { username: `u-${new Date().getTime()}`, points: 0, } - }).build() + }, factoryManager).build() const users = await factory.merge({ points: 10 }).createMany(2) assert.lengthOf(users, 2) @@ -798,7 +800,7 @@ test.group('Factory | Factory Builder | createMany', (group) => { return { username: 'virk', } - }).build() + }, factoryManager).build() const users = await factory.merge([{ username: 'nikk' }, { username: 'romain' }]).createMany(2) assert.lengthOf(users, 2) diff --git a/test/factory/factory-model.spec.ts b/test/factory/factory-model.spec.ts index be4391f5..63ac0933 100644 --- a/test/factory/factory-model.spec.ts +++ b/test/factory/factory-model.spec.ts @@ -13,6 +13,7 @@ import test from 'japa' import { HasOne } from '@ioc:Adonis/Lucid/Orm' import { column, hasOne } from '../../src/Orm/Decorators' +import { FactoryManager } from '../../src/Factory/index' import { FactoryModel } from '../../src/Factory/FactoryModel' import { HasOne as FactoryHasOne } from '../../src/Factory/Relations/HasOne' @@ -27,6 +28,7 @@ import { let db: ReturnType let BaseModel: ReturnType +const factoryManager = new FactoryManager() test.group('Factory | Factory Model', (group) => { group.before(async () => { @@ -53,7 +55,7 @@ test.group('Factory | Factory Model', (group) => { public username: string } - const factory = new FactoryModel(User, () => new User()) + const factory = new FactoryModel(User, () => new User(), factoryManager) assert.instanceOf(factory, FactoryModel) }) @@ -67,7 +69,7 @@ test.group('Factory | Factory Model', (group) => { } function stateFn () {} - const factory = new FactoryModel(User, () => new User()).state('active', stateFn) + const factory = new FactoryModel(User, () => new User(), factoryManager).state('active', stateFn) assert.deepEqual(factory.states, { active: stateFn }) }) @@ -91,7 +93,7 @@ test.group('Factory | Factory Model', (group) => { User.boot() function relatedFn () {} - const factory = new FactoryModel(User, () => new User()).relation('profile', relatedFn) + const factory = new FactoryModel(User, () => new User(), factoryManager).relation('profile', relatedFn) assert.property(factory.relations, 'profile') assert.instanceOf(factory.relations.profile, FactoryHasOne) }) @@ -106,7 +108,7 @@ test.group('Factory | Factory Model', (group) => { } function stateFn () {} - const factory = new FactoryModel(User, () => new User()).state('active', stateFn) + const factory = new FactoryModel(User, () => new User(), factoryManager).state('active', stateFn) assert.deepEqual(factory.getState('active'), stateFn) }) @@ -119,7 +121,7 @@ test.group('Factory | Factory Model', (group) => { public username: string } - const factory = new FactoryModel(User, () => new User()) + const factory = new FactoryModel(User, () => new User(), factoryManager) assert.throw( () => factory.getState('active'), 'Cannot apply undefined state \"active\". Double check the model factory', @@ -145,12 +147,12 @@ test.group('Factory | Factory Model', (group) => { } User.boot() - const profileFactory = new FactoryModel(Profile, () => new Profile()).build() + const profileFactory = new FactoryModel(Profile, () => new Profile(), factoryManager).build() function relatedFn () { return profileFactory } - const factory = new FactoryModel(User, () => new User()).relation('profile', relatedFn) + const factory = new FactoryModel(User, () => new User(), factoryManager).relation('profile', relatedFn) assert.instanceOf(factory.getRelation('profile'), FactoryHasOne) assert.deepEqual(factory.getRelation('profile').relation, User.$getRelation('profile')!) }) @@ -171,7 +173,7 @@ test.group('Factory | Factory Model', (group) => { public profile: HasOne } - const factory = new FactoryModel(User, () => new User()) + const factory = new FactoryModel(User, () => new User(), factoryManager) assert.throw( () => factory.getRelation('profile'), 'Cannot setup undefined relationship \"profile\". Double check the model factory' @@ -187,7 +189,7 @@ test.group('Factory | Factory Model', (group) => { public username: string } - const factory = () => new FactoryModel(User, () => new User()).relation('profile' as any, () => {}) + const factory = () => new FactoryModel(User, () => new User(), factoryManager).relation('profile' as any, () => {}) assert.throw( factory, 'Cannot define "profile" relationship. The relationship must exist on the "User" model first' @@ -212,7 +214,8 @@ test.group('Factory | Factory Model', (group) => { const factory = new FactoryModel(User, () => { return {} - }).build() + }, factoryManager).build() + const user = await factory.make() assert.instanceOf(user, User) }) diff --git a/test/factory/has-many.spec.ts b/test/factory/has-many.spec.ts index 49343a9c..aaaf5ba5 100644 --- a/test/factory/has-many.spec.ts +++ b/test/factory/has-many.spec.ts @@ -11,6 +11,7 @@ import test from 'japa' import { HasMany } from '@ioc:Adonis/Lucid/Relations' +import { FactoryManager } from '../../src/Factory/index' import { column, hasMany } from '../../src/Orm/Decorators' import { @@ -26,6 +27,7 @@ import { let db: ReturnType let BaseModel: ReturnType const FactoryModel = getFactoryModel() +const factoryManager = new FactoryManager() test.group('Factory | HasMany | make', (group) => { group.before(async () => { @@ -74,11 +76,11 @@ test.group('Factory | HasMany | make', (group) => { return { title: 'Adonis 101', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('posts', () => postFactory) .build() @@ -122,11 +124,11 @@ test.group('Factory | HasMany | make', (group) => { return { title: 'Adonis 101', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('posts', () => postFactory) .build() @@ -170,11 +172,11 @@ test.group('Factory | HasMany | make', (group) => { return { title: 'Adonis 101', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('posts', () => postFactory) .build() @@ -238,11 +240,11 @@ test.group('Factory | HasMany | create', (group) => { return { title: 'Adonis 101', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('posts', () => postFactory) .build() @@ -283,11 +285,11 @@ test.group('Factory | HasMany | create', (group) => { return { title: 'Adonis 101', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('posts', () => postFactory) .build() @@ -331,11 +333,11 @@ test.group('Factory | HasMany | create', (group) => { return { title: 'Adonis 101', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('posts', () => postFactory) .build() @@ -383,11 +385,11 @@ test.group('Factory | HasMany | create', (group) => { return { title: 'Adonis 101', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('posts', () => postFactory) .build() @@ -431,11 +433,11 @@ test.group('Factory | HasMany | create', (group) => { const postFactory = new FactoryModel(Post, () => { return {} - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('posts', () => postFactory) .build() diff --git a/test/factory/has-one.spec.ts b/test/factory/has-one.spec.ts index 82f21385..d5d992ed 100644 --- a/test/factory/has-one.spec.ts +++ b/test/factory/has-one.spec.ts @@ -11,6 +11,7 @@ import test from 'japa' import { HasOne } from '@ioc:Adonis/Lucid/Relations' +import { FactoryManager } from '../../src/Factory/index' import { column, hasOne } from '../../src/Orm/Decorators' import { @@ -26,6 +27,7 @@ import { let db: ReturnType let BaseModel: ReturnType const FactoryModel = getFactoryModel() +const factoryManager = new FactoryManager() test.group('Factory | HasOne | make', (group) => { group.before(async () => { @@ -74,11 +76,11 @@ test.group('Factory | HasOne | make', (group) => { return { displayName: 'virk', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('profile', () => profileFactory) .build() @@ -124,11 +126,11 @@ test.group('Factory | HasOne | make', (group) => { return { displayName: 'virk', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('profile', () => profileFactory) .build() @@ -190,11 +192,11 @@ test.group('Factory | HasOne | create', (group) => { return { displayName: 'virk', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('profile', () => profileFactory) .build() @@ -234,11 +236,11 @@ test.group('Factory | HasOne | create', (group) => { return { displayName: 'virk', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('profile', () => profileFactory) .build() @@ -281,11 +283,11 @@ test.group('Factory | HasOne | create', (group) => { return { displayName: 'virk', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('profile', () => profileFactory) .build() @@ -325,11 +327,11 @@ test.group('Factory | HasOne | create', (group) => { const profileFactory = new FactoryModel(Profile, () => { return {} - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('profile', () => profileFactory) .build() diff --git a/test/factory/many-to-many.spec.ts b/test/factory/many-to-many.spec.ts index e40fbb87..38e1fc6d 100644 --- a/test/factory/many-to-many.spec.ts +++ b/test/factory/many-to-many.spec.ts @@ -11,6 +11,7 @@ import test from 'japa' import { ManyToMany } from '@ioc:Adonis/Lucid/Relations' +import { FactoryManager } from '../../src/Factory/index' import { column, manyToMany } from '../../src/Orm/Decorators' import { @@ -26,6 +27,7 @@ import { let db: ReturnType let BaseModel: ReturnType const FactoryModel = getFactoryModel() +const factoryManager = new FactoryManager() test.group('Factory | ManyToMany | make', (group) => { group.before(async () => { @@ -71,11 +73,11 @@ test.group('Factory | ManyToMany | make', (group) => { return { name: 'Programming', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('skills', () => postFactory) .build() @@ -122,11 +124,11 @@ test.group('Factory | ManyToMany | make', (group) => { return { name: 'Programming', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('skills', () => postFactory) .build() @@ -173,11 +175,11 @@ test.group('Factory | ManyToMany | make', (group) => { return { name: 'Programming', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('skills', () => postFactory) .build() @@ -252,11 +254,11 @@ test.group('Factory | ManyToMany | create', (group) => { return { name: 'Programming', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('skills', () => postFactory) .build() @@ -300,11 +302,11 @@ test.group('Factory | ManyToMany | create', (group) => { return { name: 'Programming', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('skills', () => postFactory) .build() @@ -351,11 +353,11 @@ test.group('Factory | ManyToMany | create', (group) => { return { name: 'Programming', } - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('skills', () => postFactory) .build() @@ -413,11 +415,11 @@ test.group('Factory | ManyToMany | create', (group) => { const postFactory = new FactoryModel(Skill, () => { return {} - }).build() + }, factoryManager).build() const factory = new FactoryModel(User, () => { return {} - }) + }, factoryManager) .relation('skills', () => postFactory) .build()