aboutsummaryrefslogtreecommitdiff
path: root/test/test-db.ts
blob: fe8ae4de7dc798cfbc0843b9e9b3ae165a314e66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { default as User } from '../src/db/model/User';
import { default as UserGroup } from '../src/db/model/UserGroup';

import { Sequelize } from 'sequelize-typescript';
import * as sqlite from 'sqlite3';

export type Database = Sequelize;

export const initDB: () => Database = (): Database => {
	const dataFile: string = ':memory:';
	const db: sqlite.Database = new sqlite.Database(dataFile);
	const seq: Database = new Sequelize(`sqlite:${dataFile}`);
	seq.addModels([`${__dirname}/../src/db/model`]);
	seq.options.logging = false;
	return seq;
};

export const resetDB: (database: Database) => Promise<void> = async(
	database: Database
): Promise<void> => {
	await database.sync({
		force: true
	});
};

export interface Credentials {
	username: string;
	password: string;
	usergroups: UserGroup[];
}

export const addUser: (database?: Database) => Promise<Credentials> = async(
): Promise<Credentials> => {
	const mockUser: Credentials = {
		username: 'mock_user',
		password: 'mock_password',
		usergroups: []
	};
	await new User(mockUser).save();
	return mockUser;
};