Search in sources :

Example 1 with TableStore

use of org.molgenis.emx2.io.tablestore.TableStore in project molgenis-emx2 by molgenis.

the class TestEmx2Roles method testRolesIO.

@Test
public void testRolesIO() {
    // create user roles
    schema.addMember("bofke", "Viewer");
    TableStore store = new TableStoreForCsvInMemory();
    // export
    Emx2Members.outputRoles(store, schema);
    // empty the database, verify
    schema = schema.getDatabase().dropCreateSchema(TestEmx2Roles.class.getSimpleName());
    assertEquals(0, schema.getMembers().size());
    // import and see if consistent
    Emx2Members.inputRoles(store, schema);
    List<Member> members = schema.getMembers();
    assertEquals("bofke", members.get(0).getUser());
    assertEquals("Viewer", members.get(0).getRole());
}
Also used : TableStoreForCsvInMemory(org.molgenis.emx2.io.tablestore.TableStoreForCsvInMemory) Member(org.molgenis.emx2.Member) TableStore(org.molgenis.emx2.io.tablestore.TableStore) Test(org.junit.Test)

Example 2 with TableStore

use of org.molgenis.emx2.io.tablestore.TableStore in project molgenis-emx2 by molgenis.

the class TestReadWriteStores method executeTest.

public static void executeTest(TableStore store) throws IOException, MolgenisException {
    List<Row> rows = new ArrayList<>();
    int count = 10;
    for (int i = 1; i <= count; i++) {
        rows.add(new Row().setString("stringCol", "test" + i).setInt("intCol", i).setDecimal("decimalCol", Double.valueOf(i / 2)).setUuid("uuidCol", UUID.randomUUID()).setDate("dateCol", LocalDate.of(2019, 12, 12)).setDateTime("datetimeCol", LocalDateTime.now()).setBool("boolCol", true).setStringArray("stringarrayCol", new String[] { "a", "b,including comma," }).setIntArray("intarrayCol", new Integer[] { 1, 2 }).setDecimalArray("doubleArrayCol", new Double[] { 1.0, 2.0 }).setDecimalArray("doubleArrayCol", new Double[] { 1.0, 2.0 }).setDateArray("dateArray", new LocalDate[] { LocalDate.of(2019, 12, 12), LocalDate.of(2019, 12, 12) }).setDateTimeArray("datetimeArrayCol", new LocalDateTime[] { LocalDateTime.now(), LocalDateTime.now() }).setBoolArray("booleanArrayCol", new Boolean[] { true, false }));
    }
    StopWatch.start("created some rows");
    // write them
    store.writeTable("test", List.of(), rows);
    store.writeTable("test2", List.of(), rows);
    StopWatch.print("wrote them to " + store.getClass().getSimpleName(), count);
    List<Row> rows2 = StreamSupport.stream(store.readTable("test2").spliterator(), false).collect(Collectors.toList());
    // for (Row r : rows2) System.out.println(r);
    StopWatch.print("fromReader them back from " + store.getClass().getSimpleName(), count);
    // compare
    CompareTools.assertEquals(rows, rows2);
    // write another one
    store.writeTable("test3", List.of(), rows);
    StopWatch.print("wrote them to " + store.getClass().getSimpleName(), count);
    rows2 = StreamSupport.stream(store.readTable("test3").spliterator(), false).collect(Collectors.toList());
    // for (Row r : rows2) System.out.println(r);
    StopWatch.print("fromReader them back from " + store.getClass().getSimpleName(), count);
    // compare
    CompareTools.assertEquals(rows, rows2);
    StopWatch.print("compared succesfully");
    // write empty
    store.writeTable("test4", List.of("empty"), new ArrayList<>());
    // test that reading store that doesn't exist errors properly
    try {
        store.readTable("fake");
        fail("should have failed");
    } catch (MolgenisException me) {
        System.out.println("errored correctly:" + me);
    }
}
Also used : ArrayList(java.util.ArrayList) MolgenisException(org.molgenis.emx2.MolgenisException) Row(org.molgenis.emx2.Row) LocalDate(java.time.LocalDate)

Example 3 with TableStore

use of org.molgenis.emx2.io.tablestore.TableStore in project molgenis-emx2 by molgenis.

