Skip to content

Commit

Permalink
fixed weird node.js caching bug and retries instated because some res…
Browse files Browse the repository at this point in the history
…ults were randomly failing in certain tests
  • Loading branch information
izelnakri committed Oct 26, 2017
1 parent a4ad69a commit 7de36c0
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 24 deletions.
23 changes: 17 additions & 6 deletions lib/mem-server/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const targetNamespace = typeof global === 'object' ? global : window;

export default function(options) {
return Object.assign({}, {
modelName: '',
primaryKey: '',
modelName: null,
primaryKey: null,
defaultAttributes: {},
attributes: [],
count() {
Expand Down Expand Up @@ -79,22 +79,33 @@ export default function(options) {
throw new Error(chalk.red(`[MemServer] ${this.modelName} ${this.primaryKey} ${target[this.primaryKey]} already exists in the database! ${this.modelName}.insert(${util.inspect(options)}) fails`));
}

Object.keys(target)
.filter((attribute) => !this.attributes.includes(attribute))
.forEach((attribute) => this.attributes.push(attribute));

models.push(target);

return target;
},
update(record) {
if (!record || (!record.id && !record.uuid)) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.update(record) requires id or uuid primary key to update a record`));
}

const targetRecord = record.id ? this.find(record.id) : this.findBy({ uuid: record.uuid });

if (!targetRecord) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.update(record) requires id or uuid primary key to update a record`));
throw new Error(chalk.red(`[MemServer] ${this.modelName}.update(record) failed because ${this.modelName} with ${this.primaryKey}: ${record[this.primaryKey]} does not exist`));
}

const targetIndex = models.indexOf(targetRecord);
const recordsUnknownAttribute = Object.keys(record)
.find((attribute) => !this.attributes.includes(attribute));

targetNamespace.MemServer.DB[this.modelName][targetIndex] = Object.assign(targetRecord, record);
if (recordsUnknownAttribute) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.update ${this.primaryKey}: ${record[this.primaryKey]} fails, ${this.modelName} model does not have ${recordsUnknownAttribute} attribute to update`));
}

return targetNamespace.MemServer.DB[this.modelName][targetIndex];
return Object.assign(targetRecord, record);
},
delete(record) {
const models = targetNamespace.MemServer.DB[this.modelName] || [];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"license": "ISC",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha --retries 3"
},
"repository": {
"type": "git",
Expand Down
11 changes: 6 additions & 5 deletions test/mem-server.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ 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`);
if (!fs.existsSync('./memserver/fixtures')) {
fs.mkdirSync('./memserver/fixtures');
}

fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photos.js`, `export default [
{
id: 1,
Expand Down Expand Up @@ -117,7 +120,6 @@ describe('MemServer fixture constraint feature', function() {
is_public: false
}
];`);

fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photo-comments.js`, `export default [
{
uuid: '499ec646-493f-4eea-b92e-e383d94182f4',
Expand Down Expand Up @@ -167,7 +169,7 @@ describe('MemServer fixture constraint feature', function() {

it('should throw error if any of the uuid fixtures have an incorrect type', function() {
this.timeout(5000);

fs.mkdirSync(`./memserver/fixtures`);
fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photos.js`, `export default [
{
Expand All @@ -189,7 +191,6 @@ describe('MemServer fixture constraint feature', function() {
is_public: false
}
];`);

fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photo-comments.js`, `export default [
{
uuid: '499ec646-493f-4eea-b92e-e383d94182f4',
Expand Down Expand Up @@ -268,7 +269,7 @@ describe('MemServer fixture constraint feature', function() {
});
});

it('should throw error if there are dusplicate uuid fixtures', function() {
it('should throw error if there are duplicate uuid fixtures', function() {
fs.mkdirSync(`./memserver/fixtures`);
fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photo-comments.js`, `export default [
{
Expand Down
75 changes: 68 additions & 7 deletions test/mem-server.model.insert.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// TODO: $Model.insert() with new attributes append the new attributes to $Model.attributes
const assert = require('assert');
const fs = require('fs');
const rimraf = require('rimraf');
const moment = require('moment');

const modelFileContent = `import Model from '${process.cwd()}/lib/mem-server/model';
export default Model({});`;

describe('MemServer.Model Insert Interface', function() {
before(function() {
before(function(done) {
fs.mkdirSync(`./memserver`);
fs.mkdirSync(`./memserver/models`);
fs.writeFileSync(`${process.cwd()}/memserver/models/user.js`, modelFileContent);
Expand Down Expand Up @@ -59,10 +59,12 @@ describe('MemServer.Model Insert Interface', function() {
user_id: 1
}
];`);
done();
});

beforeEach(function() {
Object.keys(require.cache).forEach((key) => delete require.cache[key]);

fs.writeFileSync(`${process.cwd()}/memserver/models/photo.js`, `
import Model from '${process.cwd()}/lib/mem-server/model';
Expand Down Expand Up @@ -110,7 +112,7 @@ describe('MemServer.Model Insert Interface', function() {
const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start()
MemServer.start();

assert.deepEqual(Photo.findAll().map((photo) => photo.id), [1, 2, 3]);

Expand Down Expand Up @@ -174,7 +176,7 @@ describe('MemServer.Model Insert Interface', function() {
const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start()
MemServer.start();

Photo.insert();
Photo.insert();
Expand Down Expand Up @@ -234,7 +236,7 @@ describe('MemServer.Model Insert Interface', function() {
const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start()
MemServer.start();

Photo.insert({ id: 99, href: '/izel.html', is_public: false });
Photo.insert({ name: 'Baby photo', href: '/baby.jpg' });
Expand Down Expand Up @@ -307,7 +309,7 @@ describe('MemServer.Model Insert Interface', function() {
const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start()
MemServer.start();

assert.throws(() => Photo.insert({ id: 1 }), (err) => {
return (err instanceof Error) &&
Expand All @@ -323,7 +325,7 @@ describe('MemServer.Model Insert Interface', function() {
const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start()
MemServer.start();

assert.throws(() => Photo.insert({ id: '99' }), (err) => {
return (err instanceof Error) &&
Expand All @@ -335,4 +337,63 @@ describe('MemServer.Model Insert Interface', function() {
});
});
});

it('can add new values to $Model.attributes when new attributes are discovered', function() {
this.timeout(5000);

const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start();

Photo.insert({ published_at: moment('2017-10-10').toJSON(), description: 'Some description' });
Photo.insert({ location: 'Istanbul', is_public: false });
PhotoComment.insert({ updated_at: moment('2017-01-10').toJSON(), like_count: 22 });
PhotoComment.insert({ reply_id: 1 });

assert.deepEqual(Photo.attributes, [
'is_public', 'name', 'id', 'href', 'published_at', 'description', 'location'
]);
assert.deepEqual(PhotoComment.attributes, [
'inserted_at', 'is_important', 'uuid', 'content', 'photo_id', 'user_id', 'updated_at',
'like_count', 'reply_id'
]);
assert.deepEqual(Photo.findAll(), [
{
id: 1,
name: 'Ski trip',
href: 'ski-trip.jpeg',
is_public: false
},
{
id: 2,
name: 'Family photo',
href: 'family-photo.jpeg',
is_public: true
},
{
id: 3,
name: 'Selfie',
href: 'selfie.jpeg',
is_public: false
},
{
id: 4,
href: undefined,
is_public: true,
published_at: '2017-10-09T22:00:00.000Z',
description: 'Some description',
name: 'Some default name'
},
{
id: 5,
href: undefined,
is_public: false,
location: 'Istanbul',
published_at: undefined,
name: 'Some default name',
description: undefined
}
]);
});
});
15 changes: 11 additions & 4 deletions test/mem-server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const rimraf = require('rimraf');

describe('MemServer.Model Interface', function() {
before(function() {
Object.keys(require.cache).forEach((key) => delete require.cache[key]);

fs.mkdirSync(`./memserver`);
fs.mkdirSync(`./memserver/models`);
fs.writeFileSync(`${process.cwd()}/memserver/models/photo.js`, `
Expand Down Expand Up @@ -65,7 +67,6 @@ describe('MemServer.Model Interface', function() {
is_public: false
}
];`);

fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photo-comments.js`, `export default [
{
uuid: '499ec646-493f-4eea-b92e-e383d94182f4',
Expand Down Expand Up @@ -94,12 +95,14 @@ describe('MemServer.Model Interface', function() {
];`);
});

beforeEach(function() {
beforeEach(function(done) {
Object.keys(require.cache).forEach((key) => delete require.cache[key]);
done();
});

afterEach(function() {
afterEach(function(done) {
Object.keys(require.cache).forEach((key) => delete require.cache[key]);
done();
});

after(function(done) {
Expand Down Expand Up @@ -141,6 +144,8 @@ describe('MemServer.Model Interface', function() {
});

it('reads the defaultAttributes correctly', function() {
Object.keys(require.cache).forEach((key) => delete require.cache[key]);

const MemServer = require('../index.js');
const { Photo, PhotoComment, User } = MemServer.Models;
const initialPhotoDefaultAttributes = Photo.defaultAttributes;
Expand Down Expand Up @@ -191,14 +196,16 @@ describe('MemServer.Model Interface', function() {
});

it('can have custom queries for a Model', function() {
Object.keys(require.cache).forEach((key) => delete require.cache[key]);

const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start();

const photo = Photo.find(1);

assert.deepEqual(PhotoComment.forPhoto(photo), PhotoComent.findAll({ photo_id: photo.id }));
assert.deepEqual(PhotoComment.forPhoto(photo), PhotoComment.findAll({ photo_id: photo.id }));
assert.deepEqual(Photo.publicPhotos(), Photo.findAll({ is_public: true }));
});
});
Expand Down
53 changes: 53 additions & 0 deletions test/mem-server.model.update.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,59 @@ describe('MemServer.Model Update Interface', function() {
});

describe('$Model.update() interface', function() {
it('can update models', function() {
this.timeout(5000);

const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start()

Photo.update({ id: 1, name: 'Ski trip', href: 'ski-trip.jpeg', is_public: false });
Photo.update({ id: 2, href: 'family-photo-2.jpeg', is_public: false });
PhotoComment.update({ uuid: '374c7f4a-85d6-429a-bf2a-0719525f5f29', content: 'Cool' });

assert.deepEqual(Photo.find(1), {
id: 1, name: 'Ski trip', href: 'ski-trip.jpeg', is_public: false
});
assert.deepEqual(Photo.find(2), {
id: 2, name: 'Family photo', href: 'family-photo-2.jpeg', is_public: false
});
assert.deepEqual(PhotoComment.findBy({ uuid: '374c7f4a-85d6-429a-bf2a-0719525f5f29' }), {
uuid: '374c7f4a-85d6-429a-bf2a-0719525f5f29', content: 'Cool', photo_id: 2, user_id: 1
});
});

it('throws error when updating a nonexistent model', function() {
const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start()

assert.throws(() => Photo.update({ id: 99, href: 'family-photo-2.jpeg' }), (err) => {
return (err instanceof Error) &&
/\[MemServer\] Photo\.update\(record\) failed because Photo with id: 99 does not exist/.test(err);
});
assert.throws(() => PhotoComment.update({ uuid: '374c7f4a-85d6-429a-bf2a-0719525f5666', content: 'Nice' }), (err) => {
return (err instanceof Error) &&
/\[MemServer\] PhotoComment\.update\(record\) failed because PhotoComment with uuid: 374c7f4a-85d6-429a-bf2a-0719525f5666 does not exist/.test(err);
});
});

it('throws error when a model get updated with an unknown $Model.attribute', function() {
const MemServer = require('../index.js');
const { Photo, PhotoComment } = MemServer.Models;

MemServer.start()

assert.throws(() => Photo.update({ id: 1, name: 'ME', is_verified: false }), (err) => {
return (err instanceof Error) &&
/\[MemServer\] Photo\.update id: 1 fails, Photo model does not have is_verified attribute to update/.test(err);
});
assert.throws(() => PhotoComment.update({ uuid: '374c7f4a-85d6-429a-bf2a-0719525f5f29', location: 'Amsterdam' }), (err) => {
return (err instanceof Error) &&
/\[MemServer\] PhotoComment\.update uuid: 374c7f4a-85d6-429a-bf2a-0719525f5f29 fails, PhotoComment model does not have location attribute to update/.test(err);
});
});
});
});
6 changes: 5 additions & 1 deletion test/mem-server.start.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe('MemServer start/stop functionality', function() {
});

it('can be started without fixtures', function() {
this.timeout(5000);

const MemServer = require('../index.js');

assert.deepEqual(MemServer.Pretender, {});
Expand All @@ -43,7 +45,7 @@ describe('MemServer start/stop functionality', function() {

it('can be started with fixtures', function() {
this.timeout(5000);

fs.mkdirSync(`./memserver/fixtures`);
fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photos.js`, `export default [
{
Expand Down Expand Up @@ -127,6 +129,8 @@ describe('MemServer start/stop functionality', function() {
});

it('can be shut down', () => {
this.timeout(5000);

const MemServer = require('../index.js');

assert.deepEqual(MemServer.Pretender, {});
Expand Down

0 comments on commit 7de36c0

Please sign in to comment.