2020-04-23 11:40:55 +02:00
|
|
|
import MysqlConnectionManager from "../src/db/MysqlConnectionManager";
|
2020-04-23 15:43:15 +02:00
|
|
|
import Model from "../src/db/Model";
|
|
|
|
import {MIGRATIONS} from "./_migrations";
|
2020-07-24 15:40:10 +02:00
|
|
|
import ModelFactory from "../src/db/ModelFactory";
|
2020-08-05 10:45:13 +02:00
|
|
|
import {ValidationBag, ValidationError} from "../src/db/Validator";
|
2020-04-23 11:40:55 +02:00
|
|
|
|
|
|
|
class FakeDummyModel extends Model {
|
2020-07-24 15:40:10 +02:00
|
|
|
public id?: number = undefined;
|
|
|
|
public name?: string = undefined;
|
|
|
|
public date?: Date = undefined;
|
|
|
|
public date_default?: Date = undefined;
|
2020-04-23 11:40:55 +02:00
|
|
|
|
2020-06-27 14:36:50 +02:00
|
|
|
protected init(): void {
|
2020-07-24 15:40:10 +02:00
|
|
|
this.setValidation('name').acceptUndefined().between(3, 256);
|
2020-04-23 11:40:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 10:45:13 +02:00
|
|
|
let factory: ModelFactory<FakeDummyModel>;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2020-04-23 15:43:15 +02:00
|
|
|
MysqlConnectionManager.registerMigrations(MIGRATIONS);
|
2020-07-24 15:40:10 +02:00
|
|
|
ModelFactory.register(FakeDummyModel);
|
2020-04-23 11:40:55 +02:00
|
|
|
await MysqlConnectionManager.prepare();
|
2020-08-05 10:45:13 +02:00
|
|
|
|
|
|
|
// Create FakeDummyModel table
|
|
|
|
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)
|
|
|
|
)`);
|
2020-04-23 11:40:55 +02:00
|
|
|
});
|
|
|
|
|
2020-08-05 10:45:13 +02:00
|
|
|
afterAll(async () => {
|
2020-04-23 11:40:55 +02:00
|
|
|
await MysqlConnectionManager.endPool();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model', () => {
|
2020-08-05 10:45:13 +02:00
|
|
|
it('should construct properly', () => {
|
|
|
|
const date = new Date(888);
|
|
|
|
const model = factory.create({
|
|
|
|
name: 'a_name',
|
|
|
|
date: date,
|
|
|
|
non_existing_property: 'dropped_value',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.id).toBeUndefined();
|
|
|
|
expect(model.name).toBe('a_name');
|
|
|
|
expect(model.date).toBe(date);
|
|
|
|
expect(model.date_default).toBeUndefined();
|
|
|
|
expect(model.non_existing_property).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have a proper table name', () => {
|
2020-07-24 15:40:10 +02:00
|
|
|
expect(factory.table).toBe('fake_dummy_models');
|
2020-08-05 10:45:13 +02:00
|
|
|
expect(FakeDummyModel.table).toBe('fake_dummy_models');
|
2020-07-27 10:52:39 +02:00
|
|
|
expect(FakeDummyModel.create({}).table).toBe('fake_dummy_models');
|
2020-04-23 11:40:55 +02:00
|
|
|
});
|
|
|
|
|
2020-08-05 10:45:13 +02:00
|
|
|
it('should insert properly', async () => {
|
2020-04-23 11:40:55 +02:00
|
|
|
const date = new Date(569985);
|
2020-08-05 10:45:13 +02:00
|
|
|
const insertInstance: FakeDummyModel | null = factory.create({
|
2020-04-23 11:40:55 +02:00
|
|
|
name: 'name1',
|
|
|
|
date: date,
|
|
|
|
});
|
|
|
|
|
2020-08-05 10:45:13 +02:00
|
|
|
|
|
|
|
// Insert
|
|
|
|
expect(await insertInstance.exists()).toBeFalsy();
|
|
|
|
await insertInstance.save();
|
|
|
|
expect(await insertInstance.exists()).toBeTruthy();
|
|
|
|
|
|
|
|
expect(insertInstance.id).toBe(1); // Auto id from insert
|
|
|
|
expect(insertInstance.name).toBe('name1');
|
|
|
|
expect(insertInstance.date?.getTime()).toBeCloseTo(date.getTime(), -4);
|
|
|
|
expect(insertInstance.date_default).toBeDefined();
|
|
|
|
|
|
|
|
// Check that row exists in DB
|
|
|
|
const retrievedInstance = await FakeDummyModel.getById(1);
|
|
|
|
expect(retrievedInstance).toBeDefined();
|
|
|
|
expect(retrievedInstance!.id).toBe(1);
|
|
|
|
expect(retrievedInstance!.name).toBe('name1');
|
|
|
|
expect(retrievedInstance!.date?.getTime()).toBeCloseTo(date.getTime(), -4);
|
|
|
|
expect(retrievedInstance!.date_default).toBeDefined();
|
|
|
|
|
|
|
|
const failingInsertModel = factory.create({
|
|
|
|
name: 'a',
|
|
|
|
});
|
|
|
|
await expect(failingInsertModel.save()).rejects.toBeInstanceOf(ValidationBag);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update properly', async () => {
|
|
|
|
const insertModel = factory.create({
|
|
|
|
name: 'update',
|
|
|
|
});
|
|
|
|
await insertModel.save();
|
|
|
|
|
|
|
|
const preUpdatedModel = await FakeDummyModel.getById(insertModel.id);
|
|
|
|
expect(preUpdatedModel).not.toBeNull();
|
|
|
|
expect(preUpdatedModel!.name).toBe(insertModel.name);
|
|
|
|
|
|
|
|
// Update model
|
|
|
|
preUpdatedModel!.name = 'updated_name';
|
|
|
|
await preUpdatedModel!.save();
|
|
|
|
|
|
|
|
const postUpdatedModel = await FakeDummyModel.getById(insertModel.id);
|
|
|
|
expect(postUpdatedModel).not.toBeNull();
|
|
|
|
expect(postUpdatedModel!.id).toBe(insertModel.id);
|
|
|
|
expect(postUpdatedModel!.name).not.toBe(insertModel.name);
|
|
|
|
expect(postUpdatedModel!.name).toBe(preUpdatedModel!.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete properly', async () => {
|
|
|
|
const insertModel = factory.create({
|
|
|
|
name: 'delete',
|
|
|
|
});
|
|
|
|
await insertModel.save();
|
|
|
|
|
|
|
|
const preDeleteModel = await FakeDummyModel.getById(insertModel.id);
|
|
|
|
expect(preDeleteModel).not.toBeNull();
|
|
|
|
|
|
|
|
await preDeleteModel!.delete();
|
|
|
|
|
|
|
|
const postDeleteModel = await FakeDummyModel.getById(insertModel.id);
|
|
|
|
expect(postDeleteModel).toBeNull();
|
|
|
|
});
|
2020-04-23 11:40:55 +02:00
|
|
|
});
|