grapher/lib/links/tests/main.js

284 lines
9 KiB
JavaScript
Raw Normal View History

2016-09-14 16:04:08 +03:00
//import {
// PostCollection,
// CategoryCollection,
// CommentCollection,
// ResolverCollection
//} from './collections.js';
let PostCollection = new Mongo.Collection('test_post');
let CategoryCollection = new Mongo.Collection('test_category');
let CommentCollection = new Mongo.Collection('test_comment');
let ResolverCollection = new Mongo.Collection('test_resolver');
PostCollection.attachSchema(new SimpleSchema({
text: {type: String}
}));
CommentCollection.attachSchema(new SimpleSchema({
text: {type: String}
}));
CategoryCollection.attachSchema(new SimpleSchema({
text: {type: String}
}));
ResolverCollection.attachSchema(new SimpleSchema({
resourceId: {type: String}
}));
PostCollection.addLinks({
'comments': {
type: '*',
collection: CommentCollection,
2016-09-16 19:22:12 +03:00
field: 'commentIds',
index: true
},
'autoRemoveComments': {
type: '*',
collection: CommentCollection,
field: 'autoRemoveIds',
autoremove: true
2016-09-14 16:04:08 +03:00
},
'metaComments': {
type: '*',
collection: CommentCollection,
metadata: {
approved: {type: Boolean, optional: true},
date: {type: Date, optional: true},
updated: {type: Date, optional: true}
}
},
category: {
collection: CategoryCollection,
type: '1'
},
metaCategory: {
metadata: {},
collection: CategoryCollection,
type: '1'
},
pictures: {
resolve(object) {
return ResolverCollection.find({
resourceId: object._id
}).fetch();
}
}
});
CommentCollection.addLinks({
post: {
collection: PostCollection,
inversedBy: 'comments'
},
metaPost: {
collection: PostCollection,
inversedBy: 'metaComments'
}
});
CategoryCollection.addLinks({
'posts': {
collection: PostCollection,
inversedBy: 'category'
},
'metaPosts': {
collection: PostCollection,
inversedBy: 'metaCategory'
2016-09-14 16:04:08 +03:00
}
});
PostCollection.remove({});
CategoryCollection.remove({});
CommentCollection.remove({});
ResolverCollection.remove({});
describe('Collection Links', function () {
it('Test Many', function () {
let postId = PostCollection.insert({'text': 'abc'});
let commentId = CommentCollection.insert({'text': 'abc'});
let post = PostCollection.findOne(postId);
const link = PostCollection.getLink(post, 'comments');
link.add(commentId);
assert.lengthOf(link.find().fetch(), 1);
link.remove(commentId);
assert.lengthOf(link.find().fetch(), 0);
});
it('Tests One', function () {
let postId = PostCollection.insert({'text': 'abc'});
let categoryId = CategoryCollection.insert({'text': 'abc'});
let post = PostCollection.findOne(postId);
const link = PostCollection.getLink(post, 'category');
link.set(categoryId);
assert.lengthOf(link.find().fetch(), 1);
assert.equal(categoryId, link.fetch()._id);
2016-09-14 16:04:08 +03:00
link.unset();
assert.lengthOf(link.find().fetch(), 0);
});
it('Tests One Meta', function () {
let postId = PostCollection.insert({'text': 'abc'});
let categoryId = CategoryCollection.insert({'text': 'abc'});
let post = PostCollection.findOne(postId);
let link = PostCollection.getLink(post, 'metaCategory');
link.set(categoryId, {date: new Date()});
assert.lengthOf(link.find().fetch(), 1);
let metadata = link.metadata();
assert.isObject(metadata);
assert.instanceOf(metadata.date, Date);
link.metadata({
updated: new Date()
});
post = PostCollection.findOne(postId);
link = PostCollection.getLink(post, 'metaCategory');
assert.instanceOf(link.metadata().updated, Date);
link.unset();
assert.lengthOf(link.find().fetch(), 0);
});
it('Tests Many Meta', function () {
let postId = PostCollection.insert({'text': 'abc'});
let commentId = CommentCollection.insert({'text': 'abc'});
let post = PostCollection.findOne(postId);
let metaCommentsLink = PostCollection.getLink(post, 'metaComments');
metaCommentsLink.add(commentId, {date: new Date});
assert.lengthOf(metaCommentsLink.find().fetch(), 1);
// verifying reverse search
let metaComment = CommentCollection.findOne(commentId);
let metaPostLink = CommentCollection.getLink(metaComment, 'metaPost');
assert.lengthOf(metaPostLink.find().fetch(), 1);
let metadata = metaCommentsLink.metadata(commentId);
assert.isObject(metadata);
assert.instanceOf(metadata.date, Date);
metaCommentsLink.metadata(commentId, {updated: new Date});
post = PostCollection.findOne(postId);
metaCommentsLink = PostCollection.getLink(post, 'metaComments');
metadata = metaCommentsLink.metadata(commentId);
assert.instanceOf(metadata.updated, Date);
metaCommentsLink.remove(commentId);
assert.lengthOf(metaCommentsLink.find().fetch(), 0);
});
it('Tests inversedBy findings', function () {
let postId = PostCollection.insert({'text': 'abc'});
let commentId = CommentCollection.insert({'text': 'abc'});
let post = PostCollection.findOne(postId);
let comment = CommentCollection.findOne(commentId);
let commentsLink = PostCollection.getLink(post, 'comments');
2016-09-16 19:22:12 +03:00
let metaCommentsLink = PostCollection.getLink(post, 'metaComments');
2016-09-14 16:04:08 +03:00
let postLink = CommentCollection.getLink(comment, 'post');
2016-09-16 19:22:12 +03:00
let postMetaLink = CommentCollection.getLink(comment, 'metaPost');
2016-09-14 16:04:08 +03:00
commentsLink.add(comment);
2016-09-16 19:22:12 +03:00
metaCommentsLink.add(comment);
2016-09-14 16:04:08 +03:00
assert.lengthOf(postLink.find().fetch(), 1);
2016-09-16 19:22:12 +03:00
assert.lengthOf(postMetaLink.find().fetch(), 1);
2016-09-14 16:04:08 +03:00
CommentCollection.remove(comment._id);
post = PostCollection.findOne(postId);
assert.notInclude(post.commentIds, comment._id);
});
it('Tests proper resolver', function () {
let postId = PostCollection.insert({'text': 'abc'});
let uploadId = ResolverCollection.insert({'resourceId': postId});
let post = PostCollection.findOne(postId);
const link = PostCollection.getLink(post, 'pictures');
assert.lengthOf(link.fetch(), 1);
});
2016-09-16 19:22:12 +03:00
it ('Should auto-save object', function () {
let comment = {text: 'abc'};
2016-09-14 16:04:08 +03:00
2016-09-16 19:22:12 +03:00
let postId = PostCollection.insert({text: 'hello'});
const postLink = PostCollection.getLink(postId, 'comments').add(comment);
assert.isDefined(comment._id);
assert.lengthOf(postLink.fetch(), 1);
2016-09-14 16:04:08 +03:00
});
2016-09-16 19:22:12 +03:00
it ('Should have indexes set up', function () {
const raw = PostCollection.rawCollection();
const indexes = Meteor.wrapAsync(raw.indexes, raw)();
const found = _.find(indexes, index => {
return index.key.commentIds == 1;
});
assert.isObject(found);
});
it ('Should auto-remove some objects', function () {
let comment = {text: 'abc'};
let postId = PostCollection.insert({text: 'hello'});
let postLink = PostCollection.getLink(postId, 'comments').add(comment);
assert.isNotNull(comment._id);
PostCollection.remove(postId);
assert.isNotNull(CommentCollection.findOne(comment._id));
comment = {text: 'abc'};
postId = PostCollection.insert({text: 'hello'});
postLink = PostCollection.getLink(postId, 'autoRemoveComments').add(comment);
assert.isDefined(comment._id);
PostCollection.remove(postId);
assert.isUndefined(CommentCollection.findOne(comment._id));
});
it ('Should allow actions from inversed links', function () {
let comment = {text: 'abc'};
let postId = PostCollection.insert({text: 'hello'});
const commentId = CommentCollection.insert(comment);
CommentCollection.getLink(commentId, 'post').set(postId);
assert.lengthOf(PostCollection.getLink(postId, 'comments').fetch(), 1);
CommentCollection.getLink(commentId, 'post').add({text: 'hi there'});
let insertedPostViaVirtual = PostCollection.findOne({text: 'hi there'});
assert.isObject(insertedPostViaVirtual);
assert.lengthOf(PostCollection.getLink(insertedPostViaVirtual, 'comments').fetch(), 1);
const category = CategoryCollection.findOne();
let postsCategoryLink = CategoryCollection.getLink(category, 'posts');
postsCategoryLink.add(insertedPostViaVirtual);
assert.equal(category._id, PostCollection.getLink(insertedPostViaVirtual, 'category').fetch()._id);
// TESTING META
let categoryMetaPostLink = CategoryCollection.getLink(category, 'metaPosts');
categoryMetaPostLink.add(insertedPostViaVirtual, {testValue: 'boom!'});
let postMetaCategoryLink = PostCollection.getLink(insertedPostViaVirtual, 'metaCategory');
assert.equal('boom!', postMetaCategoryLink.metadata().testValue);
2016-09-16 19:22:12 +03:00
})
2016-09-14 16:04:08 +03:00
});