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());
}
Aggregations