mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-06 22:15:48 +01:00
add search test
This commit is contained in:
@@ -16,15 +16,15 @@ var search = module.exports;
|
||||
|
||||
search.search = function (data, callback) {
|
||||
var start = process.hrtime();
|
||||
var searchIn = data.searchIn || 'titlesposts';
|
||||
data.searchIn = data.searchIn || 'titlesposts';
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (searchIn === 'posts' || searchIn === 'titles' || searchIn === 'titlesposts') {
|
||||
if (data.searchIn === 'posts' || data.searchIn === 'titles' || data.searchIn === 'titlesposts') {
|
||||
searchInContent(data, next);
|
||||
} else if (searchIn === 'users') {
|
||||
} else if (data.searchIn === 'users') {
|
||||
user.search(data, next);
|
||||
} else if (searchIn === 'tags') {
|
||||
} else if (data.searchIn === 'tags') {
|
||||
topics.searchAndLoadTags(data, next);
|
||||
} else {
|
||||
next(new Error('[[error:unknown-search-filter]]'));
|
||||
@@ -80,7 +80,7 @@ function searchInContent(data, callback) {
|
||||
},
|
||||
function (results, next) {
|
||||
pids = results.pids;
|
||||
if (!results || (!results.pids.length && !results.tids.length)) {
|
||||
if (!results.pids.length && !results.tids.length) {
|
||||
return callback(null, { posts: [], matchCount: matchCount, pageCount: 1 });
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ describe('Search', function () {
|
||||
var post3Data;
|
||||
var cid1;
|
||||
var cid2;
|
||||
var cid3;
|
||||
|
||||
before(function (done) {
|
||||
async.waterfall([
|
||||
@@ -57,6 +58,14 @@ describe('Search', function () {
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
categories.create({
|
||||
name: 'Child Test Category',
|
||||
description: 'Test category created by testing script',
|
||||
parentCid: cid2,
|
||||
}, next);
|
||||
},
|
||||
function (category, next) {
|
||||
cid3 = category.cid;
|
||||
topics.post({
|
||||
uid: phoebeUid,
|
||||
cid: cid1,
|
||||
@@ -178,4 +187,45 @@ describe('Search', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not find anything', function (done) {
|
||||
search.search({
|
||||
query: 'xxxxxxxxxxxxxx',
|
||||
}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
console.log(data);
|
||||
assert(Array.isArray(data.posts));
|
||||
assert(!data.matchCount);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should search child categories', function (done) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
topics.post({
|
||||
uid: gingerUid,
|
||||
cid: cid3,
|
||||
title: 'child category topic',
|
||||
content: 'avocado cucumber carrot armadillo',
|
||||
}, next);
|
||||
},
|
||||
function (result, next) {
|
||||
search.search({
|
||||
query: 'avocado',
|
||||
searchIn: 'titlesposts',
|
||||
categories: [cid2],
|
||||
searchChildren: true,
|
||||
sortBy: 'topic.timestamp',
|
||||
sortDirection: 'desc',
|
||||
}, next);
|
||||
},
|
||||
function (result, next) {
|
||||
assert(result.posts.length, 2);
|
||||
assert(result.posts[0].topic.title === 'child category topic');
|
||||
assert(result.posts[1].topic.title === 'java mongodb redis');
|
||||
next();
|
||||
},
|
||||
], done);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user