Skip to content

Commit

Permalink
most important tests are finished
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Oct 29, 2017
1 parent ff10ccd commit 14408cb
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 120 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- create a cli
- test that passthrough works, timing options work, coalasceFindRequestWorks, do the one throw()
- derived schema attribute for $Model:
schema: {
inserted_at: {
Expand Down
10 changes: 8 additions & 2 deletions lib/mem-server/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,25 @@ export default function(options) {
},
embedReferences: {},
serializer(objectOrArray) {
if (Array.isArray(objectOrArray)) {
if (!objectOrArray) {
return;
} else if (Array.isArray(objectOrArray)) {
return objectOrArray.map((object) => this.serialize(object), []);
}

return this.serialize(objectOrArray);
},
serialize(object) { // NOTE: add links object ?
if (Array.isArray(object)) {
throw new Error(chalk.red(`[MemServer] ${this.modelName}.serialize(object) expects an object not an array. Use ${this.modelName}.serializer(data) for serializing array of records`));
}

const objectWithAllAttributes = this.attributes.reduce((result, attribute) => {
if (result[attribute] === undefined) {
result[attribute] = null;
}

return object;
return result;
}, object);

return Object.keys(this.embedReferences).reduce((result, embedKey) => {
Expand Down
16 changes: 15 additions & 1 deletion lib/mem-server/pretender-hacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ window.Pretender.prototype._handlerFor = function(verb, url, request) {
request.queryParams = Object.keys(matches.queryParams).reduce((result, key) => {
var value = matches.queryParams[key];

return Object.assign(result, { [key]: parseInt(value, 10) || value });
const targetValue = castCorrectType(value); // NOTE: maybe to qs.parse() here?

return Object.assign(result, { [key]: targetValue });
}, {});

if (request.requestBody && request.requestHeaders['Content-Type'] === 'application/json') {
Expand All @@ -29,6 +31,18 @@ window.Pretender.prototype._handlerFor = function(verb, url, request) {

return match;
};

function castCorrectType(value) {
if (parseInt(value, 10)) {
return parseInt(value, 10);
} else if (value === 'false') {
return false;
} else if (value === 'true') {
return true;
}

return value;
}
// END: Pretender Request Parameter Type Casting Hack

// HACK START: Pretender Response Defaults UX Hack: Because Pretender Response types suck UX-wise.
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": "mocha --retries 3"
"test": "sh run-tests.sh"
},
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mocha --retries 3
mocha ./test/server/mem-server.server.js
20 changes: 12 additions & 8 deletions test/mem-server.model.serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('MemServer.Model Serialize Interface', function() {
const photos = Photo.findAll({ is_public: false });
const photoComments = PhotoComment.findAll({ photo_id: 1 });

assert.deepEqual(Photo.serialize(photos), [
assert.deepEqual(Photo.serializer(photos), [
{
id: 1,
name: 'Ski trip',
Expand All @@ -122,7 +122,7 @@ describe('MemServer.Model Serialize Interface', function() {
is_public: false
}
]);
assert.deepEqual(PhotoComment.serialize(photoComments), [
assert.deepEqual(PhotoComment.serializer(photoComments), [
{
uuid: '499ec646-493f-4eea-b92e-e383d94182f4',
content: 'What a nice photo!',
Expand Down Expand Up @@ -155,12 +155,16 @@ describe('MemServer.Model Serialize Interface', function() {
const notFoundComment = PhotoComment.findBy({ uuid: '374c7f4a-85d6-429a-bf2a-0719525f5111' });
const notFoundComments = Photo.findAll({ content: 'Aint easy' });

assert.equal(Photo.serialize(notFoundPhoto), undefined);
assert.deepEqual(Photo.serialize({}), {});
assert.deepEqual(Photo.serialize(notFoundPhotos), []);
assert.equal(PhotoComment.serialize(notFoundComment), undefined);
assert.deepEqual(PhotoComment.serialize({}), {});
assert.deepEqual(PhotoComment.serialize(notFoundComments), []);
assert.equal(Photo.serializer(notFoundPhoto), undefined);
assert.deepEqual(Photo.serializer({}), {
id: null, href: null, is_public: null, name: null
});
assert.deepEqual(Photo.serializer(notFoundPhotos), []);
assert.equal(PhotoComment.serializer(notFoundComment), undefined);
assert.deepEqual(PhotoComment.serializer({}), {
uuid: null, content: null, photo_id: null, user_id: null
});
assert.deepEqual(PhotoComment.serializer(notFoundComments), []);
});

it('can serialize embeded records recursively', function() {
Expand Down
Loading

0 comments on commit 14408cb

Please sign in to comment.