Skip to content

Commit

Permalink
mem-server/model query interface tests are complete
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Oct 23, 2017
1 parent d138883 commit 3627b2f
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 53 deletions.
35 changes: 27 additions & 8 deletions lib/mem-server/model.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import chalk from 'chalk';

const ENVIRONMENT_IS_NODE = typeof global === 'object';
const targetNamespace = ENVIRONMENT_IS_NODE ? global : window;

Expand All @@ -6,10 +8,33 @@ export default function(options) {
modelName: '',
primaryKey: '',
attributes: [],
find(id) {
find(param) {
if (!param) {
throw new Error(chalk.red(`MemServer ${this.modelName}.find(id) cannot be called without a valid id`));
} else if (Array.isArray(param)) {
const models = targetNamespace.MemServer.DB[this.modelName] || [];

return models.reduce((result, model) => {
const foundModel = param.includes(model.id) ? model : null;

return foundModel ? result.concat([foundModel]) : result;
}, []);
} else if (typeof param !== 'number') {
throw new Error(chalk.red(`MemServer ${this.modelName}.find(id) cannot be called without a valid id`));
}

const models = targetNamespace.MemServer.DB[this.modelName] || [];

return models.find((model) => model.id === param);
},
findBy(options) {
if (!options) {
throw new Error(chalk.red(`MemServer ${this.modelName}.findBy(id) cannot be called without a parameter`));
}
const keys = Object.keys(options);
const models = targetNamespace.MemServer.DB[this.modelName] || [];

return models.find((model) => model.id === id);
return models.find((model) => comparison(model, options, keys, 0));
},
findAll(options={}) {
const keys = Object.keys(options);
Expand All @@ -21,12 +46,6 @@ export default function(options) {

return models.filter((model) => comparison(model, options, keys, 0));
},
findBy(options) {
const keys = Object.keys(options);
const models = targetNamespace.MemServer.DB[this.modelName] || [];

return models.find((model) => comparison(model, options, keys, 0));
},
insert(options) { // NOTE: what if there is same id?
const models = targetNamespace.MemServer.DB[this.modelName] || [];

Expand Down
5 changes: 2 additions & 3 deletions test/mem-server.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const rimraf = require('rimraf');
describe('MemServer fixture constraint feature', function() {
beforeEach(function() {
const modelFileContent = `import Model from '${process.cwd()}/lib/mem-server/model';
export default Model({});`;
export default Model({});`;

fs.mkdirSync(`./memserver`);
fs.mkdirSync(`./memserver/models`);
Expand All @@ -30,7 +29,7 @@ describe('MemServer fixture constraint feature', function() {

it('should throw error if any of the fixtures missing id or uuid', function() {
this.timeout(5000);

fs.mkdirSync(`./memserver/fixtures`);
fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photos.js`, `export default [
{
Expand Down
4 changes: 2 additions & 2 deletions test/mem-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ describe('MemServer', function() {

assert.equal(model.modelName, modelName);
assert.deepEqual(Object.keys(MemServer.Models[modelName]), [
'modelName', 'primaryKey', 'attributes', 'find', 'findAll', 'findBy', 'insert', 'bulkInsert', 'update',
'bulkUpdate', 'destroy', 'bulkDestroy', 'serialize'
'modelName', 'primaryKey', 'attributes', 'find', 'findBy', 'findAll', 'insert',
'bulkInsert', 'update', 'bulkUpdate', 'destroy', 'bulkDestroy', 'serialize'
]);
});
});
Expand Down
Loading

0 comments on commit 3627b2f

Please sign in to comment.