@@ -18,12 +18,15 @@ func costItem(day string, amount float32, currency string) oci.CostItem {
|
||||
}
|
||||
|
||||
func TestRunCostTaskSkipsFreeAndUpserts(t *testing.T) {
|
||||
// 日期取相对当前的近两天:任务查近 7 天,窗口外的行会被 Costs 过滤
|
||||
day1 := time.Now().UTC().AddDate(0, 0, -2).Format("2006-01-02")
|
||||
day2 := time.Now().UTC().AddDate(0, 0, -1).Format("2006-01-02")
|
||||
client := &fakeClient{
|
||||
tenancy: oci.TenancyInfo{Name: "acme", HomeRegionKey: "FRA"},
|
||||
costItems: []oci.CostItem{
|
||||
costItem("2026-07-01", 5.2, "USD"),
|
||||
costItem("2026-07-01", 1.4, "USD"),
|
||||
costItem("2026-07-02", 6.3, "USD"),
|
||||
costItem(day1, 5.2, "USD"),
|
||||
costItem(day1, 1.4, "USD"),
|
||||
costItem(day2, 6.3, "USD"),
|
||||
},
|
||||
}
|
||||
tasks, configs, db := newTaskEnv(t, client)
|
||||
@@ -55,13 +58,13 @@ func TestRunCostTaskSkipsFreeAndUpserts(t *testing.T) {
|
||||
t.Errorf("log = %+v, want synced 1 skipped 1", entry)
|
||||
}
|
||||
|
||||
assertCostSnapshots(t, db, map[string]float64{"2026-07-01": 6.6, "2026-07-02": 6.3})
|
||||
assertCostSnapshots(t, db, map[string]float64{day1: 6.6, day2: 6.3})
|
||||
|
||||
// 再次执行为覆盖更新,不产生重复行
|
||||
if _, err := tasks.RunTaskNow(ctx, task.ID); err != nil {
|
||||
t.Fatalf("RunTaskNow again: %v", err)
|
||||
}
|
||||
assertCostSnapshots(t, db, map[string]float64{"2026-07-01": 6.6, "2026-07-02": 6.3})
|
||||
assertCostSnapshots(t, db, map[string]float64{day1: 6.6, day2: 6.3})
|
||||
}
|
||||
|
||||
// assertCostSnapshots 断言快照表恰好为 want 中的日期与金额(配置 #1)。
|
||||
|
||||
Reference in New Issue
Block a user