64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
|
import registerMigrations from "./_registerMigrations";
|
||
|
import Model from "../src/db/Model";
|
||
|
import MysqlConnectionManager from "../src/db/MysqlConnectionManager";
|
||
|
import Validator from "../src/db/Validator";
|
||
|
|
||
|
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) => {
|
||
|
registerMigrations();
|
||
|
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);
|
||
|
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);
|
||
|
});
|