Skip to content

Commit

Permalink
Simplifying test streams and fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr committed Feb 22, 2014
1 parent 440af09 commit cf815f4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 47 deletions.
2 changes: 2 additions & 0 deletions fixture/tweet.json
@@ -1 +1,3 @@
{"created_at":"Tue Feb 18 19:13:11 +0000 2014","id":435854527493718017,"id_str":"435854527493718017","text":"@oxystagram THIS JS WHAT I DO ALL DAY OKAY","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":435854326335303680,"in_reply_to_status_id_str":"435854326335303680","in_reply_to_user_id":741288679,"in_reply_to_user_id_str":"741288679","in_reply_to_screen_name":"oxystagram","user":{"id":1338663542,"id_str":"1338663542","name":"tony stark","screen_name":"stevetonys","location":"","url":"http:\/\/eenochian.tumblr.com","description":"'what does steve smell like' 'freedom' 'TRUTH HONOUR COURAGE' 'i wanna smell him' 'smell the american flag it's the same thing'[eagle screeches in the distance]","protected":false,"followers_count":677,"friends_count":129,"listed_count":17,"created_at":"Tue Apr 09 08:35:17 +0000 2013","favourites_count":2416,"utc_offset":19800,"time_zone":"Chennai","geo_enabled":false,"verified":false,"statuses_count":14770,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/435788292425269248\/DXpc9zxI_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/435788292425269248\/DXpc9zxI_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1338663542\/1392734537","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"oxystagram","name":"ap\u262ello","id":741288679,"id_str":"741288679","indices":[0,11]}]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"en"}
{"created_at":"Tue Feb 18 19:13:11 +0000 2014","id":435854527493718017,"id_str":"435854527493718017","text":"@oxystagram THIS JS WHAT I DO ALL DAY OKAY","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":435854326335303680,"in_reply_to_status_id_str":"435854326335303680","in_reply_to_user_id":741288679,"in_reply_to_user_id_str":"741288679","in_reply_to_screen_name":"oxystagram","user":{"id":1338663542,"id_str":"1338663542","name":"tony stark","screen_name":"stevetonys","location":"","url":"http:\/\/eenochian.tumblr.com","description":"'what does steve smell like' 'freedom' 'TRUTH HONOUR COURAGE' 'i wanna smell him' 'smell the american flag it's the same thing'[eagle screeches in the distance]","protected":false,"followers_count":677,"friends_count":129,"listed_count":17,"created_at":"Tue Apr 09 08:35:17 +0000 2013","favourites_count":2416,"utc_offset":19800,"time_zone":"Chennai","geo_enabled":false,"verified":false,"statuses_count":14770,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/435788292425269248\/DXpc9zxI_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/435788292425269248\/DXpc9zxI_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1338663542\/1392734537","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"oxystagram","name":"ap\u262ello","id":741288679,"id_str":"741288679","indices":[0,11]}]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"en"}
{"created_at":"Tue Feb 18 19:13:11 +0000 2014","id":435854527493718017,"id_str":"435854527493718017","text":"@oxystagram THIS JS WHAT I DO ALL DAY OKAY","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":435854326335303680,"in_reply_to_status_id_str":"435854326335303680","in_reply_to_user_id":741288679,"in_reply_to_user_id_str":"741288679","in_reply_to_screen_name":"oxystagram","user":{"id":1338663542,"id_str":"1338663542","name":"tony stark","screen_name":"stevetonys","location":"","url":"http:\/\/eenochian.tumblr.com","description":"'what does steve smell like' 'freedom' 'TRUTH HONOUR COURAGE' 'i wanna smell him' 'smell the american flag it's the same thing'[eagle screeches in the distance]","protected":false,"followers_count":677,"friends_count":129,"listed_count":17,"created_at":"Tue Apr 09 08:35:17 +0000 2013","favourites_count":2416,"utc_offset":19800,"time_zone":"Chennai","geo_enabled":false,"verified":false,"statuses_count":14770,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/435788292425269248\/DXpc9zxI_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/435788292425269248\/DXpc9zxI_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1338663542\/1392734537","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"oxystagram","name":"ap\u262ello","id":741288679,"id_str":"741288679","indices":[0,11]}]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"en"}
50 changes: 3 additions & 47 deletions test.js
Expand Up @@ -179,7 +179,7 @@
});

stream.on('end', function () {
data.length.should.equal(10);
data.length.should.equal(3);
stream.destroy();
done();
});
Expand All @@ -201,7 +201,7 @@
});

stream.on('end', function () {
data.length.should.equal(10);
data.length.should.equal(3);
stream.destroy();
done();
});
Expand All @@ -214,50 +214,6 @@

});


function TestReader() {
Readable.apply(this);
var data = JSON.stringify(require(__dirname + '/fixture/tweet.json')) + "\n";

this._buffer = data;
this._pos = 0;
this._bufs = 10;
}

util.inherits(TestReader, Readable);

TestReader.prototype._read = function(n) {
var max = this._buffer.length - this._pos;
n = Math.max(n, 0);
var toRead = Math.min(n, max);
if (toRead === 0) {
// simulate the read buffer filling up with some more bytes some time
// in the future.
this._pos = 0;
this._bufs -= 1;
if (this._bufs <= 0) {
// read them all!
if (!this.ended)
this.push(null);
} else {
// now we have more.
// kinda cheating by calling _read, but whatever,
// it's just fake anyway.
this._read(n);
}
return;
}

var ret = this._buffer.slice(this._pos, this._pos + toRead);
this._pos += toRead;
if (this._bufs <= 1) {
// no newline on last.
ret = ret.substring(0, ret.length - 1);
}
this.push(ret);
};


function getMock (options, responseCreator) {
var stream = getStreamMock();
var emitter = new EventEmitter();
Expand Down Expand Up @@ -286,7 +242,7 @@
}

function getStreamMock (statusCode) {
var stream = new TestReader();
var stream = fs.createReadStream(__dirname + '/fixture/tweet.json');
stream.statusCode = statusCode || 200;
return stream;
}
Expand Down

0 comments on commit cf815f4

Please sign in to comment.