From 6caea1e2fe86a49ade7cc53f2bb8818fa26944ca Mon Sep 17 00:00:00 2001 From: Brook Elgie Date: Fri, 16 Dec 2016 11:00:08 +0000 Subject: [PATCH] [#840] openDataPercentCount should return 0 correctly When no entries have been approved, openDataPercentCount should return 0, not NaN. Includes tests for this and other general cases. --- census/models/utils.js | 2 +- fixtures/site.js | 11 +++++++++ tests/index.js | 3 ++- tests/loaders.js | 2 +- tests/models.js | 51 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/census/models/utils.js b/census/models/utils.js index e7425ae7..d76eda96 100644 --- a/census/models/utils.js +++ b/census/models/utils.js @@ -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; diff --git a/fixtures/site.js b/fixtures/site.js index 9c6f606f..abbca28e 100644 --- a/fixtures/site.js +++ b/fixtures/site.js @@ -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', + } + } } ]; diff --git a/tests/index.js b/tests/index.js index b2b47edb..3199650a 100644 --- a/tests/index.js +++ b/tests/index.js @@ -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); }); }); diff --git a/tests/loaders.js b/tests/loaders.js index dc6a3eeb..3e1875bc 100644 --- a/tests/loaders.js +++ b/tests/loaders.js @@ -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, ''); diff --git a/tests/models.js b/tests/models.js index 4a1b052d..902e0d6d 100644 --- a/tests/models.js +++ b/tests/models.js @@ -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);