use of org.molgenis.emx2.Row in project molgenis-emx2 by molgenis.
the class TestCompositeForeignKeys method testCompositeRefArray.
@Test
public void testCompositeRefArray() {
Schema schema = database.dropCreateSchema(TestCompositeForeignKeys.class.getSimpleName() + "RefArray");
schema.create(table("Person", column("firstName").setPkey(), column("lastName").setPkey(), column("cousins", REF_ARRAY).setRefTable("Person")));
Table p = schema.getTable("Person");
p.insert(new Row().setString("firstName", "Kwik").setString("lastName", "Duck"));
p.insert(new Row().setString("firstName", "Donald").setString("lastName", "Duck").setString("cousins.firstName", "Kwik").setString("cousins.lastName", "Duck"));
try {
p.delete(new Row().setString("firstName", "Kwik").setString("lastName", "Duck"));
fail("should have failed on foreign key error");
} catch (Exception e) {
System.out.println("errored correctly: " + e);
}
schema.create(table("Student").setInherit("Person"));
Table s = schema.getTable("Student");
s.insert(new Row().setString("firstName", "Mickey").setString("lastName", "Mouse").setString("cousins.firstName", "Kwik").setString("cousins.lastName", "Duck"));
String result = schema.query("Student").select(s("firstName"), s("lastName"), s("cousins", s("firstName"), s("lastName"))).retrieveJSON();
System.out.println(result);
// refback
schema.getTable("Person").getMetadata().add(column("uncles").setType(REFBACK).setRefTable("Person").setRefBack("cousins"));
s.insert(new Row().setString("firstName", // doesn't exist
"Kwok").setString("lastName", "Duck").setString("uncles.firstName", "Donald").setString("uncles.lastName", "Duck"));
assertTrue(List.of(s.query().select(s("firstName"), s("lastName"), s("uncles", s("firstName"), s("lastName")), s("cousins", s("firstName"), s("lastName"))).where(f("firstName", EQUALS, "Kwok")).retrieveRows().get(0).getStringArray("uncles-firstName")).contains("Donald"));
assertTrue(List.of(p.query().select(s("firstName"), s("lastName"), s("cousins", s("firstName"), s("lastName")), s("uncles", s("firstName"), s("lastName"))).where(f("firstName", EQUALS, "Donald")).retrieveRows().get(//
1).getStringArray(// TODO should be array?
"cousins-firstName")).contains("Kwok"));
// check we can sort on ref_array
p.query().select(s("firstName"), s("lastName"), s("cousins", s("firstName"), s("lastName")), s("uncles", s("firstName"), s("lastName"))).orderBy("cousins").retrieveJSON();
// check we can sort on refback to a ref_array
p.query().select(s("firstName"), s("lastName"), s("cousins", s("firstName"), s("lastName")), s("uncles", s("firstName"), s("lastName"))).orderBy("uncles").retrieveJSON();
}
use of org.molgenis.emx2.Row in project molgenis-emx2 by molgenis.
the class TestCopy method test.
@Test
public void test() {
List<Row> rows = List.of(row("A", "a1", "B", List.of("b\"11\"", "b1,2")), row("A", "a2", "B", List.of("b21", "b22")));
schema.create(table("test", column("A").setPkey(), column("B").setType(ColumnType.STRING_ARRAY)));
// copyOut
SqlTable table = (SqlTable) schema.getTable("test");
table.insert(rows);
StringWriter writer = new StringWriter();
table.copyOut(writer);
System.out.println("CopyOut");
System.out.println(writer);
// copyIn
System.out.println("CopyIn");
schema.create(table("test2", column("A").setPkey()));
table = (SqlTable) schema.getTable("test2");
table.copyIn(rows);
writer = new StringWriter();
table.copyOut(writer);
System.out.println(writer);
assertEquals(2, schema.getTable("test2").retrieveRows().size());
}
use of org.molgenis.emx2.Row in project molgenis-emx2 by molgenis.
the class TableStoreForCsvFile method writeTable.
@Override
public void writeTable(String name, List<String> columnNames, Iterable<Row> rows) {
try {
Writer writer = Files.newBufferedWriter(csvFile);
if (rows.iterator().hasNext()) {
CsvTableWriter.write(rows, writer, ',');
} else {
// only header in case no rows provided
writer.write(columnNames.stream().collect(Collectors.joining(",")));
}
writer.close();
} catch (IOException ioe) {
throw new MolgenisException("Export failed", ioe);
}
}
use of org.molgenis.emx2.Row in project molgenis-emx2 by molgenis.
the class TestTypesImport method testExcelTypesCast.
@Test
public void testExcelTypesCast() {
ClassLoader classLoader = getClass().getClassLoader();
Path path = new File(classLoader.getResource("TypeTest.xlsx").getFile()).toPath();
TableStore store = new TableStoreForXlsxFile(path);
for (Row r : store.readTable("Sheet1")) {
assertEquals("1.0", r.getString("decimal"));
assertEquals(Double.valueOf(1.0), r.getDecimal("decimal"));
assertEquals("1", r.getString("int"));
assertEquals(Integer.valueOf(1), r.getInteger("int"));
}
}
use of org.molgenis.emx2.Row in project molgenis-emx2 by molgenis.
the class TestQueryJsonGraph method testGroupBy.
// todo @Test
// we want to refactor this
public void testGroupBy() throws JsonProcessingException {
Schema schema = db.dropCreateSchema(TestQueryJsonGraph.class.getSimpleName() + "_testGroupBy");
schema.create(table("Test", column("id").setPkey(), column("tag"), column("tag_array").setType(STRING_ARRAY), column("tag_array2").setType(STRING_ARRAY)));
schema.getTable("Test").insert(row("id", 1, "tag", "blue", "tag_array", new String[] { "blue", "green" }, "tag_array2", new String[] { "yellow", "red" }), row("id", 2, "tag", "blue", "tag_array", new String[] { "green", "blue" }, "tag_array2", new String[] { "yellow", "red" }), row("id", 3, "tag", "green", "tag_array", new String[] { "blue" }, "tag_array2", new String[] { "yellow", "red" }));
ObjectMapper mapper = new ObjectMapper();
Map<String, Map<String, List<Map<String, Object>>>> result = mapper.readValue(schema.agg("Test").select(s("groupBy", s("count"), s("tag"))).retrieveJSON(), Map.class);
assertEquals(2, result.get("Test_agg").get("groupBy").get(1).get("count"));
result = mapper.readValue(schema.agg("Test").select(s("groupBy", s("count"), s("tag_array"))).retrieveJSON(), Map.class);
assertEquals(2, result.get("Test_agg").get("groupBy").get(0).get("count"));
result = mapper.readValue(schema.agg("Test").select(s("groupBy", s("count"), s("tag_array"), s("tag_array2"))).retrieveJSON(), Map.class);
assertEquals(3, result.get("Test_agg").get("groupBy").get(0).get("count"));
}
Aggregations