67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import MysqlConnectionManager from "../src/db/MysqlConnectionManager";
|
|
import Model from "../src/db/Model";
|
|
import {MIGRATIONS} from "./_migrations";
|
|
import ModelFactory from "../src/db/ModelFactory";
|
|
|
|
class FakeDummyModel extends Model {
|
|
public id?: number = undefined;
|
|
public name?: string = undefined;
|
|
public date?: Date = undefined;
|
|
public date_default?: Date = undefined;
|
|
|
|
protected init(): void {
|
|
this.setValidation('name').acceptUndefined().between(3, 256);
|
|
}
|
|
}
|
|
|
|
beforeAll(async (done) => {
|
|
MysqlConnectionManager.registerMigrations(MIGRATIONS);
|
|
ModelFactory.register(FakeDummyModel);
|
|
await MysqlConnectionManager.prepare();
|
|
done();
|
|
});
|
|
|
|
afterAll(async (done) => {
|
|
await MysqlConnectionManager.endPool();
|
|
done();
|
|
});
|
|
|
|
describe('Model', () => {
|
|
it('should have a proper table name', async () => {
|
|
const factory = ModelFactory.get(FakeDummyModel);
|
|
expect(factory.table).toBe('fake_dummy_models');
|
|
expect(factory.make({}).table).toBe('fake_dummy_models');
|
|
});
|
|
|
|
it('should insert and retrieve properly', async () => {
|
|
const factory = ModelFactory.get(FakeDummyModel);
|
|
await MysqlConnectionManager.query(`DROP TABLE IF EXISTS ${(factory.table)}`);
|
|
await MysqlConnectionManager.query(`CREATE TABLE ${(factory.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 = factory.make({
|
|
name: 'name1',
|
|
date: date,
|
|
});
|
|
|
|
console.log(instance)
|
|
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);
|
|
}); |