Search in sources :

Example 1 with TableStoreForXlsxFile

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

the class TestExcelStore method testSimple.

@Test
public void testSimple() throws IOException {
    List<Row> rows = new ArrayList<Row>();
    for (int i = 0; i < 10; i++) {
        Row r = new Row();
        r.set("name", "test" + i);
        r.set("id", i);
        rows.add(r);
    }
    Path tmp = Files.createTempDirectory("TestExcelStore");
    tmp.toFile().deleteOnExit();
    Path excelFile = tmp.resolve("test.xlsx");
    TableStoreForXlsxFile store = new TableStoreForXlsxFile(excelFile);
    store.writeTable("test", List.of(), rows);
    // and read
    store = new TableStoreForXlsxFile(excelFile);
    List<Row> rows2 = store.readTable("test");
    assertEquals(10, rows2.size());
    // empty rows
    excelFile = tmp.resolve("test.xlsx");
    store = new TableStoreForXlsxFile(excelFile);
    store.writeTable("test2", List.of("blaat"), new ArrayList<>());
}
Also used : Path(java.nio.file.Path) TableStoreForXlsxFile(org.molgenis.emx2.io.tablestore.TableStoreForXlsxFile) ArrayList(java.util.ArrayList) Row(org.molgenis.emx2.Row) Test(org.junit.Test)

Example 2 with TableStoreForXlsxFile

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

the class TestReadWriteStores method testExcelStore.

@Test
public void testExcelStore() throws IOException {
    Path tmp = Files.createTempDirectory(null);
    try {
        Path excelFile = tmp.resolve("test.xlsx");
        System.out.println("defined excel file " + excelFile);
        TableStoreForXlsxFile store = new TableStoreForXlsxFile(excelFile);
        executeTest(store);
    } catch (MolgenisException e) {
        e.printStackTrace();
    } finally {
        Files.walk(tmp).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }
    if (Files.exists(tmp))
        throw new RuntimeException("TMP directory " + tmp + " not deleted. This should never happen.");
}
Also used : Path(java.nio.file.Path) MolgenisException(org.molgenis.emx2.MolgenisException) File(java.io.File) Test(org.junit.Test)

Example 3 with TableStoreForXlsxFile

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

the class TestColumnTypeIsFile method testExcelShouldNotIncludeFileColumns.

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

Example 4 with TableStoreForXlsxFile

use of org.molgenis.emx2.io.tablestore.TableStoreForXlsxFile 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)

Aggregations

Path (java.nio.file.Path)4 Test (org.junit.Test)4 TableStoreForXlsxFile (org.molgenis.emx2.io.tablestore.TableStoreForXlsxFile)3 File (java.io.File)2 Row (org.molgenis.emx2.Row)2 TableStore (org.molgenis.emx2.io.tablestore.TableStore)2 ArrayList (java.util.ArrayList)1 MolgenisException (org.molgenis.emx2.MolgenisException)1 TableStoreForCsvFile (org.molgenis.emx2.io.tablestore.TableStoreForCsvFile)1