the class TestColumnTypeIsFile method testCsvShouldNotIncludeFileColumns.

@Test
public void testCsvShouldNotIncludeFileColumns() throws IOException {
    Path tmp = Files.createTempDirectory(null);
    Path csvFile = tmp.resolve("User.csv");
    MolgenisIO.toExcelFile(csvFile, schema.getTable("User"));
    TableStore store = new TableStoreForCsvFile(csvFile);
    assertFalse(store.readTable("User").iterator().next().getColumnNames().contains("picture"));
}
Also used : Path(java.nio.file.Path) TableStoreForCsvFile(org.molgenis.emx2.io.tablestore.TableStoreForCsvFile) TableStore(org.molgenis.emx2.io.tablestore.TableStore) Test(org.junit.Test)

Example 4 with TableStore

use of org.molgenis.emx2.io.tablestore.TableStore in project molgenis-emx2 by molgenis.

the class TestTypesImport method testTsvTypeCast.

@Test
public void testTsvTypeCast() {
    ClassLoader classLoader = getClass().getClassLoader();
    Path path = new File(classLoader.getResource("TypeTest.tsv").getFile()).toPath();
    TableStore store = new TableStoreForCsvFile(path);
    for (Row r : store.readTable("Sheet1")) {
        assertEquals("1", r.getString("column1"));
        assertEquals("2", r.getString("column2"));
    }
}
Also used : Path(java.nio.file.Path) TableStoreForCsvFile(org.molgenis.emx2.io.tablestore.TableStoreForCsvFile) 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 5 with TableStore

use of org.molgenis.emx2.io.tablestore.TableStore in project molgenis-emx2 by molgenis.

the class Emx1 method loadColumns.

private static List<Emx1Attribute> loadColumns(TableStore store, Map<String, Emx1Entity> entities, SchemaMetadata schema) {
    // line 1 is header
    int line = 2;
    List<Emx1Attribute> attributes = new ArrayList<>();
    try {
        if (store.containsTable("attributes")) {
            for (Row row : store.readTable("attributes")) {
                attributes.add(new Emx1Attribute(row));
                line++;
            }
        }
        // line 1 is header
        line = 2;
        for (Emx1Attribute attribute : attributes) {
            // create the table, if needed
            String entityName = attribute.getEntity();
            String tableName = getTableName(entities, entityName);
            TableMetadata table = schema.getTableMetadata(tableName);
            if (table == null) {
                table = schema.create(table(tableName));
            }
            // create the attribute
            ColumnType type = getColumnType(attribute.getDataType());
            Column column = column(attribute.getName()).setType(type).setRequired(!attribute.getNillable());
            // pkey
            if (attribute.getIdAttribute()) {
                column.setKey(1);
            }
            table.add(column);
            line++;
        }
    } catch (MolgenisException me) {
        throw new MolgenisException(EMX_1_IMPORT_FAILED + me.getMessage() + ". See 'attributes' line " + line, me);
    }
    return attributes;
}
Also used : ColumnType(org.molgenis.emx2.ColumnType) ArrayList(java.util.ArrayList)

Aggregations

Test (org.junit.Test)9 TableStore (org.molgenis.emx2.io.tablestore.TableStore)9 Row (org.molgenis.emx2.Row)7 Path (java.nio.file.Path)6 File (java.io.File)5 TableStoreForCsvFile (org.molgenis.emx2.io.tablestore.TableStoreForCsvFile)5 TableStoreForXlsxFile (org.molgenis.emx2.io.tablestore.TableStoreForXlsxFile)5 ArrayList (java.util.ArrayList)2 TableStoreForCsvInMemory (org.molgenis.emx2.io.tablestore.TableStoreForCsvInMemory)2 LocalDate (java.time.LocalDate)1 ColumnType (org.molgenis.emx2.ColumnType)1 Member (org.molgenis.emx2.Member)1 MolgenisException (org.molgenis.emx2.MolgenisException)1 Schema (org.molgenis.emx2.Schema)1 Setting (org.molgenis.emx2.Setting)1 TableStoreForCsvInZipFile (org.molgenis.emx2.io.tablestore.TableStoreForCsvInZipFile)1