Skip to content
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

Replace randomness in tests with deterministic randomness. #1320

Merged
merged 15 commits into from
Mar 7, 2024

Conversation

Jonas-Sander
Copy link
Collaborator

@Jonas-Sander Jonas-Sander commented Feb 26, 2024

This PR makes tests that use randomness deterministic. This is done by using the TEST_RANDOMNESS_SEED environment variable that is passed to Dart by the sz test command. This seed will be used for generating randomness, so that we can reuse a seed that made a test fail in e.g. a CI pipeline.

I replaced all instances of randomness from package:random_string with a new custom package called test_randomness.
This package also includes a seeded Random called szTestRandom that should be used in all future tests.

Requires #1318

Summary by CodeRabbit

  • New Features

    • Introduced a new library for generating random values for testing purposes, enhancing the ability to produce diverse test scenarios.
    • Added an option to randomize test case execution order, allowing for more robust test coverage.
  • Tests

    • Updated various test files across the application to utilize the new test_randomness library for generating random numbers and strings, improving test reliability and predictability.
  • Chores

    • Implemented comprehensive .gitignore rules for a wide range of development environments, ensuring a cleaner repository.
  • Documentation

    • Added metadata tracking for project properties in the test_randomness library, ensuring better project management and version control.

Copy link
Contributor

coderabbitai bot commented Feb 26, 2024

Walkthrough

The overarching change across these updates focuses on standardizing the approach to generating randomness in tests across various files. This is achieved by replacing the use of different random number generation methods and the random_string package with a unified test_randomness package. Additionally, there's an update in import organization and a minor adjustment in using prefixes for Flutter imports in one instance.

Changes

Files Summary of Changes
.../update_reminder_bloc_test.dart
.../feedback_bloc_test.dart
.../feedback_page_test.dart
.../group_join_select_courses_bloc_test.dart
.../holiday_bloc_unit_test.dart
.../homework_dialog_test.dart
.../teacher_homework_page_widget_test.dart
.../school_class_filter_view_test.dart
.../timetable_bloc_test.dart
.../blackboard_item_read_by_users_list_page_test.dart
.../past_calendrical_events_page_test.dart
.../bloc_test.dart
.../teacher_homework_page_bloc.dart
.../create_homework_util.dart
Replaced random_string and dart:math's Random with test_randomness for generating random values. Adjusted import statements accordingly.
lib/test_randomness/...
tools/sz_repo_cli/...
Introduced test_randomness package and added functionality for test case order randomization.

🐰✨

In the land of code and byte,
A rabbit hopped through tests, so light.
With a flick and a hop, randomness came,
Unifying all, test_randomness by name.

Through files and tests, it weaved its spell,
Ensuring all randomness, now unified and well.
🌟📚

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

Note: Auto-reply has been disabled for this repository by the repository owner. The CodeRabbit bot will not respond to your comments unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@github-actions github-actions bot added code quality Code quality itself (readable) but e.g. also how we might enforce better quality automatically. feature: homework feature: homework-submissions Submissions can be toggled for homeworks so that pupils can upload their solutions for the teacher. feature: timetable / calendar Includes anything regarding lessons (timetable) and events (calendar). feature: information sheet Information sheets are posted to courses as a way to announce information. feature: holidays feature: groups:courses Specific to only courses (instead of e.g. classes) feature: groups:classes Specific to only classes (instead of e.g. courses) feature: groups Groups umbrella term for courses and classes. testing user: teacher sharezone cli dependencies Changing, updating, adding or removing one or more dependencies. feature: feedback Users can send us feedback to improve the app. w: dashboard-page Page that shows a summary of all important things (homeworks, events, etc.). labels Feb 26, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7452434 and 72edc2a.
Files ignored due to path filters (10)
  • app/pubspec.lock is excluded by: !**/*.lock
  • app/pubspec.yaml is excluded by: !**/*.yaml
  • lib/abgabe/abgabe_client_lib/pubspec.lock is excluded by: !**/*.lock
  • lib/abgabe/abgabe_client_lib/pubspec.yaml is excluded by: !**/*.yaml
  • lib/firebase_hausaufgabenheft_logik/pubspec.lock is excluded by: !**/*.lock
  • lib/hausaufgabenheft_logik/pubspec.lock is excluded by: !**/*.lock
  • lib/hausaufgabenheft_logik/pubspec.yaml is excluded by: !**/*.yaml
  • lib/test_randomness/analysis_options.yaml is excluded by: !**/*.yaml
  • lib/test_randomness/pubspec.lock is excluded by: !**/*.lock
  • lib/test_randomness/pubspec.yaml is excluded by: !**/*.yaml
