Search in sources :

Example 21 with SymbolNode

use of org.activityinfo.model.formula.SymbolNode in project activityinfo by bedatadriven.

the class LookupKeySetTest method simpleHierarchyTest.

/**
 * In the case of a simple hierarchy, we are referencing a single form that
 * in turn has key reference fields.
 *
 * For example, if the field references a Village form, this should yield:
 *
 * <pre>
 *     Province.Name
 *        ^
 *        |
 *     District.Name
 *        ^
 *        |
 *     Village.Name
 * </pre>
 *
 * All fields are required because we ultimately need the reference to the village record.
 */
@Test
public void simpleHierarchyTest() {
    TestingStorageProvider catalog = new TestingStorageProvider();
    NfiForm nfiForm = catalog.getNfiForm();
    VillageForm villageForm = catalog.getVillageForm();
    AdminLevelForm territoryForm = catalog.getVillageForm().getParentForm();
    AdminLevelForm provinceForm = territoryForm.getParentForm().get();
    FormTree formTree = catalog.getFormTree(nfiForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, nfiForm.getVillageField());
    // Keys need to be topologically sorted,
    // with parent keys preceding child keys in the list
    assertThat(lookupKeySet.getKey(0).getKeyLabel(), equalTo("Province Name"));
    assertThat(lookupKeySet.getKey(1).getKeyLabel(), equalTo("Territory Name"));
    assertThat(lookupKeySet.getKey(2).getKeyLabel(), equalTo("Village Name"));
    SymbolNode villageName = Formulas.symbol(villageForm.getNameField().getId());
    SymbolNode villageTerritory = new SymbolNode(villageForm.getAdminFieldId());
    FormulaNode territoryName = new CompoundExpr(villageTerritory, territoryForm.getNameFieldId());
    FormulaNode territoryProvince = new CompoundExpr(villageTerritory, territoryForm.getParentFieldId());
    FormulaNode provinceName = new CompoundExpr(territoryProvince, provinceForm.getNameFieldId());
    assertThat(lookupKeySet.getLeafKeys(), hasSize(1));
    LookupKey leafKey = Iterables.getOnlyElement(lookupKeySet.getLeafKeys());
    assertThat(leafKey.getKeyFormulas().values(), containsInAnyOrder(villageName, territoryName, provinceName));
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree) LookupKey(org.activityinfo.model.formTree.LookupKey) Test(org.junit.Test)

Example 22 with SymbolNode

use of org.activityinfo.model.formula.SymbolNode in project activityinfo by bedatadriven.

the class FormStoreTest method serialNumberOffline.

@Test
public void serialNumberOffline() {
    TestSetup setup = new TestSetup();
    IntakeForm intakeForm = setup.getCatalog().getIntakeForm();
    // Synchronize the intake form
    setup.setConnected(true);
    setup.getFormStore().setFormOffline(intakeForm.getFormId(), true);
    setup.runScheduled();
    // Go offline
    setup.setConnected(false);
    // Create a new intake record
    FormInstance newRecord = intakeForm.getGenerator().get();
    newRecord.set(intakeForm.getProtectionCodeFieldId(), (FieldValue) null);
    Promise<Void> update = setup.getFormStore().updateRecords(new RecordTransactionBuilder().create(newRecord).build());
    assertThat(update.getState(), equalTo(Promise.State.FULFILLED));
    // Verify that we can read the new record offline
    QueryModel queryModel = new QueryModel(intakeForm.getFormId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField(intakeForm.getProtectionCodeFieldId()).as("serial");
    queryModel.setFilter(Formulas.equals(new SymbolNode("_id"), new ConstantNode(newRecord.getId().asString())));
    Connection<ColumnSet> view = setup.connect(setup.getFormStore().query(queryModel));
    assertThat(view.assertLoaded().getNumRows(), equalTo(1));
    assertThat(view.assertLoaded().getColumnView("serial").getString(0), nullValue());
    // Now go online...
    setup.setConnected(true);
    setup.getOfflineStore().syncChanges();
    ;
    setup.runScheduled();
    // Check that the serial number has been updated with the value from the server
    assertThat(view.assertLoaded().getColumnView("serial").getString(0), not(nullValue()));
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) SymbolNode(org.activityinfo.model.formula.SymbolNode) ConstantNode(org.activityinfo.model.formula.ConstantNode) ColumnSet(org.activityinfo.model.query.ColumnSet) FormInstance(org.activityinfo.model.form.FormInstance) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Aggregations

SymbolNode (org.activityinfo.model.formula.SymbolNode)22 CompoundExpr (org.activityinfo.model.formula.CompoundExpr)12 FormulaNode (org.activityinfo.model.formula.FormulaNode)8 ColumnModel (org.activityinfo.model.query.ColumnModel)6 Test (org.junit.Test)5 FormTree (org.activityinfo.model.formTree.FormTree)4 ResourceId (org.activityinfo.model.resource.ResourceId)3 JoinNode (org.activityinfo.store.query.shared.join.JoinNode)3 ConstantNode (org.activityinfo.model.formula.ConstantNode)2 ColumnSet (org.activityinfo.model.query.ColumnSet)2 ColumnView (org.activityinfo.model.query.ColumnView)2 QueryModel (org.activityinfo.model.query.QueryModel)2 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 Supplier (com.google.common.base.Supplier)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1