Search in sources :

Example 51 with FormTree

use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.

the class LookupViewModelTest method multipleLeafKeys.

@Test
public void multipleLeafKeys() {
    IdpLocationForm locationForm = catalog.getIdpLocationForm();
    FormTree formTree = catalog.getFormTree(locationForm.getFormId());
    FormField referenceField = new FormField(ResourceId.generateFieldId(ReferenceType.TYPE_CLASS)).setType(new ReferenceType(Cardinality.SINGLE, locationForm.getFormId()));
    LookupViewModel viewModel = new LookupViewModel(setup.getFormStore(), formTree, referenceField);
    assertThat(viewModel.getLookupKeys(), hasSize(3));
    LookupKeyViewModel province = viewModel.getLookupKeys().get(0);
    LookupKeyViewModel type = viewModel.getLookupKeys().get(1);
    LookupKeyViewModel name = viewModel.getLookupKeys().get(2);
    Connection<List<String>> provinceChoices = setup.connect(province.getChoices());
    Connection<List<String>> typeChoices = setup.connect(type.getChoices());
    Connection<List<String>> nameChoices = setup.connect(name.getChoices());
    Connection<Optional<RecordRef>> selection = setup.connect(viewModel.getSelectedRecord());
    viewModel.select(province.getLookupKey(), provinceChoices.assertLoaded().get(0));
    assertThat(typeChoices.assertLoaded(), hasItems(equalTo("Camp"), equalTo("Clinic"), equalTo("School")));
    viewModel.select(type.getLookupKey(), "Camp");
    assertThat(nameChoices.assertLoaded(), hasItems("Location 34", "Location 54", "Location 76"));
    viewModel.select(name.getLookupKey(), "Location 76");
    assertThat(selection.assertLoaded().get(), equalTo(new RecordRef(locationForm.getFormId(), ResourceId.valueOf("c75"))));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) Optional(com.google.common.base.Optional) RecordRef(org.activityinfo.model.type.RecordRef) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType) Test(org.junit.Test)

Example 52 with FormTree

use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.

the class LookupViewModelTest method nullKeys.

@Test
public void nullKeys() {
    FormClass keyForm = new FormClass(ResourceId.valueOf("FORM1"));
    keyForm.addField(ResourceId.valueOf("PROVINCE")).setLabel("Province").setKey(true).setRequired(true).setType(TextType.SIMPLE);
    keyForm.addField(ResourceId.valueOf("SCHOOL")).setLabel("School").setKey(true).setRequired(true).setType(TextType.SIMPLE);
    FormClass form = new FormClass(ResourceId.valueOf("FORM2"));
    FormField referenceField = new FormField(ResourceId.generateFieldId(ReferenceType.TYPE_CLASS));
    ReferenceType referenceType = new ReferenceType(Cardinality.SINGLE, ResourceId.valueOf("FORM1"));
    referenceField.setType(referenceType);
    form.addField(ResourceId.valueOf("PROJECT")).setLabel("Project name").setType(referenceType);
    FormTreeBuilder treeBuilder = new FormTreeBuilder(new FormMetadataProviderStub(form, keyForm));
    FormTree formTree = treeBuilder.queryTree(form.getId());
    Map<String, ColumnView> columnSet = new HashMap<>();
    columnSet.put("id", new StringArrayColumnView(Arrays.asList("R1", "R2", "R3", "R4")));
    columnSet.put("k1", new StringArrayColumnView(Arrays.asList("PZ", null, "PA", "PA")));
    columnSet.put("k2", new StringArrayColumnView(Arrays.asList("S1", "S2", null, "S3")));
    FormSource formSource = EasyMock.createMock(FormSource.class);
    EasyMock.expect(formSource.query(EasyMock.anyObject(QueryModel.class))).andReturn(Observable.just(new ColumnSet(3, columnSet))).anyTimes();
    EasyMock.replay(formSource);
    LookupViewModel viewModel = new LookupViewModel(formSource, formTree, referenceField);
    LookupKeyViewModel provinceKey = viewModel.getLookupKeys().get(0);
    assertThat(provinceKey.getChoices().get(), contains("PA", "PZ"));
    viewModel.select(provinceKey.getLookupKey(), "PA");
    LookupKeyViewModel schoolKey = viewModel.getLookupKeys().get(1);
    Connection<List<String>> schoolChoices = new Connection<>(schoolKey.getChoices());
    assertThat(schoolChoices.assertLoaded(), contains("S3"));
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) StringArrayColumnView(org.activityinfo.model.query.StringArrayColumnView) Connection(org.activityinfo.observable.Connection) ColumnSet(org.activityinfo.model.query.ColumnSet) ReferenceType(org.activityinfo.model.type.ReferenceType) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) FormTree(org.activityinfo.model.formTree.FormTree) FormClass(org.activityinfo.model.form.FormClass) FormSource(org.activityinfo.store.query.shared.FormSource) FormField(org.activityinfo.model.form.FormField) StringArrayColumnView(org.activityinfo.model.query.StringArrayColumnView) FormMetadataProviderStub(org.activityinfo.model.formTree.FormMetadataProviderStub) Test(org.junit.Test)

Example 53 with FormTree

use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.

the class LookupViewModelTest method noKeysTest.

@Test
public void noKeysTest() {
    TestingStorageProvider catalog = new TestingStorageProvider();
    NfiForm nfiForm = catalog.getNfiForm();
    FormTree formTree = catalog.getFormTree(nfiForm.getFormId());
    FormField referenceField = new FormField(ResourceId.generateFieldId(ReferenceType.TYPE_CLASS)).setType(new ReferenceType(Cardinality.SINGLE, nfiForm.getFormId()));
    LookupViewModel viewModel = new LookupViewModel(setup.getFormStore(), formTree, referenceField);
    LookupKeyViewModel keyViewModel = viewModel.getLookupKeys().get(0);
    Connection<List<String>> choices = setup.connect(keyViewModel.getChoices());
    assertThat(choices.assertLoaded(), hasSize(821));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType) Test(org.junit.Test)

