diff options
| author | Tom Harley | 2018-03-27 03:50:17 +0100 |
|---|---|---|
| committer | Tom Harley | 2018-03-27 03:50:17 +0100 |
| commit | ea1ee67bdd02b2ea727c09cfef1784a61aa934f4 (patch) | |
| tree | 6bf1e8a086bd5250416307501147ee4fe942b87b /test/test-db.ts | |
| parent | small fix (diff) | |
| download | medicine-ea1ee67bdd02b2ea727c09cfef1784a61aa934f4.tar.gz medicine-ea1ee67bdd02b2ea727c09cfef1784a61aa934f4.zip | |
Centralise test configuration and introduce SQLite
Tests now take their configuration from a new file test-db.ts. Additionally, this configuration sets up and uses an in-memory SQLite3 database for testing, to avoid writing to disk and to prevent edits to a current database.
Diffstat (limited to 'test/test-db.ts')
| -rw-r--r-- | test/test-db.ts | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/test/test-db.ts b/test/test-db.ts new file mode 100644 index 0000000..fe8ae4d --- /dev/null +++ b/test/test-db.ts @@ -0,0 +1,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; +}; |
