Basic CRUD API (https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) in Golang with data stored in Mongo DB.
1. cd CrudGo
2. go run main.go
3. Open Postman & test APIs
4. run testcases : go test -v OR goconvey
Note :
Fetching dependencies
$ go get github.com/smartystreets/goconvey/convey gopkg.in/mgo.v2 github.com/gorilla/mux github.com/sirupsen/logrus
1. [Install Go ](https://golang.org/doc/install)
2. [Install Mongo](https://docs.mongodb.com/manual/installation/)
3. Install GoConvey (go get "github.com/smartystreets/goconvey/convey")
4. Install Logrus (go get "github.com/sirupsen/logrus")
{ "users":
[{ "username": "jsmith", "displayName": "John Smith", "department": "Sales" },
{ "username": "jdoe", "displayName": "John Doe", "department": "Development" }]
}
-
URL
-
Method:
GET
-
URL Params
None
-
Data Params
None
-
Success Response:
Returns all users present in the database
-
Code: 200
Content:[ { "username": "jsmith", "displayName": "John Smith", "department": "Sales" }, { "username": "sbhagat", "displayName": "Sourabh Bhagat", "department": "Development" }, { "username": "jdoe", "displayName": "John Doe", "department": "Sales" } ]
-
-
Error Response:
This case occurs when mongo db is down.
- Code: 400 StatusBadRequest
Content:{ user : [] }
- Code: 400 StatusBadRequest
-
Notes:
-
URL
-
Method:
POST
-
URL Params
None
-
Data Params
displayName=[string]
department=[string]
Required:
username=[string]
-
Success Response:
-
Code: 200
Content:{ "status": 200, "message": "Inserted user into database" }
-
-
Error Response:
This case occurs when mongo db is down.
-
Code: 400 StatusBadRequest
Content:Empty username { "status": 400, "message": "Invalid input : Please enter username" } OR User already found in the database { "status": 409, "message": "User already found in the database" } OR MongoDB is down { "status": 403, "message": Unable to insert user into database }
-
-
Notes: make sure that database is created with name myDB
-
URL
-
Method:
GET
-
URL Params
username
-
Data Params
NONE
Required:
username=[string]
-
Success Response:
URL : http://localhost:8888/users/jdoe
-
Code: 200
Content:{ "displayName": "John Doe", "department": "Sales" }
-
-
Error Response:
This case occurs when mongo db is down.
-
Code: 404 StatusNotFound
Content:If username doesnot exist in the database OR Mongo is down { "status": 404, "message": "User not found" }
-
-
Notes: make sure that database is created with name myDB
-
URL
-
Method:
DELETE
-
URL Params
username
-
Data Params
NONE
Required:
username=[string]
-
Success Response:
URL : http://localhost:8888/users/jdoe
-
Code: 200
Content:{ "status": 200, "message": "Deleted user from database" }
-
-
Error Response:
This case occurs when mongo db is down.
-
Code: 404 StatusNotFound
Content:If username doesnot exist in the database OR Mongo is down { "status": 404, "message": "Unable to delete user from database" }
-
-
Notes: make sure that database is created with name myDB
sourabh:user sourabh$ go test -v
=== RUN TestInsertNewUser
Testing InsertNewUser function
Testing for incorrect input
Testing for empty username input
Error returned by InsertNewUser should not be nil ✔
Testing for correct input
Testing for valid input (inserting test user)
Error returned by InsertNewUser should be nil ✔
2 total assertions
--- PASS: TestInsertNewUser (0.01s)
=== RUN TestGetAllUsers
Testing GetAllUsers function
Testing for valid input
Error returned by GetAllUsers should be nil ✔
Length of users list returned by GetAllUsers should be not be nil ✔
4 total assertions
--- PASS: TestGetAllUsers (0.00s)
=== RUN TestGetUserByName
Testing GetUserByName function
Testing for invalid input
Testing for empty username
Error returned by GetUserByName should be nil ✔
Testing for valid input
Error returned by GetUserByName should be nil ✔
6 total assertions
--- PASS: TestGetUserByName (0.00s)
=== RUN TestDeleteUser
Testing DeleteUser function
Document should be deleted after calling DeleteUser function ✔
7 total assertions
--- PASS: TestDeleteUser (0.02s)
PASS
ok github.com/CrudGO/user 0.053s