Search in sources :

Example 21 with RecordUpdate

use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.

the class FormInputViewModelTest method testNewlyIrrelvantFieldSetToEmpty.

@Test
public void testNewlyIrrelvantFieldSetToEmpty() {
    TestingFormStore store = new TestingFormStore();
    Survey survey = store.getCatalog().getSurvey();
    RecordRef recordRef = survey.getRecordRef(8);
    FormStructure structure = fetchStructure(recordRef);
    FormInputViewModelBuilder builder = new FormInputViewModelBuilder(store, structure.getFormTree(), new TestingActivePeriodMemory());
    FormInputModel inputModel = new FormInputModel(new RecordRef(survey.getFormId(), ResourceId.generateId()));
    // The record was saved as GENDER=Female, and PREGNANT=No
    FormInputViewModel viewModel = builder.build(inputModel, structure.getExistingRecord());
    assertThat(viewModel.getField(survey.getGenderFieldId()), equalTo(new EnumValue(survey.getFemaleId())));
    assertThat(viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(true));
    assertThat(viewModel.getField(survey.getPregnantFieldId()), equalTo(new EnumValue(survey.getPregnantNo())));
    // When we change the Gender to Male, then PREGNANT should be set to empty
    inputModel = inputModel.update(survey.getGenderFieldId(), new EnumValue(survey.getMaleId()));
    viewModel = builder.build(inputModel, structure.getExistingRecord());
    assertThat(viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(false));
    RecordTransaction tx = viewModel.buildTransaction();
    assertThat(tx.getChangeArray(), arrayWithSize(1));
    RecordUpdate update = tx.getChanges().iterator().next();
    assertTrue(update.getFields().get(survey.getPregnantFieldId().asString()).isJsonNull());
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) TestingFormStore(org.activityinfo.ui.client.store.TestingFormStore) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Aggregations

RecordUpdate (org.activityinfo.model.resource.RecordUpdate)21 Test (org.junit.Test)14 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)12 Updater (org.activityinfo.store.query.server.Updater)11 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)3 ResourceId (org.activityinfo.model.resource.ResourceId)3 RecordRef (org.activityinfo.model.type.RecordRef)3 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)3 HashMap (java.util.HashMap)2 FormInstance (org.activityinfo.model.form.FormInstance)2 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)2 RecordTransactionBuilder (org.activityinfo.model.resource.RecordTransactionBuilder)2 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)2 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Map (java.util.Map)1 JsOverlay (jsinterop.annotations.JsOverlay)1 FieldMapping (org.activityinfo.geoadmin.merge2.view.mapping.FieldMapping)1