From 338231c7f0ef3d74669409b861cc5f32acdb1fab Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 22 Jul 2022 10:44:22 -0400 Subject: [PATCH] Fix tests to not depend on a `room_id` parameter in each `children_state` entry. --- tests/room_hierarchy_test.go | 74 ++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/tests/room_hierarchy_test.go b/tests/room_hierarchy_test.go index 0dd623d4..041e5d0f 100644 --- a/tests/room_hierarchy_test.go +++ b/tests/room_hierarchy_test.go @@ -37,6 +37,21 @@ func eventKey(srcRoomID, dstRoomID, evType string) string { return srcRoomID + "|" + dstRoomID + "|" + evType } +// Shared mapper function to return a structure comparison string for JSONCheckOff. +func roomToChildrenMapper(r gjson.Result) interface{} { + roomId := r.Get("room_id").Str + + result := "" + for i, res := range r.Get("children_state").Array() { + if i != 0 { + result += ";" + } + result += eventKey(roomId, res.Get("state_key").Str, res.Get("type").Str) + } + + return result +} + // Tests that the CS API for MSC2946 works correctly. Creates a space directory like: // Root // | @@ -228,12 +243,11 @@ func TestClientSpacesSummary(t *testing.T) { return nil }), // Check that the links from Root down to other rooms and spaces exist. - match.JSONCheckOff("rooms.#.children_state|@flatten", []interface{}{ - rootToR1, rootToR2, rootToSS1, - ss1ToSS2, ss2ToR3, ss2ToR4, - }, func(r gjson.Result) interface{} { - return eventKey(r.Get("room_id").Str, r.Get("state_key").Str, r.Get("type").Str) - }, nil), + match.JSONCheckOff(`rooms.#(room_type=="m.space")#")`, []interface{}{ + rootToR1 + ";" + rootToSS1 + ";" + rootToR2, + ss1ToSS2, + ss2ToR3 + ";" + ss2ToR4, + }, roomToChildrenMapper, nil), }, }) }) @@ -257,11 +271,9 @@ func TestClientSpacesSummary(t *testing.T) { return r.Get("room_id").Str }, nil), // All of the links are still there. - match.JSONCheckOff("rooms.#.children_state|@flatten", []interface{}{ - rootToR1, rootToR2, rootToSS1, ss1ToSS2, - }, func(r gjson.Result) interface{} { - return eventKey(r.Get("room_id").Str, r.Get("state_key").Str, r.Get("type").Str) - }, nil), + match.JSONCheckOff(`rooms.#(room_type=="m.space")#`, []interface{}{ + rootToR1 + ";" + rootToSS1 + ";" + rootToR2, ss1ToSS2, + }, roomToChildrenMapper, nil), }, }) }) @@ -285,11 +297,9 @@ func TestClientSpacesSummary(t *testing.T) { return r.Get("room_id").Str }, nil), // All of the links are still there. - match.JSONCheckOff("rooms.#.children_state|@flatten", []interface{}{ - rootToR1, rootToR2, - }, func(r gjson.Result) interface{} { - return eventKey(r.Get("room_id").Str, r.Get("state_key").Str, r.Get("type").Str) - }, nil), + match.JSONCheckOff(`rooms.#(room_type=="m.space")#`, []interface{}{ + rootToR1 + ";" + rootToR2, + }, roomToChildrenMapper, nil), }, }) }) @@ -350,11 +360,9 @@ func TestClientSpacesSummary(t *testing.T) { }, func(r gjson.Result) interface{} { return r.Get("room_id").Str }, nil), - match.JSONCheckOff("rooms.#.children_state|@flatten", []interface{}{ - rootToR1, rootToR2, - }, func(r gjson.Result) interface{} { - return eventKey(r.Get("room_id").Str, r.Get("state_key").Str, r.Get("type").Str) - }, nil), + match.JSONCheckOff(`rooms.#(room_type=="m.space")#`, []interface{}{ + rootToR1 + ";" + rootToR2, + }, roomToChildrenMapper, nil), }, }) }) @@ -464,11 +472,9 @@ func TestClientSpacesSummaryJoinRules(t *testing.T) { }, func(r gjson.Result) interface{} { return r.Get("room_id").Str }, nil), - match.JSONCheckOff("rooms.#.children_state|@flatten", []interface{}{ - rootToR1, rootToSS1, - }, func(r gjson.Result) interface{} { - return eventKey(r.Get("room_id").Str, r.Get("state_key").Str, r.Get("type").Str) - }, nil), + match.JSONCheckOff(`rooms.#(room_type=="m.space")#`, []interface{}{ + rootToR1 + ";" + rootToSS1, + }, roomToChildrenMapper, nil), }, }) @@ -484,11 +490,9 @@ func TestClientSpacesSummaryJoinRules(t *testing.T) { }, func(r gjson.Result) interface{} { return r.Get("room_id").Str }, nil), - match.JSONCheckOff("rooms.#.children_state|@flatten", []interface{}{ - rootToR1, rootToSS1, - }, func(r gjson.Result) interface{} { - return eventKey(r.Get("room_id").Str, r.Get("state_key").Str, r.Get("type").Str) - }, nil), + match.JSONCheckOff(`rooms.#(room_type=="m.space")#`, []interface{}{ + rootToR1 + ";" + rootToSS1, + }, roomToChildrenMapper, nil), }, }) @@ -503,11 +507,9 @@ func TestClientSpacesSummaryJoinRules(t *testing.T) { }, func(r gjson.Result) interface{} { return r.Get("room_id").Str }, nil), - match.JSONCheckOff("rooms.#.children_state|@flatten", []interface{}{ - rootToR1, rootToSS1, ss1ToR2, ss1ToR3, - }, func(r gjson.Result) interface{} { - return eventKey(r.Get("room_id").Str, r.Get("state_key").Str, r.Get("type").Str) - }, nil), + match.JSONCheckOff(`rooms.#(room_type=="m.space")#`, []interface{}{ + rootToR1 + ";" + rootToSS1, ss1ToR2 + ";" + ss1ToR3, + }, roomToChildrenMapper, nil), }, }) }