@@ -110,6 +110,68 @@ func TestLinkingSessions(t *testing.T) {
110110 require .NoError (t , db .CreateSession (s2 ))
111111}
112112
113+ // TestIDToGroupIDIndex tests that the session-ID-to-group-ID and
114+ // group-ID-to-session-ID indexes work as expected by asserting the behaviour
115+ // of the GetGroupID and GetSessionIDs methods.
116+ func TestLinkedSessions (t * testing.T ) {
117+ // Set up a new DB.
118+ db , err := NewDB (t .TempDir (), "test.db" )
119+ require .NoError (t , err )
120+ t .Cleanup (func () {
121+ _ = db .Close ()
122+ })
123+
124+ // Create a few sessions. The first one is a new session and the two
125+ // after are all linked to the prior one. All these sessions belong to
126+ // the same group. The group ID is equivalent to the session ID of the
127+ // first session.
128+ s1 := newSession (t , db , "session 1" , nil )
129+ s2 := newSession (t , db , "session 2" , & s1 .GroupID )
130+ s3 := newSession (t , db , "session 3" , & s2 .GroupID )
131+
132+ // Persist the sessions.
133+ require .NoError (t , db .CreateSession (s1 ))
134+ require .NoError (t , db .CreateSession (s2 ))
135+ require .NoError (t , db .CreateSession (s3 ))
136+
137+ // Assert that the session ID to group ID index works as expected.
138+ for _ , s := range []* Session {s1 , s2 , s3 } {
139+ groupID , err := db .GetGroupID (s .ID )
140+ require .NoError (t , err )
141+ require .Equal (t , s1 .ID , groupID )
142+ require .Equal (t , s .GroupID , groupID )
143+ }
144+
145+ // Assert that the group ID to session ID index works as expected.
146+ sIDs , err := db .GetSessionIDs (s1 .GroupID )
147+ require .NoError (t , err )
148+ require .EqualValues (t , []ID {s1 .ID , s2 .ID , s3 .ID }, sIDs )
149+
150+ // To ensure that different groups don't interfere with each other,
151+ // let's add another set of linked sessions not linked to the first.
152+ s4 := newSession (t , db , "session 4" , nil )
153+ s5 := newSession (t , db , "session 5" , & s4 .GroupID )
154+
155+ require .NotEqual (t , s4 .GroupID , s1 .GroupID )
156+
157+ // Persist the sessions.
158+ require .NoError (t , db .CreateSession (s4 ))
159+ require .NoError (t , db .CreateSession (s5 ))
160+
161+ // Assert that the session ID to group ID index works as expected.
162+ for _ , s := range []* Session {s4 , s5 } {
163+ groupID , err := db .GetGroupID (s .ID )
164+ require .NoError (t , err )
165+ require .Equal (t , s4 .ID , groupID )
166+ require .Equal (t , s .GroupID , groupID )
167+ }
168+
169+ // Assert that the group ID to session ID index works as expected.
170+ sIDs , err = db .GetSessionIDs (s5 .GroupID )
171+ require .NoError (t , err )
172+ require .EqualValues (t , []ID {s4 .ID , s5 .ID }, sIDs )
173+ }
174+
113175func newSession (t * testing.T , db Store , label string ,
114176 linkedGroupID * ID ) * Session {
115177
0 commit comments