-
Notifications
You must be signed in to change notification settings - Fork 310
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
/// 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, | ||
|
@@ -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 | ||
|
@@ -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. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.user
s 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 inexample_data.dart
.There was a problem hiding this comment.
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.)