Skip to content

Commit 473f89f

Browse files
Khader-1gnprice
authored andcommitted
test: Generate random increasing streamId for example data by default
This allows `eg.stream` to be used in contexts where it's important that the stream IDs don't collide or even that they're increasing, without the test having to explicitly give specific stream IDs when no other fact about them is relevant. Some tests are dependent on the stream IDs being static. Those are updated to use `eg.defaultStreamMessageStreamId` explicitly.
1 parent 022df71 commit 473f89f

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

test/example_data.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ final User fourthUser = user(fullName: 'Fourth User', email: 'fourth@example');
149149
// Streams and subscriptions.
150150
//
151151

152+
/// A fresh stream ID, from a random but always strictly increasing sequence.
153+
int _nextStreamId() => (_lastStreamId += 1 + Random().nextInt(10));
154+
int _lastStreamId = 200;
155+
156+
/// Construct an example stream.
157+
///
158+
/// If stream ID `streamId` is not given, it will be generated from a random
159+
/// but increasing sequence.
160+
/// Use an explicit `streamId` only if the ID needs to correspond to some
161+
/// other data in the test, or if the IDs need to increase in a different order
162+
/// from the calls to [stream].
152163
ZulipStream stream({
153164
int? streamId,
154165
String? name,
@@ -165,7 +176,7 @@ ZulipStream stream({
165176
int? streamWeeklyTraffic,
166177
}) {
167178
return ZulipStream(
168-
streamId: streamId ?? 123, // TODO generate example IDs
179+
streamId: streamId ?? _nextStreamId(),
169180
name: name ?? 'A stream', // TODO generate example names
170181
description: description ?? 'A description', // TODO generate example descriptions
171182
renderedDescription: renderedDescription ?? '<p>A description</p>', // TODO generate random
@@ -281,6 +292,8 @@ Map<String, dynamic> _messagePropertiesFromContent(String? content, String? cont
281292
int _nextMessageId() => (_lastMessageId += 1 + Random().nextInt(100));
282293
int _lastMessageId = 1000;
283294

295+
const defaultStreamMessageStreamId = 123;
296+
284297
/// Construct an example stream message.
285298
///
286299
/// If the message ID `id` is not given, it will be generated from a random
@@ -289,6 +302,9 @@ int _lastMessageId = 1000;
289302
/// in the test, or if the IDs need to increase in a different order from the
290303
/// calls to [streamMessage] and [dmMessage].
291304
///
305+
/// The message will be in `stream` if given. Otherwise,
306+
/// an example stream with ID `defaultStreamMessageStreamId` will be used.
307+
///
292308
/// See also:
293309
/// * [dmMessage], to construct an example direct message.
294310
StreamMessage streamMessage({
@@ -303,7 +319,7 @@ StreamMessage streamMessage({
303319
int? timestamp,
304320
List<MessageFlag>? flags,
305321
}) {
306-
final effectiveStream = stream ?? _stream();
322+
final effectiveStream = stream ?? _stream(streamId: defaultStreamMessageStreamId);
307323
// The use of JSON here is convenient in order to delegate parts of the data
308324
// to helper functions. The main downside is that it loses static typing
309325
// of the properties as we're constructing the data. That's probably OK

test/model/message_list_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void main() {
4141

4242
/// Initialize [model] and the rest of the test state.
4343
Future<void> prepare({Narrow narrow = const CombinedFeedNarrow()}) async {
44-
final stream = eg.stream();
44+
final stream = eg.stream(streamId: eg.defaultStreamMessageStreamId);
4545
subscription = eg.subscription(stream);
4646
store = eg.store();
4747
await store.addStream(stream);
@@ -247,13 +247,13 @@ void main() {
247247
});
248248

249249
test('MessageEvent, not in narrow', () async {
250-
final stream = eg.stream(streamId: 123);
250+
final stream = eg.stream();
251251
await prepare(narrow: StreamNarrow(stream.streamId));
252252
await prepareMessages(foundOldest: true, messages:
253253
List.generate(30, (i) => eg.streamMessage(stream: stream)));
254254

255255
check(model).messages.length.equals(30);
256-
final otherStream = eg.stream(streamId: 234);
256+
final otherStream = eg.stream();
257257
await store.handleEvent(MessageEvent(id: 0,
258258
message: eg.streamMessage(stream: otherStream)));
259259
checkNotNotified();
@@ -709,7 +709,7 @@ void main() {
709709
// doesn't need to exercise the different reasons that messages don't.
710710

711711
const timestamp = 1693602618;
712-
final stream = eg.stream();
712+
final stream = eg.stream(streamId: eg.defaultStreamMessageStreamId);
713713
Message streamMessage(int id) =>
714714
eg.streamMessage(id: id, stream: stream, topic: 'foo', timestamp: timestamp);
715715
Message dmMessage(int id) =>
@@ -790,7 +790,7 @@ void main() {
790790

791791
const t1 = 1693602618;
792792
const t2 = t1 + 86400;
793-
final stream = eg.stream();
793+
final stream = eg.stream(streamId: eg.defaultStreamMessageStreamId);
794794
Message streamMessage(int id, int timestamp, User sender) =>
795795
eg.streamMessage(id: id, sender: sender,
796796
stream: stream, topic: 'foo', timestamp: timestamp);
@@ -831,8 +831,8 @@ void main() {
831831
});
832832

833833
test('stream messages match just if same stream/topic', () {
834-
final stream0 = eg.stream(streamId: 123);
835-
final stream1 = eg.stream(streamId: 234);
834+
final stream0 = eg.stream();
835+
final stream1 = eg.stream();
836836
final messageAB = eg.streamMessage(stream: stream0, topic: 'foo');
837837
final messageXB = eg.streamMessage(stream: stream1, topic: 'foo');
838838
final messageAX = eg.streamMessage(stream: stream0, topic: 'bar');

test/model/message_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void main() {
3434

3535
/// Initialize [store] and the rest of the test state.
3636
Future<void> prepare({Narrow narrow = const CombinedFeedNarrow()}) async {
37-
final stream = eg.stream();
37+
final stream = eg.stream(streamId: eg.defaultStreamMessageStreamId);
3838
subscription = eg.subscription(stream);
3939
store = eg.store();
4040
await store.addStream(stream);

test/widgets/message_list_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void main() {
5252
UnreadMessagesSnapshot? unreadMsgs,
5353
}) async {
5454
addTearDown(testBinding.reset);
55-
streams ??= subscriptions ??= [eg.subscription(eg.stream())];
55+
streams ??= subscriptions ??= [eg.subscription(eg.stream(streamId: eg.defaultStreamMessageStreamId))];
5656
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot(
5757
streams: streams, subscriptions: subscriptions, unreadMsgs: unreadMsgs));
5858
store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

0 commit comments

Comments
 (0)