From 58d5d7d6bb285e1ce53885ddb74651ffe468a3f9 Mon Sep 17 00:00:00 2001 From: LeanBitLab <245915690+LeanBitLab@users.noreply.github.com> Date: Thu, 28 May 2026 19:06:26 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20[testing]=20Add=20missing=20test?= =?UTF-8?q?=20for=20saveLatestWeatherData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weather/BreezyWeatherFetcherTest.kt | 27 ++++++++++++++++++- pr_description.txt | 5 ++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 pr_description.txt diff --git a/app/src/test/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcherTest.kt b/app/src/test/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcherTest.kt index 14a7a31..e4a7939 100644 --- a/app/src/test/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcherTest.kt +++ b/app/src/test/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcherTest.kt @@ -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 = """ diff --git a/pr_description.txt b/pr_description.txt new file mode 100644 index 0000000..494a54f --- /dev/null +++ b/pr_description.txt @@ -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.