@@ -54,6 +54,60 @@ func TestFindUsageInRange(t *testing.T) {
5454 require .Equal (t , []db.Usage {entryInside }, listResult )
5555}
5656
57+ func TestFindUsageInRangeByUser (t * testing.T ) {
58+ conn := dbtest .ConnectForTests (t )
59+
60+ start := time .Date (2022 , 7 , 1 , 0 , 0 , 0 , 0 , time .UTC )
61+ end := time .Date (2022 , 8 , 1 , 0 , 0 , 0 , 0 , time .UTC )
62+ userID := uuid .New ()
63+
64+ attributionID := db .NewTeamAttributionID (uuid .New ().String ())
65+
66+ entryBefore := dbtest .NewUsage (t , withUserId (userID , db.Usage {
67+ AttributionID : attributionID ,
68+ EffectiveTime : db .NewVarCharTime (start .Add (- 1 * 23 * time .Hour )),
69+ Draft : true ,
70+ }))
71+
72+ entryInside := dbtest .NewUsage (t , withUserId (userID , db.Usage {
73+ AttributionID : attributionID ,
74+ EffectiveTime : db .NewVarCharTime (start .Add (2 * time .Minute )),
75+ }))
76+
77+ entryInsideOtherUser := dbtest .NewUsage (t , withUserId (uuid .New (), db.Usage {
78+ AttributionID : attributionID ,
79+ EffectiveTime : db .NewVarCharTime (start .Add (2 * time .Minute )),
80+ }))
81+
82+ entryAfter := dbtest .NewUsage (t , withUserId (userID , db.Usage {
83+ AttributionID : attributionID ,
84+ EffectiveTime : db .NewVarCharTime (end .Add (2 * time .Hour )),
85+ }))
86+
87+ usageEntries := []db.Usage {entryBefore , entryInside , entryInsideOtherUser , entryAfter }
88+ dbtest .CreateUsageRecords (t , conn , usageEntries ... )
89+ listResult , err := db .FindUsage (context .Background (), conn , & db.FindUsageParams {
90+ AttributionId : attributionID ,
91+ UserID : userID ,
92+ From : start ,
93+ To : end ,
94+ })
95+ require .NoError (t , err )
96+
97+ require .Equal (t , 1 , len (listResult ))
98+ require .Equal (t , entryInside .ID , listResult [0 ].ID )
99+
100+ summary , err := db .GetUsageSummary (context .Background (), conn , db.GetUsageSummaryParams {
101+ AttributionId : attributionID ,
102+ UserID : userID ,
103+ From : start ,
104+ To : end ,
105+ })
106+ require .NoError (t , err )
107+ require .Equal (t , entryInside .CreditCents , summary .CreditCentsUsed )
108+
109+ }
110+
57111func TestGetUsageSummary (t * testing.T ) {
58112 conn := dbtest .ConnectForTests (t )
59113
@@ -335,6 +389,13 @@ func TestListBalance(t *testing.T) {
335389 })
336390}
337391
392+ func withUserId (id uuid.UUID , usage db.Usage ) db.Usage {
393+ usage .SetCreditNoteMetaData (db.CreditNoteMetaData {
394+ UserID : id .String (),
395+ })
396+ return usage
397+ }
398+
338399func TestGetBalance (t * testing.T ) {
339400 teamAttributionID := db .NewTeamAttributionID (uuid .New ().String ())
340401 teamAttributionID2 := db .NewTeamAttributionID (uuid .New ().String ())
0 commit comments