Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 47 additions & 19 deletions __tests__/intergation/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,27 @@ import { Comment } from '../fixtures/entity/Comment';
import { Post, PostType } from '../fixtures/entity/Post';
import { clean } from '../support/cleaner';

let connection: Connection;

beforeAll(async () => {
connection = await createConnection({
database: 'typeorm-factory-test',
entities: [Post, Comment, Author],
host: 'localhost',
port: 5432,
synchronize: true,
type: 'postgres',
username: process.env.PG_USERNAME
});
await clean();
});

afterAll(() => connection.close());

afterEach(() => clean);

describe('Factory Test', () => {
let connection: Connection;
let PostFactory: Factory<Post>;
let CommentFactory: Factory<Comment>;
let AuthorFactory: Factory<Author>;
let MostLikedPost: Factory<Post>;

beforeAll(async () => {
connection = await createConnection({
database: ':memory:',
entities: [Post, Comment, Author],
synchronize: true,
type: 'sqlite'
});
await clean();
});

afterAll(() => connection.close());

afterEach(() => clean);

beforeEach(() => {
CommentFactory = new Factory(Comment)
.sequence('text', i => `text ${i}`)
Expand Down Expand Up @@ -141,3 +137,35 @@ describe('Factory Test', () => {
expect(object.title).toEqual('new title');
});
});

describe('Factory on named connection', () => {
let connection: Connection;
const connectionName = 'second';
let AuthorFactory: Factory<Author>;

beforeAll(async () => {
connection = await createConnection({
name: connectionName,
database: ':memory:',
entities: [Post, Comment, Author],
synchronize: true,
type: 'sqlite'
});
await clean(connectionName);
});

afterAll(() => connection.close());

afterEach(() => clean(connectionName));

beforeEach(() => {
AuthorFactory = new Factory(Author, connectionName)
.sequence('firstName', i => `John ${i}`)
.sequence('lastName', i => `Doe ${i}`);
});

it('works on named connections', async () => {
const author = await AuthorFactory.create({ firstName: 'Albert' });
expect(author.firstName).toEqual('Albert');
});
});
4 changes: 2 additions & 2 deletions __tests__/support/cleaner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getConnection } from 'typeorm';

export const clean = () => {
const { manager } = getConnection();
export const clean = (connectionName = 'default') => {
const { manager } = getConnection(connectionName);
const names = ['comment', 'post', 'author'];
return manager.query(names.map(name => `DELETE FROM ${name};`).join('\n'));
};
Loading