test: add unit tests for SyncJobDef and NetworkServiceImpl#1036
test: add unit tests for SyncJobDef and NetworkServiceImpl#1036andoriyaprashant wants to merge 1 commit into
Conversation
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughThis PR adds two comprehensive unit test suites for the Android Registration Client: ChangesNetworkServiceImpl Test Suite
SyncJobDef Test Suite
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Signed-off-by: andoriyaprashant <prashantandoriya@gmail.com>
9212c1f to
d244bdb
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
test/network_service_impl_test.dart (1)
253-269: ⚡ Quick winThese async “logic tests” can pass even when calls fail.
isA<Future<String>>()only checks the returned object type, not completion/result. This does not validate async behavior and can miss runtime channel failures.Suggested fix
group('checkInternetConnection Logic Tests', () { - test('method returns Future<String> type', () { - expect( - networkService.checkInternetConnection(), - isA<Future<String>>(), - ); + test('method completes with a String', () async { + await expectLater( + networkService.checkInternetConnection(), + completion(isA<String>()), + ); }); }); group('getVersionNoApp Logic Tests', () { - test('method returns Future<String> type', () { - expect( - networkService.getVersionNoApp(), - isA<Future<String>>(), - ); + test('method completes with a String', () async { + await expectLater( + networkService.getVersionNoApp(), + completion(isA<String>()), + ); }); });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/network_service_impl_test.dart` around lines 253 - 269, The tests only assert the returned object type (isA<Future<String>>), which doesn't verify the Future completes or yields a valid value; change the tests for checkInternetConnection and getVersionNoApp to actually await or assert completion and result type—use expect(networkService.checkInternetConnection(), completion(isA<String>())) or expect(networkService.checkInternetConnection(), completes) and then await the call and assert the returned String; do the same for getVersionNoApp to ensure channel/platform errors surface rather than just type-checking the Future.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/network_service_impl_test.dart`:
- Around line 208-218: The test is injecting platform errors into the wrong
channel (mockPlatformError(saveVersionChannels)) so the
saveScreenHeaderToGlobalParam error path isn't exercised; update the test to
call mockPlatformError with the screen-header method channel (e.g.,
saveScreenHeaderChannels or the exact channel constant used by
networkService.saveScreenHeaderToGlobalParam) instead of saveVersionChannels so
the platform exception is routed to networkService.saveScreenHeaderToGlobalParam
during the test.
In `@test/sync_job_def_test.dart`:
- Around line 135-167: Add tests under the existing "Boolean Field Tests" group
that pass malformed boolean values into SyncJobDef.fromJson (e.g., {'isDeleted':
'true'}, {'isDeleted': 1}, {'isActive': 'false'}, {'isActive': 0}) and assert
the parser rejects them by expecting a thrown error (use expect(...,
throwsA(...))) to lock the parsing contract; reference SyncJobDef.fromJson and
the isDeleted/isActive fields so the tests fail if the deserializer silently
accepts non-boolean types.
---
Nitpick comments:
In `@test/network_service_impl_test.dart`:
- Around line 253-269: The tests only assert the returned object type
(isA<Future<String>>), which doesn't verify the Future completes or yields a
valid value; change the tests for checkInternetConnection and getVersionNoApp to
actually await or assert completion and result type—use
expect(networkService.checkInternetConnection(), completion(isA<String>())) or
expect(networkService.checkInternetConnection(), completes) and then await the
call and assert the returned String; do the same for getVersionNoApp to ensure
channel/platform errors surface rather than just type-checking the Future.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: a7e39192-6ecf-4e11-a39c-f3864e971b01
📒 Files selected for processing (2)
test/network_service_impl_test.darttest/sync_job_def_test.dart
| test('saveScreenHeaderToGlobalParam returns empty string on platform error', | ||
| () async { | ||
| mockPlatformError(saveVersionChannels); | ||
|
|
||
| final response = await networkService.saveScreenHeaderToGlobalParam( | ||
| 'header', | ||
| 'value', | ||
| ); | ||
|
|
||
| expect(response, ''); | ||
| }); |
There was a problem hiding this comment.
Mocked error channel is likely wrong for saveScreenHeaderToGlobalParam.
Line 210 injects platform errors into saveVersionChannels, not the screen-header method channel. This can make the test pass without exercising the intended error path.
Suggested fix
+ const List<String> saveScreenHeaderChannels = <String>[
+ 'dev.flutter.pigeon.registration_client.CommonDetailsApi.saveScreenHeaderToGlobalParam',
+ 'dev.flutter.pigeon.registration_client.pigeon.CommonDetailsApi.saveScreenHeaderToGlobalParam',
+ ];
+
void clearMockHandlers() {
for (final channel in <String>[
...saveVersionChannels,
...getVersionChannels,
+ ...saveScreenHeaderChannels,
]) {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMessageHandler(channel, null);
}
}
setUp(() {
networkService = NetworkServiceImpl();
mockStringResponse(saveVersionChannels, 'saved_successfully');
mockStringResponse(getVersionChannels, '1.0.0');
+ mockStringResponse(saveScreenHeaderChannels, 'saved_successfully');
});
test('saveScreenHeaderToGlobalParam returns empty string on platform error',
() async {
- mockPlatformError(saveVersionChannels);
+ mockPlatformError(saveScreenHeaderChannels);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| test('saveScreenHeaderToGlobalParam returns empty string on platform error', | |
| () async { | |
| mockPlatformError(saveVersionChannels); | |
| final response = await networkService.saveScreenHeaderToGlobalParam( | |
| 'header', | |
| 'value', | |
| ); | |
| expect(response, ''); | |
| }); | |
| const List<String> saveScreenHeaderChannels = <String>[ | |
| 'dev.flutter.pigeon.registration_client.CommonDetailsApi.saveScreenHeaderToGlobalParam', | |
| 'dev.flutter.pigeon.registration_client.pigeon.CommonDetailsApi.saveScreenHeaderToGlobalParam', | |
| ]; | |
| void clearMockHandlers() { | |
| for (final channel in <String>[ | |
| ...saveVersionChannels, | |
| ...getVersionChannels, | |
| ...saveScreenHeaderChannels, | |
| ]) { | |
| TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger | |
| .setMockMessageHandler(channel, null); | |
| } | |
| } | |
| setUp(() { | |
| networkService = NetworkServiceImpl(); | |
| mockStringResponse(saveVersionChannels, 'saved_successfully'); | |
| mockStringResponse(getVersionChannels, '1.0.0'); | |
| mockStringResponse(saveScreenHeaderChannels, 'saved_successfully'); | |
| }); | |
| test('saveScreenHeaderToGlobalParam returns empty string on platform error', | |
| () async { | |
| mockPlatformError(saveScreenHeaderChannels); | |
| final response = await networkService.saveScreenHeaderToGlobalParam( | |
| 'header', | |
| 'value', | |
| ); | |
| expect(response, ''); | |
| }); |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@test/network_service_impl_test.dart` around lines 208 - 218, The test is
injecting platform errors into the wrong channel
(mockPlatformError(saveVersionChannels)) so the saveScreenHeaderToGlobalParam
error path isn't exercised; update the test to call mockPlatformError with the
screen-header method channel (e.g., saveScreenHeaderChannels or the exact
channel constant used by networkService.saveScreenHeaderToGlobalParam) instead
of saveVersionChannels so the platform exception is routed to
networkService.saveScreenHeaderToGlobalParam during the test.
| group('Boolean Field Tests', () { | ||
| test('isDeleted supports true value', () { | ||
| final syncJobDef = SyncJobDef.fromJson({ | ||
| 'isDeleted': true, | ||
| }); | ||
|
|
||
| expect(syncJobDef.isDeleted, true); | ||
| }); | ||
|
|
||
| test('isDeleted supports false value', () { | ||
| final syncJobDef = SyncJobDef.fromJson({ | ||
| 'isDeleted': false, | ||
| }); | ||
|
|
||
| expect(syncJobDef.isDeleted, false); | ||
| }); | ||
|
|
||
| test('isActive supports true value', () { | ||
| final syncJobDef = SyncJobDef.fromJson({ | ||
| 'isActive': true, | ||
| }); | ||
|
|
||
| expect(syncJobDef.isActive, true); | ||
| }); | ||
|
|
||
| test('isActive supports false value', () { | ||
| final syncJobDef = SyncJobDef.fromJson({ | ||
| 'isActive': false, | ||
| }); | ||
|
|
||
| expect(syncJobDef.isActive, false); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Add invalid-type boolean tests to lock parser contract.
You validate true/false, but not malformed inputs (e.g., 'true', 1). Adding these catches type-safety regressions in fromJson.
Suggested test additions
group('Boolean Field Tests', () {
@@
test('isActive supports false value', () {
final syncJobDef = SyncJobDef.fromJson({
'isActive': false,
});
expect(syncJobDef.isActive, false);
});
+
+ test('isDeleted rejects non-boolean values', () {
+ expect(
+ () => SyncJobDef.fromJson({'isDeleted': 'true'}),
+ throwsA(isA<TypeError>()),
+ );
+ });
+
+ test('isActive rejects non-boolean values', () {
+ expect(
+ () => SyncJobDef.fromJson({'isActive': 1}),
+ throwsA(isA<TypeError>()),
+ );
+ });
});🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@test/sync_job_def_test.dart` around lines 135 - 167, Add tests under the
existing "Boolean Field Tests" group that pass malformed boolean values into
SyncJobDef.fromJson (e.g., {'isDeleted': 'true'}, {'isDeleted': 1}, {'isActive':
'false'}, {'isActive': 0}) and assert the parser rejects them by expecting a
thrown error (use expect(..., throwsA(...))) to lock the parsing contract;
reference SyncJobDef.fromJson and the isDeleted/isActive fields so the tests
fail if the deserializer silently accepts non-boolean types.
|
Hi @SachinPremkumar @Pragya279 could you please review this Pull request and let me know if any changes are needed |
Fixes #1035
Summary
Added comprehensive unit tests for:
Changes Made
SyncJobDef Tests
NetworkServiceImpl Tests
Summary by CodeRabbit