swaf/test/Model.test.ts

64 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-04-23 11:40:55 +02:00
import MysqlConnectionManager from "../src/db/MysqlConnectionManager";
import Model from "../src/db/Model";
2020-04-23 11:40:55 +02:00
import Validator from "../src/db/Validator";
import {MIGRATIONS} from "./_migrations";
2020-04-23 11:40:55 +02:00
class FakeDummyModel extends Model {
public name?: string;
public date?: Date;
public date_default?: Date;
protected defineProperties(): void {
this.defineProperty<string>('name', new Validator().acceptUndefined().between(3, 256));
this.defineProperty<Date>('date', new Validator());
this.defineProperty<Date>('date_default', new Validator());
}
}
beforeAll(async (done) => {
MysqlConnectionManager.registerMigrations(MIGRATIONS);
2020-04-23 11:40:55 +02:00
await MysqlConnectionManager.prepare();
done();
});
afterAll(async (done) => {
await MysqlConnectionManager.endPool();
done();
});
describe('Model', () => {
it('should have a proper table name', async () => {
expect(FakeDummyModel.table).toBe('fake_dummy_models');
expect(new FakeDummyModel({}).table).toBe('fake_dummy_models');
});
it('should insert and retrieve properly', async () => {
await MysqlConnectionManager.query(`DROP TABLE IF EXISTS ${FakeDummyModel.table}`);
await MysqlConnectionManager.query(`CREATE TABLE ${FakeDummyModel.table}(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(256),
date DATETIME,
date_default DATETIME DEFAULT NOW(),
PRIMARY KEY(id)
)`);
const date = new Date(569985);
let instance: FakeDummyModel | null = new FakeDummyModel({
name: 'name1',
date: date,
});
await instance.save();
expect(instance.id).toBe(1);
expect(instance.name).toBe('name1');
expect(instance.date?.getTime()).toBeCloseTo(date.getTime(), -4);
expect(instance.date_default).toBeDefined();
instance = await FakeDummyModel.getById('1');
2020-04-23 11:40:55 +02:00
expect(instance).toBeDefined();
expect(instance!.id).toBe(1);
expect(instance!.name).toBe('name1');
expect(instance!.date?.getTime()).toBeCloseTo(date.getTime(), -4);
expect(instance!.date_default).toBeDefined();
}, 15000);
});