Skip to content

Commit

Permalink
solution 4.7*: helper functions and unit tests, step5
Browse files Browse the repository at this point in the history
  • Loading branch information
patchamama committed Sep 9, 2023
1 parent c9d554c commit 27b41a6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 146 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Answers to [openfullstack.com](https://fullstackopen.com) course exercises from
_Details solutions: [4.1](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/01104c7798ad53f35ee73994826f5cc3602b9b3e) | [4.2](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/7c913309759236084783f4bdfdec443c5b9e19d5)_

- [Exercises 4.3.-4.7.](https://fullstackopen.com/en/part4/structure_of_backend_application_introduction_to_testing#exercises-4-3-4-7)
_Details solutions: [4.3](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/0f1a759e78310de711531ed481917496e2f50ec8) | [4.4](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/2585dc72841e8382123fa481aa149d19dd611482) | [4.5](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/4fd94b8779e33ea32b3703a6e6a823291d247e0c) | [4.6]() | [4.7]()_
_Details solutions: [4.3](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/0f1a759e78310de711531ed481917496e2f50ec8) | [4.4](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/2585dc72841e8382123fa481aa149d19dd611482) | [4.5](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/4fd94b8779e33ea32b3703a6e6a823291d247e0c) | [4.6](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/c9d554cf41d5cbe5992fd2e2005564fab816d8ad) | [4.7]()_

### b. Testing the backend

Expand Down
221 changes: 76 additions & 145 deletions tests/blog.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
const listHelper = require('../utils/list_helper')

const bigbloglist = [
{
_id: '5a422b3a1b54a676234d17f9',
title: 'Canonical string reduction',
author: 'Edsger W. Dijkstra',
url: 'http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD808.html',
likes: 12,
__v: 0,
},
{
_id: '5a422a851b54a676234d17f7',
title: 'React patterns',
author: 'Michael Chan',
url: 'https://reactpatterns.com/',
likes: 7,
__v: 0,
},
{
_id: '5a422aa71b54a676234d17f8',
title: 'Go To Statement Considered Harmful',
author: 'Edsger W. Dijkstra',
url: 'http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html',
likes: 5,
__v: 0,
},
{
_id: '5a422b891b54a676234d17fa',
title: 'First class tests',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2017/05/05/TestDefinitions.htmll',
likes: 10,
__v: 0,
},
{
_id: '5a422ba71b54a676234d17fb',
title: 'TDD harms architecture',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html',
likes: 0,
__v: 0,
},
{
_id: '5a422bc61b54a676234d17fc',
title: 'Type wars',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html',
likes: 2,
__v: 0,
},
]

test('dummy returns one', () => {
const blogs = []

Expand Down Expand Up @@ -29,57 +80,7 @@ describe('total likes', () => {
})

test('of a bigger list is calculated right', () => {
const blogs = [
{
_id: '5a422a851b54a676234d17f7',
title: 'React patterns',
author: 'Michael Chan',
url: 'https://reactpatterns.com/',
likes: 7,
__v: 0,
},
{
_id: '5a422aa71b54a676234d17f8',
title: 'Go To Statement Considered Harmful',
author: 'Edsger W. Dijkstra',
url: 'http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html',
likes: 5,
__v: 0,
},
{
_id: '5a422b3a1b54a676234d17f9',
title: 'Canonical string reduction',
author: 'Edsger W. Dijkstra',
url: 'http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD808.html',
likes: 12,
__v: 0,
},
{
_id: '5a422b891b54a676234d17fa',
title: 'First class tests',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2017/05/05/TestDefinitions.htmll',
likes: 10,
__v: 0,
},
{
_id: '5a422ba71b54a676234d17fb',
title: 'TDD harms architecture',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html',
likes: 0,
__v: 0,
},
{
_id: '5a422bc61b54a676234d17fc',
title: 'Type wars',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html',
likes: 2,
__v: 0,
},
]
const result = listHelper.totalLikes(blogs)
const result = listHelper.totalLikes(bigbloglist)
expect(result).toBe(36)
})
})
Expand Down Expand Up @@ -107,58 +108,8 @@ describe('favorites', () => {
})

test('Most favorite with a big list', () => {
const blogs = [
{
_id: '5a422b3a1b54a676234d17f9',
title: 'Canonical string reduction',
author: 'Edsger W. Dijkstra',
url: 'http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD808.html',
likes: 12,
__v: 0,
},
{
_id: '5a422a851b54a676234d17f7',
title: 'React patterns',
author: 'Michael Chan',
url: 'https://reactpatterns.com/',
likes: 7,
__v: 0,
},
{
_id: '5a422aa71b54a676234d17f8',
title: 'Go To Statement Considered Harmful',
author: 'Edsger W. Dijkstra',
url: 'http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html',
likes: 5,
__v: 0,
},
{
_id: '5a422b891b54a676234d17fa',
title: 'First class tests',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2017/05/05/TestDefinitions.htmll',
likes: 10,
__v: 0,
},
{
_id: '5a422ba71b54a676234d17fb',
title: 'TDD harms architecture',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html',
likes: 0,
__v: 0,
},
{
_id: '5a422bc61b54a676234d17fc',
title: 'Type wars',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html',
likes: 2,
__v: 0,
},
]
const result = listHelper.favoriteBlog(blogs)
expect(result).toEqual(blogs[0])
const result = listHelper.favoriteBlog(bigbloglist)
expect(result).toEqual(bigbloglist[0])
expect(result.likes).toEqual(12)
})
})
Expand Down Expand Up @@ -186,47 +137,22 @@ describe('Authors with more blogs', () => {
expect(result).toEqual({ author: 'Robert C. Martin', entries: 1 })
})
test('More blogs with big list', () => {
const result = listHelper.mostBlogs(bigbloglist)
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', entries: 3 })
})
})

