Search in sources :

Example 11 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel 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());
}
Also used : RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) TestingFormStore(org.activityinfo.ui.client.store.TestingFormStore) Test(org.junit.Test)

Example 12 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel 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));
}
Also used : EnumValue(org.activityinfo.model.type.enumerated.EnumValue) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) Test(org.junit.Test)

Example 13 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel 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)

Example 14 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel 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());
}
Also used : ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) Test(org.junit.Test)

Example 15 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelTest method lockedMonthlySubForms.

@Test
public void lockedMonthlySubForms() {
    ClinicForm clinicForm = setup.getCatalog().getClinicForm();
    ResourceId databaseId = ResourceId.valueOf("db1");
    setup.describeDatabase(new UserDatabaseMeta.Builder().setDatabaseId(databaseId).setLabel("My Database").setOwner(true).setVersion("1").addResource(new Resource.Builder().setId(clinicForm.getFormId()).setParentId(databaseId).setLabel(clinicForm.getFormClass().getLabel()).setType(ResourceType.FORM).build()).addResource(new Resource.Builder().setId(clinicForm.getSubForm().getFormId()).setParentId(clinicForm.getFormId()).setLabel(clinicForm.getSubForm().getFormClass().getLabel()).setType(ResourceType.FORM).build()).addLock(new RecordLock.Builder().setId(ResourceId.valueOf("lock1")).setLabel("Archived").setResourceId(clinicForm.getFormId()).setDateRange(LocalDateInterval.year(2017)).build()).build());
    ResourceId consultCountFieldId = clinicForm.getSubForm().getConsultsField().getId();
    FormInputViewModelBuilder builder = builderFor(clinicForm);
    ResourceId parentRecordId = ResourceId.generateId();
    FormInputModel inputModel = new FormInputModel(new RecordRef(clinicForm.getFormId(), parentRecordId));
    FormInputViewModel viewModel = builder.build(inputModel);
    // Get the sub form view model.
    // The active ref should be set to 2017-10 by default.
    ResourceId subFormFieldId = clinicForm.getSubFormField().getId();
    SubFormViewModel subForm = viewModel.getSubForm(subFormFieldId);
    assertThat(subForm, notNullValue());
    // All of 2017 should be locked
    assertTrue(subForm.getActiveSubViewModel().isLocked());
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) Resource(org.activityinfo.model.database.Resource) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) UserDatabaseMeta(org.activityinfo.model.database.UserDatabaseMeta) Test(org.junit.Test)

Aggregations

RecordRef (org.activityinfo.model.type.RecordRef)15 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)15 Test (org.junit.Test)13 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)6 ResourceId (org.activityinfo.model.resource.ResourceId)4 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)4 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)3 LocalDate (org.activityinfo.model.type.time.LocalDate)3 TestingFormStore (org.activityinfo.ui.client.store.TestingFormStore)3 FormTree (org.activityinfo.model.formTree.FormTree)2 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)2 RecordTree (org.activityinfo.model.formTree.RecordTree)2 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)2 Quantity (org.activityinfo.model.type.number.Quantity)2 FormStore (org.activityinfo.ui.client.store.FormStore)2 Predicate (com.google.common.base.Predicate)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 java.util (java.util)1 Collections.emptyMap (java.util.Collections.emptyMap)1