diff options
| author | Tom Harley | 2018-03-27 19:19:16 +0100 |
|---|---|---|
| committer | Tom Harley | 2018-03-27 19:19:16 +0100 |
| commit | 150115e855e2c406b23aa569dd675597d6957570 (patch) | |
| tree | 7cb6cbbc7e7f2911596bb1f03dbcedd8b10e0e49 /test/test-db.ts | |
| parent | Merge branch 'cjd24/conversion' into development (diff) | |
| parent | Centralise test configuration and introduce SQLite (diff) | |
| download | medicine-150115e855e2c406b23aa569dd675597d6957570.tar.gz medicine-150115e855e2c406b23aa569dd675597d6957570.zip | |
Merge branch ‘testing-middleware’ into development
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; +}; |