describe('Authors with more likes', () => {
test('--- with empty list', () => {
const blogs = [{}]
const result = listHelper.mostLikes(blogs)
console.log(result)
expect(result).toEqual({ author: undefined, likes: undefined })
})

test('--- with one blog list', () => {
const blogs = [
{
_id: '5a422b3a1b54a676234d17f9',
title: 'Canonical string reduction',
author: 'Edsger W. Dijkstra',
url: 'http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD808.html',
likes: 12,
__v: 0,
},
{
_id: '5a422a851b54a676234d17f7',
title: 'React patterns',
author: 'Michael Chan',
url: 'https://reactpatterns.com/',
likes: 7,
__v: 0,
},
{
_id: '5a422aa71b54a676234d17f8',
title: 'Go To Statement Considered Harmful',
author: 'Edsger W. Dijkstra',
url: 'http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html',
likes: 5,
__v: 0,
},
{
_id: '5a422b891b54a676234d17fa',
title: 'First class tests',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2017/05/05/TestDefinitions.htmll',
likes: 10,
__v: 0,
},
{
_id: '5a422ba71b54a676234d17fb',
title: 'TDD harms architecture',
author: 'Robert C. Martin',
url: 'http://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html',
likes: 0,
__v: 0,
},
{
_id: '5a422bc61b54a676234d17fc',
title: 'Type wars',
Expand All @@ -236,8 +162,13 @@ describe('Authors with more blogs', () => {
__v: 0,
},
]
const result = listHelper.mostBlogs(blogs)
const result = listHelper.mostLikes(blogs)
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', entries: 3 })
expect(result).toEqual({ author: 'Robert C. Martin', likes: 2 })
})
test('--- with big blog list', () => {
const result = listHelper.mostLikes(bigbloglist)
console.log(result)
expect(result).toEqual({ author: 'Edsger W. Dijkstra', likes: 17 })
})
})
23 changes: 23 additions & 0 deletions utils/list_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,32 @@ const mostBlogs = (blogs) => {
}
}

const mostLikes = (blogs) => {
if (blogs.length === 0) return { author: undefined, likes: undefined }
else if (blogs.length === 1)
return { author: blogs[0].author, likes: blogs[0].likes }
else {
const groupedByAuthor = _.groupBy(blogs, 'author')
const authorLikes = _.mapValues(groupedByAuthor, (entries) =>
_.sumBy(entries, 'likes')
)

const authorWithMostLikes = _.maxBy(
_.keys(authorLikes),
(author) => authorLikes[author]
)

return {
author: authorWithMostLikes,
likes: authorLikes[authorWithMostLikes],
}
}
}

module.exports = {
dummy,
totalLikes,
favoriteBlog,
mostBlogs,
mostLikes,
}

0 comments on commit 27b41a6

Please sign in to comment.