krovn

Untitled

Nov 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.49 KB | None | 0 0
  1. ===============================domain/user_dao.go
  2. package domain
  3.  
  4. import (
  5.     "fmt"
  6.     "log"
  7.  
  8.     "github.com/jkrovnl/golearn2/utils"
  9. )
  10.  
  11. var (
  12.     users = map[int64]*User{
  13.         123: {Id: 321, Fname: "Janis", Lname: "Lange", Email: "[email protected]"},
  14.     }
  15.     UserDao userDaoInterface
  16. )
  17.  
  18. func init() {
  19.     UserDao = &userDao{}
  20. }
  21.  
  22. type userDaoInterface interface {
  23.     GetUser(int64) (*User, *utils.ApplicationError) //ja int32 tad init nestradas jo nav pareizi parametri
  24. }
  25.  
  26. type userDao struct{}
  27.  
  28. func (u *userDao) GetUser(userId int64) (*User, *utils.ApplicationError) {
  29.     log.Println("We are accesing database.")
  30.     if user := users[userId]; user != nil {
  31.         return user, nil
  32.     }
  33.     return nil, &utils.ApplicationError{
  34.         Message:    fmt.Sprintf("user %v was not found", userId),
  35.         StatusCode: 404,
  36.         Code:       "not_found",
  37.     }
  38. }
  39. ===============================services/user_service.go
  40. package services
  41.  
  42. import (
  43.     "github.com/jkrovnl/golearn2/domain"
  44.     "github.com/jkrovnl/golearn2/utils"
  45. )
  46.  
  47. type usersService struct{}
  48.  
  49. var UsersService usersService
  50.  
  51. func (u *usersService) GetUser(userId int64) (*domain.User, *utils.ApplicationError) {
  52.     return domain.UserDao.GetUser(userId)
  53. }
  54. ===============================services/user_service.go
  55. package services
  56.  
  57. import (
  58.     "net/http"
  59.     "testing"
  60.  
  61.     "github.com/jkrovnl/golearn2/domain"
  62.     "github.com/jkrovnl/golearn2/utils"
  63.  
  64.     "github.com/stretchr/testify/assert"
  65. )
  66.  
  67. var (
  68.     userDaoMock     usersDaoMock
  69.     getUserFunction func(userId int64) (*domain.User, *utils.ApplicationError)
  70. )
  71.  
  72. func init() {
  73.     domain.UserDao = &usersDaoMock{}
  74. }
  75.  
  76. type usersDaoMock struct{}
  77.  
  78. func (n *usersDaoMock) GetUser(userId int64) (*domain.User, *utils.ApplicationError) {
  79.     return getUserFunction(userId)
  80. }
  81.  
  82. func TestGetUserNotFoundInDatabase(t *testing.T) {
  83.     getUserFunction = func(userId int64) (*domain.User, *utils.ApplicationError) {
  84.         return nil, &utils.ApplicationError{
  85.             StatusCode: http.StatusNotFound,
  86.             Message:    "user 0 was not found",
  87.         }
  88.     }
  89.     user, err := UsersService.GetUser(0)
  90.     assert.Nil(t, user)
  91.     assert.NotNil(t, err)
  92.     assert.EqualValues(t, http.StatusNotFound, err.StatusCode)
  93.     assert.EqualValues(t, "user 0 was not found", err.Message)
  94. }
  95.  
  96. func TestGetUserNoError(t *testing.T) {
  97.     getUserFunction = func(userId int64) (*domain.User, *utils.ApplicationError) {
  98.         return &domain.User{
  99.             Id: 321,
  100.         }, nil
  101.     }
  102.     user, err := UsersService.GetUser(123)
  103.     assert.Nil(t, err)
  104.     assert.NotNil(t, user)
  105.     assert.EqualValues(t, 321, user.Id)
  106. }
Add Comment
Please, Sign In to add comment