@@ -17,6 +17,7 @@ func TestAccountBillStore_List(t *testing.T) {
1717 ctx := context .TODO ()
1818
1919 dt := time .Date (2022 , 11 , 22 , 3 , 0 , 0 , 0 , time .UTC )
20+
2021 bills := []database.AccountBill {
2122 {
2223 // included
@@ -88,13 +89,105 @@ func TestAccountBillStore_List(t *testing.T) {
8889 require .Nil (t , err )
8990 require .Equal (t , 2 , len (res .Data ))
9091 expectedData := []types.ITEM {
91- {Consumption : 21 , InstanceName : "c2" , Value : 20 , PromptToken : 0 , CompletionToken : 0 },
9292 {Consumption : 31 , InstanceName : "c1" , Value : 28 , PromptToken : 0 , CompletionToken : 0 },
93+ {Consumption : 21 , InstanceName : "c2" , Value : 20 , PromptToken : 0 , CompletionToken : 0 },
9394 }
9495 require .Equal (t , expectedData , res .Data )
9596
9697}
9798
99+ func TestAccountBillStore_ListWithDeployJoin (t * testing.T ) {
100+ db := tests .InitTestDB ()
101+ defer db .Close ()
102+ ctx := context .TODO ()
103+
104+ dt := time .Date (2022 , 11 , 22 , 3 , 0 , 0 , 0 , time .UTC )
105+
106+ deploys := []database.Deploy {
107+ {
108+ SvcName : "c2" ,
109+ SpaceID : 1 ,
110+ Status : 1 ,
111+ GitPath : "path" ,
112+ GitBranch : "main" ,
113+ Template : "tpl" ,
114+ Hardware : "hw" ,
115+ UserID : 1 ,
116+ },
117+ {
118+ SvcName : "c1" ,
119+ SpaceID : 2 ,
120+ Status : 1 ,
121+ GitPath : "path" ,
122+ GitBranch : "main" ,
123+ Template : "tpl" ,
124+ Hardware : "hw" ,
125+ UserID : 2 ,
126+ },
127+ }
128+ _ , err := db .Operator .Core .NewInsert ().Model (& deploys ).Exec (ctx )
129+ require .Nil (t , err )
130+
131+ // Give deploys distinct created_at values so d.created_at DESC is deterministic
132+ _ , err = db .Operator .Core .NewUpdate ().
133+ Model ((* database .Deploy )(nil )).
134+ Set ("created_at = ?" , dt .Add (2 * 24 * time .Hour )).
135+ Where ("svc_name = 'c2'" ).
136+ Exec (ctx )
137+ require .Nil (t , err )
138+ _ , err = db .Operator .Core .NewUpdate ().
139+ Model ((* database .Deploy )(nil )).
140+ Set ("created_at = ?" , dt .Add (1 * 24 * time .Hour )).
141+ Where ("svc_name = 'c1'" ).
142+ Exec (ctx )
143+ require .Nil (t , err )
144+
145+ bills := []database.AccountBill {
146+ {
147+ UserUUID : "foo" , Value : 3 , Consumption : 4 ,
148+ BillDate : dt .Add (- 3 * 24 * time .Hour ), CustomerID : "c1" ,
149+ Scene : types .SceneSpace ,
150+ },
151+ {
152+ UserUUID : "foo" , Value : 5 , Consumption : 6 ,
153+ BillDate : dt .Add (- 1 * 24 * time .Hour ), CustomerID : "c1" ,
154+ Scene : types .SceneSpace ,
155+ },
156+ {
157+ UserUUID : "foo" , Value : 20 , Consumption : 21 ,
158+ BillDate : dt .Add (2 * 24 * time .Hour ), CustomerID : "c1" ,
159+ Scene : types .SceneSpace ,
160+ },
161+ {
162+ UserUUID : "foo" , Value : 20 , Consumption : 21 ,
163+ BillDate : dt .Add (3 * 24 * time .Hour ), CustomerID : "c2" ,
164+ Scene : types .SceneSpace ,
165+ },
166+ }
167+ _ , err = db .Operator .Core .NewInsert ().Model (& bills ).Exec (ctx )
168+ require .Nil (t , err )
169+
170+ store := database .NewAccountBillStoreWithDB (db )
171+ res , err := store .ListByUserIDAndDate (ctx , types.AcctBillsReq {
172+ TargetUUID : "foo" ,
173+ Scene : types .SceneSpace ,
174+ StartDate : dt .Add (- 5 * 24 * time .Hour ).Format (time .RFC3339 ),
175+ EndDate : dt .Add (5 * 24 * time .Hour ).Format (time .RFC3339 ),
176+ Per : 20 ,
177+ Page : 1 ,
178+ })
179+ require .Nil (t , err )
180+ require .Equal (t , 2 , len (res .Data ))
181+ // c2 deploy has later created_at, so d.created_at DESC puts c2 first
182+ require .Equal (t , "c2" , res .Data [0 ].InstanceName )
183+ require .Equal (t , float64 (21 ), res .Data [0 ].Consumption )
184+ require .Equal (t , float64 (20 ), res .Data [0 ].Value )
185+ require .Equal (t , "c1" , res .Data [1 ].InstanceName )
186+ require .Equal (t , float64 (31 ), res .Data [1 ].Consumption )
187+ require .Equal (t , float64 (28 ), res .Data [1 ].Value )
188+
189+ }
190+
98191func TestAccountBillStore_ListBillsDetailByUserID (t * testing.T ) {
99192 db := tests .InitTestDB ()
100193 defer db .Close ()
0 commit comments