2019-11-05 13:16:02 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-11-05 13:16:02 +00:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProjectRecords(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-11-05 13:16:02 +00:00
|
|
|
utc := time.Now().UTC()
|
|
|
|
|
|
|
|
prjID, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
start := time.Date(utc.Year(), utc.Month(), 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
end := time.Date(utc.Year(), utc.Month()+1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
projectRecordsDB := db.StripeCoinPayments().ProjectRecords()
|
|
|
|
|
|
|
|
t.Run("create", func(t *testing.T) {
|
|
|
|
err = projectRecordsDB.Create(ctx,
|
|
|
|
[]stripecoinpayments.CreateProjectRecord{
|
|
|
|
{
|
2020-03-30 10:08:50 +01:00
|
|
|
ProjectID: prjID,
|
2019-11-05 13:16:02 +00:00
|
|
|
Storage: 1,
|
|
|
|
Egress: 2,
|
|
|
|
Objects: 3,
|
|
|
|
},
|
|
|
|
},
|
2020-01-07 10:41:19 +00:00
|
|
|
[]stripecoinpayments.CouponUsage{},
|
2020-02-11 14:48:28 +00:00
|
|
|
[]stripecoinpayments.CreditsSpending{},
|
2019-11-05 13:16:02 +00:00
|
|
|
start, end,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check", func(t *testing.T) {
|
2020-03-30 10:08:50 +01:00
|
|
|
err = projectRecordsDB.Check(ctx, prjID, start, end)
|
2019-11-05 13:16:02 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Equal(t, stripecoinpayments.ErrProjectRecordExists, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
page, err := projectRecordsDB.ListUnapplied(ctx, 0, 1, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(page.Records))
|
|
|
|
|
|
|
|
t.Run("consume", func(t *testing.T) {
|
|
|
|
err = projectRecordsDB.Consume(ctx, page.Records[0].ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
page, err = projectRecordsDB.ListUnapplied(ctx, 0, 1, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(page.Records))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProjectRecordsList(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2020-03-10 22:05:01 +00:00
|
|
|
now := time.Now()
|
2019-11-05 13:16:02 +00:00
|
|
|
|
2020-03-10 22:05:01 +00:00
|
|
|
start := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
|
|
|
end := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, now.Location())
|
2019-11-05 13:16:02 +00:00
|
|
|
|
|
|
|
projectRecordsDB := db.StripeCoinPayments().ProjectRecords()
|
|
|
|
|
2020-02-10 14:44:48 +00:00
|
|
|
const limit = 5
|
|
|
|
const recordsLen = limit * 4
|
2019-11-05 13:16:02 +00:00
|
|
|
|
|
|
|
var createProjectRecords []stripecoinpayments.CreateProjectRecord
|
|
|
|
for i := 0; i < recordsLen; i++ {
|
|
|
|
projID, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
createProjectRecords = append(createProjectRecords,
|
|
|
|
stripecoinpayments.CreateProjectRecord{
|
2020-03-30 10:08:50 +01:00
|
|
|
ProjectID: projID,
|
2019-11-05 13:16:02 +00:00
|
|
|
Storage: float64(i) + 1,
|
|
|
|
Egress: int64(i) + 2,
|
2020-01-07 10:41:19 +00:00
|
|
|
Objects: float64(i) + 3,
|
2019-11-05 13:16:02 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:48:28 +00:00
|
|
|
err := projectRecordsDB.Create(ctx, createProjectRecords, []stripecoinpayments.CouponUsage{}, []stripecoinpayments.CreditsSpending{}, start, end)
|
2019-11-05 13:16:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-02-10 14:44:48 +00:00
|
|
|
page, err := projectRecordsDB.ListUnapplied(ctx, 0, limit, time.Now())
|
2019-11-05 13:16:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-02-10 14:44:48 +00:00
|
|
|
records := page.Records
|
|
|
|
|
|
|
|
for page.Next {
|
|
|
|
page, err = projectRecordsDB.ListUnapplied(ctx, page.NextOffset, limit, time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
records = append(records, page.Records...)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, recordsLen, len(records))
|
2019-11-05 13:16:02 +00:00
|
|
|
assert.False(t, page.Next)
|
|
|
|
assert.Equal(t, int64(0), page.NextOffset)
|
|
|
|
|
|
|
|
for _, record := range page.Records {
|
|
|
|
for _, createRecord := range createProjectRecords {
|
|
|
|
if record.ProjectID != createRecord.ProjectID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotNil(t, record.ID)
|
|
|
|
assert.Equal(t, 16, len(record.ID))
|
|
|
|
assert.Equal(t, createRecord.ProjectID, record.ProjectID)
|
|
|
|
assert.Equal(t, createRecord.Storage, record.Storage)
|
|
|
|
assert.Equal(t, createRecord.Egress, record.Egress)
|
|
|
|
assert.Equal(t, createRecord.Objects, record.Objects)
|
2020-03-10 22:05:01 +00:00
|
|
|
assert.True(t, start.Equal(record.PeriodStart))
|
|
|
|
assert.True(t, end.Equal(record.PeriodEnd))
|
2019-11-05 13:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|