Files selected for processing (19)
  • app/test/dashboard/update_reminder_bloc_test.dart (2 hunks)
  • app/test/feedback/feedback_bloc_test.dart (1 hunks)
  • app/test/feedback/feedback_page_test.dart (1 hunks)
  • app/test/groups/group_join_select_courses_bloc_test.dart (1 hunks)
  • app/test/holidays/holiday_bloc_unit_test.dart (1 hunks)
  • app/test/homework/homework_dialog_test.dart (1 hunks)
  • app/test/homework/teacher/teacher_homework_page_widget_test.dart (1 hunks)
  • app/test/timetable/school_class_filter_view_test.dart (1 hunks)
  • app/test/timetable/timetable_bloc_test.dart (3 hunks)
  • app/test_goldens/blackboard/details/blackboard_item_read_by_users_list_page_test.dart (3 hunks)
  • app/test_goldens/calendrical_events/page/past_calendrical_events_page_test.dart (3 hunks)
  • lib/abgabe/abgabe_client_lib/test/bloc_test.dart (1 hunks)
  • lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart (1 hunks)
  • lib/hausaufgabenheft_logik/test/create_homework_util.dart (3 hunks)
  • lib/test_randomness/.gitignore (1 hunks)
  • lib/test_randomness/.metadata (1 hunks)
  • lib/test_randomness/lib/src/test_randomness.dart (1 hunks)
  • lib/test_randomness/lib/test_randomness.dart (1 hunks)
  • tools/sz_repo_cli/lib/src/commands/src/test_command.dart (10 hunks)
Files skipped from review due to trivial changes (3)
  • lib/test_randomness/.gitignore
  • lib/test_randomness/.metadata
  • lib/test_randomness/lib/test_randomness.dart
Additional comments: 25
lib/hausaufgabenheft_logik/test/create_homework_util.dart (2)
  • 13-13: The import of the test_randomness package is correctly added to support deterministic testing by generating seed-based random IDs.
  • 24-24: The usage of randomAlphaNumeric(5) from the test_randomness package for generating random IDs aligns with the goal of introducing deterministic testing. Ensure that the seed is properly managed across tests for consistent results.
lib/test_randomness/lib/src/test_randomness.dart (1)
  • 1-51: The implementation of the test_randomness library, including the handling of the randomnessSeed, the creation of the szTestRandom object, and the methods for generating random values, is correctly done to support deterministic testing. This approach ensures that tests can produce consistent and predictable outcomes when rerun with the same seed.
app/test_goldens/blackboard/details/blackboard_item_read_by_users_list_page_test.dart (2)
  • 21-21: The import of the test_randomness package is correctly added to support deterministic testing by generating seed-based random boolean values.
  • 43-43: Replacing Random().nextBool() with szTestRandom.nextBool() for generating random boolean values aligns with the goal of deterministic testing. This ensures that the randomness in tests can be controlled and reproduced with the same seed.
app/test/timetable/school_class_filter_view_test.dart (1)
  • 12-12: The import of the test_randomness package replaces random_string, aligning with the goal of deterministic testing by using seed-based random value generation.
app/test_goldens/calendrical_events/page/past_calendrical_events_page_test.dart (2)
  • 22-22: The import of the test_randomness package is correctly added to support deterministic testing by generating seed-based random values.
  • 37-47: Replacing the usage of dart:math's Random with szTestRandom from the test_randomness package for generating random values aligns with the goal of deterministic testing. This ensures that the randomness in tests can be controlled and reproduced with the same seed.
