Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/usage/pkg/stripe/stripe_test.go
2499 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package stripe
6
7
import (
8
"fmt"
9
"testing"
10
11
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
12
"github.com/stretchr/testify/require"
13
)
14
15
func TestQueriesForCustomersWithAttributionID_Single(t *testing.T) {
16
testCases := []struct {
17
Name string
18
AttributionIDs map[db.AttributionID]int64
19
ExpectedQueries []string
20
}{
21
{
22
Name: "1 team id",
23
AttributionIDs: map[db.AttributionID]int64{
24
db.NewTeamAttributionID("abcd-123"): 1,
25
},
26
ExpectedQueries: []string{"metadata['attributionId']:'team:abcd-123'"},
27
},
28
{
29
Name: "1 team id, 1 user id",
30
AttributionIDs: map[db.AttributionID]int64{
31
db.NewTeamAttributionID("abcd-123"): 1,
32
db.NewTeamAttributionID("abcd-456"): 1,
33
},
34
ExpectedQueries: []string{"metadata['attributionId']:'team:abcd-123' OR metadata['attributionId']:'team:abcd-456'"},
35
},
36
}
37
38
for _, tc := range testCases {
39
t.Run(tc.Name, func(t *testing.T) {
40
actualQueries := queriesForCustomersWithAttributionIDs(tc.AttributionIDs)
41
42
require.Equal(t, tc.ExpectedQueries, actualQueries)
43
})
44
}
45
}
46
47
func TestCustomerQueriesForTeamIds_MultipleQueries(t *testing.T) {
48
testCases := []struct {
49
Name string
50
NumberOfTeamIds int
51
ExpectedNumberOfQueries int
52
}{
53
{
54
Name: "1 team id",
55
NumberOfTeamIds: 1,
56
ExpectedNumberOfQueries: 1,
57
},
58
{
59
Name: "10 team ids",
60
NumberOfTeamIds: 10,
61
ExpectedNumberOfQueries: 1,
62
},
63
{
64
Name: "11 team ids",
65
NumberOfTeamIds: 11,
66
ExpectedNumberOfQueries: 2,
67
},
68
{
69
Name: "1000 team ids",
70
NumberOfTeamIds: 1000,
71
ExpectedNumberOfQueries: 100,
72
},
73
}
74
75
buildTeamIds := func(numberOfTeamIds int) map[db.AttributionID]int64 {
76
attributionIDs := map[db.AttributionID]int64{}
77
for i := 0; i < numberOfTeamIds; i++ {
78
attributionIDs[db.NewTeamAttributionID(fmt.Sprintf("abcd-%d", i))] = 1
79
}
80
return attributionIDs
81
}
82
83
for _, tc := range testCases {
84
t.Run(tc.Name, func(t *testing.T) {
85
teamIds := buildTeamIds(tc.NumberOfTeamIds)
86
actualQueries := queriesForCustomersWithAttributionIDs(teamIds)
87
88
require.Equal(t, tc.ExpectedNumberOfQueries, len(actualQueries))
89
})
90
}
91
}
92
93