From cc71660c96cb043c80036d619b20ffe774720b4e Mon Sep 17 00:00:00 2001 From: Opender Singh Date: Thu, 22 Oct 2020 10:02:43 +1300 Subject: [PATCH] Create protofile database model & unit tests (#2754) --- .../app/models/__tests__/proto-file.test.js | 44 +++++++++++++++ packages/insomnia-app/app/models/index.js | 3 + .../insomnia-app/app/models/proto-file.js | 55 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 packages/insomnia-app/app/models/__tests__/proto-file.test.js create mode 100644 packages/insomnia-app/app/models/proto-file.js diff --git a/packages/insomnia-app/app/models/__tests__/proto-file.test.js b/packages/insomnia-app/app/models/__tests__/proto-file.test.js new file mode 100644 index 0000000000..dd48368ac1 --- /dev/null +++ b/packages/insomnia-app/app/models/__tests__/proto-file.test.js @@ -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`', + ); + }); +}); diff --git a/packages/insomnia-app/app/models/index.js b/packages/insomnia-app/app/models/index.js index 26d1129a75..e3bd5523d1 100644 --- a/packages/insomnia-app/app/models/index.js +++ b/packages/insomnia-app/app/models/index.js @@ -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, ]; } diff --git a/packages/insomnia-app/app/models/proto-file.js b/packages/insomnia-app/app/models/proto-file.js new file mode 100644 index 0000000000..e8e6dcf528 --- /dev/null +++ b/packages/insomnia-app/app/models/proto-file.js @@ -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 = {}): Promise { + if (!patch.parentId) { + throw new Error('New Proto File missing `parentId`'); + } + + return db.docCreate(type, patch); +} + +export function remove(protoFile: ProtoFile): Promise { + return db.remove(protoFile); +} + +export function update(protoFile: ProtoFile, patch: $Shape = {}): Promise { + return db.docUpdate(protoFile, patch); +} + +export function getById(_id: string): Promise { + return db.getWhere(type, { _id }); +} + +export function getByParentId(parentId: string): Promise { + return db.getWhere(type, { parentId }); +} + +export function all(): Promise> { + return db.all(type); +}