Skip to content

test: Generate random, increasing user IDs for example data by default #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions test/example_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ const int futureZulipFeatureLevel = 9999;
// Users and accounts.
//

/// A fresh user ID, from a random but always strictly increasing sequence.
int _nextUserId() => (_lastUserId += 1 + Random().nextInt(100));
int _lastUserId = 1000;

/// Construct an example user.
///
/// If user ID `userId` is not given, it will be generated from a random
/// but increasing sequence.
Comment on lines +33 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first thought was that user IDs don't need to be ordered like message IDs do. But I guess when writing lists of users in a DM conversation one needs them sorted (unless using some function that does the sorting itself), so it's convenient for the order to be deterministic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah, I guess the ordering isn't even necessary for my use case in #410, which prompted this PR. The eg.users there aren't being used to identify a DM conversation.

But in #410 I did find it nicer to let the caller omit a user ID, and for that we needed eg.user() to give unique user IDs, and for that it was convenient to follow the pattern of message IDs in example_data.dart.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. If for something else we want to generate random distinct IDs without constraining the ordering, zulip-mobile has a handy example we can follow, implemented by Ray years ago.

(Rereading it, I guess it can't just be transcribed from JS to Dart, because it relies on JS's sparse arrays. But a sparse array is basically just a map with int keys, plus an int for the length.)

/// Use an explicit `userId` only if the ID needs to correspond to some
/// other data in the test, or if the IDs need to increase in a different order
/// from the calls to [user].
User user({
int? userId,
String? email,
Expand All @@ -32,7 +43,7 @@ User user({
Map<int, ProfileFieldUserData>? profileData,
}) {
return User(
userId: userId ?? 123, // TODO generate example IDs
userId: userId ?? _nextUserId(),
deliveryEmailStaleDoNotUse: '[email protected]',
email: email ?? '[email protected]', // TODO generate example emails
fullName: fullName ?? 'A user', // TODO generate example names
Expand Down Expand Up @@ -72,21 +83,21 @@ Account account({
);
}

final User selfUser = user(fullName: 'Self User', email: 'self@example', userId: 123);
final User selfUser = user(fullName: 'Self User', email: 'self@example');
final Account selfAccount = account(
id: 1001,
user: selfUser,
apiKey: 'asdfqwer',
);

final User otherUser = user(fullName: 'Other User', email: 'other@example', userId: 234);
final User otherUser = user(fullName: 'Other User', email: 'other@example');
final Account otherAccount = account(
id: 1002,
user: otherUser,
apiKey: 'sdfgwert',
);

final User thirdUser = user(fullName: 'Third User', email: 'third@example', userId: 345);
final User thirdUser = user(fullName: 'Third User', email: 'third@example');

////////////////////////////////////////////////////////////////
// Streams and subscriptions.
Expand Down
10 changes: 6 additions & 4 deletions test/notifications_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,13 @@ void main() {
testWidgets('find account among several', (tester) async {
final realmUrlA = Uri.parse('https://a-chat.example/');
final realmUrlB = Uri.parse('https://chat-b.example/');
final user1 = eg.user();
final user2 = eg.user();
final accounts = [
eg.account(id: 1001, realmUrl: realmUrlA, user: eg.user(userId: 123)),
eg.account(id: 1002, realmUrl: realmUrlA, user: eg.user(userId: 234)),
eg.account(id: 1003, realmUrl: realmUrlB, user: eg.user(userId: 123)),
eg.account(id: 1004, realmUrl: realmUrlB, user: eg.user(userId: 234)),
eg.account(id: 1001, realmUrl: realmUrlA, user: user1),
eg.account(id: 1002, realmUrl: realmUrlA, user: user2),
eg.account(id: 1003, realmUrl: realmUrlB, user: user1),
eg.account(id: 1004, realmUrl: realmUrlB, user: user2),
];
for (final account in accounts) {
testBinding.globalStore.insertAccount(account.toCompanion(false));
Expand Down