Search in sources :

Example 96 with Row

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();
}
Also used : Table(org.molgenis.emx2.Table) Schema(org.molgenis.emx2.Schema) Row(org.molgenis.emx2.Row) Test(org.junit.Test)

Example 97 with Row

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());
}
Also used : StringWriter(java.io.StringWriter) Row(org.molgenis.emx2.Row) Test(org.junit.Test)

Example 98 with Row

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);
    }
}
Also used : MolgenisException(org.molgenis.emx2.MolgenisException) IOException(java.io.IOException) Writer(java.io.Writer) CsvTableWriter(org.molgenis.emx2.io.readers.CsvTableWriter)

Example 99 with Row

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"));
    }
}
Also used : Path(java.nio.file.Path) TableStoreForXlsxFile(org.molgenis.emx2.io.tablestore.TableStoreForXlsxFile) Row(org.molgenis.emx2.Row) TableStoreForCsvFile(org.molgenis.emx2.io.tablestore.TableStoreForCsvFile) File(java.io.File) TableStoreForXlsxFile(org.molgenis.emx2.io.tablestore.TableStoreForXlsxFile) TableStore(org.molgenis.emx2.io.tablestore.TableStore) Test(org.junit.Test)

Example 100 with Row

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"));
}
Also used : Schema(org.molgenis.emx2.Schema) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Test (org.junit.Test)61 Row (org.molgenis.emx2.Row)39 Row (com.google.bigtable.v2.Row)16 Table (org.molgenis.emx2.Table)13 ByteString (com.google.protobuf.ByteString)11 ArrayList (java.util.ArrayList)9 Function (com.google.common.base.Function)8 MolgenisException (org.molgenis.emx2.MolgenisException)8 Schema (org.molgenis.emx2.Schema)8 Row (com.google.api.ads.admanager.axis.v202108.Row)6 Family (com.google.bigtable.v2.Family)6 IOException (java.io.IOException)6 ByteKey (org.apache.beam.sdk.io.range.ByteKey)6 Row (com.google.api.ads.admanager.axis.v202105.Row)5 Row (com.google.api.ads.admanager.axis.v202111.Row)5 Row (com.google.api.ads.admanager.axis.v202202.Row)5 Row (com.google.api.ads.admanager.jaxws.v202105.Row)5 Row (com.google.api.ads.admanager.jaxws.v202108.Row)5 Row (com.google.api.ads.admanager.jaxws.v202202.Row)5 Path (java.nio.file.Path)5