From 2ecaac6ea04ec4555d09a50ae6e8e721775450dc Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 19 Sep 2023 10:39:00 +0530 Subject: [PATCH] refactor: migrate factories codebase --- adonis-typings/factory.ts | 36 +- .../factory_builder.ts} | 73 +- .../factory_context.ts} | 6 +- .../factory_model.ts} | 142 ++- src/{Factory => factory}/index.ts | 21 +- .../Base.ts => factory/relations/base.ts} | 20 +- .../relations/belongs_to.ts} | 13 +- .../relations/has_many.ts} | 13 +- .../relations/has_one.ts} | 13 +- .../relations/many_to_many.ts} | 23 +- test-helpers/index.ts | 13 - ...{belongs-to.spec.ts => belongs_to.spec.ts} | 309 +++-- test/factory/factory-model.spec.ts | 270 ----- ...uilder.spec.ts => factory_builder.spec.ts} | 1046 +++++++++-------- test/factory/factory_model.spec.ts | 305 +++++ .../{has-many.spec.ts => has_many.spec.ts} | 494 ++++---- .../{has-one.spec.ts => has_one.spec.ts} | 270 ++--- ...y-to-many.spec.ts => many_to_many.spec.ts} | 345 +++--- 18 files changed, 1774 insertions(+), 1638 deletions(-) rename src/{Factory/FactoryBuilder.ts => factory/factory_builder.ts} (86%) rename src/{Factory/FactoryContext.ts => factory/factory_context.ts} (71%) rename src/{Factory/FactoryModel.ts => factory/factory_model.ts} (53%) rename src/{Factory => factory}/index.ts (58%) rename src/{Factory/Relations/Base.ts => factory/relations/base.ts} (71%) rename src/{Factory/Relations/BelongsTo.ts => factory/relations/belongs_to.ts} (72%) rename src/{Factory/Relations/HasMany.ts => factory/relations/has_many.ts} (75%) rename src/{Factory/Relations/HasOne.ts => factory/relations/has_one.ts} (75%) rename src/{Factory/Relations/ManyToMany.ts => factory/relations/many_to_many.ts} (74%) rename test/factory/{belongs-to.spec.ts => belongs_to.spec.ts} (59%) delete mode 100644 test/factory/factory-model.spec.ts rename test/factory/{factory-builder.spec.ts => factory_builder.spec.ts} (56%) create mode 100644 test/factory/factory_model.spec.ts rename test/factory/{has-many.spec.ts => has_many.spec.ts} (58%) rename test/factory/{has-one.spec.ts => has_one.spec.ts} (56%) rename test/factory/{many-to-many.spec.ts => many_to_many.spec.ts} (60%) diff --git a/adonis-typings/factory.ts b/adonis-typings/factory.ts index b6d686b8..a8bfd24c 100644 --- a/adonis-typings/factory.ts +++ b/adonis-typings/factory.ts @@ -191,17 +191,19 @@ export interface FactoryBuilderContract< /** * Define custom database connection */ - connection(connection: string): this + connection(connection: string): FactoryBuilderContract /** * Define custom query client */ - client(client: QueryClientContract): this + client(client: QueryClientContract): FactoryBuilderContract /** * Apply pre-defined state */ - apply(...states: K[]): this + apply( + ...states: K[] + ): FactoryBuilderContract /** * Create/make relationships for explicitly defined related factories @@ -219,14 +221,16 @@ export interface FactoryBuilderContract< } : never ) => void - ): this + ): FactoryBuilderContract /** * Define pivot attributes when persisting a many to many * relationship. Results in a noop, when not called * for a many to many relationship */ - pivotAttributes(attributes: ModelObject | ModelObject[]): this + pivotAttributes( + attributes: ModelObject | ModelObject[] + ): FactoryBuilderContract /** * Merge custom set of attributes. They are passed to the merge method of @@ -235,13 +239,15 @@ export interface FactoryBuilderContract< * For `createMany` and `makeMany`, you can pass an array of attributes mapped * according to the array index. */ - merge(attributes: OneOrMany>): this + merge( + attributes: OneOrMany> + ): FactoryBuilderContract /** * Merge custom set of attributes with the correct factory builder * model and all of its relationships as well */ - mergeRecursive(attributes: any): this + mergeRecursive(attributes: any): FactoryBuilderContract /** * Define custom runtime context. This method is usually called by @@ -251,7 +257,7 @@ export interface FactoryBuilderContract< * Do not define a custom context, unless you know what you are really * doing. */ - useCtx(ctx: FactoryContextContract): this + useCtx(ctx: FactoryContextContract): FactoryBuilderContract /** * Tap into the persistence layer of factory builder. Allows one @@ -262,9 +268,9 @@ export interface FactoryBuilderContract< callback: ( row: InstanceType, ctx: FactoryContextContract, - builder: this + builder: FactoryBuilderContract ) => void - ): this + ): FactoryBuilderContract /** * Make model instance without persitance. The make method @@ -356,6 +362,11 @@ export interface FactoryModelContract { callback: StateCallback ): this & { states: { [P in K]: StateCallback } } + /** + * Returns state callback + */ + getState(state: keyof this['states']): StateCallback + /** * Define a relationship on another factory */ @@ -364,6 +375,11 @@ export interface FactoryModelContract { callback: Relation ): this & { relations: { [P in K]: Relation } } + /** + * Returns registered relationship for a factory + */ + getRelation(relation: keyof this['relations']): FactoryRelationContract + /** * Define before hooks. Only `create` event is invoked * during the before lifecycle diff --git a/src/Factory/FactoryBuilder.ts b/src/factory/factory_builder.ts similarity index 86% rename from src/Factory/FactoryBuilder.ts rename to src/factory/factory_builder.ts index 1d4f9bda..ec7d12d6 100644 --- a/src/Factory/FactoryBuilder.ts +++ b/src/factory/factory_builder.ts @@ -7,22 +7,29 @@ * file that was distributed with this source code. */ -import { QueryClientContract } from '@ioc:Adonis/Lucid/Database' -import { LucidRow, LucidModel, ModelAdapterOptions, ModelObject } from '@ioc:Adonis/Lucid/Orm' +import { QueryClientContract } from '../../adonis-typings/database.js' +import { + LucidRow, + LucidModel, + ModelAdapterOptions, + ModelObject, +} from '../../adonis-typings/model.js' import { FactoryModelContract, FactoryContextContract, FactoryBuilderContract, FactoryRelationContract, -} from '@ioc:Adonis/Lucid/Factory' +} from '../../adonis-typings/factory.js' -import { FactoryModel } from './FactoryModel' -import { FactoryContext } from './FactoryContext' +import { FactoryModel } from './factory_model.js' +import { FactoryContext } from './factory_context.js' /** * Factory builder exposes the API to create/persist factory model instances. */ -export class FactoryBuilder implements FactoryBuilderContract> { +export class FactoryBuilder + implements FactoryBuilderContract> +{ /** * Relationships to setup. Do note: It is possible to load one relationship * twice. A practical use case is to apply different states. For example: @@ -102,7 +109,7 @@ export class FactoryBuilder implements FactoryBuilderContract void): this { + with(name: string, count?: number, callback?: (factory: never) => void): this { const relation = this.factory.getRelation(name) if (relation.relation.type === 'belongsTo') { - this.withBelongsToRelations.push({ name, count, callback }) + this.withBelongsToRelations.push({ name, count, callback: callback as any }) return this } - this.withRelations.push({ name, count, callback }) + this.withRelations.push({ name, count, callback: callback as any }) return this } @@ -345,7 +352,7 @@ export class FactoryBuilder implements FactoryBuilderContract this.appliedStates.add(state)) return this } @@ -354,7 +361,7 @@ export class FactoryBuilder implements FactoryBuilderContract void - ): this { + tap(callback: (row: LucidRow, state: FactoryContextContract, builder: this) => void): this { this.tapCallbacks.push(callback) return this } @@ -393,17 +398,17 @@ export class FactoryBuilder implements FactoryBuilderContract i) @@ -419,15 +424,15 @@ export class FactoryBuilder implements FactoryBuilderContract i) @@ -461,7 +466,7 @@ export class FactoryBuilder implements FactoryBuilderContract implements FactoryModelContr * Method to instantiate a new model instance. This method can be * overridden using the `newUp` public method. */ - public newUpModelInstance: NewUpCallback> = ( + newUpModelInstance: NewUpCallback> = ( attributes, _, model @@ -63,24 +61,27 @@ export class FactoryModel implements FactoryModelContr * Method to merge runtime attributes with the model instance. This method * can be overridden using the `merge` method. */ - public mergeAttributes: MergeCallback> = (model, attributes) => { + mergeAttributes: MergeCallback> = ( + model, + attributes + ) => { model.merge(attributes) } /** * A collection of factory states */ - public states: { [key: string]: StateCallback } = {} + states: { [key: string]: StateCallback } = {} /** * A collection of factory relations */ - public relations: { [relation: string]: FactoryRelationContract } = {} + relations: { [relation: string]: FactoryRelationContract } = {} /** * A set of registered hooks */ - public hooks = new Hooks() + hooks = new Hooks() constructor( public model: Model, @@ -91,16 +92,16 @@ export class FactoryModel implements FactoryModelContr /** * Register a before event hook */ - public before(event: EventsList, handler: HooksHandler): this { - this.hooks.add('before', event, handler) + before(event: 'makeStubbed' | 'create', handler: HooksHandler): this { + this.hooks.add(`before:${event}`, handler) return this } /** * Register an after event hook */ - public after(event: EventsList, handler: HooksHandler): this { - this.hooks.add('after', event, handler) + after(event: EventsList, handler: HooksHandler): this { + this.hooks.add(`after:${event}`, handler) return this } @@ -108,7 +109,7 @@ export class FactoryModel implements FactoryModelContr * Returns state callback defined on the model factory. Raises an * exception, when state is not registered */ - public getState(state: string): StateCallback { + getState(state: string): StateCallback { const stateCallback = this.states[state] if (!stateCallback) { throw new Error(`Cannot apply undefined state "${state}". Double check the model factory`) @@ -121,11 +122,13 @@ export class FactoryModel implements FactoryModelContr * Returns the pre-registered relationship factory function, along with * the original model relation. */ - public getRelation(relation: string): FactoryRelationContract { - const relationship = this.relations[relation] + getRelation(relation: keyof this['relations']): FactoryRelationContract { + const relationship = this.relations[relation as string] if (!relationship) { throw new Error( - `Cannot reference "${relation}" relationship. Make sure to setup the relationship within the factory` + `Cannot reference "${String( + relation + )}" relationship. Make sure to setup the relationship within the factory` ) } @@ -136,18 +139,21 @@ export class FactoryModel implements FactoryModelContr * Define custom state for the factory. When executing the factory, * you can apply the pre-defined states */ - public state(state: string, callback: StateCallback): any { + state( + state: K, + callback: StateCallback + ): this & { states: { [P in K]: StateCallback } } { this.states[state] = callback - return this + return this as this & { states: { [P in K]: StateCallback } } } /** * Define a relationship on another factory */ - public relation>>( - relation: Extract, - callback: any - ): any { + relation>, Relation>( + relation: K, + callback: Relation + ): this & { relations: { [P in K]: Relation } } { const modelRelation = this.model.$getRelation(relation) as RelationshipsContract /** @@ -156,7 +162,7 @@ export class FactoryModel implements FactoryModelContr if (!modelRelation) { throw new Error( [ - `Cannot define "${relation}" relationship.`, + `Cannot define "${String(relation)}" relationship.`, `The relationship must exist on the "${this.model.name}" model first`, ].join(' ') ) @@ -164,33 +170,33 @@ export class FactoryModel implements FactoryModelContr switch (modelRelation.type) { case 'belongsTo': - this.relations[relation] = new BelongsTo(modelRelation, callback) + this.relations[relation as string] = new BelongsTo(modelRelation, callback as any) break case 'hasOne': - this.relations[relation] = new HasOne(modelRelation, callback) + this.relations[relation as string] = new HasOne(modelRelation, callback as any) break case 'hasMany': - this.relations[relation] = new HasMany(modelRelation, callback) + this.relations[relation as string] = new HasMany(modelRelation, callback as any) break case 'manyToMany': - this.relations[relation] = new ManyToMany(modelRelation, callback) + this.relations[relation as string] = new ManyToMany(modelRelation, callback as any) break case 'hasManyThrough': throw new Error( [ - `Cannot define "${relation}" relationship.`, + `Cannot define "${String(relation)}" relationship.`, '"hasManyThrough" relationship does not have any persistance API', ].join(' ') ) } - return this + return this as this & { relations: { [P in K]: Relation } } } /** * Define a custom `newUp` method */ - public newUp(callback: NewUpCallback): this { + newUp(callback: NewUpCallback): this { this.newUpModelInstance = callback return this } @@ -198,7 +204,7 @@ export class FactoryModel implements FactoryModelContr /** * Define a custom `merge` method */ - public merge(callback: MergeCallback): this { + merge(callback: MergeCallback): this { this.mergeAttributes = callback return this } @@ -207,29 +213,36 @@ export class FactoryModel implements FactoryModelContr * Build factory model and return factory builder. The builder is then * used to make/create model instances */ - public build() { + build() { /** * Return a build object, which proxies all of the factory builder * method and invokes them with a fresh instance. */ - const builder = { - factory: this, - query(options?: ModelAdapterOptions, viaRelation?: FactoryRelationContract) { - return new FactoryBuilder(this.factory, options, viaRelation) + const builder: FactoryBuilderQueryContract> = { + factory: this as FactoryModelContract, + query( + options?: ModelAdapterOptions, + viaRelation?: FactoryRelationContract + ): FactoryBuilderContract> { + return new FactoryBuilder( + this.factory as unknown as FactoryModel, + options, + viaRelation + ) as unknown as FactoryBuilderContract> }, tap(callback) { return this.query().tap(callback) }, - client(...args: any[]) { + client(...args) { return this.query().client(...args) }, - connection(...args: any[]) { + connection(...args) { return this.query().connection(...args) }, - apply(...args: any[]) { + apply(...args) { return this.query().apply(...args) }, - with(relation, ...args: any[]) { + with(relation, ...args) { return this.query().with(relation, ...args) }, merge(attributes) { @@ -241,23 +254,26 @@ export class FactoryModel implements FactoryModelContr useCtx(ctx) { return this.query().useCtx(ctx) }, - make(callback) { - return this.query().make(callback) + make() { + return this.query().make() + }, + makeStubbed() { + return this.query().makeStubbed() }, - makeStubbed(callback) { - return this.query().makeStubbed(callback) + create() { + return this.query().create() }, - create(callback) { - return this.query().create(callback) + makeMany(count) { + return this.query().makeMany(count) }, - makeMany(count, callback) { - return this.query().makeMany(count, callback) + makeStubbedMany(count) { + return this.query().makeStubbedMany(count) }, - makeStubbedMany(count, callback) { - return this.query().makeStubbedMany(count, callback) + createMany(count) { + return this.query().createMany(count) }, - createMany(count, callback) { - return this.query().createMany(count, callback) + pivotAttributes(attributes) { + return this.query().pivotAttributes(attributes) }, } diff --git a/src/Factory/index.ts b/src/factory/index.ts similarity index 58% rename from src/Factory/index.ts rename to src/factory/index.ts index 7d0cd4ca..fad645a8 100644 --- a/src/Factory/index.ts +++ b/src/factory/index.ts @@ -7,35 +7,42 @@ * file that was distributed with this source code. */ -import { LucidModel, LucidRow } from '@ioc:Adonis/Lucid/Orm' -import { FactoryManagerContract, DefineCallback, StubIdCallback } from '@ioc:Adonis/Lucid/Factory' -import { FactoryModel } from './FactoryModel' +import { LucidModel, LucidRow } from '../../adonis-typings/model.js' +import { + DefineCallback, + FactoryModelContract, + StubIdCallback, +} from '../../adonis-typings/factory.js' +import { FactoryModel } from './factory_model.js' /** * Factory manager exposes the API to register factories. */ -export class FactoryManager implements FactoryManagerContract { +export class FactoryManager { private stubCounter = 1 private stubIdCallback: StubIdCallback = (counter) => counter /** * Returns the next id */ - public getNextId(model: LucidRow) { + getNextId(model: LucidRow) { return this.stubIdCallback(this.stubCounter++, model) } /** * Define a factory model */ - public define(model: Model, callback: DefineCallback) { + define( + model: Model, + callback: DefineCallback + ): FactoryModelContract { return new FactoryModel(model, callback, this) } /** * Define custom callback to generate stub ids */ - public stubId(callback: StubIdCallback) { + stubId(callback: StubIdCallback): void { this.stubIdCallback = callback } } diff --git a/src/Factory/Relations/Base.ts b/src/factory/relations/base.ts similarity index 71% rename from src/Factory/Relations/Base.ts rename to src/factory/relations/base.ts index 562c75bd..da4760c6 100644 --- a/src/Factory/Relations/Base.ts +++ b/src/factory/relations/base.ts @@ -7,26 +7,26 @@ * file that was distributed with this source code. */ -import { LucidModel, LucidRow } from '@ioc:Adonis/Lucid/Orm' +import { LucidModel, LucidRow } from '../../../adonis-typings/model.js' import { RelationCallback, FactoryModelContract, FactoryContextContract, FactoryBuilderQueryContract, FactoryRelationContract, -} from '@ioc:Adonis/Lucid/Factory' +} from '../../../adonis-typings/factory.js' /** * Base relation to be extended by other factory relations */ export abstract class BaseRelation { - protected ctx: FactoryContextContract + protected ctx?: FactoryContextContract private attributes: any = {} - public parent: LucidRow + declare parent: LucidRow constructor( - private factory: () => FactoryBuilderQueryContract> + private factory: () => FactoryBuilderQueryContract> ) {} /** @@ -43,14 +43,18 @@ export abstract class BaseRelation { callback(builder) } - builder.useCtx(this.ctx).mergeRecursive(this.attributes) + if (this.ctx) { + builder.useCtx(this.ctx) + } + + builder.mergeRecursive(this.attributes) return builder } /** * Merge attributes with the relationship and its children */ - public merge(attributes: any) { + merge(attributes: any) { this.attributes = attributes return this } @@ -59,7 +63,7 @@ export abstract class BaseRelation { * Use custom ctx. This must always be called by the factory, otherwise * `make` and `create` calls will fail. */ - public useCtx(ctx: FactoryContextContract): this { + useCtx(ctx: FactoryContextContract): this { this.ctx = ctx return this } diff --git a/src/Factory/Relations/BelongsTo.ts b/src/factory/relations/belongs_to.ts similarity index 72% rename from src/Factory/Relations/BelongsTo.ts rename to src/factory/relations/belongs_to.ts index c620a205..ffdfcd49 100644 --- a/src/Factory/Relations/BelongsTo.ts +++ b/src/factory/relations/belongs_to.ts @@ -7,15 +7,16 @@ * file that was distributed with this source code. */ -import { LucidModel, LucidRow, BelongsToRelationContract } from '@ioc:Adonis/Lucid/Orm' +import { LucidModel, LucidRow } from '../../../adonis-typings/model.js' +import { BelongsToRelationContract } from '../../../adonis-typings/relations.js' import { RelationCallback, FactoryModelContract, FactoryRelationContract, FactoryBuilderQueryContract, -} from '@ioc:Adonis/Lucid/Factory' +} from '../../../adonis-typings/factory.js' -import { BaseRelation } from './Base' +import { BaseRelation } from './base.js' /** * A belongs to factory relation @@ -23,7 +24,7 @@ import { BaseRelation } from './Base' export class BelongsTo extends BaseRelation implements FactoryRelationContract { constructor( public relation: BelongsToRelationContract, - factory: () => FactoryBuilderQueryContract> + factory: () => FactoryBuilderQueryContract> ) { super(factory) this.relation.boot() @@ -32,7 +33,7 @@ export class BelongsTo extends BaseRelation implements FactoryRelationContract { /** * Make relationship and set it on the parent model instance */ - public async make(parent: LucidRow, callback?: RelationCallback) { + async make(parent: LucidRow, callback?: RelationCallback) { const factory = this.compile(this, parent, callback) const related = await factory.makeStubbed() @@ -43,7 +44,7 @@ export class BelongsTo extends BaseRelation implements FactoryRelationContract { /** * Persist relationship and set it on the parent model instance */ - public async create(parent: LucidRow, callback?: RelationCallback) { + async create(parent: LucidRow, callback?: RelationCallback) { const factory = this.compile(this, parent, callback) const related = await factory.create() diff --git a/src/Factory/Relations/HasMany.ts b/src/factory/relations/has_many.ts similarity index 75% rename from src/Factory/Relations/HasMany.ts rename to src/factory/relations/has_many.ts index 34badece..e411e2e4 100644 --- a/src/Factory/Relations/HasMany.ts +++ b/src/factory/relations/has_many.ts @@ -7,15 +7,16 @@ * file that was distributed with this source code. */ -import { LucidModel, LucidRow, HasManyRelationContract } from '@ioc:Adonis/Lucid/Orm' +import { LucidModel, LucidRow } from '../../../adonis-typings/model.js' +import { HasManyRelationContract } from '../../../adonis-typings/relations.js' import { RelationCallback, FactoryModelContract, FactoryRelationContract, FactoryBuilderQueryContract, -} from '@ioc:Adonis/Lucid/Factory' +} from '../../../adonis-typings/factory.js' -import { BaseRelation } from './Base' +import { BaseRelation } from './base.js' /** * Has many to factory relation @@ -23,7 +24,7 @@ import { BaseRelation } from './Base' export class HasMany extends BaseRelation implements FactoryRelationContract { constructor( public relation: HasManyRelationContract, - factory: () => FactoryBuilderQueryContract> + factory: () => FactoryBuilderQueryContract> ) { super(factory) this.relation.boot() @@ -32,7 +33,7 @@ export class HasMany extends BaseRelation implements FactoryRelationContract { /** * Make relationship and set it on the parent model instance */ - public async make(parent: LucidRow, callback?: RelationCallback, count?: number) { + async make(parent: LucidRow, callback?: RelationCallback, count?: number) { const factory = this.compile(this, parent, callback) const customAttributes = {} @@ -50,7 +51,7 @@ export class HasMany extends BaseRelation implements FactoryRelationContract { /** * Persist relationship and set it on the parent model instance */ - public async create(parent: LucidRow, callback?: RelationCallback, count?: number) { + async create(parent: LucidRow, callback?: RelationCallback, count?: number) { const factory = this.compile(this, parent, callback) const customAttributes = {} diff --git a/src/Factory/Relations/HasOne.ts b/src/factory/relations/has_one.ts similarity index 75% rename from src/Factory/Relations/HasOne.ts rename to src/factory/relations/has_one.ts index 0271b8a5..7b057d5a 100644 --- a/src/Factory/Relations/HasOne.ts +++ b/src/factory/relations/has_one.ts @@ -7,15 +7,16 @@ * file that was distributed with this source code. */ -import { LucidModel, LucidRow, HasOneRelationContract } from '@ioc:Adonis/Lucid/Orm' +import { LucidModel, LucidRow } from '../../../adonis-typings/model.js' +import { HasOneRelationContract } from '../../../adonis-typings/relations.js' import { RelationCallback, FactoryModelContract, FactoryRelationContract, FactoryBuilderQueryContract, -} from '@ioc:Adonis/Lucid/Factory' +} from '../../../adonis-typings/factory.js' -import { BaseRelation } from './Base' +import { BaseRelation } from './base.js' /** * Has one to factory relation @@ -23,7 +24,7 @@ import { BaseRelation } from './Base' export class HasOne extends BaseRelation implements FactoryRelationContract { constructor( public relation: HasOneRelationContract, - factory: () => FactoryBuilderQueryContract> + factory: () => FactoryBuilderQueryContract> ) { super(factory) this.relation.boot() @@ -32,7 +33,7 @@ export class HasOne extends BaseRelation implements FactoryRelationContract { /** * Make relationship and set it on the parent model instance */ - public async make(parent: LucidRow, callback?: RelationCallback) { + async make(parent: LucidRow, callback?: RelationCallback) { const factory = this.compile(this, parent, callback) const customAttributes = {} this.relation.hydrateForPersistance(parent, customAttributes) @@ -44,7 +45,7 @@ export class HasOne extends BaseRelation implements FactoryRelationContract { /** * Persist relationship and set it on the parent model instance */ - public async create(parent: LucidRow, callback?: RelationCallback) { + async create(parent: LucidRow, callback?: RelationCallback) { const factory = this.compile(this, parent, callback) const customAttributes = {} diff --git a/src/Factory/Relations/ManyToMany.ts b/src/factory/relations/many_to_many.ts similarity index 74% rename from src/Factory/Relations/ManyToMany.ts rename to src/factory/relations/many_to_many.ts index 6cd1d2a1..a72e0354 100644 --- a/src/Factory/Relations/ManyToMany.ts +++ b/src/factory/relations/many_to_many.ts @@ -7,20 +7,17 @@ * file that was distributed with this source code. */ -import { - ManyToManyRelationContract, - LucidModel, - LucidRow, - ModelObject, -} from '@ioc:Adonis/Lucid/Orm' +import { LucidModel, LucidRow, ModelObject } from '../../../adonis-typings/model.js' +import { ManyToManyRelationContract } from '../../../adonis-typings/relations.js' + import { RelationCallback, FactoryModelContract, FactoryRelationContract, FactoryBuilderQueryContract, -} from '@ioc:Adonis/Lucid/Factory' +} from '../../../adonis-typings/factory.js' -import { BaseRelation } from './Base' +import { BaseRelation } from './base.js' /** * Many to many factory relation @@ -30,7 +27,7 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract constructor( public relation: ManyToManyRelationContract, - factory: () => FactoryBuilderQueryContract> + factory: () => FactoryBuilderQueryContract> ) { super(factory) this.relation.boot() @@ -39,7 +36,7 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract /** * Make relationship and set it on the parent model instance */ - public async make(parent: LucidRow, callback?: RelationCallback, count?: number) { + async make(parent: LucidRow, callback?: RelationCallback, count?: number) { const builder = this.compile(this, parent, callback) const instances = await builder.makeStubbedMany(count || 1) parent.$setRelated(this.relation.relationName, instances) @@ -48,7 +45,7 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract /** * Define pivot attributes */ - public pivotAttributes(attributes: ModelObject | ModelObject[]) { + pivotAttributes(attributes: ModelObject | ModelObject[]) { this.attributesForPivotTable = attributes return this } @@ -56,7 +53,7 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract /** * Persist relationship and set it on the parent model instance */ - public async create(parent: LucidRow, callback?: RelationCallback, count?: number) { + async create(parent: LucidRow, callback?: RelationCallback, count?: number) { const builder = this.compile(this, parent, callback) const instances = await builder.createMany(count || 1) @@ -79,7 +76,7 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract /** * Make pivot insert query */ - await this.relation.client(parent, this.ctx.$trx!).attach(relatedForeignKeyValues) + await this.relation.client(parent, this.ctx?.$trx!).attach(relatedForeignKeyValues) /** * Setup in-memory relationship diff --git a/test-helpers/index.ts b/test-helpers/index.ts index 26c8f2f5..7e492e7d 100644 --- a/test-helpers/index.ts +++ b/test-helpers/index.ts @@ -439,19 +439,6 @@ export function getBaseModel(adapter: AdapterContract, application: Application< return BaseModel } -/** - * Returns the factory model - */ -export function getFactoryModel() { - return FactoryModel as { - new ( - model: Model, - callback: DefineCallback, - manager: FactoryManagerContract - ): FactoryModelContract - } -} - /** * Fake adapter implementation */ diff --git a/test/factory/belongs-to.spec.ts b/test/factory/belongs_to.spec.ts similarity index 59% rename from test/factory/belongs-to.spec.ts rename to test/factory/belongs_to.spec.ts index be5b52b3..9456ea05 100644 --- a/test/factory/belongs-to.spec.ts +++ b/test/factory/belongs_to.spec.ts @@ -7,97 +7,84 @@ * file that was distributed with this source code. */ -/// - import { test } from '@japa/runner' -import type { BelongsTo } from '@ioc:Adonis/Lucid/Orm' -import { ApplicationContract } from '@ioc:Adonis/Core/Application' +import type { BelongsTo } from '../../adonis-typings/relations.js' -import { FactoryManager } from '../../src/Factory/index' -import { column, belongsTo } from '../../src/Orm/Decorators' +import { FactoryManager } from '../../src/factory/index.js' +import { column, belongsTo } from '../../src/orm/decorators/index.js' import { - fs, setup, getDb, cleanup, ormAdapter, resetTables, getBaseModel, - setupApplication, - getFactoryModel, -} from '../../test-helpers' - -let db: ReturnType -let app: ApplicationContract -let BaseModel: ReturnType -const FactoryModel = getFactoryModel() +} from '../../test-helpers/index.js' +import { AppFactory } from '@adonisjs/core/factories/app' + const factoryManager = new FactoryManager() test.group('Factory | BelongTo | make', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('make stubbed model with relationship', async ({ assert }) => { + test('make stubbed model with relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column() - public id: number + declare id: number @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string @belongsTo(() => User) - public user: BelongsTo + declare user: BelongsTo } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ) + }) .relation('user', () => factory) .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ).build() + }) + .build() const profile = await profileFactory.with('user').makeStubbed() assert.exists(profile.id) @@ -109,54 +96,55 @@ test.group('Factory | BelongTo | make', (group) => { assert.equal(profile.user.id, profile.userId) }) - test('pass custom attributes to the relationship', async ({ assert }) => { + test('pass custom attributes to the relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column() - public id: number + declare id: number @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string @belongsTo(() => User) - public user: BelongsTo + declare user: BelongsTo } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ) + }) .relation('user', () => factory) .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { points: 0, } - }, - factoryManager - ).build() + }) + .build() const profile = await profileFactory .with('user', 1, (related) => related.merge({ points: 10 })) @@ -172,55 +160,55 @@ test.group('Factory | BelongTo | make', (group) => { assert.equal(profile.user.points, 10) }) - test('invoke make hook on the related factory during make stubbed', async ({ assert }) => { + test('invoke make hook on the related factory during make stubbed', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column() - public id: number + declare id: number @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string @belongsTo(() => User) - public user: BelongsTo + declare user: BelongsTo } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } User.boot() - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ) + }) .relation('user', () => factory) .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { points: 0, } - }, - factoryManager - ) + }) .after('make', (_, user) => { user.id = 100 }) @@ -244,65 +232,61 @@ test.group('Factory | BelongTo | make', (group) => { test.group('Factory | BelongTo | create', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('create model with relationship', async ({ assert }) => { + test('create model with relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string @belongsTo(() => User) - public user: BelongsTo + declare user: BelongsTo } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ) + }) .relation('user', () => factory) .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ).build() + }) + .build() const profile = await profileFactory.with('user').create() @@ -319,51 +303,52 @@ test.group('Factory | BelongTo | create', (group) => { assert.equal(profiles[0].user_id, users[0].id) }) - test('pass custom attributes to the relationship', async ({ assert }) => { + test('pass custom attributes to the relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string @belongsTo(() => User) - public user: BelongsTo + declare user: BelongsTo } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ) + }) .relation('user', () => factory) .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { points: 0, } - }, - factoryManager - ).build() + }) + .build() const profile = await profileFactory .with('user', 1, (related) => related.merge({ points: 10 })) @@ -376,51 +361,52 @@ test.group('Factory | BelongTo | create', (group) => { assert.equal(profile.user.points, 10) }) - test('create model with custom foreign key', async ({ assert }) => { + test('create model with custom foreign key', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column({ columnName: 'user_id' }) - public authorId: number + declare authorId: number @column() - public displayName: string + declare displayName: string @belongsTo(() => User, { foreignKey: 'authorId' }) - public user: BelongsTo + declare user: BelongsTo } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ) + }) .relation('user', () => factory) .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { points: 0, } - }, - factoryManager - ).build() + }) + .build() const profile = await profileFactory .with('user', 1, (related) => related.merge({ points: 10 })) @@ -433,41 +419,44 @@ test.group('Factory | BelongTo | create', (group) => { assert.equal(profile.user.points, 10) }) - test('rollback changes on error', async ({ assert }) => { + test('rollback changes on error', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(3) class Profile extends BaseModel { @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string @belongsTo(() => User) - public user: BelongsTo + declare user: BelongsTo } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ) + }) .relation('user', () => factory) .build() @@ -480,15 +469,13 @@ test.group('Factory | BelongTo | create', (group) => { */ await db.table('users').insert({ username: 'virk' }) - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() try { await profileFactory.with('user').create() diff --git a/test/factory/factory-model.spec.ts b/test/factory/factory-model.spec.ts deleted file mode 100644 index 7d505383..00000000 --- a/test/factory/factory-model.spec.ts +++ /dev/null @@ -1,270 +0,0 @@ -/* - * @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 { test } from '@japa/runner' - -import type { 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' - -import { - fs, - setup, - getDb, - cleanup, - ormAdapter, - resetTables, - getBaseModel, - setupApplication, -} from '../../test-helpers' -import { ApplicationContract } from '@ioc:Adonis/Core/Application' - -let db: ReturnType -let app: ApplicationContract -let BaseModel: ReturnType -const factoryManager = new FactoryManager() - -test.group('Factory | Factory Model', (group) => { - group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) - await setup() - }) - - group.teardown(async () => { - await db.manager.closeAll() - await cleanup() - await fs.cleanup() - }) - - group.each.teardown(async () => { - await resetTables() - }) - - test('define model factory', async ({ assert }) => { - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - } - - const factory = new FactoryModel(User, () => new User(), factoryManager) - assert.instanceOf(factory, FactoryModel) - }) - - test('define factory state', async ({ assert }) => { - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - } - - function stateFn() {} - const factory = new FactoryModel(User, () => new User(), factoryManager).state( - 'active', - stateFn - ) - assert.deepEqual(factory.states, { active: stateFn }) - }) - - test('define factory relation', async ({ assert }) => { - class Profile extends BaseModel { - @column() - public userId: number - } - Profile.boot() - - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - - @hasOne(() => Profile) - public profile: HasOne - } - User.boot() - - function relatedFn() {} - const factory = new FactoryModel(User, () => new User(), factoryManager).relation( - 'profile', - relatedFn - ) - assert.property(factory.relations, 'profile') - assert.instanceOf(factory.relations.profile, FactoryHasOne) - }) - - test('get pre-registered state', async ({ assert }) => { - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - } - - function stateFn() {} - const factory = new FactoryModel(User, () => new User(), factoryManager).state( - 'active', - stateFn - ) - assert.deepEqual(factory.getState('active'), stateFn) - }) - - test('raise exception when state is not registered', async ({ assert }) => { - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - } - - const factory = new FactoryModel(User, () => new User(), factoryManager) - assert.throws( - () => factory.getState('active'), - 'Cannot apply undefined state "active". Double check the model factory' - ) - }) - - test('get pre-registered relationship', async ({ assert }) => { - class Profile extends BaseModel { - @column() - public userId: number - } - Profile.boot() - - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - - @hasOne(() => Profile) - public profile: HasOne - } - User.boot() - - const profileFactory = new FactoryModel(Profile, () => new Profile(), factoryManager).build() - function relatedFn() { - return profileFactory - } - - 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')!) - }) - - test('raise exception when relation is not defined', async ({ assert }) => { - class Profile extends BaseModel {} - Profile.boot() - - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - - @hasOne(() => Profile) - public profile: HasOne - } - - const factory = new FactoryModel(User, () => new User(), factoryManager) - assert.throws( - () => factory.getRelation('profile'), - 'Cannot reference "profile" relationship. Make sure to setup the relationship within the factory' - ) - }) - - test('do not allow registering relationships not defined on the model', async ({ assert }) => { - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - } - - const factory = () => - new FactoryModel(User, () => new User(), factoryManager).relation('profile' as any, () => {}) - assert.throws( - factory, - 'Cannot define "profile" relationship. The relationship must exist on the "User" model first' - ) - }) - - test('build factory', async ({ assert }) => { - class Profile extends BaseModel {} - Profile.boot() - - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - - @hasOne(() => Profile) - public profile: HasOne - } - - const factory = new FactoryModel( - User, - () => { - return {} - }, - factoryManager - ).build() - - const user = await factory.make() - assert.instanceOf(user, User) - }) - - test('return model instance from the factory callback', async ({ assert }) => { - class Profile extends BaseModel {} - Profile.boot() - - class User extends BaseModel { - @column({ isPrimary: true }) - public id: number - - @column() - public username: string - - @hasOne(() => Profile) - public profile: HasOne - } - - const factory = new FactoryModel( - User, - () => { - return new User() - }, - factoryManager - ).build() - - const user = await factory.make() - assert.instanceOf(user, User) - }) -}) diff --git a/test/factory/factory-builder.spec.ts b/test/factory/factory_builder.spec.ts similarity index 56% rename from test/factory/factory-builder.spec.ts rename to test/factory/factory_builder.spec.ts index 5cd24110..c668dcd5 100644 --- a/test/factory/factory-builder.spec.ts +++ b/test/factory/factory_builder.spec.ts @@ -7,71 +7,61 @@ * file that was distributed with this source code. */ -/// - import { test } from '@japa/runner' -import { randomUUID } from 'crypto' -import { column } from '../../src/Orm/Decorators' -import { FactoryManager } from '../../src/Factory/index' -import { FactoryContext } from '../../src/Factory/FactoryContext' -import { FactoryBuilder } from '../../src/Factory/FactoryBuilder' +import { randomUUID } from 'node:crypto' + +import { column } from '../../src/orm/decorators/index.js' +import { FactoryManager } from '../../src/factory/index.js' +import { FactoryContext } from '../../src/factory/factory_context.js' +import { FactoryBuilder } from '../../src/factory/factory_builder.js' import { - fs, setup, getDb, cleanup, ormAdapter, resetTables, getBaseModel, - getFactoryModel, - setupApplication, -} from '../../test-helpers' -import { ApplicationContract } from '@ioc:Adonis/Core/Application' - -let db: ReturnType -let app: ApplicationContract -let BaseModel: ReturnType -const FactoryModel = getFactoryModel() +} from '../../test-helpers/index.js' +import { AppFactory } from '@adonisjs/core/factories/app' + const factoryManager = new FactoryManager() test.group('Factory | Factory Builder | make', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('apply factory model state', async ({ assert }) => { + test('apply factory model state', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points = 10)) .build() @@ -81,25 +71,28 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('applying a state twice must be a noop', async ({ assert }) => { + test('applying a state twice must be a noop', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => { user.points += 10 }) @@ -112,25 +105,28 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('pass instance of builder to the state callback', async ({ assert }) => { + test('pass instance of builder to the state callback', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user, __, builder) => { assert.instanceOf(builder, FactoryBuilder) user.merge({ points: 10 }) @@ -143,27 +139,31 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('merge custom attributes', async ({ assert }) => { + test('merge custom attributes', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const user = await factory.merge({ username: 'nikk' }).makeStubbed() assert.equal(user.username, 'nikk') @@ -171,27 +171,30 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('define custom merge function', async ({ assert }) => { + test('define custom merge function', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .merge(() => {}) .build() @@ -201,27 +204,30 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('pass builder to custom merge function', async ({ assert }) => { + test('pass builder to custom merge function', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .merge((_, __, ___, builder) => { assert.instanceOf(builder, FactoryBuilder) }) @@ -233,27 +239,30 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('define custom newUp function', async ({ assert }) => { + test('define custom newUp function', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .newUp((attributes) => { const user = new User() user.fill(attributes) @@ -270,27 +279,30 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('pass model to custom newUp function', async ({ assert }) => { + test('pass model to custom newUp function', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .newUp((attributes) => { const user = new User() user.fill(attributes) @@ -307,27 +319,30 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('pass factory builder to custom newUp function', async ({ assert }) => { + test('pass factory builder to custom newUp function', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .newUp((attributes, _, __, builder) => { assert.instanceOf(builder, FactoryBuilder) @@ -346,27 +361,31 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('use 0 index elements when attributes are defined as an array', async ({ assert }) => { + test('use 0 index elements when attributes are defined as an array', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const user = await factory.merge([{ username: 'nikk' }, { username: 'romain' }]).makeStubbed() assert.equal(user.username, 'nikk') @@ -374,29 +393,32 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('invoke after make hook', async ({ assert }) => { + test('invoke after make hook', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(6) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .after('make', (_, user, ctx) => { assert.instanceOf(_, FactoryBuilder) assert.instanceOf(user, User) @@ -410,29 +432,32 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('invoke after makeStubbed hook', async ({ assert }) => { + test('invoke after makeStubbed hook', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(6) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .after('makeStubbed', (_, user, ctx) => { assert.instanceOf(_, FactoryBuilder) assert.instanceOf(user, User) @@ -446,29 +471,32 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('define custom id inside make hook', async ({ assert }) => { + test('define custom id inside make hook', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(3) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .after('make', (_, user, ctx) => { if (ctx.isStubbed) { user.id = 100 @@ -482,27 +510,31 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('tap into model persistence before makeStubbed', async ({ assert }) => { + test('tap into model persistence before makeStubbed', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const user = await factory .tap(($user, ctx) => { @@ -517,27 +549,31 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('tap into model persistence before make', async ({ assert }) => { + test('tap into model persistence before make', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const user = await factory .tap(($user) => { @@ -549,27 +585,30 @@ test.group('Factory | Factory Builder | make', (group) => { assert.isFalse(user.$isPersisted) }) - test('bubble erros when makeStubbed fails', async ({ assert }) => { + test('bubble erros when makeStubbed fails', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .state('foo', () => { throw new Error('boom') }) @@ -578,27 +617,30 @@ test.group('Factory | Factory Builder | make', (group) => { await assert.rejects(async () => await factory.apply('foo').makeStubbed(), 'boom') }) - test('bubble erros when make fails', async ({ assert }) => { + test('bubble erros when make fails', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .state('foo', () => { throw new Error('boom') }) @@ -610,41 +652,39 @@ test.group('Factory | Factory Builder | make', (group) => { test.group('Factory | Factory Builder | makeMany', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('apply factory model state', async ({ assert }) => { + test('apply factory model state', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points = 10)) .build() @@ -659,25 +699,28 @@ test.group('Factory | Factory Builder | makeMany', (group) => { assert.isFalse(users[1].$isPersisted) }) - test('applying a state twice must be a noop', async ({ assert }) => { + test('applying a state twice must be a noop', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points += 10)) .build() @@ -693,27 +736,31 @@ test.group('Factory | Factory Builder | makeMany', (group) => { assert.isFalse(users[1].$isPersisted) }) - test('define custom attributes accepted by the newUp method', async ({ assert }) => { + test('define custom attributes accepted by the newUp method', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const users = await factory.merge({ username: 'nikk' }).makeStubbedMany(2) assert.lengthOf(users, 2) @@ -727,27 +774,31 @@ test.group('Factory | Factory Builder | makeMany', (group) => { assert.isFalse(users[1].$isPersisted) }) - test('define index specific attributes for makeMany', async ({ assert }) => { + test('define index specific attributes for makeMany', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const users = await factory .merge([{ username: 'nikk' }, { username: 'romain' }]) @@ -763,27 +814,30 @@ test.group('Factory | Factory Builder | makeMany', (group) => { assert.isFalse(users[1].$isPersisted) }) - test('run makeStubbed hook for all the model instances', async ({ assert }) => { + test('run makeStubbed hook for all the model instances', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(15) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .after('makeStubbed', (_, user, ctx) => { assert.instanceOf(_, FactoryBuilder) assert.instanceOf(user, User) @@ -804,27 +858,30 @@ test.group('Factory | Factory Builder | makeMany', (group) => { assert.isFalse(users[1].$isPersisted) }) - test('run make hook for all the model instances', async ({ assert }) => { + test('run make hook for all the model instances', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(15) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .after('make', (_, user, ctx) => { assert.instanceOf(_, FactoryBuilder) assert.instanceOf(user, User) @@ -845,27 +902,30 @@ test.group('Factory | Factory Builder | makeMany', (group) => { assert.isFalse(users[1].$isPersisted) }) - test('tap into model persistence before makeStubbedMany', async ({ assert }) => { + test('tap into model persistence before makeStubbedMany', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(15) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points = 10)) .build() @@ -889,27 +949,30 @@ test.group('Factory | Factory Builder | makeMany', (group) => { assert.isFalse(users[1].$isPersisted) }) - test('tap into model persistence before makeMany', async ({ assert }) => { + test('tap into model persistence before makeMany', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(13) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points = 10)) .build() @@ -931,25 +994,28 @@ test.group('Factory | Factory Builder | makeMany', (group) => { assert.isFalse(users[1].$isPersisted) }) - test('bubble errors when makeStubbedMany fails', async ({ assert }) => { + test('bubble errors when makeStubbedMany fails', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .merge(() => { throw new Error('boom') }) @@ -961,25 +1027,28 @@ test.group('Factory | Factory Builder | makeMany', (group) => { ) }) - test('bubble errors when makeMany fails', async ({ assert }) => { + test('bubble errors when makeMany fails', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .merge(() => { throw new Error('boom') }) @@ -991,41 +1060,39 @@ test.group('Factory | Factory Builder | makeMany', (group) => { test.group('Factory | Factory Builder | create', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('apply factory model state', async ({ assert }) => { + test('apply factory model state', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points = 10)) .build() @@ -1034,25 +1101,28 @@ test.group('Factory | Factory Builder | create', (group) => { assert.isTrue(user.$isPersisted) }) - test('applying a state twice must be a noop', async ({ assert }) => { + test('applying a state twice must be a noop', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points += 10)) .build() @@ -1061,81 +1131,92 @@ test.group('Factory | Factory Builder | create', (group) => { assert.isTrue(user.$isPersisted) }) - test('define custom attributes accepted by the newUp method', async ({ assert }) => { + test('define custom attributes accepted by the newUp method', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const user = await factory.merge({ username: 'nikk' }).create() assert.equal(user.username, 'nikk') assert.isTrue(user.$isPersisted) }) - test('use index 0 elements when attributes are defined as an array', async ({ assert }) => { + test('use index 0 elements when attributes are defined as an array', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const user = await factory.merge([{ username: 'nikk' }, { username: 'romain' }]).create() assert.equal(user.username, 'nikk') assert.isTrue(user.$isPersisted) }) - test('invoke before and after create hook', async ({ assert }) => { + test('invoke before and after create hook', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(4) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .before('create', (_, user) => { assert.isFalse(user.$isPersisted) }) @@ -1150,28 +1231,31 @@ test.group('Factory | Factory Builder | create', (group) => { assert.isTrue(user.$isPersisted) }) - test('invoke after make hook', async ({ assert }) => { + test('invoke after make hook', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(6) const stack: string[] = [] class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .before('create', (_, user) => { stack.push('beforeCreate') assert.isFalse(user.$isPersisted) @@ -1193,27 +1277,30 @@ test.group('Factory | Factory Builder | create', (group) => { assert.deepEqual(stack, ['afterMake', 'beforeCreate', 'afterCreate']) }) - test('define custom connection', async ({ assert }) => { + test('define custom connection', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(3) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points = 10)) .build() @@ -1229,27 +1316,30 @@ test.group('Factory | Factory Builder | create', (group) => { assert.isTrue(user.$isPersisted) }) - test('define custom query client', async ({ assert }) => { + test('define custom query client', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(3) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points = 10)) .build() @@ -1267,27 +1357,30 @@ test.group('Factory | Factory Builder | create', (group) => { assert.isTrue(user.$isPersisted) }) - test('invoke tap callback before persisting the model', async ({ assert }) => { + test('invoke tap callback before persisting the model', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(6) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points = 10)) .build() @@ -1308,25 +1401,28 @@ test.group('Factory | Factory Builder | create', (group) => { assert.isTrue(user.$isPersisted) }) - test('bubble errors when create fails', async ({ assert }) => { + test('bubble errors when create fails', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', () => { throw new Error('boo') }) @@ -1339,41 +1435,39 @@ test.group('Factory | Factory Builder | create', (group) => { test.group('Factory | Factory Builder | createMany', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('apply factory model state', async ({ assert }) => { + test('apply factory model state', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points = 10)) .build() @@ -1385,25 +1479,28 @@ test.group('Factory | Factory Builder | createMany', (group) => { assert.isTrue(users[1].$isPersisted) }) - test('applying a state twice must be a noop', async ({ assert }) => { + test('applying a state twice must be a noop', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .state('withPoints', (user) => (user.points += 10)) .build() @@ -1415,28 +1512,32 @@ test.group('Factory | Factory Builder | createMany', (group) => { assert.isTrue(users[1].$isPersisted) }) - test('define custom attributes accepted by the newUp method', async ({ assert }) => { + test('define custom attributes accepted by the newUp method', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: randomUUID(), points: 0, } - }, - factoryManager - ).build() + }) + .build() const users = await factory.merge({ points: 10 }).createMany(2) assert.lengthOf(users, 2) @@ -1446,27 +1547,31 @@ test.group('Factory | Factory Builder | createMany', (group) => { assert.isTrue(users[1].$isPersisted) }) - test('define index specific attributes for makeMany', async ({ assert }) => { + test('define index specific attributes for makeMany', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const users = await factory.merge([{ username: 'nikk' }, { username: 'romain' }]).createMany(2) assert.lengthOf(users, 2) @@ -1476,29 +1581,33 @@ test.group('Factory | Factory Builder | createMany', (group) => { assert.isTrue(users[1].$isPersisted) }) - test('invoke tap before persisting all models', async ({ assert }) => { + test('invoke tap before persisting all models', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(11) class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ).build() + }) + .build() const users = await factory .merge([{ username: 'nikk' }, { username: 'romain' }]) @@ -1516,29 +1625,32 @@ test.group('Factory | Factory Builder | createMany', (group) => { assert.isTrue(users[1].$isPersisted) }) - test('bubble errors when createMany fails', async ({ assert }) => { + test('bubble errors when createMany fails', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + let index = 0 class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number + declare points: number } - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return { username: 'virk', } - }, - factoryManager - ) + }) .merge(() => { index++ if (index === 2) { diff --git a/test/factory/factory_model.spec.ts b/test/factory/factory_model.spec.ts new file mode 100644 index 00000000..49558722 --- /dev/null +++ b/test/factory/factory_model.spec.ts @@ -0,0 +1,305 @@ +/* + * @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 { test } from '@japa/runner' + +import { FactoryManager } from '../../src/factory/index.js' +import type { HasOne } from '../../adonis-typings/relations.js' +import { column, hasOne } from '../../src/orm/decorators/index.js' +import { FactoryModel } from '../../src/factory/factory_model.js' +import { HasOne as FactoryHasOne } from '../../src/factory/relations/has_one.js' + +import { + setup, + getDb, + cleanup, + ormAdapter, + resetTables, + getBaseModel, +} from '../../test-helpers/index.js' +import { AppFactory } from '@adonisjs/core/factories/app' + +const factoryManager = new FactoryManager() + +test.group('Factory | Factory Model', (group) => { + group.setup(async () => { + await setup() + }) + + group.teardown(async () => { + await cleanup() + }) + + group.each.teardown(async () => { + await resetTables() + }) + + test('define model factory', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + } + + const factory = factoryManager.define(User, () => new User()) + assert.instanceOf(factory, FactoryModel) + }) + + test('define factory state', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + } + + function stateFn() {} + const factory = factoryManager.define(User, () => new User()).state('active', stateFn) + assert.deepEqual(factory.states, { active: stateFn }) + }) + + test('define factory relation', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class Profile extends BaseModel { + @column() + declare userId: number + } + Profile.boot() + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + + @hasOne(() => Profile) + declare profile: HasOne + } + User.boot() + + function relatedFn() {} + const factory = factoryManager.define(User, () => new User()).relation('profile', relatedFn) + assert.property(factory.relations, 'profile') + assert.instanceOf(factory.relations.profile, FactoryHasOne) + }) + + test('get pre-registered state', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + } + + function stateFn() {} + const factory = factoryManager.define(User, () => new User()).state('active', stateFn) + assert.deepEqual(factory.getState('active'), stateFn) + }) + + test('raise exception when state is not registered', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + } + + const factory = factoryManager.define(User, () => new User()) + assert.throws( + () => factory.getState('active' as never), + 'Cannot apply undefined state "active". Double check the model factory' + ) + }) + + test('get pre-registered relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class Profile extends BaseModel { + @column() + declare userId: number + } + Profile.boot() + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + + @hasOne(() => Profile) + declare profile: HasOne + } + User.boot() + + const profileFactory = factoryManager.define(Profile, () => new Profile()).build() + function relatedFn() { + return profileFactory + } + + const factory = factoryManager.define(User, () => new User()).relation('profile', relatedFn) + assert.instanceOf(factory.getRelation('profile'), FactoryHasOne) + assert.deepEqual(factory.getRelation('profile').relation, User.$getRelation('profile')!) + }) + + test('raise exception when relation is not defined', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class Profile extends BaseModel {} + Profile.boot() + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + + @hasOne(() => Profile) + declare profile: HasOne + } + + const factory = factoryManager.define(User, () => new User()) + assert.throws( + () => factory.getRelation('profile' as never), + 'Cannot reference "profile" relationship. Make sure to setup the relationship within the factory' + ) + }) + + test('do not allow registering relationships not defined on the model', async ({ + fs, + assert, + }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + } + + const factory = () => + factoryManager.define(User, () => new User()).relation('profile' as any, () => {}) + assert.throws( + factory, + 'Cannot define "profile" relationship. The relationship must exist on the "User" model first' + ) + }) + + test('build factory', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class Profile extends BaseModel {} + Profile.boot() + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + + @hasOne(() => Profile) + declare profile: HasOne + } + + const factory = factoryManager + .define(User, () => { + return {} + }) + .build() + + const user = await factory.make() + assert.instanceOf(user, User) + }) + + test('return model instance from the factory callback', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + + class Profile extends BaseModel {} + Profile.boot() + + class User extends BaseModel { + @column({ isPrimary: true }) + declare id: number + + @column() + declare username: string + + @hasOne(() => Profile) + declare profile: HasOne + } + + const factory = factoryManager + .define(User, () => { + return new User() + }) + .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 similarity index 58% rename from test/factory/has-many.spec.ts rename to test/factory/has_many.spec.ts index f103adba..130556f0 100644 --- a/test/factory/has-many.spec.ts +++ b/test/factory/has_many.spec.ts @@ -7,94 +7,82 @@ * file that was distributed with this source code. */ -/// - import { test } from '@japa/runner' -import type { HasMany } from '@ioc:Adonis/Lucid/Orm' -import { FactoryManager } from '../../src/Factory/index' -import { column, hasMany } from '../../src/Orm/Decorators' +import type { HasMany } from '../../adonis-typings/relations.js' + +import { FactoryManager } from '../../src/factory/index.js' +import { column, hasMany } from '../../src/orm/decorators/index.js' import { - fs, setup, getDb, cleanup, ormAdapter, resetTables, getBaseModel, - getFactoryModel, - setupApplication, -} from '../../test-helpers' -import { ApplicationContract } from '@ioc:Adonis/Core/Application' - -let db: ReturnType -let app: ApplicationContract -let BaseModel: ReturnType -const FactoryModel = getFactoryModel() +} from '../../test-helpers/index.js' +import { AppFactory } from '@adonisjs/core/factories/app' + const factoryManager = new FactoryManager() test.group('Factory | HasMany | make', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('makeStubbed model with relationship', async ({ assert }) => { + test('makeStubbed model with relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Post extends BaseModel { @column() - public id: number + declare id: number @column() - public userId: number + declare userId: number @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -110,47 +98,48 @@ test.group('Factory | HasMany | make', (group) => { assert.equal(user.posts[0].userId, user.id) }) - test('pass custom attributes to relationship', async ({ assert }) => { + test('pass custom attributes to relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Post extends BaseModel { @column() - public userId: number + declare userId: number @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -166,47 +155,48 @@ test.group('Factory | HasMany | make', (group) => { assert.equal(user.posts[0].title, 'Lucid 101') }) - test('make many relationship', async ({ assert }) => { + test('make many relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Post extends BaseModel { @column() - public userId: number + declare userId: number @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -225,53 +215,54 @@ test.group('Factory | HasMany | make', (group) => { assert.equal(user.posts[1].title, 'Lucid 101') }) - test('merge attributes with the relationship', async ({ assert }) => { + test('merge attributes with the relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Post extends BaseModel { @column() - public userId: number + declare userId: number @column() - public tenantId: string + declare tenantId: string @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public tenantId: string + declare tenantId: string @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -297,63 +288,59 @@ test.group('Factory | HasMany | make', (group) => { test.group('Factory | HasMany | create', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('create model with relationship', async ({ assert }) => { + test('create model with relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Post extends BaseModel { @column() - public userId: number + declare userId: number @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -373,47 +360,48 @@ test.group('Factory | HasMany | create', (group) => { assert.equal(posts[0].user_id, users[0].id) }) - test('pass custom attributes to relationship', async ({ assert }) => { + test('pass custom attributes to relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Post extends BaseModel { @column() - public userId: number + declare userId: number @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -429,47 +417,48 @@ test.group('Factory | HasMany | create', (group) => { assert.equal(user.posts[0].title, 'Lucid 101') }) - test('create many relationship', async ({ assert }) => { + test('create many relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Post extends BaseModel { @column() - public userId: number + declare userId: number @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -489,47 +478,48 @@ test.group('Factory | HasMany | create', (group) => { assert.equal(user.posts[1].title, 'Lucid 101') }) - test('create relationship with custom foreign key', async ({ assert }) => { + test('create relationship with custom foreign key', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Post extends BaseModel { @column({ columnName: 'user_id' }) - public authorId: number + declare authorId: number @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post, { foreignKey: 'authorId' }) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -545,47 +535,48 @@ test.group('Factory | HasMany | create', (group) => { assert.equal(user.posts[0].title, 'Lucid 101') }) - test('rollback changes on error', async ({ assert }) => { + test('rollback changes on error', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(3) class Post extends BaseModel { @column() - public userId: number + declare userId: number @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return {} - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -602,53 +593,54 @@ test.group('Factory | HasMany | create', (group) => { assert.lengthOf(posts, 0) }) - test('pass custom attributes to relationship', async ({ assert }) => { + test('pass custom attributes to relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Post extends BaseModel { @column() - public userId: number + declare userId: number @column() - public tenantId: string + declare tenantId: string @column() - public title: string + declare title: string } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public tenantId: string + declare tenantId: string @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() @@ -667,83 +659,81 @@ test.group('Factory | HasMany | create', (group) => { assert.equal(user.posts[0].title, 'Lucid 101') }) - test('pass custom attributes to deep nested relationship', async ({ assert }) => { + test('pass custom attributes to deep nested relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Comment extends BaseModel { @column() - public postId: number + declare postId: number @column() - public tenantId: string + declare tenantId: string @column() - public body: string + declare body: string } Comment.boot() class Post extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public userId: number + declare userId: number @column() - public tenantId: string + declare tenantId: string @column() - public title: string + declare title: string @hasMany(() => Comment) - public comments: HasMany + declare comments: HasMany } Post.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public tenantId: string + declare tenantId: string @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasMany(() => Post) - public posts: HasMany + declare posts: HasMany } - const commentFactory = new FactoryModel( - Comment, - () => { + const commentFactory = factoryManager + .define(Comment, () => { return { body: 'Nice post', } - }, - factoryManager - ).build() + }) + .build() - const postFactory = new FactoryModel( - Post, - () => { + const postFactory = factoryManager + .define(Post, () => { return { title: 'Adonis 101', } - }, - factoryManager - ) + }) .relation('comments', () => commentFactory) .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('posts', () => postFactory) .build() diff --git a/test/factory/has-one.spec.ts b/test/factory/has_one.spec.ts similarity index 56% rename from test/factory/has-one.spec.ts rename to test/factory/has_one.spec.ts index ff5074da..592498ad 100644 --- a/test/factory/has-one.spec.ts +++ b/test/factory/has_one.spec.ts @@ -7,94 +7,82 @@ * file that was distributed with this source code. */ -/// - import { test } from '@japa/runner' -import type { HasOne } from '@ioc:Adonis/Lucid/Orm' -import { FactoryManager } from '../../src/Factory/index' -import { column, hasOne } from '../../src/Orm/Decorators' +import type { HasOne } from '../../adonis-typings/relations.js' + +import { FactoryManager } from '../../src/factory/index.js' +import { column, hasOne } from '../../src/orm/decorators/index.js' import { - fs, setup, getDb, cleanup, ormAdapter, resetTables, getBaseModel, - getFactoryModel, - setupApplication, -} from '../../test-helpers' -import { ApplicationContract } from '@ioc:Adonis/Core/Application' - -let db: ReturnType -let app: ApplicationContract -let BaseModel: ReturnType -const FactoryModel = getFactoryModel() +} from '../../test-helpers/index.js' +import { AppFactory } from '@adonisjs/core/factories/app' + const factoryManager = new FactoryManager() test.group('Factory | HasOne | make', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('make model with relationship', async ({ assert }) => { + test('make model with relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column() - public id: number + declare id: number @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasOne(() => Profile) - public profile: HasOne + declare profile: HasOne } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('profile', () => profileFactory) .build() @@ -109,50 +97,51 @@ test.group('Factory | HasOne | make', (group) => { assert.equal(user.profile.userId, user.id) }) - test('pass custom attributes to relationship', async ({ assert }) => { + test('pass custom attributes to relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column() - public id: number + declare id: number @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasOne(() => Profile) - public profile: HasOne + declare profile: HasOne } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('profile', () => profileFactory) .build() @@ -172,63 +161,59 @@ test.group('Factory | HasOne | make', (group) => { test.group('Factory | HasOne | create', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('create model with relationship', async ({ assert }) => { + test('create model with relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasOne(() => Profile) - public profile: HasOne + declare profile: HasOne } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('profile', () => profileFactory) .build() @@ -247,47 +232,48 @@ test.group('Factory | HasOne | create', (group) => { assert.equal(profiles[0].user_id, users[0].id) }) - test('pass custom attributes to relationship', async ({ assert }) => { + test('pass custom attributes to relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasOne(() => Profile) - public profile: HasOne + declare profile: HasOne } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('profile', () => profileFactory) .build() @@ -302,47 +288,48 @@ test.group('Factory | HasOne | create', (group) => { assert.equal(user.profile.userId, user.id) }) - test('create model with custom foreign key', async ({ assert }) => { + test('create model with custom foreign key', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Profile extends BaseModel { @column({ columnName: 'user_id' }) - public authorId: number + declare authorId: number @column() - public displayName: string + declare displayName: string } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasOne(() => Profile, { foreignKey: 'authorId' }) - public profile: HasOne + declare profile: HasOne } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return { displayName: 'virk', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('profile', () => profileFactory) .build() @@ -354,47 +341,48 @@ test.group('Factory | HasOne | create', (group) => { assert.equal(user.profile.authorId, user.id) }) - test('rollback changes on error', async ({ assert }) => { + test('rollback changes on error', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(3) class Profile extends BaseModel { @column() - public userId: number + declare userId: number @column() - public displayName: string + declare displayName: string } Profile.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @hasOne(() => Profile) - public profile: HasOne + declare profile: HasOne } - const profileFactory = new FactoryModel( - Profile, - () => { + const profileFactory = factoryManager + .define(Profile, () => { return {} - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(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 similarity index 60% rename from test/factory/many-to-many.spec.ts rename to test/factory/many_to_many.spec.ts index 4ca8941f..d7b800c4 100644 --- a/test/factory/many-to-many.spec.ts +++ b/test/factory/many_to_many.spec.ts @@ -6,92 +6,79 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - -/// - import { test } from '@japa/runner' -import type { ManyToMany } from '@ioc:Adonis/Lucid/Orm' -import { FactoryManager } from '../../src/Factory/index' -import { column, manyToMany } from '../../src/Orm/Decorators' +import type { ManyToMany } from '../../adonis-typings/relations.js' + +import { FactoryManager } from '../../src/factory/index.js' +import { column, manyToMany } from '../../src/orm/decorators/index.js' import { - fs, setup, getDb, cleanup, ormAdapter, resetTables, getBaseModel, - getFactoryModel, - setupApplication, -} from '../../test-helpers' -import { ApplicationContract } from '@ioc:Adonis/Core/Application' - -let db: ReturnType -let app: ApplicationContract -let BaseModel: ReturnType -const FactoryModel = getFactoryModel() +} from '../../test-helpers/index.js' +import { AppFactory } from '@adonisjs/core/factories/app' + const factoryManager = new FactoryManager() test.group('Factory | ManyToMany | make', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('make model with relationship', async ({ assert }) => { + test('make model with relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Skill extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public name: string + declare name: string } Skill.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @manyToMany(() => Skill) - public skills: ManyToMany + declare skills: ManyToMany } - const postFactory = new FactoryModel( - Skill, - () => { + const postFactory = factoryManager + .define(Skill, () => { return { name: 'Programming', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('skills', () => postFactory) .build() @@ -107,47 +94,48 @@ test.group('Factory | ManyToMany | make', (group) => { assert.isFalse(user.skills[0].$isPersisted) }) - test('pass custom attributes to relationship', async ({ assert }) => { + test('pass custom attributes to relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Skill extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public name: string + declare name: string } Skill.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @manyToMany(() => Skill) - public skills: ManyToMany + declare skills: ManyToMany } - const postFactory = new FactoryModel( - Skill, - () => { + const postFactory = factoryManager + .define(Skill, () => { return { name: 'Programming', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('skills', () => postFactory) .build() @@ -164,47 +152,48 @@ test.group('Factory | ManyToMany | make', (group) => { assert.equal(user.skills[0].name, 'Dancing') }) - test('make many relationship', async ({ assert }) => { + test('make many relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Skill extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public name: string + declare name: string } Skill.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @manyToMany(() => Skill) - public skills: ManyToMany + declare skills: ManyToMany } - const postFactory = new FactoryModel( - Skill, - () => { + const postFactory = factoryManager + .define(Skill, () => { return { name: 'Programming', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('skills', () => postFactory) .build() @@ -229,63 +218,59 @@ test.group('Factory | ManyToMany | make', (group) => { test.group('Factory | ManyToMany | create', (group) => { group.setup(async () => { - app = await setupApplication() - db = getDb(app) - BaseModel = getBaseModel(ormAdapter(db), app) await setup() }) group.teardown(async () => { - await db.manager.closeAll() await cleanup() - await fs.cleanup() }) group.each.teardown(async () => { await resetTables() }) - test('create model with relationship', async ({ assert }) => { + test('create model with relationship', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Skill extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public name: string + declare name: string } Skill.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @manyToMany(() => Skill) - public skills: ManyToMany + declare skills: ManyToMany } - const postFactory = new FactoryModel( - Skill, - () => { + const postFactory = factoryManager + .define(Skill, () => { return { name: 'Programming', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('skills', () => postFactory) .build() @@ -306,47 +291,48 @@ test.group('Factory | ManyToMany | create', (group) => { assert.equal(skillUsers[0].skill_id, skills[0].id) }) - test('pass custom attributes', async ({ assert }) => { + test('pass custom attributes', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Skill extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public name: string + declare name: string } Skill.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @manyToMany(() => Skill) - public skills: ManyToMany + declare skills: ManyToMany } - const postFactory = new FactoryModel( - Skill, - () => { + const postFactory = factoryManager + .define(Skill, () => { return { name: 'Programming', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('skills', () => postFactory) .build() @@ -361,47 +347,48 @@ test.group('Factory | ManyToMany | create', (group) => { assert.equal(user.skills[0].name, 'Dancing') }) - test('create many relationships', async ({ assert }) => { + test('create many relationships', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Skill extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public name: string + declare name: string } Skill.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @manyToMany(() => Skill) - public skills: ManyToMany + declare skills: ManyToMany } - const postFactory = new FactoryModel( - Skill, - () => { + const postFactory = factoryManager + .define(Skill, () => { return { name: 'Programming', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('skills', () => postFactory) .build() @@ -420,47 +407,48 @@ test.group('Factory | ManyToMany | create', (group) => { assert.equal(user.skills[1].name, 'Programming') }) - test('rollback changes on error', async ({ assert }) => { + test('rollback changes on error', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + assert.plan(4) class Skill extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public name: string + declare name: string } Skill.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @manyToMany(() => Skill) - public skills: ManyToMany + declare skills: ManyToMany } - const postFactory = new FactoryModel( - Skill, - () => { + const postFactory = factoryManager + .define(Skill, () => { return {} - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('skills', () => postFactory) .build() @@ -479,47 +467,48 @@ test.group('Factory | ManyToMany | create', (group) => { assert.lengthOf(userSkills, 0) }) - test('define pivot attributes for the pivot table', async ({ assert }) => { + test('define pivot attributes for the pivot table', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + const adapter = ormAdapter(db) + const BaseModel = getBaseModel(adapter, app) + class Skill extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public name: string + declare name: string } Skill.boot() class User extends BaseModel { @column({ isPrimary: true }) - public id: number + declare id: number @column() - public username: string + declare username: string @column() - public points: number = 0 + points: number = 0 @manyToMany(() => Skill) - public skills: ManyToMany + declare skills: ManyToMany } - const postFactory = new FactoryModel( - Skill, - () => { + const postFactory = factoryManager + .define(Skill, () => { return { name: 'Programming', } - }, - factoryManager - ).build() + }) + .build() - const factory = new FactoryModel( - User, - () => { + const factory = factoryManager + .define(User, () => { return {} - }, - factoryManager - ) + }) .relation('skills', () => postFactory) .build()