Example 54 with FormTree

use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.

the class LookupViewModelTest method initialSetup.

@Test
public void initialSetup() {
    FormTree formTree = catalog.getFormTree(nfiForm.getFormId());
    LookupViewModel viewModel = new LookupViewModel(setup.getFormStore(), formTree, nfiForm.getVillageField());
    viewModel.select(new RecordRef(villageForm.getFormId(), ResourceId.valueOf("c207")));
    Connection<Optional<String>> provinceLabel = setup.connect(viewModel.getLookupKeys().get(0).getSelectedKey());
    Connection<Optional<String>> territoryLabel = setup.connect(viewModel.getLookupKeys().get(1).getSelectedKey());
    Connection<Optional<String>> villageLabel = setup.connect(viewModel.getLookupKeys().get(2).getSelectedKey());
    assertThat(provinceLabel.assertLoaded().get(), equalTo("Province 11"));
    assertThat(territoryLabel.assertLoaded().get(), equalTo("Territory 24"));
    assertThat(villageLabel.assertLoaded().get(), equalTo("Village 208"));
    // Change the territory
    Connection<List<String>> territoryChoices = setup.connect(viewModel.getLookupKeys().get(1).getChoices());
    String newTerritory = territoryChoices.assertLoaded().get(3);
    viewModel.select(viewModel.getLookupKeys().get(1).getLookupKey(), newTerritory);
    // Check updated labels
    assertThat(provinceLabel.assertLoaded().get(), equalTo("Province 11"));
    assertThat(territoryLabel.assertLoaded().get(), equalTo(newTerritory));
    assertThat(villageLabel.assertLoaded().isPresent(), equalTo(false));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) Optional(com.google.common.base.Optional) RecordRef(org.activityinfo.model.type.RecordRef) Test(org.junit.Test)

Example 55 with FormTree

use of org.activityinfo.model.formTree.FormTree in project activityinfo by bedatadriven.

the class LookupViewModelTest method select.

@Test
public void select() {
    FormTree formTree = catalog.getFormTree(nfiForm.getFormId());
    LookupViewModel viewModel = new LookupViewModel(setup.getFormStore(), formTree, nfiForm.getVillageField());
    LookupKeyViewModel province = viewModel.getLookupKeys().get(0);
    assertThat(province.getKeyLabel(), equalTo("Province Name"));
    // The second level should now reflect these choices
    LookupKeyViewModel territory = viewModel.getLookupKeys().get(1);
    assertThat(territory.getKeyLabel(), equalTo("Territory Name"));
    // Finally the third level is where we choose the village
    LookupKeyViewModel village = viewModel.getLookupKeys().get(2);
    assertThat(village.getKeyLabel(), equalTo("Village Name"));
    // Connect to the lists
    Connection<List<String>> provinceList = setup.connect(province.getChoices());
    Connection<List<String>> territoryList = setup.connect(territory.getChoices());
    Connection<List<String>> villageList = setup.connect(village.getChoices());
    // Connect to the selection
    Connection<Optional<RecordRef>> selection = setup.connect(viewModel.getSelectedRecord());
    // Initially... no selection
    assertThat(selection.assertLoaded().isPresent(), equalTo(false));
    // Initially the root level should have all choices (16)
    assertThat(provinceList.assertLoaded(), hasSize(16));
    // Select an item in the first level, should change
    // the options available to the second level
    viewModel.select(province.getLookupKey(), province.getChoices().get().get(2));
    territoryList.assertChanged();
    System.out.println(territoryList.assertLoaded());
    assertThat(territoryList.assertLoaded(), hasSize(7));
    System.out.println(territoryList.assertLoaded());
    // Select a territory
    viewModel.select(territory.getLookupKey(), territoryList.assertLoaded().get(2));
    villageList.assertChanged();
    // Select a village
    viewModel.select(village.getLookupKey(), villageList.assertLoaded().get(5));
    assertThat(selection.assertLoaded().get(), equalTo(new RecordRef(villageForm.getFormId(), ResourceId.valueOf("c207"))));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) Optional(com.google.common.base.Optional) RecordRef(org.activityinfo.model.type.RecordRef) Test(org.junit.Test)

Aggregations

FormTree (org.activityinfo.model.formTree.FormTree)55 Test (org.junit.Test)35 ResourceId (org.activityinfo.model.resource.ResourceId)12 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)10 FormClass (org.activityinfo.model.form.FormClass)9 ColumnSet (org.activityinfo.model.query.ColumnSet)9 QueryModel (org.activityinfo.model.query.QueryModel)8 ImportModel (org.activityinfo.ui.client.component.importDialog.model.ImportModel)8 PastedTable (org.activityinfo.ui.client.component.importDialog.model.source.PastedTable)8 FormField (org.activityinfo.model.form.FormField)7 RecordRef (org.activityinfo.model.type.RecordRef)7 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)6 FormulaNode (org.activityinfo.model.formula.FormulaNode)6 ReferenceType (org.activityinfo.model.type.ReferenceType)6 LookupKey (org.activityinfo.model.formTree.LookupKey)5 ValidatedRowTable (org.activityinfo.ui.client.component.importDialog.model.validation.ValidatedRowTable)5 Optional (com.google.common.base.Optional)4 ColumnView (org.activityinfo.model.query.ColumnView)4 Nullable (javax.annotation.Nullable)3 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)3