commit-crimes / shelf-life

1 stars 0 forks source link

Test/127 implement m1 feedback concerning testing #138

Closed alextheprophecy closed 1 week ago

alextheprophecy commented 1 week ago

PR: Test/127 - Implement M1 Feedback Concerning Testing

Summary:

This PR introduces a series of enhancements focused on improving unit test coverage and refactoring for clarity and maintainability inside our models/view-models.

Tests added

  1. in FoodFactsViewModelTest.kt:
    • Tested that Factory companion object correctly instantiates a ViewModel and throws an exception when given an unknown ViewModel class
  2. in OpenFoodFactsRepositoryTest.kt:
    • Fixed current tests that were not actually checking properly their results. Used CountDownLatch for OnSuccess and OnFailure cases, in order to wait before checking results
  3. in FoodItemTest:
    • Added tests for getImportantDetails, getRemainingDays, isExpired, and edge case handling in helper functions.
  4. in ListItemsViewModelTest:
    • Tested failure logging on repository errors, confirmed Factory behavior, and validated exception handling for unknown ViewModel classes.
  5. in HouseHoldViewModelTest:
    • Leveraged ShadowLog to validate error logging on failure, tested updates to Household flows, and verified functions like checkIfHouseholdNameExists work with both populated and empty lists.

Small modifications outside Tests:

  1. Refactored OpenFoodFactsRepository:
    • Added a buildUrl function to streamline URL construction, avoiding potential double-slash issues. This was especially relevant for the testing with mock URLs.
  2. Improved FoodItem Output:
    • Updated getImportantDetails in FoodItem.kt to produce a cleaner, more readable string.
  3. Updated ListFoodItemsViewModel Factory:
    • Modified the Factory object in ListFoodItemsViewModel to accept a FoodItemRepository as a parameter, enhancing flexibility.

Purpose and Impact

This PR is heavily focused on testing to meet M1 feedback criteria and enhance code reliability and readability.

The current batch of tests achieves nearly 95% for model/foodfacts, household, foodItem packages.

Future Improvement

FoodItemRepositoryFirestore needs a test for the init. This however requires some complex mocking of the authorised user, which I was not able to acheive in this PR. ToDo in a future task!

sonarcloud[bot] commented 1 week ago

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
100.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud