Create protofile database model & unit tests (#2754)

This commit is contained in:
Opender Singh
2020-10-22 10:02:43 +13:00
committed by GitHub
parent edcfcf5cef
commit cc71660c96
3 changed files with 102 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
import * as models from '../index';
import { globalBeforeEach } from '../../__jest__/before-each';
describe('init()', () => {
beforeEach(globalBeforeEach);
it('contains all required fields', async () => {
expect(models.protoFile.init()).toEqual({
name: 'New Proto File',
protoText: '',
});
});
});
describe('create()', () => {
beforeEach(globalBeforeEach);
it('creates a valid protofile', async () => {
Date.now = jest.fn().mockReturnValue(1478795580200);
const request = await models.protoFile.create({
name: 'My File',
parentId: 'fld_124',
protoText: 'some proto text',
});
const expected = {
_id: 'pf_cc1dd2ca4275747aa88199e8efd42403',
created: 1478795580200,
modified: 1478795580200,
parentId: 'fld_124',
type: 'ProtoFile',
name: 'My File',
protoText: 'some proto text',
};
expect(request).toEqual(expected);
expect(await models.protoFile.getById(expected._id)).toEqual(expected);
});
it('fails when missing parentId', async () => {
Date.now = jest.fn().mockReturnValue(1478795580200);
expect(() => models.protoFile.create({ name: 'no parentId' })).toThrow(
'New Proto File missing `parentId`',
);
});
});

View File

@@ -17,6 +17,7 @@ import * as _stats from './stats';
import * as _unitTest from './unit-test';
import * as _unitTestResult from './unit-test-result';
import * as _unitTestSuite from './unit-test-suite';
import * as _protoFile from './proto-file';
import * as _workspace from './workspace';
import * as _workspaceMeta from './workspace-meta';
import { generateId } from '../common/misc';
@@ -48,6 +49,7 @@ export const stats = _stats;
export const unitTest = _unitTest;
export const unitTestSuite = _unitTestSuite;
export const unitTestResult = _unitTestResult;
export const protoFile = _protoFile;
export const workspace = _workspace;
export const workspaceMeta = _workspaceMeta;
@@ -73,6 +75,7 @@ export function all() {
unitTestSuite,
unitTestResult,
unitTest,
protoFile,
];
}

View File

@@ -0,0 +1,55 @@
// @flow
import * as db from '../common/database';
import type { BaseModel } from './index';
export const name = 'Proto File';
export const type = 'ProtoFile';
export const prefix = 'pf';
export const canDuplicate = true;
export const canSync = true;
type BaseProtoFile = {
name: string,
protoText: string,
};
export type ProtoFile = BaseModel & BaseProtoFile;
export function init(): BaseProtoFile {
return {
name: 'New Proto File',
protoText: '',
};
}
export function migrate(doc: ProtoFile): ProtoFile {
return doc;
}
export function create(patch: $Shape<ProtoFile> = {}): Promise<ProtoFile> {
if (!patch.parentId) {
throw new Error('New Proto File missing `parentId`');
}
return db.docCreate(type, patch);
}
export function remove(protoFile: ProtoFile): Promise<void> {
return db.remove(protoFile);
}
export function update(protoFile: ProtoFile, patch: $Shape<ProtoFile> = {}): Promise<ProtoFile> {
return db.docUpdate(protoFile, patch);
}
export function getById(_id: string): Promise<ProtoFile | null> {
return db.getWhere(type, { _id });
}
export function getByParentId(parentId: string): Promise<ProtoFile | null> {
return db.getWhere(type, { parentId });
}
export function all(): Promise<Array<ProtoFile>> {
return db.all(type);
}