use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class HrdCatalogTest method simpleFormTest.
@Test
public void simpleFormTest() {
ResourceId collectionId = ResourceId.generateId();
ResourceId villageField = ResourceId.valueOf("FV");
ResourceId countField = ResourceId.valueOf("FC");
FormClass formClass = new FormClass(collectionId);
formClass.setParentFormId(ResourceId.valueOf("foo"));
formClass.setLabel("NFI Distributions");
formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(formClass);
Optional<FormStorage> storage = catalog.getForm(collectionId);
assertTrue(storage.isPresent());
TypedRecordUpdate village1 = new TypedRecordUpdate();
village1.setUserId(userId);
village1.setRecordId(ResourceId.generateSubmissionId(formClass));
village1.set(villageField, TextValue.valueOf("Rutshuru"));
village1.set(countField, new Quantity(1000));
TypedRecordUpdate village2 = new TypedRecordUpdate();
village2.setUserId(userId);
village2.setRecordId(ResourceId.generateSubmissionId(formClass));
village2.set(villageField, TextValue.valueOf("Beni"));
village2.set(countField, new Quantity(230));
storage.get().add(village1);
storage.get().add(village2);
QueryModel queryModel = new QueryModel(collectionId);
queryModel.selectResourceId().as("id");
queryModel.selectField("VILLAGE").as("village");
queryModel.selectField("BENE").as("family_count");
queryModel.selectExpr("BENE*5").as("individual_count");
ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
ColumnSet columnSet = builder.build(queryModel);
System.out.println(columnSet);
assertThat(columnSet.getNumRows(), equalTo(2));
List<RecordVersion> versions1 = ((VersionedFormStorage) storage.get()).getVersions(village1.getRecordId());
assertThat(versions1, hasSize(1));
RecordVersion version = versions1.get(0);
assertThat(version.getRecordId(), equalTo(village1.getRecordId()));
assertThat(version.getUserId(), equalTo((long) userId));
assertThat(version.getType(), equalTo(RecordChangeType.CREATED));
}
use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class ImportSimpleTest method locationWithMissingAdminLevel.
@Test
public void locationWithMissingAdminLevel() throws IOException {
FormTree formTree = assertResolves(locator.getFormTree(VILLAGE_FORM_ID));
FormTreePrettyPrinter.print(formTree);
importModel = new ImportModel(formTree);
// Step 1: User pastes in data to import
PastedTable source = new PastedTable(Resources.toString(getResource(getClass(), "qis-villages.csv"), Charsets.UTF_8));
source.parseAllRows();
assertThat(source.getRows().size(), equalTo(1));
importModel.setSource(source);
importer = new Importer(locator, formTree, FieldImportStrategies.get(JvmConverterFactory.get()));
dumpList("COLUMNS", source.getColumns());
// Step 2: User maps imported columns to FormFields
List<ImportTarget> targets = importer.getImportTargets();
dumpList("FIELDS", targets);
importModel.setColumnAction(columnIndex("Name"), target("Name"));
importModel.setColumnAction(columnIndex("District"), target("District Name"));
// Step 3: Validate for user
ValidatedRowTable validatedResult = assertResolves(importer.validateRows(importModel));
showValidationGrid(validatedResult);
assertResolves(importer.persist(importModel));
// AND... verify
QueryModel queryModel = new QueryModel(VILLAGE_FORM_ID);
queryModel.selectExpr("Name").as("name");
queryModel.selectField(CuidAdapter.field(VILLAGE_FORM_ID, CuidAdapter.ADMIN_FIELD)).as("admin");
ColumnSet columnSet = assertResolves(locator.queryTable(queryModel));
assertThat(columnSet.getNumRows(), equalTo(1));
assertThat(columnSet.getColumnView("name").getString(0), equalTo("Village 1"));
assertThat(columnSet.getColumnView("admin").getString(0), equalTo(CuidAdapter.cuid(CuidAdapter.ADMIN_ENTITY_DOMAIN, 2).asString()));
}
use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class SerialNumberTest method testWithPrefix.
@Test
public void testWithPrefix() {
TestDatabase database = harness.createDatabase();
ActivityInfoClient client = harness.client();
FormClass formClass = new FormClass(harness.newFormId());
formClass.setLabel("Test Prefix");
formClass.setDatabaseId(database.getId());
FormField partnerField = CuidAdapter.partnerField(formClass);
formClass.addElement(partnerField);
FormField provinceField = formClass.addField(harness.newFieldId()).setType(TextType.SIMPLE).setLabel("Province Code").setCode("PROVINCE").setRequired(true);
FormField fileNumberField = formClass.addField(harness.newFieldId()).setCode("SN").setType(new SerialNumberType("PROVINCE", 5)).setLabel("Record Number");
client.createForm(formClass);
FormInstance kunduz1 = new FormInstance(harness.newRecordId(), formClass.getId());
kunduz1.set(provinceField.getId(), "KUNDUZ");
kunduz1.set(partnerField.getId(), database.getDefaultPartner());
client.createRecord(kunduz1);
FormInstance kunduz2 = new FormInstance(harness.newRecordId(), formClass.getId());
kunduz2.set(provinceField.getId(), "KUNDUZ");
kunduz2.set(partnerField.getId(), database.getDefaultPartner());
client.createRecord(kunduz2);
FormInstance takhar = new FormInstance(harness.newRecordId(), formClass.getId());
takhar.set(provinceField.getId(), "TAKHAR");
takhar.set(partnerField.getId(), database.getDefaultPartner());
client.createRecord(takhar);
// Verify that the serial numbers have been assigned
kunduz1 = client.getTypedRecord(formClass, kunduz1.getId());
kunduz2 = client.getTypedRecord(formClass, kunduz2.getId());
takhar = client.getTypedRecord(formClass, takhar.getId());
assertThat(kunduz1.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 1)));
assertThat(kunduz2.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 2)));
assertThat(takhar.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("TAKHAR", 1)));
// Now verify that we can query a table with serial numbers
QueryModel queryModel = new QueryModel(formClass.getId());
queryModel.selectResourceId().as("id");
queryModel.selectField("SN");
ColumnSet columnSet = client.queryTable(queryModel);
ColumnView sn = columnSet.getColumnView("SN");
}
use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class SurveyTest method updateAndQuery.
@Test
public void updateAndQuery() {
// Post a number of records
RecordGenerator generator = survey.getGenerator();
int numRows = 20;
for (int i = 0; i < numRows; i++) {
client.createRecord(generator.get());
}
// Now query a few records
QueryModel queryModel = new QueryModel(survey.getFormId());
queryModel.selectField(survey.getGenderFieldId()).as("gender");
queryModel.selectField(survey.getAgeFieldId()).as("age");
queryModel.selectField(survey.getDobFieldId()).as("dob");
ColumnSet columnSet = client.queryTable(queryModel);
ColumnView gender = columnSet.getColumnView("gender");
ColumnView age = columnSet.getColumnView("age");
ColumnView dob = columnSet.getColumnView("dob");
assertThat(gender.numRows(), equalTo(numRows));
assertThat(gender.getType(), equalTo(ColumnType.STRING));
assertThat(age.numRows(), equalTo(numRows));
}
use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.
the class SurveyTest method updateTransaction.
@Test
public void updateTransaction() {
RecordTransactionBuilder tx = new RecordTransactionBuilder();
tx.create(survey.getGenerator().get());
tx.create(survey.getGenerator().get());
client.update(tx.build());
QueryModel queryModel = new QueryModel(survey.getFormId());
queryModel.selectField(survey.getGenderFieldId()).as("gender");
ColumnSet columnSet = client.queryTable(queryModel);
assertThat(columnSet.getNumRows(), equalTo(2));
}
Aggregations