app/test/feedback/feedback_page_test.dart (1)
  • 18-18: The import of the test_randomness package replaces random_string, aligning with the goal of deterministic testing by using seed-based random value generation.
app/test/dashboard/update_reminder_bloc_test.dart (2)
  • 10-13: Reordering of import statements is a minor change that improves the organization of imports.
  • 142-142: Using szTestRandom.nextInt() for generating a random number within the Release class aligns with the goal of deterministic testing. This ensures that the randomness in tests can be controlled and reproduced with the same seed.
tools/sz_repo_cli/lib/src/commands/src/test_command.dart (3)
  • 38-46: The addition of the test-randomize-ordering-seed option to specify a seed for randomizing test case execution order is a valuable enhancement to the testing framework. This allows for more controlled and deterministic testing scenarios.
  • 82-83: Correct usage of the test-randomize-ordering-seed option in the runTests function to pass the seed to the testing framework enhances deterministic testing capabilities.
  • 133-140: The implementation in _runTestsDart to pass the test-randomize-ordering-seed to the Dart testing command correctly supports deterministic testing by allowing the specification of a seed.
app/test/groups/group_join_select_courses_bloc_test.dart (1)
  • 21-21: The import change from random_string to test_randomness is consistent with the PR's objective to introduce determinism into tests. Ensure that all instances of randomness in this test file now utilize the test_randomness package to maintain deterministic behavior.
app/test/holidays/holiday_bloc_unit_test.dart (1)
  • 13-13: The import change from random_string to test_randomness aligns with the PR's goal of deterministic testing. Verify that the test_randomness package is correctly used throughout the test to ensure deterministic outcomes.
app/test/feedback/feedback_bloc_test.dart (1)
  • 17-17: The import change from random_string to test_randomness supports the PR's aim for deterministic testing. Ensure that the test_randomness package is utilized correctly in all tests to achieve deterministic outcomes.
lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart (1)
  • 17-17: The import change from random_string to test_randomness is consistent with the PR's objective to ensure deterministic testing. Confirm that the deterministic approach is correctly applied in functions like randomHomeworkViewWith to maintain consistency across the project.
app/test/timetable/timetable_bloc_test.dart (2)
  • 20-20: The addition of the test_randomness package is aligned with the PR's objective to introduce determinism into tests. This change enables the use of a seed-based approach for generating random numbers, which is crucial for reproducing test scenarios.
  • 52-52: The replacement of Random().nextInt() with szTestRandom.nextInt(200).toString() is a key part of making the tests deterministic. This ensures that the randomness in tests can be controlled and reproduced using a seed. Please ensure that szTestRandom is properly initialized and that its usage here aligns with the intended test scenarios.
app/test/homework/teacher/teacher_homework_page_widget_test.dart (2)
  • 17-17: The modification to prefix flutter.Color with flutter is a good practice to avoid naming conflicts with other Color classes from different packages. This change improves code clarity.
  • 29-29: The addition of the test_randomness package is aligned with the PR's objective to introduce determinism into tests. This change enables the use of a seed-based approach for generating random values, which is crucial for reproducing test scenarios. Ensure that the test_randomness package is used appropriately throughout the tests to replace non-deterministic randomness.
lib/abgabe/abgabe_client_lib/test/bloc_test.dart (2)
  • 28-28: The import statement for test_randomness replaces the previous random_string package, aligning with the PR's objective to introduce determinism into tests. This change is crucial for achieving consistent and predictable test outcomes. Ensure that all instances where randomness is required in tests are now utilizing this new package.
  • 28-28: While the import change to test_randomness is correctly applied, it's important to verify that the functionality provided by this package is properly utilized throughout the test suite. Specifically, look for instances where random values are generated for test purposes and ensure they are now derived from a seeded random generator provided by test_randomness. This might require a deeper review of the test cases and possibly the implementation of the test_randomness package itself.
app/test/homework/homework_dialog_test.dart (1)
  • 28-28: The import statement has been updated from 'package:random_string/random_string.dart' to 'package:test_randomness/test_randomness.dart'. This change aligns with the PR's objective to introduce determinism into tests by leveraging a seed-based approach for randomness. Ensure that all instances where randomness is required in this test file (and others affected by this change) are updated to use the new test_randomness package functionalities appropriately.

