-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug(article-ratings):fix errors with article ratings
- Loading branch information
Halimah Oladosu
authored and
Halimah Oladosu
committed
Jul 23, 2019
1 parent
60e7b73
commit 44b355a
Showing
9 changed files
with
134 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @method averageRatings | ||
* - Helps update the article table | ||
* | ||
* @param {integer} articleId - Id of the article to be rated | ||
* | ||
* @return {Object} ratingDetails | ||
*/ | ||
|
||
import model from '../db/models'; | ||
|
||
const { Rating } = model; | ||
|
||
const averageRatings = async articleId => { | ||
const allRatings = await Rating.findAndCountAll({ | ||
where: { articleId } | ||
}); | ||
|
||
let totalSum = 0; | ||
|
||
for (let i = 0; i < allRatings.count; i += 1) { | ||
const eachRating = allRatings.rows[i].rating; | ||
const newSum = eachRating + totalSum; | ||
totalSum = newSum; | ||
} | ||
const averageRating = | ||
parseFloat((totalSum / allRatings.count).toFixed(1)) || 0; | ||
const ratingDetails = { | ||
totalNumberOfRatings: allRatings.count, | ||
sumOfRatings: totalSum, | ||
averageRating | ||
}; | ||
return ratingDetails; | ||
}; | ||
export default averageRatings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import chai, { expect } from 'chai'; | ||
import averageRatings from '../../helpers/average-ratings'; | ||
import models from '../../db/models'; | ||
|
||
const { Rating } = models; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('Average Ratings', () => { | ||
afterEach(() => sinon.restore()); | ||
|
||
describe('averageratings', () => { | ||
it('get the article id', async () => { | ||
const mockResult = { | ||
totalNumberOfRatings: 5, | ||
sumOfRatings: 15, | ||
averageRating: 3 | ||
}; | ||
|
||
const allRatingsMock = { | ||
rows: [ | ||
{ | ||
rating: 1 | ||
}, | ||
{ | ||
rating: 2 | ||
}, | ||
{ | ||
rating: 3 | ||
}, | ||
{ | ||
rating: 4 | ||
}, | ||
{ | ||
rating: 5 | ||
} | ||
], | ||
count: 5 | ||
}; | ||
|
||
sinon.stub(Rating, 'findAndCountAll').returns(allRatingsMock); | ||
const result = await averageRatings(1); | ||
|
||
expect(result).to.deep.equals(mockResult); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters