1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
package tables_test
import (
"context"
"database/sql"
"testing"
"github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/syncapi/storage/postgres"
"github.com/matrix-org/dendrite/syncapi/storage/sqlite3"
"github.com/matrix-org/dendrite/syncapi/storage/tables"
"github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/dendrite/test"
)
func newRelationsTable(t *testing.T, dbType test.DBType) (tables.Relations, *sql.DB, func()) {
t.Helper()
connStr, close := test.PrepareDBConnectionString(t, dbType)
db, err := sqlutil.Open(&config.DatabaseOptions{
ConnectionString: config.DataSource(connStr),
}, sqlutil.NewExclusiveWriter())
if err != nil {
t.Fatalf("failed to open db: %s", err)
}
var tab tables.Relations
switch dbType {
case test.DBTypePostgres:
tab, err = postgres.NewPostgresRelationsTable(db)
case test.DBTypeSQLite:
var stream sqlite3.StreamIDStatements
if err = stream.Prepare(db); err != nil {
t.Fatalf("failed to prepare stream stmts: %s", err)
}
tab, err = sqlite3.NewSqliteRelationsTable(db, &stream)
}
if err != nil {
t.Fatalf("failed to make new table: %s", err)
}
return tab, db, close
}
func compareRelationsToExpected(t *testing.T, tab tables.Relations, r types.Range, expected []types.RelationEntry) {
ctx := context.Background()
relations, _, err := tab.SelectRelationsInRange(ctx, nil, roomID, "a", "", "", r, 50)
if err != nil {
t.Fatal(err)
}
if len(relations[relType]) != len(expected) {
t.Fatalf("incorrect number of values returned for range %v (got %d, want %d)", r, len(relations[relType]), len(expected))
}
for i := 0; i < len(relations[relType]); i++ {
got := relations[relType][i]
want := expected[i]
if got != want {
t.Fatalf("range %v position %d should have been %q but got %q", r, i, got, want)
}
}
}
const roomID = "!roomid:server"
const childType = "m.room.something"
const relType = "m.reaction"
func TestRelationsTable(t *testing.T) {
ctx := context.Background()
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
tab, _, close := newRelationsTable(t, dbType)
defer close()
// Insert some relations
for _, child := range []string{"b", "c", "d"} {
if err := tab.InsertRelation(ctx, nil, roomID, "a", child, childType, relType); err != nil {
t.Fatal(err)
}
}
// Check the max position, we've inserted three things so it
// should be 3
if max, err := tab.SelectMaxRelationID(ctx, nil); err != nil {
t.Fatal(err)
} else if max != 3 {
t.Fatalf("max position should have been 3 but got %d", max)
}
// Query some ranges for "a"
for r, expected := range map[types.Range][]types.RelationEntry{
{From: 0, To: 10, Backwards: false}: {
{Position: 1, EventID: "b"},
{Position: 2, EventID: "c"},
{Position: 3, EventID: "d"},
},
{From: 1, To: 2, Backwards: false}: {
{Position: 2, EventID: "c"},
},
{From: 1, To: 3, Backwards: false}: {
{Position: 2, EventID: "c"},
{Position: 3, EventID: "d"},
},
{From: 10, To: 0, Backwards: true}: {
{Position: 3, EventID: "d"},
{Position: 2, EventID: "c"},
{Position: 1, EventID: "b"},
},
{From: 3, To: 1, Backwards: true}: {
{Position: 2, EventID: "c"},
{Position: 1, EventID: "b"},
},
} {
compareRelationsToExpected(t, tab, r, expected)
}
// Now delete one of the relations
if err := tab.DeleteRelation(ctx, nil, roomID, "c"); err != nil {
t.Fatal(err)
}
// Query some more ranges for "a"
for r, expected := range map[types.Range][]types.RelationEntry{
{From: 0, To: 10, Backwards: false}: {
{Position: 1, EventID: "b"},
{Position: 3, EventID: "d"},
},
{From: 1, To: 2, Backwards: false}: {},
{From: 1, To: 3, Backwards: false}: {
{Position: 3, EventID: "d"},
},
{From: 10, To: 0, Backwards: true}: {
{Position: 3, EventID: "d"},
{Position: 1, EventID: "b"},
},
{From: 3, To: 1, Backwards: true}: {
{Position: 1, EventID: "b"},
},
} {
compareRelationsToExpected(t, tab, r, expected)
}
// Insert some new relations
for _, child := range []string{"e", "f", "g", "h"} {
if err := tab.InsertRelation(ctx, nil, roomID, "a", child, childType, relType); err != nil {
t.Fatal(err)
}
}
// Check the max position, we've inserted four things so it
// should now be 7
if max, err := tab.SelectMaxRelationID(ctx, nil); err != nil {
t.Fatal(err)
} else if max != 7 {
t.Fatalf("max position should have been 3 but got %d", max)
}
// Query last set of ranges for "a"
for r, expected := range map[types.Range][]types.RelationEntry{
{From: 0, To: 10, Backwards: false}: {
{Position: 1, EventID: "b"},
{Position: 3, EventID: "d"},
{Position: 4, EventID: "e"},
{Position: 5, EventID: "f"},
{Position: 6, EventID: "g"},
{Position: 7, EventID: "h"},
},
{From: 1, To: 2, Backwards: false}: {},
{From: 1, To: 3, Backwards: false}: {
{Position: 3, EventID: "d"},
},
{From: 10, To: 0, Backwards: true}: {
{Position: 7, EventID: "h"},
{Position: 6, EventID: "g"},
{Position: 5, EventID: "f"},
{Position: 4, EventID: "e"},
{Position: 3, EventID: "d"},
{Position: 1, EventID: "b"},
},
{From: 6, To: 3, Backwards: true}: {
{Position: 5, EventID: "f"},
{Position: 4, EventID: "e"},
{Position: 3, EventID: "d"},
},
} {
compareRelationsToExpected(t, tab, r, expected)
}
})
}
|