Comment on lines 25 to 31
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:path/path.dart';
import 'package:random_string/random_string.dart';
import 'package:test_randomness/test_randomness.dart';
import 'package:sharezone/homework/homework_dialog/homework_dialog.dart';
import 'package:sharezone/homework/homework_dialog/homework_dialog_bloc.dart';
import 'package:sharezone/main/application_bloc.dart';
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-1000]

Overall, the test file appears well-structured and comprehensive, covering a wide range of scenarios for the HomeworkDialog component. However, it's essential to ensure that all randomness in tests, such as generating random IDs, booleans, or other data, now utilizes the test_randomness package's functionalities to maintain determinism. This approach will enhance the reproducibility of test scenarios and facilitate debugging when tests fail. Additionally, consider reviewing the entire test suite for consistency in using the new deterministic randomness approach and ensuring that all tests adhere to best practices for clarity, maintainability, and efficiency.

@@ -25,7 +25,7 @@ import 'package:common_domain_models/common_domain_models.dart';
import 'package:files_basics/files_models.dart';
import 'package:files_basics/local_file.dart';

import 'package:random_string/random_string.dart';
import 'package:test_randomness/test_randomness.dart';
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding documentation or comments explaining the purpose and usage of the test_randomness package within this test suite, especially if there are specific patterns or practices that developers should follow when using this package for deterministic testing. This will help maintain the clarity and maintainability of the test suite.

Copy link

github-actions bot commented Feb 26, 2024

Visit the preview URL for this PR (updated for commit b100ad3):

https://sharezone-website-dev--pr1320-sz-test-randomness-f-39gopfye.web.app

(expires Sun, 10 Mar 2024 20:33:31 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 372b0431a96247f908d9a97d5d865de1c8b3b04e

Copy link

github-actions bot commented Feb 26, 2024

Visit the preview URL for this PR (updated for commit b100ad3):

https://sharezone-test--pr1320-sz-test-randomness-f-dondktz3.web.app

(expires Sun, 10 Mar 2024 20:33:48 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 4cb3ae61e1e018abfd9841fd3239f5b49ccc034b

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 72edc2a and f26812c.
Files selected for processing (2)
  • lib/test_randomness/lib/src/test_randomness.dart (1 hunks)
  • lib/test_randomness/lib/test_randomness.dart (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • lib/test_randomness/lib/src/test_randomness.dart
  • lib/test_randomness/lib/test_randomness.dart

Copy link
Member

@nilsreichardt nilsreichardt left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +31 to +32
test_randomness:
path: ../test_randomness
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this dependency only used in /test, so could it be a dev dependency?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, because the method randomHomeworkViewWith is inside the normal code for a fake "new" teacher bloc with random states that is still left inside the code. I wouldn't want to remove with this PR, so this usage is still present and thus I need to have it in the normal dependencies.

@Jonas-Sander Jonas-Sander added this pull request to the merge queue Mar 7, 2024
Merged via the queue into main with commit f07dfac Mar 7, 2024
33 checks passed
@Jonas-Sander Jonas-Sander deleted the sz-test-randomness-from-seed branch March 7, 2024 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality Code quality itself (readable) but e.g. also how we might enforce better quality automatically. dependencies Changing, updating, adding or removing one or more dependencies. feature: feedback Users can send us feedback to improve the app. feature: groups:classes Specific to only classes (instead of e.g. courses) feature: groups:courses Specific to only courses (instead of e.g. classes) feature: groups Groups umbrella term for courses and classes. feature: holidays feature: homework feature: homework-submissions Submissions can be toggled for homeworks so that pupils can upload their solutions for the teacher. feature: information sheet Information sheets are posted to courses as a way to announce information. feature: timetable / calendar Includes anything regarding lessons (timetable) and events (calendar). sharezone cli testing user: teacher w: dashboard-page Page that shows a summary of all important things (homeworks, events, etc.).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants