fixed seq method and adapted definitions to fix correction (#36386)

This commit is contained in:
Ariel Barabas 2019-07-01 17:21:27 -03:00 committed by Ryan Cavanaugh
parent c4ab682647
commit 4eebb7a04e
2 changed files with 138 additions and 108 deletions

View File

@ -1,45 +1,66 @@
import * as factory from "factory-girl";
interface User {
username?: string;
score?: number;
email?: string;
roles?: Role[];
creditCard?: any;
boss?: User;
username?: string;
score?: number;
email?: string;
roles?: Role[];
creditCard?: any;
boss?: User;
}
interface Role {
id?: number;
name?: string;
id?: number;
name?: string;
}
interface SuperUser extends User {
superpower: string;
superpower: string;
}
// Testing setAdapter
factory.setAdapter("my-adapter", "my-adapter-name");
// Testing sequence to use it on its own
const scoreSequence = factory.seq<number>('User.score', score => score + 1);
// Testing define with seq, assoc, assocAttrs, assocMany
factory.define<User>("user", {}, {
username: "Bob",
score: factory.seq<number>("User.score", score => score + 1),
email: factory.seq<string>("User.email", num => `email-${1}@users.com`),
roles: factory.assocMany("Role", 3, "id"),
creditCard: factory.assocAttrs("CreditCard", "creditCard", { number: "1234" }),
boss: factory.assoc("User", "boss")
}, {
afterBuild: (model, attrs, options) => {},
afterCreate: (model, attrs, options) => {}
});
factory.define<User>(
'user',
{},
{
username: () => (Math.random() > 0.5 ? 'Bob' : 'Robert'),
score: scoreSequence,
email: factory.seq<string>('User.email', num => `email-${1}@users.com`),
roles: factory.assocMany('Role', 3, 'id'),
creditCard: factory.assocAttrs('CreditCard', 'creditCard', { number: '1234' }),
boss: factory.assoc('User', 'boss'),
},
{
afterBuild: (model, attrs, options) => {},
afterCreate: (model, attrs, options) => {},
}
);
// Testing extend, with and without options
factory.extend("user", "superuser", { superpower: "flight" });
factory.extend("user", "superuser", { superpower: "flight" }, {
afterBuild: (model, attrs, options) => {},
afterCreate: (model, attrs, options) => {}
factory.extend(
'user',
'superuser',
{ superpower: 'flight' },
{
afterBuild: (model, attrs, options) => {},
afterCreate: (model, attrs, options) => {},
}
);
factory.extend('user', 'email-related', () => {
const score = scoreSequence();
return {
score,
email: factory.seq<string>('User.email', num => `email-${num}-${score}@users.com`),
};
});
// Testing attrs, with and without attributes
@ -55,40 +76,41 @@ factory.build<User>("user").then(user => user.username);
factory.build<User>("user", { score: 10 }).then(user => user.username);
// Testing buildMany, with and without attributes and options
factory.buildMany<User>("user", 3)
.then(users => users.map(user => user.username));
factory.buildMany<User>('user', 3).then(users => users.map(user => user.username));
factory.buildMany<User>("user", 3, { username: "John McClane" })
.then(users => users.map(user => user.username));
factory.buildMany<User>('user', 3, { username: 'John McClane' }).then(users => users.map(user => user.username));
// Testing buildMany with a list of attributes
factory.buildMany<User>("user", [
{ username: "Jake Blues" },
{ username: "Elwood Blues" }
]).then(users => users.map(user => user.username));
factory
.buildMany<User>('user', [{ username: 'Jake Blues' }, { username: 'Elwood Blues' }])
.then(users => users.map(user => user.username));
// Testing create, with and without attributes
factory.create<User>("user").then(user => user.username);
factory.create<User>("user", { score: 10 }).then(user => user.username);
// Testing createMany, with and without attributes
factory.createMany<User>("user", 3)
.then(users => users.map(user => user.username));
factory.createMany<User>('user', 3).then(users => users.map(user => user.username));
factory.createMany<User>("user", 3, { username: "Rocky Balboa" })
.then(users => users.map(user => user.username));
factory.createMany<User>('user', 3, { username: 'Rocky Balboa' }).then(users => users.map(user => user.username));
// Testing createMany with options
factory.createMany<User>("user", 3, { username: "John Rambo" }, {
afterBuild: (model, attrs, options) => {},
afterCreate: (model, attrs, options) => {}
}).then(users => users.map(user => user.username));
factory
.createMany<User>(
'user',
3,
{ username: 'John Rambo' },
{
afterBuild: (model, attrs, options) => {},
afterCreate: (model, attrs, options) => {},
}
)
.then(users => users.map(user => user.username));
// Testing createMany with a list of attributes
factory.createMany<User>("user", [
{ username: "Emmett Brown" },
{ username: "Marty McFly" }
]).then(users => users.map(user => user.username));
factory
.createMany<User>('user', [{ username: 'Emmett Brown' }, { username: 'Marty McFly' }])
.then(users => users.map(user => user.username));
// Testing cleanUp
factory.cleanUp();

View File

@ -9,86 +9,94 @@
declare const factory: factory.Static;
declare namespace factory {
interface Static {
/**
* Associate the factory to other model
*/
assoc(model: string, attributes: string): any;
type Generator<T> = () => T;
/**
* Associate the factory to a model that's not persisted
*/
assocAttrs(name: string, key?: string, attributes?: any): any;
type Definition<T> = T | Generator<T>;
/**
* Associate the factory to multiple other models
*/
assocMany(model: string, num: number, attributes: string): any[];
type Attributes<T> = Definition<{
[P in keyof T]: Definition<T[P]>;
}>;
/**
* Generates and returns model attributes as an object hash instead of the model instance
*/
attrs<T>(name: string, attrs?: Partial<T>): Promise<T>;
interface Static {
/**
* Associate the factory to other model
*/
assoc(model: string, attributes: string): any;
/**
* Generates and returns a collection of model attributes as an object hash instead of the model instance
*/
attrsMany<T>(name: string, num: number, attrs?: Array<Partial<T>>): Promise<T[]>;
/**
* Associate the factory to a model that's not persisted
*/
assocAttrs(name: string, key?: string, attributes?: any): any;
/**
* Builds a new model instance that is not persisted
*/
build<T>(name: string, attrs?: Partial<T>): Promise<T>;
/**
* Associate the factory to multiple other models
*/
assocMany(model: string, num: number, attributes: string): any[];
/**
* Builds an array of model instances that are persisted
*/
buildMany<T>(name: string, num: number, attrs?: Partial<T>): Promise<T[]>;
buildMany<T>(name: string, attrs?: Array<Partial<T>>): Promise<T[]>;
/**
* Generates and returns model attributes as an object hash instead of the model instance
*/
attrs<T>(name: string, attrs?: Attributes<Partial<T>>): Promise<T>;
/**
* Destroys all of the created models
*/
cleanUp(): void;
/**
* Generates and returns a collection of model attributes as an object hash instead of the model instance
*/
attrsMany<T>(name: string, num: number, attrs?: ReadonlyArray<Attributes<Partial<T>>>): Promise<T[]>;
/**
* Builds a new model instance that is persisted
*/
create<T>(name: string, attrs?: Partial<T>): Promise<T>;
/**
* Builds a new model instance that is not persisted
*/
build<T>(name: string, attrs?: Attributes<Partial<T>>): Promise<T>;
/**
* Builds an array of model instances that are persisted
*/
createMany<T>(name: string, num: number, attrs?: Partial<T>, buildOptions?: Options<T>): Promise<T[]>;
createMany<T>(name: string, attrs?: Array<Partial<T>>, buildOptions?: Options<T>): Promise<T[]>;
/**
* Builds an array of model instances that are persisted
*/
buildMany<T>(name: string, num: number, attrs?: Attributes<Partial<T>>): Promise<T[]>;
buildMany<T>(name: string, attrs?: ReadonlyArray<Attributes<Partial<T>>>): Promise<T[]>;
/**
* Define a new factory with a set of options
*/
define<T>(name: string, model: any, attrs: T, options?: Options<T>): void;
/**
* Destroys all of the created models
*/
cleanUp(): void;
/**
* Extends a factory
*/
extend(parent: string, name: string, initializer: any, options?: Options<any>): any;
/**
* Builds a new model instance that is persisted
*/
create<T>(name: string, attrs?: Attributes<Partial<T>>): Promise<T>;
/**
* Generate values sequentially inside a factory
*/
seq<T>(name: string, fn: (sequence: number) => T): T;
/**
* Builds an array of model instances that are persisted
*/
createMany<T>(name: string, num: number, attrs?: Attributes<Partial<T>>, buildOptions?: Options<T>): Promise<T[]>;
createMany<T>(name: string, attrs?: ReadonlyArray<Attributes<Partial<T>>>, buildOptions?: Options<T>): Promise<T[]>;
/**
* Register an adapter, either as default or tied to a specific model
*/
setAdapter(adapter: any, name?: string): void;
}
/**
* Define a new factory with a set of options
*/
define<T>(name: string, model: any, attrs: Attributes<T>, options?: Options<T>): void;
interface Options<T> {
afterBuild?: Hook<T>;
afterCreate?: Hook<T>;
}
/**
* Extends a factory
*/
extend(parent: string, name: string, initializer: any, options?: Options<any>): any;
type Hook<T> = (model: any, attrs: T[], options: any) => void;
/**
* Generate values sequentially inside a factory
*/
seq<T>(name: string, fn: (sequence: number) => T): Generator<T>;
/**
* Register an adapter, either as default or tied to a specific model
*/
setAdapter(adapter: any, name?: string): void;
}
interface Options<T> {
afterBuild?: Hook<T>;
afterCreate?: Hook<T>;
}
type Hook<T> = (model: any, attrs: T[], options: any) => void;
}
export = factory;