Skip to content

Commit

Permalink
Add test case for profile parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Aug 16, 2013
1 parent 2dad0d1 commit d9112c4
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ exports.parse = function(json) {
}

var profile = {};
profile.id = json.id;
profile.id = String(json.id);
if (json.id_str) { profile.id = json.id_str; }
profile.username = json.screen_name;
profile.displayName = json.name;
profile.photos = [{ value: json.profile_image_url_https }];
Expand Down
129 changes: 129 additions & 0 deletions test/data/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"profile_sidebar_fill_color": "F8FCF2",
"profile_sidebar_border_color": "547980",
"profile_background_tile": true,
"name": "Ryan Sarver",
"profile_image_url": "http://a0.twimg.com/profile_images/1777569006/image1327396628_normal.png",
"created_at": "Mon Feb 26 18:05:55 +0000 2007",
"location": "San Francisco, CA",
"follow_request_sent": false,
"profile_link_color": "547980",
"is_translator": false,
"id_str": "795649",
"default_profile": false,
"contributors_enabled": true,
"favourites_count": 3162,
"url": null,
"profile_image_url_https": "https://si0.twimg.com/profile_images/1777569006/image1327396628_normal.png",
"utc_offset": -28800,
"id": 795649,
"profile_use_background_image": true,
"listed_count": 1586,
"profile_text_color": "594F4F",
"lang": "en",
"followers_count": 276334,
"protected": false,
"notifications": true,
"profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/113854313/xa60e82408188860c483d73444d53e21.png",
"profile_background_color": "45ADA8",
"verified": false,
"geo_enabled": true,
"time_zone": "Pacific Time (US & Canada)",
"description": "Director, Platform at Twitter. Detroit and Boston export. Foodie and over-the-hill hockey player. @devon's lesser half",
"default_profile_image": false,
"profile_background_image_url": "http://a0.twimg.com/profile_background_images/113854313/xa60e82408188860c483d73444d53e21.png",
"status": {
"coordinates": null,
"favorited": false,
"truncated": false,
"created_at": "Sat Aug 25 19:33:11 +0000 2012",
"retweeted_status": {
"coordinates": null,
"favorited": false,
"truncated": false,
"created_at": "Sat Aug 25 19:20:36 +0000 2012",
"id_str": "239442171466493953",
"entities": {
"urls": [
{
"expanded_url": "http://nbcnews.to/NtkRTJ",
"url": "http://t.co/f8ivBrVd",
"indices": [
102,
122
],
"display_url": "nbcnews.to/NtkRTJ"
}
],
"hashtags": [

],
"user_mentions": [

]
},
"in_reply_to_user_id_str": null,
"contributors": null,
"text": "Neil Armstrong has died at the age of 82 from complications from heart operations he had 3 weeks ago. http://t.co/f8ivBrVd",
"retweet_count": 112,
"in_reply_to_status_id_str": null,
"id": 239442171466493953,
"geo": null,
"retweeted": false,
"possibly_sensitive": false,
"in_reply_to_user_id": null,
"place": null,
"in_reply_to_screen_name": null,
"source": "web",
"in_reply_to_status_id": null
},
"id_str": "239445335481647105",
"entities": {
"urls": [
{
"expanded_url": "http://nbcnews.to/NtkRTJ",
"url": "http://t.co/f8ivBrVd",
"indices": [
115,
135
],
"display_url": "nbcnews.to/NtkRTJ"
}
],
"hashtags": [

],
"user_mentions": [
{
"name": "NBC News",
"id_str": "14173315",
"id": 14173315,
"indices": [
3,
11
],
"screen_name": "NBCNews"
}
]
},
"in_reply_to_user_id_str": null,
"contributors": null,
"text": "RT @NBCNews: Neil Armstrong has died at the age of 82 from complications from heart operations he had 3 weeks ago. http://t.co/f8ivBrVd",
"retweet_count": 112,
"in_reply_to_status_id_str": null,
"id": 239445335481647105,
"geo": null,
"retweeted": false,
"possibly_sensitive": false,
"in_reply_to_user_id": null,
"place": null,
"in_reply_to_screen_name": null,
"source": "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter for iPhone</a>",
"in_reply_to_status_id": null
},
"statuses_count": 13728,
"friends_count": 1780,
"following": true,
"show_all_inline_media": true,
"screen_name": "rsarver"
}
27 changes: 27 additions & 0 deletions test/profile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var fs = require('fs')
, parse = require('../lib/profile').parse;


describe('profile.parse', function() {

describe('example profile', function() {
var profile;

before(function(done) {
fs.readFile('test/data/example.json', 'utf8', function(err, data) {
if (err) { return done(err); }
profile = parse(data);
console.log(profile);
done();
});
});

it('should parse profile', function() {
expect(profile.id).to.equal('795649');
expect(profile.username).to.equal('rsarver');
expect(profile.displayName).to.equal('Ryan Sarver');
expect(profile.photos[0].value).to.equal('https://si0.twimg.com/profile_images/1777569006/image1327396628_normal.png');
});
});

});
4 changes: 2 additions & 2 deletions test/strategy.options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ describe('Strategy#userProfile', function() {

it('should parse profile', function() {
expect(profile.provider).to.equal('twitter');
expect(profile.id, '1705');
expect(profile.username, 'jaredhanson');
expect(profile.id).to.equal('1705');
expect(profile.username).to.equal('jaredhanson');
});

it('should not set raw property', function() {
Expand Down
8 changes: 4 additions & 4 deletions test/strategy.profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ describe('Strategy#userProfile', function() {

it('should parse profile', function() {
expect(profile.provider).to.equal('twitter');
expect(profile.id, '6253282');
expect(profile.username, 'twitterapi');
expect(profile.displayName, 'Twitter API');
expect(profile.photos[0].value, 'https://si0.twimg.com/profile_images/1438634086/avatar_normal.png');
expect(profile.id).to.equal('6253282');
expect(profile.username).to.equal('twitterapi');
expect(profile.displayName).to.equal('Twitter API');
expect(profile.photos[0].value).to.equal('https://si0.twimg.com/profile_images/1438634086/avatar_normal.png');
});

it('should set raw property', function() {
Expand Down

2 comments on commit d9112c4

@yokobond
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of "profile.id" doesn't match the "JSON.parse(data).id". It will be a trouble to find items in MongoDB.
The 'profile.test.js' should have this test.

expect(profile.id).to.be.a('number');

@jaredhanson
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's normalized to a string to conform to the Portable Contacts schema.

Also, Twitter recommends using "id_str" to avoid potential problems regarding big integers: https://dev.twitter.com/docs/twitter-ids-json-and-snowflake.

If you want an integer, you'll need to parseInt(profile.id), but its more future-proof to use a string type in your DB schema.

Please sign in to comment.