mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
943818f7d4
将 OpenFlare 与平台业务的数据访问从 model 与 apps 直连迁入 repository, model 仅保留实体与无 IO 规则;补充 code-check 架构守卫与开发规范。
105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package stats
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/model"
|
|
"github.com/Rain-kl/Wavelet/internal/repository"
|
|
"github.com/Rain-kl/Wavelet/internal/testhelper"
|
|
)
|
|
|
|
func TestApplyUploadStatsDeltaTxWithinTransaction(t *testing.T) {
|
|
_, _, cleanup := testhelper.SetupTestEnvironment(t)
|
|
defer cleanup()
|
|
ctx := context.Background()
|
|
|
|
upload := &model.Upload{
|
|
ID: 42002,
|
|
FileSize: 256,
|
|
MimeType: "image/jpeg",
|
|
Extension: "jpg",
|
|
Type: "avatar",
|
|
Status: model.UploadStatusUsed,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
|
|
if err := repository.RunInTransaction(ctx, func(tx *gorm.DB) error {
|
|
return ApplyUploadStatsDeltaTx(tx, upload, 1)
|
|
}); err != nil {
|
|
t.Fatalf("ApplyUploadStatsDeltaTx returned error: %v", err)
|
|
}
|
|
|
|
stats, err := loadUploadStats(ctx)
|
|
if err != nil {
|
|
t.Fatalf("loadUploadStats returned error: %v", err)
|
|
}
|
|
if stats.TotalCount != 1 || stats.TotalSize != 256 {
|
|
t.Fatalf("unexpected total stats: count=%d size=%d", stats.TotalCount, stats.TotalSize)
|
|
}
|
|
}
|
|
|
|
func TestApplyUploadStatsAddAndRemove(t *testing.T) {
|
|
_, _, cleanup := testhelper.SetupTestEnvironment(t)
|
|
defer cleanup()
|
|
ctx := context.Background()
|
|
|
|
upload := &model.Upload{
|
|
ID: 42001,
|
|
FileSize: 128,
|
|
MimeType: "image/png",
|
|
Extension: "png",
|
|
Type: "avatar",
|
|
Status: model.UploadStatusUsed,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
if err := ApplyUploadStatsAdd(ctx, upload); err != nil {
|
|
t.Fatalf("ApplyUploadStatsAdd returned error: %v", err)
|
|
}
|
|
|
|
stats, err := loadUploadStats(ctx)
|
|
if err != nil {
|
|
t.Fatalf("loadUploadStats returned error: %v", err)
|
|
}
|
|
if stats.TotalCount != 1 || stats.TotalSize != 128 {
|
|
t.Fatalf("unexpected total stats: count=%d size=%d", stats.TotalCount, stats.TotalSize)
|
|
}
|
|
|
|
if err := ApplyUploadStatsRemove(ctx, upload); err != nil {
|
|
t.Fatalf("ApplyUploadStatsRemove returned error: %v", err)
|
|
}
|
|
|
|
stats, err = loadUploadStats(ctx)
|
|
if err != nil {
|
|
t.Fatalf("loadUploadStats after remove returned error: %v", err)
|
|
}
|
|
if stats.TotalCount != 0 || stats.TotalSize != 0 {
|
|
t.Fatalf("expected zeroed total stats, got count=%d size=%d", stats.TotalCount, stats.TotalSize)
|
|
}
|
|
}
|
|
|
|
type uploadStatsSnapshot struct {
|
|
TotalCount int64
|
|
TotalSize int64
|
|
}
|
|
|
|
func loadUploadStats(ctx context.Context) (uploadStatsSnapshot, error) {
|
|
rows, err := repository.ListUploadStatsByDimension(ctx, model.UploadStatDimensionTotal)
|
|
if err != nil {
|
|
return uploadStatsSnapshot{}, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return uploadStatsSnapshot{}, nil
|
|
}
|
|
return uploadStatsSnapshot{
|
|
TotalCount: rows[0].FileCount,
|
|
TotalSize: rows[0].FileSize,
|
|
}, nil
|
|
}
|