Skip to content

Commit

Permalink
ongoing internal relationship query work
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Oct 27, 2017
1 parent ccc4994 commit fa4ac1e
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/mem-server/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ export default function(options) {
},
embedReferences: {},
serializer(objectOrArray) {
if (Array.is(objectOrArray)) {
if (Array.isArray(objectOrArray)) {
return objectOrArray.reduce((object) => this.serialize(object), []);
}

return this.serialize(objectOrArray);
},
serialize(object) { // NOTE: add links object ?
return this.embedReferences.reduce((result, embedObject) => {
return Object.keys(this.embedReferences).reduce((result, embedObject) => {
const embedKey = Object.keys(embedObject)[0];
const embedModel = embedObject[embedKey];
const embeddedRecords = this.getRelationship(object, embedKey, embedModel);
Expand Down
2 changes: 1 addition & 1 deletion test/mem-server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('MemServer.Model Interface', function() {
if (fs.existsSync(`${process.cwd()}/memserver`)) {
rimraf.sync(`${process.cwd()}/memserver`);
}

done();
});

Expand Down
180 changes: 180 additions & 0 deletions test/mem-server.model.relationships.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
const assert = require('assert');
const fs = require('fs');
const rimraf = require('rimraf');

describe('MemServer.Model Relationships 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`, `
import Model from '${process.cwd()}/lib/mem-server/model';
export default Model({
});
`);
fs.writeFileSync(`${process.cwd()}/memserver/models/photo-comment.js`, `
import Model from '${process.cwd()}/lib/mem-server/model';
export default Model({
});
`);
fs.writeFileSync(`${process.cwd()}/memserver/models/user.js`, `
import Model from '${process.cwd()}/lib/mem-server/model';
export default Model({});
`);
fs.writeFileSync(`${process.cwd()}/memserver/models/activity.js`, `
import Model from '${process.cwd()}/lib/mem-server/model';
export default Model({
});
`);
fs.writeFileSync(`${process.cwd()}/memserver/server.js`, 'export default function(Models) {}');
fs.mkdirSync(`./memserver/fixtures`);
fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photos.js`, `export default [
{
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
}
];`);
fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photo-comments.js`, `export default [
{
uuid: '499ec646-493f-4eea-b92e-e383d94182f4',
content: 'What a nice photo!',
photo_id: 1,
user_id: 1
},
{
uuid: '77653ad3-47e4-4ec2-b49f-57ea36a627e7',
content: 'I agree',
photo_id: 1,
user_id: 2
},
{
uuid: 'd351963d-e725-4092-a37c-1ca1823b57d3',
content: 'I was kidding',
photo_id: 1,
user_id: 1
},
{
uuid: '374c7f4a-85d6-429a-bf2a-0719525f5f29',
content: 'Interesting indeed',
photo_id: 2,
user_id: 1
}
];`);
fs.writeFileSync(`${process.cwd()}/memserver/fixtures/activities.js`, `export default [
{
id: 1,
user_id: 1,
photo_id: 1
},
{
id: 2,
user_id: 1,
photo_id: null
}
];`);
});

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

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

after(function(done) {
if (fs.existsSync(`${process.cwd()}/memserver`)) {
rimraf.sync(`${process.cwd()}/memserver`);
}

done();
});

// it('can register relationship embeds before runtime', function() {
// const MemServer = require('../index.js');
// const { Photo, PhotoComment } = MemServer.Models;
//
// MemServer.start();
//
//
// });

// it('can register relationships embeds during runtime', function() {
//
// });

it('works for hasOne/belongsTo relationships both sides', function() {
const MemServer = require('../index.js');
const { Photo, Activity } = MemServer.Models;

MemServer.start();

const activity = Photo.getRelationship(Photo.find(1), 'activity');

assert.deepEqual(activity, [{ id: 1, user_id: 1, photo_id: 1 }]);
assert.deepEqual(Photo.getRelationship(Photo.find(2), 'activity'), null);
assert.deepEqual(Activity.getRelationship(activity, 'photo'), [
{ id: 1, name: 'Ski trip', href: 'ski-trip.jpeg', is_public: false }
]);
assert.deepEqual(Activity.getRelationship(Activity.find(2), 'photo'), null);
});

// it('works for hasMany/belongsTo relationships both side', function() {
// const MemServer = require('../index.js');
// const { Photo, PhotoComment } = MemServer.Models;
//
// MemServer.start();
//
// const firstPhotoComments = Photo.getRelationship(Photo.find(1), 'comments');
// const secondPhotoComments = Photo.getRelationship(Photo.find(2), 'comments');
// const thirdPhotoComments = Photo.getRelationship(Photo.find(3), 'comments');
//
// assert.deepEqual(firstPhotoComments, [
//
// ]);
// assert.deepEqual(secondPhotoComments, [
//
// ]);
// assert.deepEqual(thirdPhotoComments, [
//
// ]);
// assert.deepEqual(PhotoComment.getRelationship(firstPhotoComments, 'photo'), [
//
// ]);
// assert.deepEqual(PhotoComment.getRelationship(secondPhotoComments, 'photo'), [
//
// ]);
// assert.deepEqual(PhotoComment.getRelationship(thirdPhotoComments, 'photo'), [
//
// ]);
// });
//
// it('works for custom named hasOne/belongsTo and hasMany/belongsTo relationships both side', function() {
//
// });
//
// it('throws an error when relationship reference is invalid', function() {
// // NOTE: setup userActivity <-> Photo relationship
// });
});

0 comments on commit fa4ac1e

Please sign in to comment.