From ef20dfead5974a37670bf63fdfdf0c807731083c Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Wed, 16 Oct 2024 13:40:08 +0100 Subject: [PATCH 1/4] Remove entity forms from save form reset test --- .../feature/settings/ResetProjectTest.kt | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/collect_app/src/androidTest/java/org/odk/collect/android/feature/settings/ResetProjectTest.kt b/collect_app/src/androidTest/java/org/odk/collect/android/feature/settings/ResetProjectTest.kt index 4587001d3e3..661f8d17f09 100644 --- a/collect_app/src/androidTest/java/org/odk/collect/android/feature/settings/ResetProjectTest.kt +++ b/collect_app/src/androidTest/java/org/odk/collect/android/feature/settings/ResetProjectTest.kt @@ -41,16 +41,12 @@ class ResetProjectTest { } @Test - fun canResetSavedFormsAndEntities() { - testDependencies.server.addForm("one-question-entity-registration.xml") - testDependencies.server.addForm( - "one-question-entity-update.xml", - listOf(EntityListItem("people.csv")) - ) + fun canResetSavedForms() { + testDependencies.server.addForm("one-question.xml") rule.withMatchExactlyProject(testDependencies.server.url) - .startBlankForm("One Question Entity Registration") - .fillOutAndFinalize(FormEntryPage.QuestionAndAnswer("Name", "Logan Roy")) + .startBlankForm("One Question") + .fillOutAndFinalize(FormEntryPage.QuestionAndAnswer("what is your age", "34")) .openProjectSettingsDialog() .clickSettings() @@ -62,11 +58,7 @@ class ResetProjectTest { .clickOKOnDialog(MainMenuPage()) .clickDrafts() - .assertTextDoesNotExist("One Question Entity Registration") - .pressBack(MainMenuPage()) - - .startBlankForm("One Question Entity Update") - .assertTextDoesNotExist("Logan Roy") + .assertTextDoesNotExist("One Question") } @Test From b7adc689243979ad6ca86596e6180a424fdfb0a3 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Wed, 16 Oct 2024 14:17:04 +0100 Subject: [PATCH 2/4] Don't reset saved that shouldn't deleted --- .../feature/settings/ResetProjectTest.kt | 1 - .../InstancesDataService.kt | 10 ++++++-- .../android/projects/ProjectResetter.kt | 2 +- .../InstancesDataServiceTest.kt | 25 +++++++++++++++++++ .../odk/collect/formstest/InstanceFixtures.kt | 5 ++-- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/collect_app/src/androidTest/java/org/odk/collect/android/feature/settings/ResetProjectTest.kt b/collect_app/src/androidTest/java/org/odk/collect/android/feature/settings/ResetProjectTest.kt index 661f8d17f09..9f5e0c27523 100644 --- a/collect_app/src/androidTest/java/org/odk/collect/android/feature/settings/ResetProjectTest.kt +++ b/collect_app/src/androidTest/java/org/odk/collect/android/feature/settings/ResetProjectTest.kt @@ -3,7 +3,6 @@ package org.odk.collect.android.feature.settings import org.junit.Rule import org.junit.Test import org.junit.rules.RuleChain -import org.odk.collect.android.support.StubOpenRosaServer.EntityListItem import org.odk.collect.android.support.TestDependencies import org.odk.collect.android.support.pages.AccessControlPage import org.odk.collect.android.support.pages.FormEntryPage diff --git a/collect_app/src/main/java/org/odk/collect/android/instancemanagement/InstancesDataService.kt b/collect_app/src/main/java/org/odk/collect/android/instancemanagement/InstancesDataService.kt index 93dac080d38..a42bc3cabb7 100644 --- a/collect_app/src/main/java/org/odk/collect/android/instancemanagement/InstancesDataService.kt +++ b/collect_app/src/main/java/org/odk/collect/android/instancemanagement/InstancesDataService.kt @@ -22,6 +22,8 @@ import org.odk.collect.androidshared.data.AppState import org.odk.collect.androidshared.data.getData import org.odk.collect.forms.Form import org.odk.collect.forms.instances.Instance +import org.odk.collect.forms.instances.Instance.STATUS_COMPLETE +import org.odk.collect.forms.instances.Instance.STATUS_SUBMISSION_FAILED import org.odk.collect.metadata.PropertyManager import org.odk.collect.projects.ProjectDependencyFactory import java.io.File @@ -180,14 +182,18 @@ class InstancesDataService( } } - fun deleteAll(projectId: String): Boolean { + fun reset(projectId: String): Boolean { val projectDependencyModule = projectDependencyModuleFactory.create(projectId) val instancesRepository = projectDependencyModule.instancesRepository return projectDependencyModule.instancesLock.withLock { acquiredLock: Boolean -> if (acquiredLock) { - instancesRepository.deleteAll() + instancesRepository.all.forEach { + if (it.canDeleteBeforeSend() || it.status != STATUS_COMPLETE && it.status != STATUS_SUBMISSION_FAILED) { + instancesRepository.delete(it.dbId) + } + } update(projectId) true } else { diff --git a/collect_app/src/main/java/org/odk/collect/android/projects/ProjectResetter.kt b/collect_app/src/main/java/org/odk/collect/android/projects/ProjectResetter.kt index 57fcc979c57..53a48d122a7 100644 --- a/collect_app/src/main/java/org/odk/collect/android/projects/ProjectResetter.kt +++ b/collect_app/src/main/java/org/odk/collect/android/projects/ProjectResetter.kt @@ -72,7 +72,7 @@ class ProjectResetter( private fun resetInstances() { entitiesRepositoryFactory.create(projectId).clear() - if (!instancesDataService.deleteAll(projectId) || + if (!instancesDataService.reset(projectId) || !deleteFolderContent(storagePaths.instancesDir) ) { failedResetActions.add(ResetAction.RESET_INSTANCES) diff --git a/collect_app/src/test/java/org/odk/collect/android/instancemanagement/InstancesDataServiceTest.kt b/collect_app/src/test/java/org/odk/collect/android/instancemanagement/InstancesDataServiceTest.kt index 01bc4dd202f..c1f7e8ec189 100644 --- a/collect_app/src/test/java/org/odk/collect/android/instancemanagement/InstancesDataServiceTest.kt +++ b/collect_app/src/test/java/org/odk/collect/android/instancemanagement/InstancesDataServiceTest.kt @@ -17,6 +17,11 @@ import org.odk.collect.android.projects.ProjectDependencyModule import org.odk.collect.android.utilities.ChangeLocks import org.odk.collect.androidshared.data.AppState import org.odk.collect.forms.instances.Instance.STATUS_COMPLETE +import org.odk.collect.forms.instances.Instance.STATUS_INCOMPLETE +import org.odk.collect.forms.instances.Instance.STATUS_INVALID +import org.odk.collect.forms.instances.Instance.STATUS_SUBMISSION_FAILED +import org.odk.collect.forms.instances.Instance.STATUS_SUBMITTED +import org.odk.collect.forms.instances.Instance.STATUS_VALID import org.odk.collect.formstest.FormFixtures import org.odk.collect.formstest.InMemFormsRepository import org.odk.collect.formstest.InMemInstancesRepository @@ -105,4 +110,24 @@ class InstancesDataServiceTest { val result = instancesDataService.sendInstances("projectId") assertThat(result, equalTo(false)) } + + @Test + fun `#reset does not reset instances that can't be deleted before sending`() { + val formsRepository = projectDependencyModule.formsRepository + val form = formsRepository.save(FormFixtures.form()) + + val instancesRepository = projectDependencyModule.instancesRepository + instancesRepository.save(InstanceFixtures.instance(form = form, canDeleteBeforeSend = false, status = STATUS_INCOMPLETE)) + instancesRepository.save(InstanceFixtures.instance(form = form, canDeleteBeforeSend = false, status = STATUS_COMPLETE)) + instancesRepository.save(InstanceFixtures.instance(form = form, canDeleteBeforeSend = false, status = STATUS_INVALID)) + instancesRepository.save(InstanceFixtures.instance(form = form, canDeleteBeforeSend = false, status = STATUS_VALID)) + instancesRepository.save(InstanceFixtures.instance(form = form, canDeleteBeforeSend = false, status = STATUS_SUBMITTED)) + instancesRepository.save(InstanceFixtures.instance(form = form, canDeleteBeforeSend = false, status = STATUS_SUBMISSION_FAILED)) + + instancesDataService.reset(projectDependencyModule.projectId) + val remainingInstances = instancesRepository.all + assertThat(remainingInstances.size, equalTo(2)) + assertThat(remainingInstances.any { it.status == STATUS_COMPLETE }, equalTo(true)) + assertThat(remainingInstances.any { it.status == STATUS_SUBMISSION_FAILED }, equalTo(true)) + } } diff --git a/forms-test/src/main/java/org/odk/collect/formstest/InstanceFixtures.kt b/forms-test/src/main/java/org/odk/collect/formstest/InstanceFixtures.kt index e27e4d0395c..d9551626174 100644 --- a/forms-test/src/main/java/org/odk/collect/formstest/InstanceFixtures.kt +++ b/forms-test/src/main/java/org/odk/collect/formstest/InstanceFixtures.kt @@ -12,7 +12,8 @@ object InstanceFixtures { displayName: String? = null, dbId: Long? = null, form: Form? = null, - deletedDate: Long? = null + deletedDate: Long? = null, + canDeleteBeforeSend: Boolean = true ): Instance { val instancesDir = TempFiles.createTempDir() return InstanceUtils.buildInstance("formId", "version", instancesDir.absolutePath) @@ -26,7 +27,7 @@ object InstanceFixtures { } } .deletedDate(deletedDate) - .canDeleteBeforeSend(true) + .canDeleteBeforeSend(canDeleteBeforeSend) .build() } } From 5e85de34561ad5c6a602e5f5e99d5e653b8386b3 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Wed, 16 Oct 2024 14:19:09 +0100 Subject: [PATCH 3/4] Use helper function --- .../collect/android/instancemanagement/InstancesDataService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/instancemanagement/InstancesDataService.kt b/collect_app/src/main/java/org/odk/collect/android/instancemanagement/InstancesDataService.kt index a42bc3cabb7..cdbaad8fa35 100644 --- a/collect_app/src/main/java/org/odk/collect/android/instancemanagement/InstancesDataService.kt +++ b/collect_app/src/main/java/org/odk/collect/android/instancemanagement/InstancesDataService.kt @@ -190,7 +190,7 @@ class InstancesDataService( return projectDependencyModule.instancesLock.withLock { acquiredLock: Boolean -> if (acquiredLock) { instancesRepository.all.forEach { - if (it.canDeleteBeforeSend() || it.status != STATUS_COMPLETE && it.status != STATUS_SUBMISSION_FAILED) { + if (it.canDelete()) { instancesRepository.delete(it.dbId) } } From 72bdab16b5c195ef43eee0bd79c0225b07b0b27c Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Wed, 16 Oct 2024 16:16:07 +0100 Subject: [PATCH 4/4] Update string --- strings/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/src/main/res/values/strings.xml b/strings/src/main/res/values/strings.xml index 235cf96f2e9..0f09c35f969 100644 --- a/strings/src/main/res/values/strings.xml +++ b/strings/src/main/res/values/strings.xml @@ -981,7 +981,7 @@ Select All Clear All All settings (internal settings, saved settings) - Saved forms and entities (instances folder, instances database, entities database) + Saved forms (instances folder, instances database) Blank forms (forms folder, forms database, itemsets database) Map layers (layers folder) Form load cache (.cache folder)