-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add StatsDataSource and refactor StatsRepository #22513
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
Conversation
- Create StatsDataSource interface and implementation to abstract data fetching - Add StatsModule for dependency injection - Refactor StatsRepository to use the new data source - Rename todaysstat -> todaystats package in newstats - Add StatsRepositoryTest with comprehensive test coverage
Generated by 🚫 Danger |
- Create StatsDataSource interface and implementation to abstract data fetching - Add StatsModule for dependency injection - Refactor StatsRepository to use the new data source - Rename todaysstat -> todaystats package in newstats - Add StatsRepositoryTest with comprehensive test coverage
…/github.com/wordpress-mobile/WordPress-Android into feat/CMM-1143-new-stats-views-card-tests-i2 # Conflicts: # WordPress/src/main/java/org/wordpress/android/ui/newstats/datasource/StatsDataSourceImpl.kt
|
| App Name | Jetpack | |
| Flavor | Jalapeno | |
| Build Type | Debug | |
| Version | pr22513-d93b3f7 | |
| Commit | d93b3f7 | |
| Direct Download | jetpack-prototype-build-pr22513-d93b3f7.apk |
|
| App Name | WordPress | |
| Flavor | Jalapeno | |
| Build Type | Debug | |
| Version | pr22513-d93b3f7 | |
| Commit | d93b3f7 | |
| Direct Download | wordpress-prototype-build-pr22513-d93b3f7.apk |
|
Claude finished @adalpari's task in 2m 15s —— View job Code Review Complete ✅I've completed a comprehensive review of this PR. Overall, this is a well-structured refactoring that successfully introduces a testable abstraction layer. The code quality is high, with excellent documentation and comprehensive test coverage. Here are my findings: ✨ Strengths
🐛 Issues Found1. Line Length Violation (Minor)File: The test function name exceeds 120 characters: fun `given successful response, when fetchDailyViewsForWeek is called, then data source is called with DAY unit`() =Recommendation: Shorten to fit within 120 characters: fun `given successful response, when fetchDailyViewsForWeek called, then uses DAY unit`() =2. Incomplete Error Handling in StatsDataSourceImpl (Medium)File: else -> {
StatsVisitsDataResult.Error("Unknown error")
}The Recommendation: Handle all when (result) {
is WpRequestResult.Success -> { /* ... */ }
is WpRequestResult.WpError -> {
StatsVisitsDataResult.Error(result.errorMessage)
}
is WpRequestResult.NetworkError -> {
StatsVisitsDataResult.Error("Network error: ${result.message}")
}
is WpRequestResult.HttpError -> {
StatsVisitsDataResult.Error("HTTP error ${result.code}: ${result.message}")
}
// Handle other specific cases
}This provides better error messages for debugging and user feedback. 3. Potential Race Condition in Lazy Initialization (Low)File: private val wpComApiClient: WpComApiClient by lazy {
check(accessToken != null) { "DataSource not initialized" }
wpComApiClientProvider.getWpComApiClient(accessToken!!)
}While Recommendation: Use private val wpComApiClient: WpComApiClient by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
check(accessToken != null) { "DataSource not initialized" }
wpComApiClientProvider.getWpComApiClient(accessToken!!)
}Or alternatively, make private var wpComApiClient: WpComApiClient? = null
override fun init(accessToken: String) {
this.accessToken = accessToken
this.wpComApiClient = wpComApiClientProvider.getWpComApiClient(accessToken)
}4. Missing Test Coverage for StatsDataSourceImpl (Medium)File: As noted by Danger, Recommendation: Add tests for
5. Inconsistent Null Checking (Low)File: The code checks Recommendation: Choose one approach:
I recommend the second approach for cleaner code: override suspend fun fetchStatsVisits(...): StatsVisitsDataResult {
// Remove this check since wpComApiClient.lazy will check it
val params = StatsVisitsParams(...)
val result = wpComApiClient.request { ... }
// ...
}💡 Suggestions for Improvement1. Consider Adding Metrics for Empty Data (Minor)File: When 2. ThreadLocal DateFormat Documentation (Documentation)File: The comment explains why 3. Test Organization (Minor)File: The tests are well-organized with region comments. Consider extracting common test data into a 4. Package Naming Consistency (Positive Feedback)The rename from 🔒 Security Review✅ No security concerns identified:
⚡ Performance Review✅ Performance looks good:
One note: The 📋 Testing ReviewTest Coverage: Excellent ✅
Test Quality: High ✅
Missing: Unit tests for 📝 Summary
Recommendation: Approve with minor improvements The refactoring successfully achieves its goals. The identified issues are mostly minor and can be addressed in follow-up commits. The only medium-priority items are:
Great work on this refactoring! The abstraction layer will make future testing much easier. |
WordPress/src/test/java/org/wordpress/android/ui/newstats/repository/StatsRepositoryTest.kt
Show resolved
Hide resolved
WordPress/src/main/java/org/wordpress/android/ui/newstats/datasource/StatsDataSourceImpl.kt
Show resolved
Hide resolved
WordPress/src/main/java/org/wordpress/android/ui/newstats/datasource/StatsDataSourceImpl.kt
Outdated
Show resolved
Hide resolved
WordPress/src/main/java/org/wordpress/android/ui/newstats/datasource/StatsDataSourceImpl.kt
Outdated
Show resolved
Hide resolved
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #22513 +/- ##
==========================================
+ Coverage 38.67% 38.83% +0.15%
==========================================
Files 2199 2201 +2
Lines 106874 106879 +5
Branches 15062 15055 -7
==========================================
+ Hits 41337 41507 +170
+ Misses 62094 61929 -165
Partials 3443 3443 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@nbradbury I forgot to set this PR to "Ready to review" 😓 |
nbradbury
left a comment
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.
@adalpari This looks good and I'll approve it, but I'm not sure if you want to address anything mentioned by Claude before merging ![]()
Thank you. Yes, I did achieve some of its concerns, and I keep working on the code in the next iteration PR |





Description
This PR introduces a
StatsDataSourceabstraction layer to decoupleStatsRepositoryfrom thewordpress-rs uniffi objects, enabling proper unit testing. The refactoring extracts API communication
into a separate data source interface that can be mocked in tests.
Key changes:
StatsDataSourceinterface with data models (StatsVisitsData,StatsUnit, etc.)StatsDataSourceImplthat wraps the wordpress-rs API clientStatsRepositoryto use the newStatsDataSourceinterfaceStatsModuleDagger Hilt module to bind the implementationStatsRepository(25 test cases)todaysstat→todaysstatsfor correct plural namingTesting instructions
Unit tests: