Skip to content

Commit bb78e6c

Browse files
committed
test: Generate random, increasing user IDs for example data by default
Like we did for message IDs in 13e54f6. This time there aren't any (or anyway I didn't find any) call sites where we could simply remove a `userId` argument and the test would still work. I've made the one simplification to a test that seemed very easy and minimal.
1 parent 0c4653d commit bb78e6c

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

test/example_data.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ const int futureZulipFeatureLevel = 9999;
2424
// Users and accounts.
2525
//
2626

27+
/// A fresh user ID, from a random but always strictly increasing sequence.
28+
int _nextUserId() => (_lastUserId += 1 + Random().nextInt(100));
29+
int _lastUserId = 1000;
30+
31+
/// Construct an example user.
32+
///
33+
/// If user ID `userId` is not given, it will be generated from a random
34+
/// but increasing sequence.
35+
/// Use an explicit `userId` only if the ID needs to correspond to some
36+
/// other data in the test, or if the IDs need to increase in a different order
37+
/// from the calls to [user].
2738
User user({
2839
int? userId,
2940
String? email,
@@ -32,7 +43,7 @@ User user({
3243
Map<int, ProfileFieldUserData>? profileData,
3344
}) {
3445
return User(
35-
userId: userId ?? 123, // TODO generate example IDs
46+
userId: userId ?? _nextUserId(),
3647
deliveryEmailStaleDoNotUse: '[email protected]',
3748
email: email ?? '[email protected]', // TODO generate example emails
3849
fullName: fullName ?? 'A user', // TODO generate example names
@@ -72,21 +83,21 @@ Account account({
7283
);
7384
}
7485

75-
final User selfUser = user(fullName: 'Self User', email: 'self@example', userId: 123);
86+
final User selfUser = user(fullName: 'Self User', email: 'self@example');
7687
final Account selfAccount = account(
7788
id: 1001,
7889
user: selfUser,
7990
apiKey: 'asdfqwer',
8091
);
8192

82-
final User otherUser = user(fullName: 'Other User', email: 'other@example', userId: 234);
93+
final User otherUser = user(fullName: 'Other User', email: 'other@example');
8394
final Account otherAccount = account(
8495
id: 1002,
8596
user: otherUser,
8697
apiKey: 'sdfgwert',
8798
);
8899

89-
final User thirdUser = user(fullName: 'Third User', email: 'third@example', userId: 345);
100+
final User thirdUser = user(fullName: 'Third User', email: 'third@example');
90101

91102
////////////////////////////////////////////////////////////////
92103
// Streams and subscriptions.

test/notifications_test.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,13 @@ void main() {
261261
testWidgets('find account among several', (tester) async {
262262
final realmUrlA = Uri.parse('https://a-chat.example/');
263263
final realmUrlB = Uri.parse('https://chat-b.example/');
264+
final user1 = eg.user();
265+
final user2 = eg.user();
264266
final accounts = [
265-
eg.account(id: 1001, realmUrl: realmUrlA, user: eg.user(userId: 123)),
266-
eg.account(id: 1002, realmUrl: realmUrlA, user: eg.user(userId: 234)),
267-
eg.account(id: 1003, realmUrl: realmUrlB, user: eg.user(userId: 123)),
268-
eg.account(id: 1004, realmUrl: realmUrlB, user: eg.user(userId: 234)),
267+
eg.account(id: 1001, realmUrl: realmUrlA, user: user1),
268+
eg.account(id: 1002, realmUrl: realmUrlA, user: user2),
269+
eg.account(id: 1003, realmUrl: realmUrlB, user: user1),
270+
eg.account(id: 1004, realmUrl: realmUrlB, user: user2),
269271
];
270272
for (final account in accounts) {
271273
testBinding.globalStore.insertAccount(account.toCompanion(false));

0 commit comments

Comments
 (0)