Files
online-backend/internal/cards/visamaster/db/postgresql_get_test.go

87 lines
2.4 KiB
Go

package db
import (
"context"
"testing"
"time"
"online-back/internal/cards/visamaster"
"online-back/internal/config"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
)
func testRepo(t *testing.T) (visamaster.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 TestGetByIDIncludesBranchAndUser(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.Branch.Id != item.BranchId {
t.Fatalf("branch.id=%d does not match branchId=%d", item.Branch.Id, item.BranchId)
}
if item.Branch.Name.Tm == "" && item.Branch.Name.En == "" {
t.Fatalf("branchId=%d but branch.name is empty: %+v", item.BranchId, item.Branch.Name)
}
if item.UserId != nil {
if item.User == nil {
t.Fatalf("userId=%d but user object is nil", *item.UserId)
}
if item.User.Name == "" && item.User.Username == "" {
t.Fatalf("userId=%d but user name/username are empty", *item.UserId)
}
}
t.Logf("id=%d branchId=%d branch=%q userId=%v user=%v",
item.Id, item.BranchId, item.Branch.Name.Tm, item.UserId, item.User)
}
func TestListIncludesBranchAndUser(t *testing.T) {
repo, closeRepo := testRepo(t)
defer closeRepo()
list, err := repo.List(context.Background(), visamaster.ListFilter{Page: 1, Limit: 10})
if err != nil {
t.Fatalf("List: %v", err)
}
if list.Count == 0 || len(list.VisaMasterPaymentOrders) == 0 {
t.Fatal("List returned no orders")
}
for _, item := range list.VisaMasterPaymentOrders {
if item.BranchId != 0 && item.Branch.Name.Tm == "" && item.Branch.Name.En == "" {
t.Errorf("order %d branchId=%d missing branch.name", item.Id, item.BranchId)
}
if item.UserId != nil && (item.User == nil || (item.User.Name == "" && item.User.Username == "")) {
t.Errorf("order %d userId=%d missing user details", item.Id, *item.UserId)
}
}
}