Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ad2857

Browse files
Merge pull request #1 from fajarcandraaa/develop
unit testing
2 parents 90663e1 + a45f638 commit 6ad2857

File tree

27 files changed

+298
-912
lines changed

27 files changed

+298
-912
lines changed

‎README.md‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
# simple-golang-unit-testing
1+
# Simple Golang Unit Testing
2+
3+
![Simple Golang Unit Testing](https://i.ytimg.com/vi/IorsKSduDaM/maxresdefault.jpg)
4+
5+
We will learn about implementing in Golang.
6+
7+
## Topics :
8+
- Setting up the Golang Project
9+
- Defining the Product Entity
10+
- Connecting to the database
11+
- Implementing Unit test with 1 feature to find data by ID
12+
13+
## Our Step :
14+
- In the first step, we create an entities to represent the structure of our data in the entity package
15+
- In the second step, we create query a function to handle our query
16+
- Then register the new function in our model interface
17+
- In the third step, we create algorithm function to handle all condition in our flow proccess
18+
- and again, we register it into source contract interface
19+
- In fourth step is create faker data or mock data
20+
- than, we create a seederes, to input fake data to database testing
21+
- And the last step is, we create unit test with 2 case, 1 positive cae and 1 negative case
22+
23+
## Notes :
24+
Last but not least, let's start to try to write a function that we think will be used many times in helpers.
25+
Happy coding and keep learning 😜

‎config/app/app.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎config/config.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎config/routers/base.go‎

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,11 @@
11
package routers
22

33
import (
4-
"fmt"
5-
"log"
6-
"net/http"
7-
"os"
8-
9-
"github.com/fajarcandraaa/simple-golang-unit-testing/config/app"
10-
"github.com/fajarcandraaa/simple-golang-unit-testing/route"
11-
"github.com/fajarcandraaa/simple-golang-unit-testing/src"
124
"github.com/gorilla/mux"
135
"github.com/jinzhu/gorm"
14-
15-
_ "github.com/jinzhu/gorm/dialects/mysql" //mysql database driver
16-
_ "github.com/jinzhu/gorm/dialects/postgres" //postgres database driver
176
)
187

19-
type Service struct {
20-
Home *src.Home
21-
User *route.UserHandler
22-
}
23-
248
type Serve struct {
25-
DB *gorm.DB
26-
Router *mux.Router
27-
Service Service
28-
}
29-
30-
// Initialize is used to initialize db driver connection
31-
func (s *Serve) Initialize(Dbdriver, DbUser, DbPassword, DbPort, DbHost, DbName string) {
32-
var err error
33-
//Set migration table
34-
registry := app.SetMigrationTable()
35-
36-
//initiate database driver
37-
if Dbdriver == "mysql" {
38-
DBURL := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", DbUser, DbPassword, DbHost, DbPort, DbName)
39-
s.DB, err = gorm.Open(Dbdriver, DBURL)
40-
if err != nil {
41-
fmt.Printf("Cannot connect to %s database", Dbdriver)
42-
log.Fatal("This is the error:", err)
43-
} else {
44-
fmt.Printf("We are connected to the %s database", Dbdriver)
45-
}
46-
}
47-
if Dbdriver == "postgres" {
48-
DBURL := fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=disable password=%s", DbHost, DbPort, DbUser, DbName, DbPassword)
49-
s.DB, err = gorm.Open(Dbdriver, DBURL)
50-
if err != nil {
51-
fmt.Printf("Cannot connect to %s database", Dbdriver)
52-
log.Fatal("This is the error:", err)
53-
} else {
54-
fmt.Printf("We are connected to the %s database", Dbdriver)
55-
}
56-
}
57-
58-
//Migration proccess
59-
s.DB.Debug().AutoMigrate(registry...) //database migration
60-
61-
s.Router = mux.NewRouter()
62-
63-
s.initializeRoutes()
64-
}
65-
66-
// Run is used to execute connection and run our service
67-
func (s *Serve) Run() {
68-
port := os.Getenv("APP_PORT")
69-
fmt.Println("Listening to port ", port)
70-
log.Fatal(http.ListenAndServe(":"+port, s.Router))
9+
DB *gorm.DB
10+
Router *mux.Router
7111
}

‎config/routers/router.go‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎config/routers/routers.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎config/server.go‎

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎entity/entity.go‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎entity/userentity/user.go‎

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package userentity
22

33
import (
44
"time"
5-
6-
validation "github.com/go-ozzo/ozzo-validation/v4"
75
)
86

97
// User -> initialization user entity
@@ -21,58 +19,6 @@ type User struct {
2119
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"`
2220
}
2321

24-
// Users represent body for list users
25-
type Users struct {
26-
ID string `json:"id"`
27-
Firstname string `json:"first_name"`
28-
Lastname string `json:"last_name"`
29-
Phone string `json:"phone_number"`
30-
Avatar string `json:"avatar"`
31-
Email string `json:"email"`
32-
Username string `json:"username"`
33-
Status string `json:"status"`
34-
CreatedAt string `json:"created_at"`
35-
UpdatedAt string `json:"updated_at"`
36-
}
37-
38-
// Users represent body for get data from user
39-
type UserData struct {
40-
ID string `json:"id"`
41-
Firstname string `json:"first_name"`
42-
Lastname string `json:"last_name"`
43-
Phone string `json:"phone_number"`
44-
Avatar string `json:"avatar"`
45-
Email string `json:"email"`
46-
Username string `json:"username"`
47-
Status string `json:"status"`
48-
CreatedAt string `json:"created_at"`
49-
UpdatedAt string `json:"updated_at"`
50-
}
51-
52-
type UserRequest struct {
53-
Firstname string `json:"first_name"`
54-
Lastname string `json:"last_name"`
55-
Phone string `json:"phone_number"`
56-
Avatar string `json:"avatar"`
57-
Email string `json:"email"`
58-
Username string `json:"username"`
59-
Password string `json:"password"`
60-
Status string `json:"status"`
61-
}
62-
63-
// UserRequestValidate is to validate input request
64-
func UserRequestValidate(ur *UserRequest) error {
65-
err := validation.Errors{
66-
"first_name": validation.Validate(&ur.Firstname, validation.Required, validation.Length(2, 40)),
67-
"last_name": validation.Validate(&ur.Lastname, validation.Required),
68-
"email": validation.Validate(&ur.Email, validation.Required),
69-
"username": validation.Validate(&ur.Username, validation.Required),
70-
"password": validation.Validate(&ur.Password, validation.Required),
71-
}
72-
73-
return err.Filter()
74-
}
75-
7622
// FindUser is struct to handle respone while find user by ID
7723
type FindUser struct {
7824
Firstname string `json:"first_name"`
@@ -86,54 +32,3 @@ type FindUser struct {
8632
CreatedAt string `json:"created_at"`
8733
UpdatedAt string `json:"updated_at"`
8834
}
89-
90-
func SetParameterUpdateUser(user *User, payload *UserData) *User {
91-
var userdata User
92-
93-
if payload.Firstname == "" {
94-
userdata.Firstname = user.Firstname
95-
} else {
96-
userdata.Firstname = payload.Firstname
97-
}
98-
99-
if payload.Lastname == "" {
100-
userdata.Lastname = user.Lastname
101-
} else {
102-
userdata.Lastname = payload.Lastname
103-
}
104-
105-
if payload.Phone == "" {
106-
userdata.Phone = user.Phone
107-
} else {
108-
userdata.Phone = payload.Phone
109-
}
110-
111-
if payload.Avatar == "" {
112-
userdata.Avatar = user.Avatar
113-
} else {
114-
userdata.Avatar = payload.Avatar
115-
}
116-
117-
if payload.Email == "" {
118-
userdata.Email = user.Email
119-
} else {
120-
userdata.Email = payload.Email
121-
}
122-
123-
if payload.Username == "" {
124-
userdata.Username = user.Username
125-
} else {
126-
userdata.Username = payload.Username
127-
}
128-
129-
if payload.Status == "" {
130-
userdata.Status = user.Status
131-
} else {
132-
userdata.Status = payload.Status
133-
}
134-
135-
userdata.Password = user.Password
136-
userdata.UpdatedAt = time.Now()
137-
138-
return &userdata
139-
}

‎go.mod‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ module github.com/fajarcandraaa/simple-golang-unit-testing
33
go 1.19
44

55
require (
6-
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
76
github.com/gorilla/mux v1.8.0
87
github.com/jinzhu/gorm v1.9.16
98
github.com/joho/godotenv v1.5.1
109
)
1110

11+
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/jinzhu/now v1.1.4 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
gopkg.in/yaml.v3 v3.0.1 // indirect
16+
)
17+
1218
require (
1319
github.com/go-sql-driver/mysql v1.5.0
14-
github.com/google/uuid v1.3.0
1520
github.com/jinzhu/inflection v1.0.0 // indirect
1621
github.com/lib/pq v1.1.1
1722
github.com/pkg/errors v0.9.1
18-
github.com/stretchr/testify v1.8.1// indirect
23+
github.com/stretchr/testify v1.8.1
1924
golang.org/x/crypto v0.6.0
2025
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /