RSS-26: Adding unit test for filtering all articles

This commit is contained in:
Kip Gebhardt 2015-06-11 14:28:57 -07:00
parent 66e6cb2d64
commit c03532da24
9 changed files with 76 additions and 10 deletions

View File

@ -75,7 +75,7 @@ RssBraider.prototype.processFeed = function(feed_name, format, callback)
file_path = source.file_path || null,
source_articles = [];
logger.debug("Requesting source:" + source.name + " at " + url + " for feed:" + feed_name);
// logger.debug("Requesting source:" + source.name + " at " + url + " for feed:" + feed_name);
// todo: Check if source.file is set and set up a fs stream read
var feedparser = new FeedParser();
if (url) {
@ -218,7 +218,7 @@ RssBraider.prototype.runPlugins = function (item, itemOptions, source, feed_name
for (var i = 0; i < plugins_list.length; i++) {
var plugin_name = plugins_list[i];
if (self.plugins[plugin_name]) {
logger.info("DEBUG runPlugins running " + plugin_name + " for item " + item.guid + " in feed: " + feed.meta.title);
// logger.info("DEBUG runPlugins running " + plugin_name + " for item " + item.guid + " in feed: " + feed.meta.title);
itemOptions = self.plugins[plugin_name](item, itemOptions, source);
} else {
logger.error("A plugin named '" + plugin_name + "' hasn't been registered");
@ -227,10 +227,10 @@ RssBraider.prototype.runPlugins = function (item, itemOptions, source, feed_name
// If itemOptions comes back null, skip this item
// as one of the plugins decided to toss it
if (itemOptions === null) {
logger.info("Plugin rejected item", plugin_name, item.guid, feed.meta.title);
// logger.info("Plugin rejected item", plugin_name, item.guid, feed.meta.title);
break;
} else {
logger.info("Plugin completed", plugin_name, item.guid, feed.meta.title);
// logger.info("Plugin completed", plugin_name, item.guid, feed.meta.title);
}
}
return itemOptions;

View File

@ -0,0 +1,9 @@
module.exports = function (item, itemOptions, source) {
if (!item || !itemOptions) {
return;
}
// This plugin removes all items by returning null instead of the processed
// itemOptions
return null;
};

View File

@ -0,0 +1,10 @@
module.exports = function (item, itemOptions, source) {
if (!item || !itemOptions) {
return;
}
// This plugin does no processing
// It's just a template
return itemOptions;
};

View File

@ -83,5 +83,4 @@ module.exports = function (item, itemOptions, source) {
});
}
return itemOptions;
};

View File

@ -41,7 +41,7 @@
"rss": "git://github.com/rv-kip/node-rss.git#8d1420"
},
"devDependencies": {
"tape": "^4.0.0",
"mockdate": "^1.0.3"
"mockdate": "^1.0.3",
"tape": "^4.0.0"
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title><![CDATA[A Feed with no elements]]></title>
<description><![CDATA[This feed will have no elements as a result of the filter_all_articles plugin. Used for unit tests.]]></description>
<link>http://github.com/dylang/node-rss</link>
<generator>rss-braider</generator>
<lastBuildDate>Wed, 31 Dec 2014 00:00:01 GMT</lastBuildDate>
</channel>
</rss>

18
test/feeds/no_elements.js Normal file
View File

@ -0,0 +1,18 @@
var feed = {
"feed_name" : "no_elements",
"plugins" : ['filter_all_articles'], // No articles make it through
"meta" : {
"title" : "A Feed with no elements",
"description" : "This feed will have no elements as a result of the filter_all_articles plugin. Used for unit tests.",
"url" : "http://rss.nytimes.com/services/xml/rss/nyt/Technology.xml",
},
"sources" : [
{
"name" : "nyt_tech",
"feed_url" : "http://rss.nytimes.com/services/xml/rss/nyt/Technology.xml",
"count" : 5,
"fullname" : "NYT Technology"
}
]
};
exports.feed = feed;

View File

@ -16,11 +16,10 @@ var feed = {
},
"sources" : [
{
"name" : "sample_feed",
"name" : "sample_source",
"count" : 1,
"file_path" : __dirname + "/../input_files/sample_feed.xml",
},
}
]
};
exports.feed = feed;

View File

@ -111,4 +111,25 @@ test('sort by date asc', function(t) {
});
});
test('filter all articles out using plugin', function(t) {
t.plan(1);
var feeds = {};
feeds.sample_feed = require("./feeds/no_elements").feed;
var braider_options = {
feeds : feeds,
indent : " ",
date_sort_order : "asc",
plugins_directories : [__dirname + '/../lib/example_plugins/']
};
var rss_braider = RssBraider.createClient(braider_options);
rss_braider.processFeed('sample_feed', 'rss', function(err, data){
if (err) {
return t.fail(err);
}
console.log(data);
t.equal(data, expectedOutput.emptyFeed);
});
});