From fdc4fcf6c979d81fa166c1c299ca85c1869b49fa Mon Sep 17 00:00:00 2001 From: Keworker Date: Sat, 29 Jul 2023 09:04:37 +0500 Subject: [PATCH 1/6] Update packaging.yaml This project doesn't use Jetpack DataStore. --- .google/packaging.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.google/packaging.yaml b/.google/packaging.yaml index f5d25d2e9..def9ece13 100644 --- a/.google/packaging.yaml +++ b/.google/packaging.yaml @@ -41,7 +41,6 @@ solutions: - Flow - JetpackHilt - JetpackRoom - - JetpackDataStore - JetpackWorkManager - JetpackNavigation - JetpackLifecycle From 3c229e06e32fe9fa6de31aaf94e9d38cbd1c5760 Mon Sep 17 00:00:00 2001 From: Keworker <79995640807la@gmail.com> Date: Wed, 2 Aug 2023 11:49:25 +0500 Subject: [PATCH 2/6] Update Room version. Replace Insert with Upsert at PlantDao. --- .../samples/apps/sunflower/data/GardenPlantingDaoTest.kt | 2 +- .../com/google/samples/apps/sunflower/data/PlantDaoTest.kt | 2 +- .../com/google/samples/apps/sunflower/data/PlantDao.kt | 7 +++---- .../samples/apps/sunflower/workers/SeedDatabaseWorker.kt | 2 +- gradle/libs.versions.toml | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt index 6c9734ced..ade47cbaf 100644 --- a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt +++ b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/GardenPlantingDaoTest.kt @@ -47,7 +47,7 @@ class GardenPlantingDaoTest { database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build() gardenPlantingDao = database.gardenPlantingDao() - database.plantDao().insertAll(testPlants) + database.plantDao().upsertAll(testPlants) testGardenPlantingId = gardenPlantingDao.insertGardenPlanting(testGardenPlanting) } diff --git a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt index 709604cae..8f06f12a5 100644 --- a/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt +++ b/app/src/androidTest/java/com/google/samples/apps/sunflower/data/PlantDaoTest.kt @@ -47,7 +47,7 @@ class PlantDaoTest { plantDao = database.plantDao() // Insert plants in non-alphabetical order to test that results are sorted by name - plantDao.insertAll(listOf(plantB, plantC, plantA)) + plantDao.upsertAll(listOf(plantB, plantC, plantA)) } @After fun closeDb() { diff --git a/app/src/main/java/com/google/samples/apps/sunflower/data/PlantDao.kt b/app/src/main/java/com/google/samples/apps/sunflower/data/PlantDao.kt index 1abdca465..e6825fd92 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/data/PlantDao.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/data/PlantDao.kt @@ -17,9 +17,8 @@ package com.google.samples.apps.sunflower.data import androidx.room.Dao -import androidx.room.Insert -import androidx.room.OnConflictStrategy import androidx.room.Query +import androidx.room.Upsert import kotlinx.coroutines.flow.Flow /** @@ -36,6 +35,6 @@ interface PlantDao { @Query("SELECT * FROM plants WHERE id = :plantId") fun getPlant(plantId: String): Flow - @Insert(onConflict = OnConflictStrategy.REPLACE) - suspend fun insertAll(plants: List) + @Upsert + suspend fun upsertAll(plants: List) } diff --git a/app/src/main/java/com/google/samples/apps/sunflower/workers/SeedDatabaseWorker.kt b/app/src/main/java/com/google/samples/apps/sunflower/workers/SeedDatabaseWorker.kt index aa3277dfa..a6058f02e 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/workers/SeedDatabaseWorker.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/workers/SeedDatabaseWorker.kt @@ -42,7 +42,7 @@ class SeedDatabaseWorker( val plantList: List = Gson().fromJson(jsonReader, plantType) val database = AppDatabase.getInstance(applicationContext) - database.plantDao().insertAll(plantList) + database.plantDao().upsertAll(plantList) Result.success() } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d7645583f..3a8bd7b01 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -51,7 +51,7 @@ pagingCompose = "1.0.0-alpha18" profileInstaller = "1.2.0" recyclerView = "1.3.0-alpha02" retrofit = "2.9.0" -room = "2.4.3" +room = "2.5.2" runner = "1.0.1" # @keep targetSdk = "33" From 205adc7cfe1c882c8cbe547e4bf28f1af630fdae Mon Sep 17 00:00:00 2001 From: Keworker <79995640807la@gmail.com> Date: Sun, 6 Aug 2023 19:26:54 +0500 Subject: [PATCH 3/6] Replace additional function with custom matcher for better assertion errors. --- .../apps/sunflower/data/ConvertersTest.kt | 2 +- .../apps/sunflower/data/GardenPlantingTest.kt | 20 ++----- .../samples/apps/sunflower/data/PlantTest.kt | 2 +- .../apps/sunflower/test/HasSameDateWIth.kt | 57 +++++++++++++++++++ .../sunflower/utilities/GrowZoneUtilTest.kt | 2 +- 5 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWIth.kt diff --git a/app/src/test/java/com/google/samples/apps/sunflower/data/ConvertersTest.kt b/app/src/test/java/com/google/samples/apps/sunflower/data/ConvertersTest.kt index 1eca92cbb..a43601484 100644 --- a/app/src/test/java/com/google/samples/apps/sunflower/data/ConvertersTest.kt +++ b/app/src/test/java/com/google/samples/apps/sunflower/data/ConvertersTest.kt @@ -24,7 +24,7 @@ import java.util.Calendar.MONTH import java.util.Calendar.SEPTEMBER import java.util.Calendar.YEAR -class ConvertersTest { +internal class ConvertersTest { private val cal = Calendar.getInstance().apply { set(YEAR, 1998) diff --git a/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt b/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt index 545e07601..7912da85a 100644 --- a/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt +++ b/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt @@ -16,30 +16,20 @@ package com.google.samples.apps.sunflower.data -import org.hamcrest.CoreMatchers.equalTo +import com.google.samples.apps.sunflower.test.HasSameDateWIth.Companion.hasSameDateWith import org.hamcrest.MatcherAssert.assertThat import org.junit.Assert.assertEquals import org.junit.Test import java.util.Calendar -import java.util.Calendar.DAY_OF_MONTH -import java.util.Calendar.MONTH -import java.util.Calendar.YEAR -class GardenPlantingTest { +internal class GardenPlantingTest { @Test fun testDefaultValues() { val gardenPlanting = GardenPlanting("1") - val cal = Calendar.getInstance() - assertYMD(cal, gardenPlanting.plantDate) - assertYMD(cal, gardenPlanting.lastWateringDate) + val calendar = Calendar.getInstance() + assertThat(gardenPlanting.plantDate, hasSameDateWith(calendar)) + assertThat(gardenPlanting.lastWateringDate, hasSameDateWith(calendar)) assertEquals(0L, gardenPlanting.gardenPlantingId) } - - // Only Year/Month/Day precision is needed for comparing GardenPlanting Calendar entries - private fun assertYMD(expectedCal: Calendar, actualCal: Calendar) { - assertThat(actualCal.get(YEAR), equalTo(expectedCal.get(YEAR))) - assertThat(actualCal.get(MONTH), equalTo(expectedCal.get(MONTH))) - assertThat(actualCal.get(DAY_OF_MONTH), equalTo(expectedCal.get(DAY_OF_MONTH))) - } } diff --git a/app/src/test/java/com/google/samples/apps/sunflower/data/PlantTest.kt b/app/src/test/java/com/google/samples/apps/sunflower/data/PlantTest.kt index da847f144..5c20f0130 100644 --- a/app/src/test/java/com/google/samples/apps/sunflower/data/PlantTest.kt +++ b/app/src/test/java/com/google/samples/apps/sunflower/data/PlantTest.kt @@ -24,7 +24,7 @@ import org.junit.Test import java.util.Calendar import java.util.Calendar.DAY_OF_YEAR -class PlantTest { +internal class PlantTest { private lateinit var plant: Plant diff --git a/app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWIth.kt b/app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWIth.kt new file mode 100644 index 000000000..6c5ad93fd --- /dev/null +++ b/app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWIth.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.sunflower.test + +import org.hamcrest.Description +import org.hamcrest.Factory +import org.hamcrest.Matcher +import org.hamcrest.TypeSafeDiagnosingMatcher +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Calendar.DAY_OF_MONTH +import java.util.Calendar.MONTH +import java.util.Calendar.YEAR + +/** + * Calendar matcher. + * Only Year/Month/Day precision is needed for comparing GardenPlanting Calendar entries + */ +internal class HasSameDateWIth( + private val expected: Calendar +) : TypeSafeDiagnosingMatcher() { + private val formatter = SimpleDateFormat("dd.MM.yyyy") + + override fun describeTo(description: Description?) { + description?.appendText(formatter.format(expected.time)) + } + + override fun matchesSafely(actual: Calendar?, mismatchDescription: Description?): Boolean { + return (actual?.let { + actual.get(YEAR) == expected.get(YEAR) && + actual.get(MONTH) == expected.get(MONTH) && + actual.get(DAY_OF_MONTH) == expected.get(DAY_OF_MONTH) + } ?: false).also { _ -> + mismatchDescription?.appendText("was ") + ?.appendText(actual?.time?.let { formatter.format(it) } ?: "null") + } + } + + companion object { + @Factory + fun hasSameDateWith(expected: Calendar): Matcher = HasSameDateWIth(expected) + } +} diff --git a/app/src/test/java/com/google/samples/apps/sunflower/utilities/GrowZoneUtilTest.kt b/app/src/test/java/com/google/samples/apps/sunflower/utilities/GrowZoneUtilTest.kt index 7279389be..54d09e7c2 100644 --- a/app/src/test/java/com/google/samples/apps/sunflower/utilities/GrowZoneUtilTest.kt +++ b/app/src/test/java/com/google/samples/apps/sunflower/utilities/GrowZoneUtilTest.kt @@ -19,7 +19,7 @@ package com.google.samples.apps.sunflower.utilities import org.junit.Assert.assertEquals import org.junit.Test -class GrowZoneUtilTest { +internal class GrowZoneUtilTest { @Test fun getZoneForLatitude() { assertEquals(13, getZoneForLatitude(0.0)) From f91a28fc2b4b58ac33bfaa0b1aa36e4f5427b971 Mon Sep 17 00:00:00 2001 From: Keworker <79995640807la@gmail.com> Date: Fri, 11 Aug 2023 23:07:04 +0500 Subject: [PATCH 4/6] Fix typo in world with. Add better documentation comments for the code. --- .../apps/sunflower/data/GardenPlantingTest.kt | 6 +++--- .../test/{HasSameDateWIth.kt => HasSameDateWith.kt} | 13 +++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) rename app/src/test/java/com/google/samples/apps/sunflower/test/{HasSameDateWIth.kt => HasSameDateWith.kt} (80%) diff --git a/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt b/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt index 7912da85a..1fd83fcc7 100644 --- a/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt +++ b/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt @@ -16,9 +16,9 @@ package com.google.samples.apps.sunflower.data -import com.google.samples.apps.sunflower.test.HasSameDateWIth.Companion.hasSameDateWith +import com.google.samples.apps.sunflower.test.HasSameDateWith.Companion.hasSameDateWith +import org.hamcrest.CoreMatchers.`is` import org.hamcrest.MatcherAssert.assertThat -import org.junit.Assert.assertEquals import org.junit.Test import java.util.Calendar @@ -30,6 +30,6 @@ internal class GardenPlantingTest { val calendar = Calendar.getInstance() assertThat(gardenPlanting.plantDate, hasSameDateWith(calendar)) assertThat(gardenPlanting.lastWateringDate, hasSameDateWith(calendar)) - assertEquals(0L, gardenPlanting.gardenPlantingId) + assertThat(gardenPlanting.gardenPlantingId, `is`(0L)) } } diff --git a/app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWIth.kt b/app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWith.kt similarity index 80% rename from app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWIth.kt rename to app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWith.kt index 6c5ad93fd..50e886509 100644 --- a/app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWIth.kt +++ b/app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWith.kt @@ -30,7 +30,7 @@ import java.util.Calendar.YEAR * Calendar matcher. * Only Year/Month/Day precision is needed for comparing GardenPlanting Calendar entries */ -internal class HasSameDateWIth( +internal class HasSameDateWith( private val expected: Calendar ) : TypeSafeDiagnosingMatcher() { private val formatter = SimpleDateFormat("dd.MM.yyyy") @@ -51,7 +51,16 @@ internal class HasSameDateWIth( } companion object { + /** + * Creates a matcher for [Calendar]s that only matches when year, month and day of + * actual calendar are equal to year, month and day of expected calendar. + * + * For example: + * assertThat(someDate, hasSameDateWith(Calendar.getInstance())) + * + * @param expected calendar that has expected year, month and day [Calendar] + */ @Factory - fun hasSameDateWith(expected: Calendar): Matcher = HasSameDateWIth(expected) + fun hasSameDateWith(expected: Calendar): Matcher = HasSameDateWith(expected) } } From 4c9408133333b104d42220ffda7aad8155f314ac Mon Sep 17 00:00:00 2001 From: Keworker <79995640807la@gmail.com> Date: Thu, 14 Sep 2023 05:47:10 +0500 Subject: [PATCH 5/6] Resolve review comments. Update AGP. --- .../apps/sunflower/data/GardenPlantingTest.kt | 6 +++--- .../{HasSameDateWith.kt => CalendarMatcher.kt} | 15 ++++++++------- gradle/libs.versions.toml | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) rename app/src/test/java/com/google/samples/apps/sunflower/test/{HasSameDateWith.kt => CalendarMatcher.kt} (84%) diff --git a/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt b/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt index 1fd83fcc7..404b72313 100644 --- a/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt +++ b/app/src/test/java/com/google/samples/apps/sunflower/data/GardenPlantingTest.kt @@ -16,7 +16,7 @@ package com.google.samples.apps.sunflower.data -import com.google.samples.apps.sunflower.test.HasSameDateWith.Companion.hasSameDateWith +import com.google.samples.apps.sunflower.test.CalendarMatcher.Companion.equalTo import org.hamcrest.CoreMatchers.`is` import org.hamcrest.MatcherAssert.assertThat import org.junit.Test @@ -28,8 +28,8 @@ internal class GardenPlantingTest { fun testDefaultValues() { val gardenPlanting = GardenPlanting("1") val calendar = Calendar.getInstance() - assertThat(gardenPlanting.plantDate, hasSameDateWith(calendar)) - assertThat(gardenPlanting.lastWateringDate, hasSameDateWith(calendar)) + assertThat(gardenPlanting.plantDate, equalTo(calendar)) + assertThat(gardenPlanting.lastWateringDate, equalTo(calendar)) assertThat(gardenPlanting.gardenPlantingId, `is`(0L)) } } diff --git a/app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWith.kt b/app/src/test/java/com/google/samples/apps/sunflower/test/CalendarMatcher.kt similarity index 84% rename from app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWith.kt rename to app/src/test/java/com/google/samples/apps/sunflower/test/CalendarMatcher.kt index 50e886509..f9ee5eb9e 100644 --- a/app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWith.kt +++ b/app/src/test/java/com/google/samples/apps/sunflower/test/CalendarMatcher.kt @@ -30,7 +30,7 @@ import java.util.Calendar.YEAR * Calendar matcher. * Only Year/Month/Day precision is needed for comparing GardenPlanting Calendar entries */ -internal class HasSameDateWith( +internal class CalendarMatcher( private val expected: Calendar ) : TypeSafeDiagnosingMatcher() { private val formatter = SimpleDateFormat("dd.MM.yyyy") @@ -40,13 +40,14 @@ internal class HasSameDateWith( } override fun matchesSafely(actual: Calendar?, mismatchDescription: Description?): Boolean { - return (actual?.let { - actual.get(YEAR) == expected.get(YEAR) && - actual.get(MONTH) == expected.get(MONTH) && - actual.get(DAY_OF_MONTH) == expected.get(DAY_OF_MONTH) - } ?: false).also { _ -> + return if (actual != null && actual.get(YEAR) == expected.get(YEAR) && + actual.get(MONTH) == expected.get(MONTH) && + actual.get(DAY_OF_MONTH) == expected.get(DAY_OF_MONTH) + ) true + else { mismatchDescription?.appendText("was ") ?.appendText(actual?.time?.let { formatter.format(it) } ?: "null") + false } } @@ -61,6 +62,6 @@ internal class HasSameDateWith( * @param expected calendar that has expected year, month and day [Calendar] */ @Factory - fun hasSameDateWith(expected: Calendar): Matcher = HasSameDateWith(expected) + fun equalTo(expected: Calendar): Matcher = CalendarMatcher(expected) } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d78f30516..fc44a93f9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,7 +16,7 @@ [versions] accessibilityTestFramework = "4.0.0" activityCompose = "1.7.2" -androidGradlePlugin = "8.0.2" +androidGradlePlugin = "8.1.1" benchmark = "1.1.0" # @keep compileSdk = "33" From cfdbc2791097a5ef6820d9624a8bf16627aecfcc Mon Sep 17 00:00:00 2001 From: Keworker <79995640807la@gmail.com> Date: Fri, 20 Oct 2023 06:26:20 +0500 Subject: [PATCH 6/6] Resolve comments. --- .../apps/sunflower/test/CalendarMatcher.kt | 16 +++++++++------- gradle/libs.versions.toml | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/src/test/java/com/google/samples/apps/sunflower/test/CalendarMatcher.kt b/app/src/test/java/com/google/samples/apps/sunflower/test/CalendarMatcher.kt index f9ee5eb9e..7c406a66a 100644 --- a/app/src/test/java/com/google/samples/apps/sunflower/test/CalendarMatcher.kt +++ b/app/src/test/java/com/google/samples/apps/sunflower/test/CalendarMatcher.kt @@ -40,15 +40,17 @@ internal class CalendarMatcher( } override fun matchesSafely(actual: Calendar?, mismatchDescription: Description?): Boolean { - return if (actual != null && actual.get(YEAR) == expected.get(YEAR) && + if (actual == null) { + mismatchDescription?.appendText("was null") + return false + } + if (actual.get(YEAR) == expected.get(YEAR) && actual.get(MONTH) == expected.get(MONTH) && actual.get(DAY_OF_MONTH) == expected.get(DAY_OF_MONTH) - ) true - else { - mismatchDescription?.appendText("was ") - ?.appendText(actual?.time?.let { formatter.format(it) } ?: "null") - false - } + ) + return true + mismatchDescription?.appendText("was ")?.appendText(formatter.format(actual.time)) + return false } companion object { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index cbbda55f4..1261807cb 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,7 +16,7 @@ [versions] accessibilityTestFramework = "4.0.0" activityCompose = "1.7.2" -androidGradlePlugin = "8.1.1" +androidGradlePlugin = "8.0.2" benchmark = "1.1.0" # @keep compileSdk = "33"