Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,39 @@ class BreezyWeatherFetcherTest {
private val prefsName = "lwidget_breezy_weather_data"
private val keyWeatherJson = "weather_json"

private lateinit var mockEditor: SharedPreferences.Editor

@Before
fun setup() {
mockPrefs = mock()
mockEditor = mock {
on { putString(org.mockito.kotlin.any(), org.mockito.kotlin.any()) } doReturn it
}
mockPrefs = mock {
on { edit() } doReturn mockEditor
}
mockContext = mock {
on { getSharedPreferences(prefsName, Context.MODE_PRIVATE) } doReturn mockPrefs
}
}

@Test
fun `saveLatestWeatherData saves json string to preferences`() {
val validJson = """{"temp": 20}"""

BreezyWeatherFetcher.saveLatestWeatherData(mockContext, validJson)

org.mockito.kotlin.verify(mockEditor).putString(keyWeatherJson, validJson)
org.mockito.kotlin.verify(mockEditor).apply()
}

@Test
fun `saveLatestWeatherData with empty string saves empty string`() {
BreezyWeatherFetcher.saveLatestWeatherData(mockContext, "")

org.mockito.kotlin.verify(mockEditor).putString(keyWeatherJson, "")
org.mockito.kotlin.verify(mockEditor).apply()
}

@Test
fun `fetchLocalWeather with valid json returns parsed data`() {
val validJson = """
Expand Down
5 changes: 5 additions & 0 deletions pr_description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
🎯 **What:** The testing gap addressed: Added tests for the `saveLatestWeatherData` function in `BreezyWeatherFetcher.kt` which was missing from the test suite.

πŸ“Š **Coverage:** What scenarios are now tested: The test covers the interaction with `SharedPreferences.Editor`, ensuring both `putString` and `apply` are called correctly. It tests the happy path with valid JSON as well as an edge case with an empty string.

✨ **Result:** The improvement in test coverage: Increased test coverage for `BreezyWeatherFetcher`, guaranteeing that weather data caching works as expected.
Loading