Files
online-backend/internal/lending/loanpaidletter/db/postgresql_get_test.go
T

94 lines
2.7 KiB
Go

package db
import (
"context"
"testing"
"time"
"online-back/internal/config"
"online-back/internal/lending/loanpaidletter"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
)
func testRepo(t *testing.T) (loanpaidletter.Repository, func()) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
t.Cleanup(cancel)
pool, err := postgresql.NewClient(ctx, 2, config.StorageConfig{
Host: "localhost",
Port: "5424",
Database: "bank_db",
Username: "bank_admin",
Password: "bank@#23#@_password",
PgPoolMaxConn: 4,
})
if err != nil {
t.Skipf("postgres unavailable: %v", err)
}
return NewRepository(pool, logging.GetLogger()), func() { pool.Close() }
}
func TestGetByIDIncludesNamesNextToIDs(t *testing.T) {
repo, closeRepo := testRepo(t)
defer closeRepo()
ctx := context.Background()
item, err := repo.GetByID(ctx, 1)
if err != nil {
t.Fatalf("GetByID: %v", err)
}
if item.BranchId == 0 {
t.Fatal("branchId is empty")
}
if item.BranchName.Tm == "" && item.BranchName.En == "" {
t.Fatalf("branchId=%d but branchName is empty: %+v", item.BranchId, item.BranchName)
}
if item.BranchUniqueCode == "" {
t.Fatalf("branchId=%d but branchUniqueCode is empty", item.BranchId)
}
if item.ProvinceId == nil || *item.ProvinceId == 0 {
t.Fatal("provinceId is empty")
}
if item.ProvinceName.Tm == "" && item.ProvinceName.En == "" {
t.Fatalf("provinceId=%d but provinceName is empty: %+v", *item.ProvinceId, item.ProvinceName)
}
if item.RegionName.Tm == "" && item.RegionName.En == "" {
t.Fatalf("region=%q but regionName is empty: %+v", item.Region, item.RegionName)
}
if item.UserId != nil {
if item.UserName == "" && item.UserUsername == "" {
t.Fatalf("userId=%d but userName and userUsername are empty", *item.UserId)
}
}
t.Logf("id=%d branchId=%d branch=%q/%s provinceId=%d province=%q userId=%v user=%q",
item.Id, item.BranchId, item.BranchName.Tm, item.BranchUniqueCode,
*item.ProvinceId, item.ProvinceName.Tm, item.UserId, item.UserName)
}
func TestListIncludesNamesNextToIDs(t *testing.T) {
repo, closeRepo := testRepo(t)
defer closeRepo()
list, err := repo.List(context.Background(), loanpaidletter.ListFilter{Page: 1, Limit: 10})
if err != nil {
t.Fatalf("List: %v", err)
}
if list.Count == 0 || len(list.LoanPaidLetters) == 0 {
t.Fatal("List returned no letters")
}
for _, item := range list.LoanPaidLetters {
if item.BranchId != 0 && item.BranchName.Tm == "" && item.BranchName.En == "" {
t.Errorf("letter %d branchId=%d missing branchName", item.Id, item.BranchId)
}
if item.UserId != nil && item.UserName == "" && item.UserUsername == "" {
t.Errorf("letter %d userId=%d missing userName", item.Id, *item.UserId)
}
}
}