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..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 @@ -41,16 +40,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 +57,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 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..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 @@ -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.canDelete()) { + 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() } } 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)