Skip to content

Commit

Permalink
feat: add fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspearson committed Mar 8, 2021
1 parent e5de2e4 commit e30166a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 4 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ yarn db:migration:generate:missing InitialSchema

## TODO

- Add fixtures
- Improve README

## Contribution Guidelines
Expand Down
12 changes: 11 additions & 1 deletion api.http
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
########### USER ###########
########################################################################

### Login
### Login (with fixture user)
POST {{baseUrl}}/user/login HTTP/1.1
Accept: application/json
Content-Type: application/json

{
"email": "msmith@fixture.example.com",
"password": "secret"
}

### Login (with created user)
POST {{baseUrl}}/user/login HTTP/1.1
Accept: application/json
Content-Type: application/json
Expand Down
28 changes: 28 additions & 0 deletions src/db/fixtures/seeder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Connection, ObjectType } from 'typeorm';
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';

import { User } from '@/db/entities/user.entity';
import { userFixtures } from '@/db/fixtures/user.fixtures';
import { encryptPassword } from '@/utils/password.utils';

type Entity = ObjectType<Record<string, unknown>>;

interface Fixture {
entity: Entity;
values: QueryDeepPartialEntity<Entity>[];
}

const fixtures: Fixture[] = [{ entity: User, values: userFixtures }];

export async function seedDatabase(connection: Connection): Promise<void> {
for (const fixture of fixtures) {
if (fixture.entity === User) {
for (const value of fixture.values) {
const user = value as User;
user.password = await encryptPassword(user.password);
}
}
const repository = connection.getRepository(fixture.entity);
await repository.save(fixture.values);
}
}
14 changes: 14 additions & 0 deletions src/db/fixtures/user.fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';

import { User } from '@/db/entities/user.entity';

export const userFixtures: QueryDeepPartialEntity<User>[] = [
{
id: 1,
enabled: true,
email: 'msmith@fixture.example.com' as Email,
firstName: 'Morty',
lastName: 'Smith',
password: 'secret',
},
];
3 changes: 2 additions & 1 deletion src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Connection, createConnection, getConnectionOptions } from 'typeorm';

import { Environment } from '@/common/enums/environment.enum';
import { configureConnectionOptions } from '@/db/config.db';
import { seedDatabase } from '@/db/fixtures/seeder';
import { logger } from '@/logger';

interface AdditionalConnectionOptions {
Expand All @@ -20,8 +21,8 @@ export async function init(options?: AdditionalConnectionOptions): Promise<Conne
configureConnectionOptions(connectionOptions);
const connection = await createConnection(Object.assign(connectionOptions, options));
if (process.env.NODE_ENV === Environment.Development) {
// TODO: Seed the database with fixtures
logger.debug('Seeding database');
await seedDatabase(connection);
}
return connection;
}
3 changes: 2 additions & 1 deletion test/integration/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Client } from 'pg';
import { Connection } from 'typeorm';

import * as db from '@/db';
import { seedDatabase } from '@/db/fixtures/seeder';

export async function closeConnections(connection?: Connection): Promise<void> {
if (connection) {
Expand Down Expand Up @@ -34,6 +35,6 @@ export async function setupDatabase(database: string): Promise<Connection> {
dropSchema: true,
logging: false,
});
// TODO: load fixtures
await seedDatabase(connection);
return connection;
}
1 change: 1 addition & 0 deletions test/integration/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = Object.assign({}, require('../../jest.config.js'), {
'!src/common/dto/index.ts',
'src/db/**/*.ts',
'!src/db/config.db.ts',
'!src/db/fixtures/**/*.ts',
'!src/db/index.ts',
'!src/db/migrations/*.ts',
'src/**/*.controller.ts',
Expand Down

0 comments on commit e30166a

Please sign in to comment.