81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
package rechargeagency
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"chatapp3-golang/internal/config"
|
|
"chatapp3-golang/internal/model"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func newRechargeAgencyProfileTestService(t *testing.T) *Service {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.RechargeAgencySellerInfo{}); err != nil {
|
|
t.Fatalf("auto migrate: %v", err)
|
|
}
|
|
return NewService(config.Config{}, db, nil)
|
|
}
|
|
|
|
func TestSellerProfileWhatsappUpsert(t *testing.T) {
|
|
service := newRechargeAgencyProfileTestService(t)
|
|
user := AuthUser{
|
|
UserID: 2042274349343506434,
|
|
SysOrigin: "likei",
|
|
}
|
|
|
|
empty, err := service.GetProfile(context.Background(), user)
|
|
if err != nil {
|
|
t.Fatalf("GetProfile() error = %v", err)
|
|
}
|
|
if empty.SysOrigin != "LIKEI" || empty.UserID != user.UserID || empty.Whatsapp != "" {
|
|
t.Fatalf("empty profile = %+v", empty)
|
|
}
|
|
|
|
saved, err := service.UpdateWhatsapp(context.Background(), user, UpdateWhatsappRequest{Whatsapp: " +8613800138000 "})
|
|
if err != nil {
|
|
t.Fatalf("UpdateWhatsapp() error = %v", err)
|
|
}
|
|
if saved.Whatsapp != "+8613800138000" {
|
|
t.Fatalf("saved whatsapp = %q", saved.Whatsapp)
|
|
}
|
|
|
|
updated, err := service.UpdateWhatsapp(context.Background(), user, UpdateWhatsappRequest{Whatsapp: "+8613900139000"})
|
|
if err != nil {
|
|
t.Fatalf("second UpdateWhatsapp() error = %v", err)
|
|
}
|
|
if updated.Whatsapp != "+8613900139000" {
|
|
t.Fatalf("updated whatsapp = %q", updated.Whatsapp)
|
|
}
|
|
|
|
got, err := service.GetProfile(context.Background(), user)
|
|
if err != nil {
|
|
t.Fatalf("GetProfile() after save error = %v", err)
|
|
}
|
|
if got.Whatsapp != "+8613900139000" {
|
|
t.Fatalf("got whatsapp = %q", got.Whatsapp)
|
|
}
|
|
}
|
|
|
|
func TestUpdateWhatsappValidatesInput(t *testing.T) {
|
|
service := newRechargeAgencyProfileTestService(t)
|
|
user := AuthUser{
|
|
UserID: 7,
|
|
SysOrigin: "LIKEI",
|
|
}
|
|
|
|
if _, err := service.UpdateWhatsapp(context.Background(), user, UpdateWhatsappRequest{}); err == nil {
|
|
t.Fatal("UpdateWhatsapp() empty error is nil")
|
|
}
|
|
if _, err := service.UpdateWhatsapp(context.Background(), user, UpdateWhatsappRequest{Whatsapp: strings.Repeat("1", 65)}); err == nil {
|
|
t.Fatal("UpdateWhatsapp() long error is nil")
|
|
}
|
|
}
|