To check the blog related to this repository: Writing Integration Tests, A Comprehensive Guide for REST API with MongoDB
This is a simple project to demonstrate how to do integration tests for your API's with golang.
Here is an example request and response from this API
curl --location 'http://localhost:3030/api/books'
Response
{
"books": [
{
"title": "Crime and Punishment",
"author": {
"id": "654e618a60034d917aa0ae63",
"name": "Dostoyevski"
},
"likes": 12,
"comments": [
{
"postTitle": "Crime and Punishment",
"comment": "great read",
"likes": 3
}
]
},
{
"title": "Notes From The Underground",
"author": {
"id": "654e618a60034d917aa0ae63",
"name": "Dostoyevski"
},
"likes": 100,
"comments": [
{
"postTitle": "Notes From The Underground",
"comment": "good info",
"likes": 0
},
{
"postTitle": "Notes From The Underground",
"comment": "I liked this post",
"likes": 12
}
]
},
{
"title": "Meditations",
"author": {
"id": "654e619760034d917aa0ae64",
"name": "Marcus Aurelius"
},
"likes": 200,
"comments": [
{
"postTitle": "Meditations",
"comment": "very nice book",
"likes": 8
}
]
}
]
}
Another API Request
curl --location 'http://localhost:3030/api/author/654e619760034d917aa0ae64/books'
Response
{
"books": [
{
"title": "Meditations",
"author": {
"id": "654e619760034d917aa0ae64",
"name": "Marcus Aurelius"
},
"likes": 200,
"comments": [
{
"postTitle": "Meditations",
"comment": "very nice book",
"likes": 8
}
]
}
]
}
To see how the end2end tests are done, please check integrationtest folder.
first create the mongo database.
docker-compose up -d mongo
It will generate a new MongoDB container and populate the collections with some data.
then run
go run cmd/main.go
you will see
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.11.3
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:3030