230 lines
7.8 KiB
Go
230 lines
7.8 KiB
Go
package trafficusage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
g.Log().SetWriter(io.Discard)
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
type upstreamStub struct {
|
|
dailyCalls int
|
|
monthlyCalls int
|
|
dailyOutput []model.SimBaseRes[model.SimDataUsageDailyOutputItem]
|
|
monthlyOutput []model.SimBaseRes[model.SimDataUsageMonthlyOutputItem]
|
|
dailyErr error
|
|
monthlyErr error
|
|
}
|
|
|
|
func (s *upstreamStub) QuerySimDatausageDaily(_ context.Context, _ *model.SimDataUsageDailyInput) ([]model.SimBaseRes[model.SimDataUsageDailyOutputItem], error) {
|
|
s.dailyCalls++
|
|
return s.dailyOutput, s.dailyErr
|
|
}
|
|
|
|
func (s *upstreamStub) QuerySimDatausageMonthly(_ context.Context, _ *model.SimDataUsageMonthlyInput) ([]model.SimBaseRes[model.SimDataUsageMonthlyOutputItem], error) {
|
|
s.monthlyCalls++
|
|
return s.monthlyOutput, s.monthlyErr
|
|
}
|
|
|
|
type repositoryStub struct {
|
|
dailyCalls int
|
|
monthlyCalls int
|
|
dailyItems []model.TrafficUsageDailyItem
|
|
monthlyItems []model.TrafficUsageMonthlyItem
|
|
dailyErr error
|
|
monthlyErr error
|
|
}
|
|
|
|
func (s *repositoryStub) UpdateDaily(_ context.Context, items []model.TrafficUsageDailyItem) error {
|
|
s.dailyCalls++
|
|
s.dailyItems = items
|
|
return s.dailyErr
|
|
}
|
|
|
|
func (s *repositoryStub) UpdateMonthly(_ context.Context, items []model.TrafficUsageMonthlyItem) error {
|
|
s.monthlyCalls++
|
|
s.monthlyItems = items
|
|
return s.monthlyErr
|
|
}
|
|
|
|
func TestRetryDailySuccess(t *testing.T) {
|
|
upstream := &upstreamStub{
|
|
dailyOutput: []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{
|
|
{
|
|
SimKey: &model.SimKey{ICCID: "89860001"},
|
|
ResData: model.SimDataUsageDailyOutputItem{QueryDate: "20260714", DataAmount: 12.5},
|
|
},
|
|
},
|
|
}
|
|
repository := &repositoryStub{}
|
|
service := New(upstream, repository)
|
|
message := model.TrafficUsageDailyRetryMessage{
|
|
SimKey: &model.SimKey{ICCID: "89860001"},
|
|
Card: model.TrafficUsageSimCard{Iccid: "89860001", PackageCode: "PKG-1"},
|
|
QueryDate: "20260714",
|
|
}
|
|
|
|
if err := service.RetryDaily(context.Background(), message); err != nil {
|
|
t.Fatalf("RetryDaily returned error: %v", err)
|
|
}
|
|
if upstream.dailyCalls != 1 || repository.dailyCalls != 1 {
|
|
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.dailyCalls, repository.dailyCalls)
|
|
}
|
|
if len(repository.dailyItems) != 1 {
|
|
t.Fatalf("expected one daily item, got %d", len(repository.dailyItems))
|
|
}
|
|
item := repository.dailyItems[0]
|
|
if item.Iccid != "89860001" || item.UsageMb != 12.5 || item.PackageCode != "PKG-1" {
|
|
t.Fatalf("unexpected daily item: %+v", item)
|
|
}
|
|
if got := item.Date.Format("20060102"); got != "20260714" {
|
|
t.Fatalf("unexpected daily date: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestRetryDailyPreservesRetryBehavior(t *testing.T) {
|
|
t.Run("period mismatch", func(t *testing.T) {
|
|
upstream := &upstreamStub{dailyOutput: []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{{SimKey: &model.SimKey{ICCID: "89860001"}, ResData: model.SimDataUsageDailyOutputItem{QueryDate: "20260713", DataAmount: 1}}}}
|
|
repository := &repositoryStub{}
|
|
service := New(upstream, repository)
|
|
if err := service.RetryDaily(context.Background(), validDailyMessage()); err == nil {
|
|
t.Fatal("expected period mismatch failure")
|
|
}
|
|
if repository.dailyCalls != 0 {
|
|
t.Fatal("period mismatch was persisted")
|
|
}
|
|
})
|
|
t.Run("unsupported channel is permanent", func(t *testing.T) {
|
|
upstream := &upstreamStub{dailyOutput: []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{{SimKey: &model.SimKey{ICCID: "89860001"}, Code: 1, Message: "daily usage query is not supported"}}}
|
|
service := New(upstream, &repositoryStub{})
|
|
err := service.RetryDaily(context.Background(), validDailyMessage())
|
|
var failure *Failure
|
|
if !errors.As(err, &failure) || !failure.Permanent {
|
|
t.Fatalf("expected permanent failure, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("upstream failure", func(t *testing.T) {
|
|
upstream := &upstreamStub{dailyErr: errors.New("upstream failed")}
|
|
repository := &repositoryStub{}
|
|
service := New(upstream, repository)
|
|
|
|
if err := service.RetryDaily(context.Background(), validDailyMessage()); err == nil {
|
|
t.Fatal("expected retryable failure")
|
|
}
|
|
if upstream.dailyCalls != 1 || repository.dailyCalls != 0 {
|
|
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.dailyCalls, repository.dailyCalls)
|
|
}
|
|
})
|
|
|
|
t.Run("repository failure", func(t *testing.T) {
|
|
upstream := &upstreamStub{dailyOutput: validDailyOutput()}
|
|
repository := &repositoryStub{dailyErr: errors.New("repository failed")}
|
|
service := New(upstream, repository)
|
|
|
|
if err := service.RetryDaily(context.Background(), validDailyMessage()); err == nil {
|
|
t.Fatal("expected persistence failure")
|
|
}
|
|
if upstream.dailyCalls != 1 || repository.dailyCalls != 1 {
|
|
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.dailyCalls, repository.dailyCalls)
|
|
}
|
|
})
|
|
|
|
t.Run("invalid date", func(t *testing.T) {
|
|
upstream := &upstreamStub{}
|
|
repository := &repositoryStub{}
|
|
service := New(upstream, repository)
|
|
message := validDailyMessage()
|
|
message.QueryDate = "invalid"
|
|
|
|
if err := service.RetryDaily(context.Background(), message); err == nil {
|
|
t.Fatal("expected invalid date failure")
|
|
}
|
|
if upstream.dailyCalls != 0 || repository.dailyCalls != 0 {
|
|
t.Fatalf("invalid date reached dependencies")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestRetryMonthlySuccessAndRetry(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
upstream := &upstreamStub{
|
|
monthlyOutput: []model.SimBaseRes[model.SimDataUsageMonthlyOutputItem]{
|
|
{
|
|
SimKey: &model.SimKey{ICCID: "89860002"},
|
|
ResData: model.SimDataUsageMonthlyOutputItem{DeadLine: "202607", DataAmount: 25.5},
|
|
},
|
|
},
|
|
}
|
|
repository := &repositoryStub{}
|
|
service := New(upstream, repository)
|
|
message := model.TrafficUsageMonthlyRetryMessage{
|
|
SimKey: &model.SimKey{ICCID: "89860002"},
|
|
Card: model.TrafficUsageSimCard{Iccid: "89860002", PackageCode: "PKG-2"},
|
|
QueryDate: "202607",
|
|
}
|
|
|
|
if err := service.RetryMonthly(context.Background(), message); err != nil {
|
|
t.Fatalf("RetryMonthly returned error: %v", err)
|
|
}
|
|
if upstream.monthlyCalls != 1 || repository.monthlyCalls != 1 {
|
|
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.monthlyCalls, repository.monthlyCalls)
|
|
}
|
|
if len(repository.monthlyItems) != 1 {
|
|
t.Fatalf("expected one monthly item, got %d", len(repository.monthlyItems))
|
|
}
|
|
item := repository.monthlyItems[0]
|
|
if item.Iccid != "89860002" || item.UsageMb != 25.5 || item.PackageCode != "PKG-2" {
|
|
t.Fatalf("unexpected monthly item: %+v", item)
|
|
}
|
|
if got := item.Month.Format("200601"); got != "202607" {
|
|
t.Fatalf("unexpected month: %s", got)
|
|
}
|
|
})
|
|
|
|
t.Run("upstream failure", func(t *testing.T) {
|
|
upstream := &upstreamStub{monthlyErr: errors.New("upstream failed")}
|
|
repository := &repositoryStub{}
|
|
service := New(upstream, repository)
|
|
message := model.TrafficUsageMonthlyRetryMessage{
|
|
SimKey: &model.SimKey{ICCID: "89860002"},
|
|
Card: model.TrafficUsageSimCard{Iccid: "89860002"},
|
|
QueryDate: "202607",
|
|
}
|
|
|
|
if err := service.RetryMonthly(context.Background(), message); err == nil {
|
|
t.Fatal("expected retryable failure")
|
|
}
|
|
if upstream.monthlyCalls != 1 || repository.monthlyCalls != 0 {
|
|
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.monthlyCalls, repository.monthlyCalls)
|
|
}
|
|
})
|
|
}
|
|
|
|
func validDailyMessage() model.TrafficUsageDailyRetryMessage {
|
|
return model.TrafficUsageDailyRetryMessage{
|
|
SimKey: &model.SimKey{ICCID: "89860001"},
|
|
Card: model.TrafficUsageSimCard{Iccid: "89860001"},
|
|
QueryDate: "20260714",
|
|
}
|
|
}
|
|
|
|
func validDailyOutput() []model.SimBaseRes[model.SimDataUsageDailyOutputItem] {
|
|
return []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{
|
|
{
|
|
SimKey: &model.SimKey{ICCID: "89860001"},
|
|
ResData: model.SimDataUsageDailyOutputItem{QueryDate: "20260714", DataAmount: 1},
|
|
},
|
|
}
|
|
}
|