Skip to content

Commit

Permalink
Merge pull request #110 from Kos-Kita/testingkos
Browse files Browse the repository at this point in the history
Testingkos
  • Loading branch information
lendral3n authored Feb 14, 2024
2 parents a4019f5 + 902d40c commit 70a8577
Show file tree
Hide file tree
Showing 17 changed files with 1,800 additions and 22 deletions.
4 changes: 0 additions & 4 deletions features/booking/handler/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ func RequestToCoreBook(input BookRequest, userIdLogin uint) booking.BookingCore
BoardingHouseId: input.BoardingHouseId,
Method: input.Method,
Bank: input.Bank,
// Payment: booking.PaymentCore{
// Method: input.Method,
// Bank: input.Bank,
// },
}
}

Expand Down
32 changes: 15 additions & 17 deletions features/booking/handler/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,24 @@ import (
)

type BookingResponse struct {
Code string `json:"booking_code,omitempty"`
Status string `json:"status,omitempty"`
Total float64 `json:"total,omitempty"`
PaymentMethod string `json:"payment_method,omitempty"`
PaymentBank string `json:"bank,omitempty"`
PaymentVirtualNumber string `json:"virtual_number,omitempty"`
PaymentBillKey string `json:"key_bill,omitempty"`
PaymentBillCode string `json:"code_bill,omitempty"`
PaymentExpiredAt *time.Time `json:"payment_expired,omitempty"`
Code string `json:"booking_code"`
Status string `json:"status"`
Total float64 `json:"total"`
PaymentMethod string `json:"payment_method"`
PaymentBank string `json:"bank"`
PaymentVirtualNumber string `json:"virtual_number"`
PaymentExpiredAt *time.Time `json:"payment_expired"`
}

type BookingHistoryResponse struct {
KosId uint `json:"kos_id,omitempty"`
KosName string `json:"kos_name,omitempty"`
KosFasilitas []string `json:"kos_fasilitas,omitempty"`
KosLokasi string `json:"kos_lokasi,omitempty"`
KosRating float64 `json:"kos_rating,omitempty"`
KosMainFoto string `json:"kos_main_foto,omitempty"`
PaymentStatus string `json:"payment_status,omitempty"`
TotalHarga float64 `json:"total_harga,omitempty"`
KosId uint `json:"kos_id"`
KosName string `json:"kos_name"`
KosFasilitas []string `json:"kos_fasilitas"`
KosLokasi string `json:"kos_lokasi"`
KosRating float64 `json:"kos_ratingy"`
KosMainFoto string `json:"kos_main_foto"`
PaymentStatus string `json:"payment_status"`
TotalHarga float64 `json:"total_harga"`
}

func CoreToResponseBook(core *booking.BookingCore) BookingResponse {
Expand Down
1 change: 0 additions & 1 deletion features/kos/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Core struct {
User user.Core
}


type CoreInput struct {
ID uint
Name string `validate:"required"`
Expand Down
138 changes: 138 additions & 0 deletions features/kos/service/logic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package service

import (
"KosKita/features/kos"
"KosKita/features/user"
"KosKita/mocks"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreate(t *testing.T) {
repo := new(mocks.KosDataInterface)
srvUser := new(mocks.UserServiceInterface)
srv := New(repo, srvUser)

returnData := kos.CoreInput{
UserID: 1,
Name: "KOS",
Description: "kos ini murah sekali",
Category: "putra",
Price: 100000,
Rooms: 10,
Address: "Jl. Kost Murah No.1",
Longitude: "123.456",
Latitude: "98.768",
KosFacilities: []kos.KosFacilityCore{
{
Facility: "AC",
},
{
Facility: "TV",
},
},
KosRules: []kos.KosRuleCore{
{
Rule: "Dilarang Merokok",
},
{
Rule: "Dilarang Membawa Hewan Peliharaan",
},
},
}

userIdLogin := int(returnData.UserID)

t.Run("user is not owner", func(t *testing.T) {
srvUser.On("GetById", userIdLogin).Return(&user.Core{Role: "renter"}, nil).Once()

kosId, err := srv.Create(userIdLogin, returnData)

assert.Error(t, err)
assert.Equal(t, uint(0), kosId)
assert.EqualError(t, err, "anda bukan owner")
})

t.Run("name is empty", func(t *testing.T) {
srvUser.On("GetById", userIdLogin).Return(&user.Core{Role: "owner"}, nil).Once()

returnData.Name = ""
_, err := srv.Create(userIdLogin, returnData)

assert.Error(t, err)
assert.EqualError(t, err, "name anda kosong")
returnData.Name = "Kos Murah"
})

t.Run("category is empty", func(t *testing.T) {
srvUser.On("GetById", userIdLogin).Return(&user.Core{Role: "owner"}, nil).Once()

returnData.Category = ""
_, err := srv.Create(userIdLogin, returnData)

assert.Error(t, err)
assert.EqualError(t, err, "category anda kosong")
returnData.Category = "putra"
})

t.Run("price is zero or less", func(t *testing.T) {
srvUser.On("GetById", userIdLogin).Return(&user.Core{Role: "owner"}, nil).Once()

returnData.Price = 0
_, err := srv.Create(userIdLogin, returnData)

assert.Error(t, err)
assert.EqualError(t, err, "harga anda kosong")
returnData.Price = 1000000
})

t.Run("rooms is zero or less", func(t *testing.T) {
srvUser.On("GetById", userIdLogin).Return(&user.Core{Role: "owner"}, nil).Once()

returnData.Rooms = 0
_, err := srv.Create(userIdLogin, returnData)

assert.Error(t, err)
assert.EqualError(t, err, "rooms anda kosong")
returnData.Rooms = 10
})

t.Run("address is empty", func(t *testing.T) {
srvUser.On("GetById", userIdLogin).Return(&user.Core{Role: "owner"}, nil).Once()

returnData.Address = ""
_, err := srv.Create(userIdLogin, returnData)

assert.Error(t, err)
assert.EqualError(t, err, "alamat anda kosong")
returnData.Address = "Jl. Kost Murah No.1"
})

t.Run("error from repository", func(t *testing.T) {
srvUser.On("GetById", userIdLogin).Return(&user.Core{Role: "owner"}, nil)
repo.On("Insert", userIdLogin, returnData).Return(uint(0), errors.New("database error")).Once()

kosId, err := srv.Create(userIdLogin, returnData)

assert.Error(t, err)
assert.NotNil(t, kosId)
assert.Equal(t, uint(0), kosId)
assert.EqualError(t, err, "database error")

repo.AssertExpectations(t)
})

t.Run("success", func(t *testing.T) {
srvUser.On("GetById", userIdLogin).Return(&user.Core{Role: "owner"}, nil)
repo.On("Insert", userIdLogin, returnData).Return(returnData.ID, nil).Once()

kosId, err := srv.Create(userIdLogin, returnData)

assert.NoError(t, err)
assert.Equal(t, returnData.ID, kosId)

repo.AssertExpectations(t)
})
}
56 changes: 56 additions & 0 deletions mocks/AdminServiceInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 70a8577

Please sign in to comment.