Search in sources :

Example 6 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class FormsRepositoryTest method getAllNotDeletedByFormIdAndVersion_onlyReturnsNotDeletedFormsThatMatchVersion.

@Test
public void getAllNotDeletedByFormIdAndVersion_onlyReturnsNotDeletedFormsThatMatchVersion() {
    FormsRepository formsRepository = buildSubject();
    formsRepository.save(FormUtils.buildForm("id", "1", getFormFilesPath()).deleted(true).build());
    formsRepository.save(FormUtils.buildForm("id", "1", getFormFilesPath()).deleted(false).build());
    formsRepository.save(FormUtils.buildForm("id", "2", getFormFilesPath()).deleted(true).build());
    formsRepository.save(FormUtils.buildForm("id", "2", getFormFilesPath()).deleted(false).build());
    List<Form> forms = formsRepository.getAllNotDeletedByFormIdAndVersion("id", "2");
    assertThat(forms.size(), is(1));
    assertThat(forms.get(0).getVersion(), equalTo("2"));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Example 7 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class FormsRepositoryTest method save_addsHashBasedOnFormFile.

@Test
public void save_addsHashBasedOnFormFile() {
    FormsRepository formsRepository = buildSubject();
    Form form = FormUtils.buildForm("id", "version", getFormFilesPath()).build();
    assertThat(form.getMD5Hash(), equalTo(null));
    formsRepository.save(form);
    String expectedHash = Md5.getMd5Hash(new File(form.getFormFilePath()));
    assertThat(formsRepository.get(1L).getMD5Hash(), equalTo(expectedHash));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) File(java.io.File) Test(org.junit.Test)

Example 8 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class FormsRepositoryTest method getAllByFormIdAndVersion_whenFormHasNullVersion_returnsAllMatchingForms.

@Test
public void getAllByFormIdAndVersion_whenFormHasNullVersion_returnsAllMatchingForms() {
    FormsRepository formsRepository = buildSubject();
    formsRepository.save(FormUtils.buildForm("1", null, getFormFilesPath()).build());
    formsRepository.save(FormUtils.buildForm("1", null, getFormFilesPath()).build());
    formsRepository.save(FormUtils.buildForm("1", "7", getFormFilesPath()).build());
    List<Form> forms = formsRepository.getAllByFormIdAndVersion("1", null);
    assertThat(forms.size(), is(2));
    assertThat(forms.get(0).getVersion(), is(nullValue()));
    assertThat(forms.get(1).getVersion(), is(nullValue()));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Example 9 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class InMemFormsRepository method delete.

@Override
public void delete(Long id) {
    Optional<Form> formToRemove = forms.stream().filter(f -> f.getDbId().equals(id)).findFirst();
    if (formToRemove.isPresent()) {
        Form form = formToRemove.get();
        deleteFilesForForm(form);
        forms.remove(form);
    }
}
Also used : IOException(java.io.IOException) TempFiles(org.odk.collect.shared.TempFiles) FormsRepository(org.odk.collect.forms.FormsRepository) Supplier(java.util.function.Supplier) File(java.io.File) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) Form(org.odk.collect.forms.Form) FileUtils.deleteDirectory(org.apache.commons.io.FileUtils.deleteDirectory) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Md5(org.odk.collect.shared.strings.Md5) Optional(java.util.Optional) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) Form(org.odk.collect.forms.Form)

Example 10 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class FormsRepositoryTest method deleteByMd5Hash_deletesFormsWithMatchingHash.

@Test
public void deleteByMd5Hash_deletesFormsWithMatchingHash() {
    FormsRepository formsRepository = buildSubject();
    formsRepository.save(FormUtils.buildForm("id1", "version", getFormFilesPath()).build());
    formsRepository.save(FormUtils.buildForm("id1", "version", getFormFilesPath()).build());
    formsRepository.save(FormUtils.buildForm("id2", "version", getFormFilesPath()).build());
    List<Form> id1Forms = formsRepository.getAllByFormIdAndVersion("id1", "version");
    assertThat(id1Forms.size(), is(2));
    assertThat(id1Forms.get(0).getMD5Hash(), is(id1Forms.get(1).getMD5Hash()));
    formsRepository.deleteByMd5Hash(id1Forms.get(0).getMD5Hash());
    assertThat(formsRepository.getAll().size(), is(1));
    assertThat(formsRepository.getAll().get(0).getFormId(), is("id2"));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Aggregations

Form (org.odk.collect.forms.Form)62 Test (org.junit.Test)35 File (java.io.File)22 FormsRepository (org.odk.collect.forms.FormsRepository)21 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Analytics (org.odk.collect.analytics.Analytics)13 FormSource (org.odk.collect.forms.FormSource)12 FormUtils.buildForm (org.odk.collect.formstest.FormUtils.buildForm)12 MediaFile (org.odk.collect.forms.MediaFile)9 FormsRepositoryProvider (org.odk.collect.android.utilities.FormsRepositoryProvider)7 ManifestFile (org.odk.collect.forms.ManifestFile)7 Instance (org.odk.collect.forms.instances.Instance)7 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)7 ArrayList (java.util.ArrayList)5 FormController (org.odk.collect.android.javarosawrapper.FormController)4 ViewModelProvider (androidx.lifecycle.ViewModelProvider)3 DatabaseObjectMapper.getValuesFromForm (org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromForm)3 Intent (android.content.Intent)2 View (android.view.View)2 TextView (android.widget.TextView)2