Search in sources :

Example 36 with QueryModel

use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.

the class ApiTestHarness method createDatabase.

public TestDatabase createDatabase() {
    String name = aliasTable.createAlias("db");
    ActivityInfoClient client = client();
    ResourceId databaseId = client.createDatabase(name);
    int id = CuidAdapter.getLegacyIdFromCuid(databaseId);
    // Query list of partners
    QueryModel queryModel = new QueryModel(CuidAdapter.partnerFormId(id));
    queryModel.selectResourceId().as("id");
    ColumnSet columnSet = client.queryTable(queryModel);
    ColumnView partnerIdView = columnSet.getColumnView("id");
    ResourceId firstPartnerId = ResourceId.valueOf(partnerIdView.getString(0));
    return new TestDatabase(databaseId, name, firstPartnerId);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ActivityInfoClient(org.activityinfo.client.ActivityInfoClient) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel)

Example 37 with QueryModel

use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.

the class DateFunctionQueryTest method testToday.

@Test
public void testToday() {
    TestingStorageProvider catalog = new TestingStorageProvider();
    IntakeForm intakeForm = catalog.getIntakeForm();
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormScanCache(), new NullFormSupervisor());
    QueryModel queryModel = new QueryModel(intakeForm.getFormId());
    queryModel.selectExpr("TODAY()").as("today");
    queryModel.selectExpr("YEARFRAC(TODAY(), DOB)").as("age");
    queryModel.selectExpr("DOB").as("dob");
    ColumnSet columnSet = builder.build(queryModel);
    ColumnView today = columnSet.getColumnView("today");
    ColumnView age = columnSet.getColumnView("age");
    ColumnView dob = columnSet.getColumnView("dob");
    for (int i = 0; i < columnSet.getNumRows(); i++) {
        if (dob.getString(i) != null) {
            double ageInYears = age.getDouble(i);
            System.out.println(dob.get(i) + " " + ageInYears);
            if (Double.isNaN(ageInYears)) {
                throw new AssertionError();
            }
        }
    }
    System.out.println(age);
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) NullFormScanCache(org.activityinfo.store.query.shared.NullFormScanCache) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 38 with QueryModel

use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.

the class EnumerationQueryTest method enumRefTests.

@Test
public void enumRefTests() {
    TestingStorageProvider catalog = new TestingStorageProvider();
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormScanCache(), new NullFormSupervisor());
    IntakeForm intakeForm = catalog.getIntakeForm();
    QueryModel queryModel = new QueryModel(intakeForm.getFormId());
    queryModel.selectField(intakeForm.getNationalityFieldId()).as("nationality");
    queryModel.selectExpr(intakeForm.getNationalityFieldId() + "==" + "\"" + intakeForm.getPalestinianId() + "\"").as("palestinian");
    queryModel.selectExpr("\"" + intakeForm.getPalestinianId() + "\"" + "==" + intakeForm.getNationalityFieldId()).as("palestinianInverse");
    ColumnSet columnSet = builder.build(queryModel);
    ColumnView nationality = columnSet.getColumnView("nationality");
    ColumnView palestinian = columnSet.getColumnView("palestinian");
    ColumnView palestinianInverse = columnSet.getColumnView("palestinianInverse");
    // Correct Tests
    assertThat(nationality.get(1).toString(), equalTo(NAT_PAL));
    assertThat(Boolean.valueOf(palestinian.get(1).toString()), equalTo(true));
    assertThat(Boolean.valueOf(palestinianInverse.get(1).toString()), equalTo(true));
    // Incorrect Tests
    // Multiple selected values should return null
    assertThat(nationality.get(5), equalTo(null));
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) NullFormScanCache(org.activityinfo.store.query.shared.NullFormScanCache) ColumnView(org.activityinfo.model.query.ColumnView) EnumColumnView(org.activityinfo.model.query.EnumColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 39 with QueryModel

use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.

the class SubFormDeletionTest method deleteParent.

@Test
public void deleteParent() {
    QueryModel queryModel = new QueryModel(ReferralSubForm.FORM_ID);
    queryModel.selectResourceId().as("id");
    queryModel.selectExpr(new CompoundExpr(IncidentForm.FORM_ID, ColumnModel.ID_SYMBOL)).as("parent");
    queryModel.selectExpr(new ConstantNode(1)).as("count");
    ColumnSet columnSet = query(queryModel);
    assertThat(columnSet.getNumRows(), equalTo(ReferralSubForm.ROW_COUNT));
    System.out.println(columnSet.getColumnView("parent"));
    // Now delete a number of parent records
    delete(IncidentForm.FORM_ID, "c528");
    // The query results should reflect the change
    columnSet = query(queryModel);
    assertThat(columnSet.getNumRows(), equalTo(ReferralSubForm.ROW_COUNT - 2));
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) ConstantNode(org.activityinfo.model.formula.ConstantNode) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 40 with QueryModel

use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.

the class UnaryFunctionQueryTest method divideFunctionTest.

@Test
public void divideFunctionTest() {
    QueryModel queryModel = new QueryModel(intakeForm.getFormId());
    makeQueryExprAndExpectColumnModelException(queryModel, "/1", "divOne");
    makeQueryExprAndExpectColumnModelException(queryModel, "/1.0", "divOneDouble");
    makeQueryExprAndExpectColumnModelException(queryModel, "1/", "oneDiv");
    makeQueryExprAndExpectColumnModelException(queryModel, "1.0/", "oneDivDouble");
}
Also used : QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Aggregations

QueryModel (org.activityinfo.model.query.QueryModel)59 ColumnSet (org.activityinfo.model.query.ColumnSet)40 Test (org.junit.Test)30 ColumnView (org.activityinfo.model.query.ColumnView)21 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)13 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)11 FormTree (org.activityinfo.model.formTree.FormTree)9 ResourceId (org.activityinfo.model.resource.ResourceId)9 FormClass (org.activityinfo.model.form.FormClass)7 Nullable (javax.annotation.Nullable)6 FormField (org.activityinfo.model.form.FormField)6 ColumnModel (org.activityinfo.model.query.ColumnModel)6 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)6 QuantityType (org.activityinfo.model.type.number.QuantityType)5 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)4 RowBasedJsonWriter (org.activityinfo.store.query.output.RowBasedJsonWriter)4 Charsets (com.google.common.base.Charsets)3 Operation (io.swagger.v3.oas.annotations.Operation)3 FieldPath (org.activityinfo.model.formTree.FieldPath)3 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)3