Skip to content

Commit

Permalink
Merge pull request #66 from Caknoooo/task/65-adjust-unit-test
Browse files Browse the repository at this point in the history
fix: adjust unit test and remove unused file
  • Loading branch information
Caknoooo authored Jul 9, 2024
2 parents 7b1c8cb + 7bb7a35 commit a2f814a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
Binary file removed main
Binary file not shown.
4 changes: 0 additions & 4 deletions railway.yml

This file was deleted.

13 changes: 9 additions & 4 deletions tests/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func SetUpDatabaseConnection() *gorm.DB {
if getenv("APP_ENV") != constants.ENUM_RUN_PRODUCTION {
err := godotenv("../.env")
if err != nil {
panic(err)
panic("Error loading .env file: " + err.Error())
}
}

Expand All @@ -32,20 +32,25 @@ func SetUpDatabaseConnection() *gorm.DB {
dbName = getenv("DB_NAME")
dbPort = getenv("DB_PORT")

if dbUser == "" || dbPass == "" || dbHost == "" || dbName == "" || dbPort == "" {
panic("Missing required environment variables")
}

dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v TimeZone=Asia/Jakarta", dbHost, dbUser, dbPass, dbName, dbPort)

db, err := gorm.Open(postgres.New(postgres.Config{
DSN: dsn,
PreferSimpleProtocol: true,
}), &gorm.Config{})
if err != nil {
panic(err)
panic("Failed to connect to database: " + err.Error())
}

return db
}

func Test_DBConnection(t *testing.T) {
db := SetUpDatabaseConnection()
assert.Equal(t, db.Error, nil, "Success Connect to Database")
assert.NotNil(t, db)
assert.NoError(t, db.Error, "Expected no error during database connection")
assert.NotNil(t, db, "Expected a non-nil database connection")
}
71 changes: 45 additions & 26 deletions tests/user_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -30,47 +31,65 @@ func SetupControllerUser() controller.UserController {
return userController
}

func InsertTestUser() ([]entity.User, error) {
db := SetUpDatabaseConnection()
users := []entity.User{
{
Name: "admin",
Email: "admin1234@gmail.com",
},
{
Name: "user",
Email: "user1234@gmail.com",
},
}

for _, user := range users {
if err := db.Create(&user).Error; err != nil {
return nil, err
}
}

return users, nil
}

func Test_GetAllUser_OK(t *testing.T) {
r := SetUpRoutes()
userController := SetupControllerUser()
r.GET("/api/user", userController.GetAllUser)

expectedUsers, err := InsertTestUser()
if err != nil {
t.Fatalf("Failed to insert test users: %v", err)
}

req, _ := http.NewRequest(http.MethodGet, "/api/user", nil)
w := httptest.NewRecorder()

r.ServeHTTP(w, req)

users := []entity.User{
{
Name: "testing",
Email: "testing1@gmail.com",
},
{
Name: "testing2",
Email: "testing2@gmail.com",
},
assert.Equal(t, http.StatusOK, w.Code)

type Response struct {
Data []entity.User `json:"data"`
}

expectedUsers, err := InsertTestBook()
var response Response
err = json.Unmarshal(w.Body.Bytes(), &response)
if err != nil {
t.Error(err)
t.Fatalf("Failed to unmarshal response body: %v", err)
}

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, users, expectedUsers, "Success Get All User")
}
actualUsers := response.Data

func InsertTestBook() ([]entity.User, error) {
user := []entity.User{
{
Name: "testing",
Email: "testing1@gmail.com",
},
{
Name: "testing2",
Email: "testing2@gmail.com",
},
for _, expectedUser := range expectedUsers {
found := false
for _, actualUser := range actualUsers {
if expectedUser.Name == actualUser.Name && expectedUser.Email == actualUser.Email {
found = true
break
}
}
assert.True(t, found, "Expected user not found in actual users: %v", expectedUser)
}

return user, nil
}

0 comments on commit a2f814a

Please sign in to comment.