Skip to content

Commit

Permalink
more api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 27, 2016
1 parent 687cce6 commit 777914b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/posts/category.js
Expand Up @@ -21,36 +21,36 @@ module.exports = function (Posts) {
};

Posts.getCidsByPids = function (pids, callback) {
Posts.getPostsFields(pids, ['tid'], function (err, posts) {
if (err) {
return callback(err);
}

var tids = posts.map(function (post) {
return post.tid;
}).filter(function (tid, index, array) {
return tid && array.indexOf(tid) === index;
});

topics.getTopicsFields(tids, ['cid'], function (err, topics) {
if (err) {
return callback(err);
}
var tids;
var postData;
async.waterfall([
function (next) {
Posts.getPostsFields(pids, ['tid'], next);
},
function (_postData, next) {
postData = _postData;
tids = postData.map(function (post) {
return post.tid;
}).filter(function (tid, index, array) {
return tid && array.indexOf(tid) === index;
});

topics.getTopicsFields(tids, ['cid'], next);
},
function (topicData, next) {
var map = {};
topics.forEach(function (topic, index) {
topicData.forEach(function (topic, index) {
if (topic) {
map[tids[index]] = topic.cid;
}
});

var cids = posts.map(function (post) {
var cids = postData.map(function (post) {
return map[post.tid];
});

callback(null, cids);
});
});
next(null, cids);
}
], callback);
};

Posts.filterPidsByCid = function (pids, cid, callback) {
Expand Down
29 changes: 29 additions & 0 deletions test/controllers.js
Expand Up @@ -16,6 +16,7 @@ describe('Controllers', function () {

var tid;
var cid;
var pid;
var fooUid;

before(function (done) {
Expand All @@ -38,6 +39,7 @@ describe('Controllers', function () {

topics.post({uid: results.user, title: 'test topic title', content: 'test topic content', cid: results.category.cid}, function (err, result) {
tid = result.topicData.tid;
pid = result.postData.pid;
done(err);
});
});
Expand Down Expand Up @@ -462,6 +464,33 @@ describe('Controllers', function () {
});
});

it('should get post data', function (done) {
request(nconf.get('url') + '/api/post/pid/' + pid, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
done();
});
});

it('should get topic data', function (done) {
request(nconf.get('url') + '/api/topic/tid/' + tid, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
done();
});
});

it('should get category data', function (done) {
request(nconf.get('url') + '/api/category/cid/' + cid, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
done();
});
});

after(function (done) {
db.emptydb(done);
});
Expand Down

0 comments on commit 777914b

Please sign in to comment.