use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class FormInputViewModelTest method testSurveyEdit.
@Test
public void testSurveyEdit() {
TestingFormStore store = new TestingFormStore();
Survey survey = store.getCatalog().getSurvey();
RecordRef recordRef = survey.getRecordRef(5);
FormStructure stucture = fetchStructure(recordRef);
FormInputViewModelBuilder builder = new FormInputViewModelBuilder(store, stucture.getFormTree(), new TestingActivePeriodMemory());
FormInputModel inputModel = new FormInputModel(new RecordRef(survey.getFormId(), ResourceId.generateId()));
FormInputViewModel viewModel = builder.build(inputModel, stucture.getExistingRecord());
assertTrue(viewModel.isValid());
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class FormInputViewModelTest method fetchStructure.
private FormStructure fetchStructure(ResourceId formId) {
ResourceId newRecordId = ResourceId.generateSubmissionId(formId);
RecordRef newRecordRef = new RecordRef(formId, newRecordId);
return fetchStructure(newRecordRef);
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class FormInputViewModelTest method testSurveyRelevance.
@Test
public void testSurveyRelevance() {
Survey survey = setup.getCatalog().getSurvey();
FormInputViewModelBuilder builder = builderFor(survey);
// Start with no input
FormInputModel inputModel = new FormInputModel(new RecordRef(survey.getFormId(), ResourceId.generateId()));
// Is this valid?
FormInputViewModel viewModel = builder.build(inputModel);
// Fields with invalid relevance are considered relevant
assertTrue("field with bad relevance is relevant", viewModel.isRelevant(survey.getAgeFieldId()));
assertThat("pregnant is not yet relevant", viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(false));
assertThat("prenatale care is not relevant", viewModel.isRelevant(survey.getPrenataleCareFieldId()), equalTo(false));
assertThat("form is valid", viewModel.isValid(), equalTo(false));
// Answer the gender
inputModel = inputModel.update(survey.getGenderFieldId(), new FieldInput(new EnumValue(survey.getFemaleId())));
viewModel = builder.build(inputModel);
assertThat("pregnant is now relevant", viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(true));
assertThat("prenatale care is still not relevant", viewModel.isRelevant(survey.getPrenataleCareFieldId()), equalTo(false));
// Answer pregnant = yes
inputModel = inputModel.update(survey.getPregnantFieldId(), new FieldInput(new EnumValue(survey.getPregnantId())));
viewModel = builder.build(inputModel);
assertThat("pregnant is still relevant", viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(true));
assertThat("prenatale is now relevant", viewModel.isRelevant(survey.getPrenataleCareFieldId()), equalTo(true));
// Change gender = Male
inputModel = inputModel.update(survey.getGenderFieldId(), new FieldInput(new EnumValue(survey.getMaleId())));
viewModel = builder.build(inputModel);
assertThat("pregnant is not relevant", viewModel.isRelevant(survey.getPregnantFieldId()), equalTo(false));
assertThat("prenatale is not relevant", viewModel.isRelevant(survey.getPrenataleCareFieldId()), equalTo(false));
}
use of org.activityinfo.model.type.RecordRef 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());
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class FormInputViewModelTest method requiredSubFormFields.
@Test
public void requiredSubFormFields() {
BioDataForm bioDataForm = setup.getCatalog().getBioDataForm();
IncidentForm incidentForm = setup.getCatalog().getIncidentForm();
ReferralSubForm referralSubForm = setup.getCatalog().getReferralSubForm();
FormInputViewModelBuilder builder = builderFor(incidentForm);
FormInputModel inputModel = new FormInputModel(new RecordRef(incidentForm.getFormId(), ResourceId.generateId()));
// Fill in required fields
inputModel = inputModel.update(IncidentForm.PROTECTION_CODE_FIELD_ID, new ReferenceValue(bioDataForm.getRecordRef(0)));
// Should be valid as we have only a placeholder sub form...
FormInputViewModel viewModel = builder.build(inputModel);
FormInputViewModel referralRecord = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID).getSubRecords().get(0);
assertTrue(referralRecord.isPlaceholder());
assertThat(viewModel.isValid(), equalTo(true));
// Now add a new referral sub form
// Without completing all required fields, and make sure the form is invalid
inputModel = inputModel.update(referralRecord.getRecordRef(), referralSubForm.getContactNumber().getId(), FieldInput.EMPTY);
viewModel = builder.build(inputModel);
referralRecord = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID).getSubRecords().get(0);
assertFalse("subform is invalid", referralRecord.isValid());
assertFalse("parent is invalid", viewModel.isValid());
}
Aggregations