Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed May 5, 2017
1 parent 4afd73b commit 808c1ca
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 61 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ npmApis.getTodayUpdates().then((data) => {
});
```

### getYesterdayUpdates

Get yesterday update modules

```js
const npmApis = require('npm-apis');
npmApis.getYesterdayUpdates().then((data) => {
console.info(data);
});
```

### getDependeds

Get the module depended informations
Expand Down
21 changes: 10 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const request = require('superagent');
const _ = require('lodash');
const moment = require('moment');
const stream = require('stream');

exports.timeout = 30 * 1000;
Expand Down Expand Up @@ -190,27 +189,27 @@ function getDownloadsByDay(name, day) {
* @return {Integer} The downloads of modules
*/
exports.getYesterdayDownloads = (name) => {
const yesterday = moment().add(-1, 'day').toISOString().substring(0, 10);
const yesterday = new Date(Date.now() - (24 * 3600 * 1000)).toISOString().substring(0, 10);
return getDownloadsByDay(name, yesterday);
};

/**
* Get the update moudles of today
* @return {Array} The module list
*/
exports.getTodayUpdates = () => {
const url = addRegistry('/-/all/static/today.json');
return request.get(url)
exports.getTodayUpdates = (url) => {
const requestUrl = url || addRegistry('/-/all/static/today.json');
return request.get(requestUrl)
.then(res => _.map(res.body, item => item.name));
};

/**
* Get the update moudles of yesterday
* @return {Array} The module list
*/
exports.getYesterdayUpdates = () => {
const url = addRegistry('/-/all/static/yesterday.json');
return request.get(url)
exports.getYesterdayUpdates = (url) => {
const requestUrl = url || addRegistry('/-/all/static/yesterday.json');
return request.get(requestUrl)
.then(res => _.map(res.body, item => item.name));
};

Expand All @@ -219,9 +218,9 @@ exports.getYesterdayUpdates = () => {
* Get the depended count list
* @return {Array} THe depended count informations list
*/
exports.getDependeds = () => {
const url = addRegistry('/-/_view/dependedUpon?group_level=1');
return request.get(url)
exports.getDependeds = (url) => {
const requestUrl = url || addRegistry('/-/_view/dependedUpon?group_level=1');
return request.get(requestUrl)
.then((res) => {
const arr = [];
_.forEach(res.body.rows, (item) => {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
},
"dependencies": {
"lodash": "^4.17.4",
"moment": "^2.18.1",
"superagent": "^3.5.2"
},
"devDependencies": {
Expand Down
61 changes: 12 additions & 49 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,15 @@
const request = require('superagent');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const _ = require('lodash');

const npmApis = require('..');
const originalEnd = request.Request.prototype.end;

npmApis.timeout = 0;

function getAllJSON(fn) {
const file = path.join(__dirname, '../assets/all.json');
fs.readFile(file, (err, buf) => {
if (err) {
fn(err);
return;
}
return fn(null, {
body: JSON.parse(buf),
});
});
}

function getDepended(fn) {
const file = path.join(__dirname, '../assets/depended.json');
fs.readFile(file, (err, buf) => {
if (err) {
fn(err);
return;
}
return fn(null, {
body: JSON.parse(buf),
});
});
}

if (process.env.MOCK) {
request.Request.prototype.end = function(fn) {
const url = this.url;
switch (url) {
case 'https://registry.npmjs.org/-/all/static/all.json':
getAllJSON(fn);
break;
case 'https://registry.npmjs.org/-/_view/dependedUpon?group_level=1':
getDepended(fn);
break;
default:
originalEnd.apply(this, [fn]);
}
};
}

describe('npm-apis', () => {
it('getAll', function(done) {
this.timeout(300 * 1000);
npmApis.getAll().then((data) => {
npmApis.getAll('http://oidmt881u.bkt.clouddn.com/all.json').then((data) => {
assert(data.length > 400 * 1000);
assert(data.indexOf('influxdb-nodejs') !== -1);
done();
Expand All @@ -63,13 +18,13 @@ describe('npm-apis', () => {

it('getDependeds', function(done) {
this.timeout(300 * 1000);
npmApis.getDependeds().then((data) => {
npmApis.getDependeds('http://oidmt881u.bkt.clouddn.com/depended.json').then((data) => {
assert(data.length > 50 * 1000);
assert(_.find(data, item => item.name === 'lodash'));
done();
}).catch(done);
});

it('getUser', function(done) {
this.timeout(10 * 1000);
const name = 'tree.xie';
Expand Down Expand Up @@ -123,12 +78,20 @@ describe('npm-apis', () => {

it('getTodayUpdates', function(done) {
this.timeout(10 * 1000);
npmApis.getTodayUpdates().then((data) => {
npmApis.getTodayUpdates('http://oidmt881u.bkt.clouddn.com/today.json').then((data) => {
assert.notEqual(data.length, 0);
done();
}).catch(done);
});

it('getYesterdayUpdates', function(done) {
this.timeout(10 * 1000);
npmApis.getYesterdayUpdates('http://oidmt881u.bkt.clouddn.com/yesterday.json').then((data) => {
assert.notEqual(data.length, 0);
done();
}).catch(done);
})

it('getScore', function(done) {
this.timeout(10 * 1000);
npmApis.getScore('express').then((data) => {
Expand Down

0 comments on commit 808c1ca

Please sign in to comment.