Skip to content

Instantly share code, notes, and snippets.

@ameensol
Created March 13, 2015 19:59
Show Gist options
  • Save ameensol/54f2c50928b930a9e249 to your computer and use it in GitHub Desktop.
Save ameensol/54f2c50928b930a9e249 to your computer and use it in GitHub Desktop.
var chai = require('chai');
var assert = chai.assert;
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var Fixture = require('sequelize-fixtures')
var isStream = require('isstream')
var streamify = require('stream-array')
chai.use(sinonChai);
chai.should();
var TopTweeters = require('../../streams/topTweeters')
var topTweeters = new TopTweeters({size: 3})
module.exports = function (sequelize, models) {
var Topic = models.Topic
var List = models.List
var Tweeter = models.Tweeter
describe.only('topTweeters', function () {
// 1 Topic, 3 Lists, and 6 Members, with 1 member in all 3 Lists, and
// 2 members in only 2 lists -- should result in 3 top members
var topic1 = {id: 1}
var list1 = {id: 22, TopicLists: [1]}
var list2 = {id: 33, TopicLists: [1]}
var list3 = {id: 44, TopicLists: [1]}
// 1 in all 3 lists
var tweeter1 = {id: 444, AuthorityPosition: [22, 33, 44]}
// 2 in only 2 lists
var tweeter2 = {id: 555, AuthorityPosition: [22, 44]}
var tweeter3 = {id: 666, AuthorityPosition: [33, 44]}
// 3 in only 1 lists
var tweeter4 = {id: 777, AuthorityPosition: [22]}
var tweeter5 = {id: 888, AuthorityPosition: [33]}
var tweeter6 = {id: 999, AuthorityPosition: [44]}
var fixtures = [
{model: 'Topic', data: topic1},
{model: 'List', data: list1},
{model: 'List', data: list2},
{model: 'List', data: list3},
{model: 'Tweeter', data: tweeter1},
{model: 'Tweeter', data: tweeter2},
{model: 'Tweeter', data: tweeter3},
{model: 'Tweeter', data: tweeter4},
{model: 'Tweeter', data: tweeter5},
{model: 'Tweeter', data: tweeter6},
]
var mockTopics
beforeEach(function (done) {
sequelize.sync({force: true}).then(function () {
Fixture.loadFixtures(fixtures, models).then(function () {
Topic.findAll({include: [
{model: List, include: [
{model: Tweeter, as: 'Authorities'}
]}
]}).then(function (savedTopics) {
mockTopics = savedTopics
done()
})
})
})
})
it('should be a stream', function () {
assert(isStream(topTweeters))
})
it('should associate a topic with its top tweeters in a streaming way',
function (done) {
var mockTopic = mockTopics[0]
var getIds = function (arr) {
return arr.map(function (e) {
return parseInt(e.id)
})
}
streamify(mockTopics).pipe(topTweeters)
topTweeters.once('readable', function () {
var topic = topTweeters.read()
// The same topic that went in should be piped out
assert.equal(mockTopic.id, topic.id)
topic.getTopTweeters().then(function (tweeters) {
// Should only have 3 topTweeters
assert.equal(tweeters.length, 3)
// top tweeter ids should be correct
assert.sameMembers(getIds(tweeters), [444, 555, 666])
done()
})
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment