Skip to content

Commit 2d2a8ab

Browse files
committed
Batch up all errors of a room before logging
This allows us to collect all incorrect data about a room and print it. A side-effect is that one doesn't need to re-run the tests to see all the broken fields. In addition, the `must.MatchGJSON` was changed to a `should`, which now re-polls `/publicRooms`. This prevents the test from exiting early upon receiving an entry that's missing some keys.
1 parent f31eeff commit 2d2a8ab

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

tests/csapi/public_rooms_test.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package csapi_tests
22

33
import (
4+
"fmt"
45
"net/http"
56
"testing"
67
"time"
@@ -12,6 +13,7 @@ import (
1213
"github.com/matrix-org/complement/helpers"
1314
"github.com/matrix-org/complement/match"
1415
"github.com/matrix-org/complement/must"
16+
"github.com/matrix-org/complement/should"
1517
)
1618

1719
func TestPublicRooms(t *testing.T) {
@@ -148,14 +150,21 @@ func TestPublicRooms(t *testing.T) {
148150

149151
// Check each room in the public rooms list
150152
for _, roomData := range chunk.Array() {
151-
// Verify required keys are present
152-
must.MatchGJSON(
153-
t,
153+
// Verify required keys are present. This applies to any room we see.
154+
err := should.MatchGJSON(
154155
roomData,
155156
match.JSONKeyPresent("world_readable"),
156157
match.JSONKeyPresent("guest_can_join"),
157158
match.JSONKeyPresent("num_joined_members"),
158159
)
160+
if err != nil {
161+
// This room is missing required keys, log and try again.
162+
roomId := roomData.Get("room_id").Str
163+
t.Logf("Room %s data missing required keys: %s", roomId, err.Error())
164+
return false
165+
}
166+
167+
validationErrors := make([]error, 0)
159168

160169
canonicalAlias := roomData.Get("canonical_alias").Str
161170
name := roomData.Get("name").Str
@@ -185,38 +194,47 @@ func TestPublicRooms(t *testing.T) {
185194

186195
// Verify member count
187196
if numMembers != 1 {
188-
t.Logf("Room %s has %d members, expected 1", matchedAlias, numMembers)
189-
return false
197+
err = fmt.Errorf("Room %s has %d members, expected 1", matchedAlias, numMembers)
198+
validationErrors = append(validationErrors, err)
190199
}
191200

192201
// Verify name field
193202
if roomConfig.name != "" {
194203
if name != roomConfig.name {
195-
t.Logf("Room %s has name '%s', expected '%s'", matchedAlias, name, roomConfig.name)
196-
return false
204+
err = fmt.Errorf("Room %s has name '%s', expected '%s'", matchedAlias, name, roomConfig.name)
205+
validationErrors = append(validationErrors, err)
197206
}
198207
} else {
199208
if name != "" {
200-
t.Logf("Room %s has unexpected name '%s', expected no name", matchedAlias, name)
201-
return false
209+
err = fmt.Errorf("Room %s has unexpected name '%s', expected no name", matchedAlias, name)
210+
validationErrors = append(validationErrors, err)
202211
}
203212
}
204213

205214
// Verify topic field
206215
if roomConfig.topic != "" {
207216
if topic != roomConfig.topic {
208-
t.Logf("Room %s has topic '%s', expected '%s'", matchedAlias, topic, roomConfig.topic)
209-
return false
217+
err = fmt.Errorf("Room %s has topic '%s', expected '%s'", matchedAlias, topic, roomConfig.topic)
218+
validationErrors = append(validationErrors, err)
210219
}
211220
} else {
212221
if topic != "" {
213-
t.Logf("Room %s has unexpected topic '%s', expected no topic", matchedAlias, topic)
214-
return false
222+
err = fmt.Errorf("Room %s has unexpected topic '%s', expected no topic", matchedAlias, topic)
223+
validationErrors = append(validationErrors, err)
224+
}
225+
}
226+
227+
if len(validationErrors) > 0 {
228+
for _, e := range validationErrors {
229+
t.Logf("Validation error for room %s: %s", matchedAlias, e.Error())
215230
}
231+
232+
return false
216233
}
217234

218235
// Mark this room as correctly found
219236
foundRooms[matchedAlias] = true
237+
220238
t.Logf("Successfully validated room %s", matchedAlias)
221239
}
222240

0 commit comments

Comments
 (0)