Skip to content

Commit

Permalink
solution 4.6*: funciones auxiliares y pruebas unitarias, paso 4
Browse files Browse the repository at this point in the history
  • Loading branch information
patchamama committed Sep 9, 2023
1 parent 4fd94b8 commit c9d554c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 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]() | [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]() | [4.7]()_

### b. Testing the backend

Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"cors": "2.8.5",
"dotenv": "16.3.1",
"express": "4.18.2",
"lodash": "^4.17.21",
"mongoose": "7.5.0"
},
"devDependencies": {
Expand Down
79 changes: 79 additions & 0 deletions tests/blog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,82 @@ describe('favorites', () => {
expect(result.likes).toEqual(12)
})
})

describe('Authors with more blogs', () => {
test('More blogs with empty list', () => {
const blogs = [{}]
const result = listHelper.mostBlogs(blogs)
console.log(result)
expect(result).toEqual({ author: undefined, entries: 1 })
})
test('More blogs with one element in the list', () => {
const blogs = [
{
_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.mostBlogs(blogs)
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', entries: 1 })
})
test('More blogs with 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.mostBlogs(blogs)
console.log(result)
expect(result).toEqual({ author: 'Robert C. Martin', entries: 3 })
})
})
19 changes: 18 additions & 1 deletion utils/list_helper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _ = require('lodash')

const dummy = (blogs) => {
// ...
return 1
Expand All @@ -14,12 +16,27 @@ const favoriteBlog = (blogs) => {
const reducer = (max, item) => {
return max.likes > item.likes ? max : item
}
console.log(blogs.reduce(reducer, 0))
// console.log(blogs.reduce(reducer, 0))
return blogs.length === 0 ? {} : blogs.reduce(reducer, { likes: 0 })
}

const mostBlogs = (blogs) => {
if (blogs.length === 0) return { author: undefined, entries: 1 }
else if (blogs.length === 1) return { author: blogs[0].author, entries: 1 }
else {
const blogsByAuthor = _.groupBy(blogs, 'author')
const authorEntries = _.map(blogsByAuthor, (blogs, author) => ({
author,
entries: blogs.length,
}))
console.log(_.maxBy(authorEntries, 'entries'))
return _.maxBy(authorEntries, 'entries')
}
}

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

0 comments on commit c9d554c

Please sign in to comment.