Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
[#840] openDataPercentCount should return 0 correctly
Browse files Browse the repository at this point in the history
When no entries have been approved, openDataPercentCount should return
0, not NaN. Includes tests for this and other general cases.
  • Loading branch information
brew committed Dec 16, 2016
1 parent c77d8eb commit 6caea1e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion census/models/utils.js
Expand Up @@ -111,7 +111,7 @@ var processStats = function(data, options) {
}).length;
data.stats.openDataPercent = parseInt(
(data.stats.currentEntryOpenCount / data.stats.currentEntryCount) * 100,
10);
10) || 0;
} else {
data.stats.currentEntryCount = 0;
data.stats.currentEntryOpenCount = 0;
Expand Down
11 changes: 11 additions & 0 deletions fixtures/site.js
Expand Up @@ -33,6 +33,17 @@ var objects = [
locales: ['en', 'es', 'uk']
}
}
},
{
model: 'Site',
data: {
id: 'site3',
settings: {
places: 'https://docs.google.com/spreadsheets/d/1QvZFGyICiuZmRxVll6peXkND_6QmHl7IQ_BYCw5Sso4/edit#gid=1',
datasets: 'https://docs.google.com/spreadsheets/d/18mw_Ig9zvwb514VQsTGfrg0WMrcUngxBIRzWSxpguho/edit#gid=0',
questions: 'https://docs.google.com/spreadsheets/d/1nwmk8uJEK-4K6-5SdBgoLUlUos-BhfYRgjS74YkhDGc/edit#gid=3',
}
}
}
];

Expand Down
3 changes: 2 additions & 1 deletion tests/index.js
Expand Up @@ -20,7 +20,8 @@ describe('Open Data Census Tests', function() {
it('counts all sites', function() {
let query = models.Site.findAll();
return query.then(results => {
assert.equal(results.length, 2);
// 3 sites loaded from fixtures
assert.equal(results.length, 3);
});
});

Expand Down
2 changes: 1 addition & 1 deletion tests/loaders.js
Expand Up @@ -299,7 +299,7 @@ describe('System Control page', function () {
this.app.get('models').Site.count(),
this.app.get('models').Site.findById(siteID),
function(count, siteData) {
assert.equal(count, 2);
assert.equal(count, 3);
assert.isNotNull(siteData);
assert.notEqual(siteData.places, '');
assert.notEqual(siteData.places, '');
Expand Down
51 changes: 51 additions & 0 deletions tests/models.js
Expand Up @@ -281,6 +281,57 @@ describe('Place instance methods', function() {
});
});

describe('Stats object', function() {
this.timeout(20000);

beforeEach(utils.setupFixtures);
afterEach(utils.dropFixtures);

before(function() {
this.dataOptions = {
models: models,
domain: 'site3',
dataset: null,
place: null,
year: null,
cascade: true,
scoredQuestionsOnly: true,
locale: null,
with: {Entry: true, Dataset: true, Place: true, Question: true}
};
});

it('has 0 openDataPercent for site with no entries', function() {
return modelUtils.getData(this.dataOptions)
.then(data => {
expect(data.stats).to.have.property('openDataPercent');
expect(data.stats.openDataPercent).to.equal(0);
});
});

it('has expected openDataPercent for site with entries', function() {
let dataOptions = _.assign(this.dataOptions, {domain: 'site1'});
return modelUtils.getData(dataOptions)
.then(data => {
expect(data.stats).to.have.property('openDataPercent');
expect(data.stats.openDataPercent).to.equal(25);
});
});

it('has 0 openDataPercent when entries are excluded', function() {
let dataOptions = _.assign(this.dataOptions,
{
domain: 'site1',
with: {Entry: false, Dataset: false, Place: false, Question: false}
});
return modelUtils.getData(dataOptions)
.then(data => {
expect(data.stats).to.have.property('openDataPercent');
expect(data.stats.openDataPercent).to.equal(0);
});
});
});

describe('Data access layer', function() {
this.timeout(20000);

Expand Down

0 comments on commit 6caea1e

Please sign in to comment.