5
5
package db_test
6
6
7
7
import (
8
+ "context"
8
9
"github.com/gitpod-io/gitpod/usage/pkg/db"
9
10
"github.com/google/uuid"
10
11
"github.com/stretchr/testify/require"
@@ -30,3 +31,70 @@ func TestTeamMembership_WriteRead(t *testing.T) {
30
31
require .NoError (t , tx .Error )
31
32
require .Equal (t , membership , read )
32
33
}
34
+
35
+ func TestListTeamMembershipsForUserIDs (t * testing.T ) {
36
+ conn := db .ConnectForTests (t )
37
+ userWithTwoTeams := uuid .New ()
38
+ memberships := []db.TeamMembership {
39
+ {
40
+ ID : uuid .New (),
41
+ TeamID : uuid .New (),
42
+ UserID : uuid .New (),
43
+ Role : db .TeamMembershipRole_Member ,
44
+ },
45
+ {
46
+ ID : uuid .New (),
47
+ TeamID : uuid .New (),
48
+ UserID : userWithTwoTeams ,
49
+ Role : db .TeamMembershipRole_Member ,
50
+ },
51
+ {
52
+ ID : uuid .New (),
53
+ TeamID : uuid .New (),
54
+ UserID : userWithTwoTeams ,
55
+ Role : db .TeamMembershipRole_Member ,
56
+ },
57
+ }
58
+
59
+ tx := conn .Create (& memberships )
60
+ require .NoError (t , tx .Error )
61
+
62
+ for _ , scenario := range []struct {
63
+ Name string
64
+ UserIDs []uuid.UUID
65
+ Expected int
66
+ }{
67
+ {
68
+ Name : "no user ids return empty" ,
69
+ UserIDs : nil ,
70
+ Expected : 0 ,
71
+ },
72
+ {
73
+ Name : "no matching user IDs returns empty" ,
74
+ UserIDs : []uuid.UUID {uuid .New ()},
75
+ Expected : 0 ,
76
+ },
77
+ {
78
+ Name : "one matching, and one not returns only one record" ,
79
+ UserIDs : []uuid.UUID {memberships [0 ].UserID , uuid .New ()},
80
+ Expected : 1 ,
81
+ },
82
+ {
83
+ Name : "all matching returns all records (with duplicate userID in query)" ,
84
+ UserIDs : []uuid.UUID {memberships [0 ].UserID , memberships [1 ].UserID , memberships [2 ].UserID },
85
+ Expected : 3 ,
86
+ },
87
+ {
88
+ Name : "one ID with multiple memberships returns all membership for the ID" ,
89
+ UserIDs : []uuid.UUID {userWithTwoTeams },
90
+ Expected : 2 ,
91
+ },
92
+ } {
93
+ t .Run (scenario .Name , func (t * testing.T ) {
94
+ retrieved , err := db .ListTeamMembershipsForUserIDs (context .Background (), conn , scenario .UserIDs )
95
+ require .NoError (t , err )
96
+
97
+ require .Len (t , retrieved , scenario .Expected )
98
+ })
99
+ }
100
+ }
0 commit comments