This project demonstrates a Three-Layer Architecture in Golang with unit testing and mock integration, using:
net/httpfor handlers- A custom
servicelayer for business logic - A
storelayer for data access - Fully unit-tested using Goβs
testingpackage and mock functions
microservice/ β βββ Models/ β βββ user/ β β βββ user.go β β βββ user_test.go # β model-related tests (if any) β βββ task/ β βββ task.go β βββ task_test.go β βββ Store/ β βββ user/ β β βββ store.go β β βββ store_test.go # β user store tests β βββ task/ β βββ store.go β βββ store_test.go # β task store tests β βββ Service/ β βββ user/ β β βββ service.go β β βββ service_test.go # β service logic test β βββ task/ β βββ service.go β βββ service_test.go β βββ Handler/ β βββ user/ β β βββ handler.go β β βββ handler_test.go # β HTTP handler test β βββ task/ β βββ handler.go β βββ handler_test.go β βββ Database/ β βββ connection.go β βββ connection_test.go # β DB connection test β βββ main.go
//no issue golangci-lint run checked
zopdev@ZopSmarts-MacBook-Pro microservice % golangci-lint run
0 issues.
// test coverge
go test ./... --coverprofile=c.out
microservice coverage: 0.0% of statements
? microservice/Models/task [no test files]
? microservice/Models/user [no test files]
ok microservice/database 2.033s coverage: 0.0% of statements [no tests to run]
ok microservice/handler/task 1.496s coverage: 73.2% of statements
ok microservice/handler/user 1.237s coverage: 71.4% of statements
ok microservice/service/task 0.936s coverage: 84.6% of statements
ok microservice/service/user 1.752s coverage: 82.4% of statements
ok microservice/store/task 0.512s coverage: 88.1% of statements
ok microservice/store/user 2.360s coverage: 82.5% of statements