-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
"github.com/xmtp/xmtpd/pkg/testutils" | ||
"github.com/xmtp/xmtpd/pkg/utils" | ||
) | ||
|
||
func TestIncrementUnsettledUsage(t *testing.T) { | ||
ctx := context.Background() | ||
db, _, cleanup := testutils.NewDB(t, ctx) | ||
defer cleanup() | ||
|
||
querier := queries.New(db) | ||
payerId := testutils.RandomInt32() | ||
originatorId := testutils.RandomInt32() | ||
minutesSinceEpoch := utils.MinutesSinceEpochNow() | ||
|
||
require.NoError(t, querier.IncrementUnsettledUsage(ctx, queries.IncrementUnsettledUsageParams{ | ||
PayerID: payerId, | ||
OriginatorID: originatorId, | ||
MinutesSinceEpoch: minutesSinceEpoch, | ||
SpendPicodollars: 100, | ||
})) | ||
|
||
unsettledUsage, err := querier.GetPayerUnsettledUsage( | ||
ctx, | ||
queries.GetPayerUnsettledUsageParams{ | ||
PayerID: payerId, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, unsettledUsage, int64(100)) | ||
|
||
require.NoError(t, querier.IncrementUnsettledUsage(ctx, queries.IncrementUnsettledUsageParams{ | ||
PayerID: payerId, | ||
OriginatorID: originatorId, | ||
MinutesSinceEpoch: minutesSinceEpoch, | ||
SpendPicodollars: 100, | ||
})) | ||
|
||
unsettledUsage, err = querier.GetPayerUnsettledUsage( | ||
ctx, | ||
queries.GetPayerUnsettledUsageParams{ | ||
PayerID: payerId, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, unsettledUsage, int64(200)) | ||
} | ||
|
||
func TestGetUnsettledUsage(t *testing.T) { | ||
ctx := context.Background() | ||
db, _, cleanup := testutils.NewDB(t, ctx) | ||
defer cleanup() | ||
|
||
querier := queries.New(db) | ||
payerId := testutils.RandomInt32() | ||
originatorId := testutils.RandomInt32() | ||
|
||
addUsage := func(minutesSinceEpoch int32, spendPicodollars int64) { | ||
require.NoError( | ||
t, | ||
querier.IncrementUnsettledUsage(ctx, queries.IncrementUnsettledUsageParams{ | ||
PayerID: payerId, | ||
OriginatorID: originatorId, | ||
MinutesSinceEpoch: minutesSinceEpoch, | ||
SpendPicodollars: spendPicodollars, | ||
}), | ||
) | ||
} | ||
|
||
addUsage(1, 100) | ||
addUsage(2, 200) | ||
addUsage(3, 300) | ||
|
||
unsettledUsage, err := querier.GetPayerUnsettledUsage( | ||
ctx, | ||
queries.GetPayerUnsettledUsageParams{ | ||
PayerID: payerId, | ||
MinutesSinceEpochGt: 2, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, unsettledUsage, int64(300)) | ||
|
||
unsettledUsage, err = querier.GetPayerUnsettledUsage( | ||
ctx, | ||
queries.GetPayerUnsettledUsageParams{ | ||
PayerID: payerId, | ||
MinutesSinceEpochGt: 1, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, unsettledUsage, int64(500)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
DROP TABLE IF EXISTS unsettled_usage; | ||
|
||
DROP INDEX IF EXISTS idx_unsettled_usage_payer_id; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE unsettled_usage( | ||
payer_id INTEGER NOT NULL, | ||
originator_id INTEGER NOT NULL, | ||
minutes_since_epoch INTEGER NOT NULL, | ||
spend_picodollars BIGINT NOT NULL, | ||
PRIMARY KEY (payer_id, originator_id, minutes_since_epoch) | ||
); | ||
|
||
CREATE INDEX idx_unsettled_usage_payer_id ON unsettled_usage(payer_id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package utils | ||
|
||
import "time" | ||
|
||
func MinutesSinceEpoch(timestamp time.Time) int32 { | ||
durationSinceEpoch := timestamp.Sub(time.Unix(0, 0)) | ||
|
||
return int32(durationSinceEpoch.Minutes()) | ||
} | ||
|
||
func MinutesSinceEpochNow() int32 { | ||
return MinutesSinceEpoch(time.Now()) | ||
} |