|
1 | 1 | package session |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 | "time" |
6 | 7 |
|
@@ -172,6 +173,94 @@ func TestLinkedSessions(t *testing.T) { |
172 | 173 | require.EqualValues(t, []ID{s4.ID, s5.ID}, sIDs) |
173 | 174 | } |
174 | 175 |
|
| 176 | +// TestCheckSessionGroupPredicate asserts that the CheckSessionGroupPredicate |
| 177 | +// method correctly checks if each session in a group passes a predicate. |
| 178 | +func TestCheckSessionGroupPredicate(t *testing.T) { |
| 179 | + // Set up a new DB. |
| 180 | + db, err := NewDB(t.TempDir(), "test.db") |
| 181 | + require.NoError(t, err) |
| 182 | + t.Cleanup(func() { |
| 183 | + _ = db.Close() |
| 184 | + }) |
| 185 | + |
| 186 | + // We will use the Label of the Session to test that the predicate |
| 187 | + // function is checked correctly. |
| 188 | + |
| 189 | + // Add a new session to the DB. |
| 190 | + s1 := newSession(t, db, "label 1", nil) |
| 191 | + require.NoError(t, db.CreateSession(s1)) |
| 192 | + |
| 193 | + // Check that the group passes against an appropriate predicate. |
| 194 | + ok, err := db.CheckSessionGroupPredicate( |
| 195 | + s1.GroupID, func(s *Session) bool { |
| 196 | + return strings.Contains(s.Label, "label 1") |
| 197 | + }, |
| 198 | + ) |
| 199 | + require.NoError(t, err) |
| 200 | + require.True(t, ok) |
| 201 | + |
| 202 | + // Check that the group fails against an appropriate predicate. |
| 203 | + ok, err = db.CheckSessionGroupPredicate( |
| 204 | + s1.GroupID, func(s *Session) bool { |
| 205 | + return strings.Contains(s.Label, "label 2") |
| 206 | + }, |
| 207 | + ) |
| 208 | + require.NoError(t, err) |
| 209 | + require.False(t, ok) |
| 210 | + |
| 211 | + // Add a new session to the same group as the first one. |
| 212 | + s2 := newSession(t, db, "label 2", &s1.GroupID) |
| 213 | + require.NoError(t, db.CreateSession(s2)) |
| 214 | + |
| 215 | + // Check that the group passes against an appropriate predicate. |
| 216 | + ok, err = db.CheckSessionGroupPredicate( |
| 217 | + s1.GroupID, func(s *Session) bool { |
| 218 | + return strings.Contains(s.Label, "label") |
| 219 | + }, |
| 220 | + ) |
| 221 | + require.NoError(t, err) |
| 222 | + require.True(t, ok) |
| 223 | + |
| 224 | + // Check that the group fails against an appropriate predicate. |
| 225 | + ok, err = db.CheckSessionGroupPredicate( |
| 226 | + s1.GroupID, func(s *Session) bool { |
| 227 | + return strings.Contains(s.Label, "label 1") |
| 228 | + }, |
| 229 | + ) |
| 230 | + require.NoError(t, err) |
| 231 | + require.False(t, ok) |
| 232 | + |
| 233 | + // Add a new session that is not linked to the first one. |
| 234 | + s3 := newSession(t, db, "completely different", nil) |
| 235 | + require.NoError(t, db.CreateSession(s3)) |
| 236 | + |
| 237 | + // Ensure that the first group is unaffected. |
| 238 | + ok, err = db.CheckSessionGroupPredicate( |
| 239 | + s1.GroupID, func(s *Session) bool { |
| 240 | + return strings.Contains(s.Label, "label") |
| 241 | + }, |
| 242 | + ) |
| 243 | + require.NoError(t, err) |
| 244 | + require.True(t, ok) |
| 245 | + |
| 246 | + // And that the new session is evaluated separately. |
| 247 | + ok, err = db.CheckSessionGroupPredicate( |
| 248 | + s3.GroupID, func(s *Session) bool { |
| 249 | + return strings.Contains(s.Label, "label") |
| 250 | + }, |
| 251 | + ) |
| 252 | + require.NoError(t, err) |
| 253 | + require.False(t, ok) |
| 254 | + |
| 255 | + ok, err = db.CheckSessionGroupPredicate( |
| 256 | + s3.GroupID, func(s *Session) bool { |
| 257 | + return strings.Contains(s.Label, "different") |
| 258 | + }, |
| 259 | + ) |
| 260 | + require.NoError(t, err) |
| 261 | + require.True(t, ok) |
| 262 | +} |
| 263 | + |
175 | 264 | func newSession(t *testing.T, db Store, label string, |
176 | 265 | linkedGroupID *ID) *Session { |
177 | 266 |
|
|
0 commit comments