Search in sources :

Example 1 with VariableInterval

use of edu.harvard.iq.dataverse.datavariable.DataVariable.VariableInterval in project dataverse by IQSS.

the class CSVFileReaderTest method testVariables.

/*
     * This test will read the CSV File From Hell, above, then will inspect
     * the DataTable object produced by the plugin, and verify that the
     * individual DataVariables have been properly typed.
     */
@Test
public void testVariables() {
    String testFile = "src/test/java/edu/harvard/iq/dataverse/ingest/tabulardata/impl/plugins/csv/IngestCSV.csv";
    String[] expectedVariableNames = { "ints", "Strings", "Times", "Not quite Times", "Dates", "Not quite Dates", "Numbers", "Not quite Ints", "Not quite Numbers", "Column that hates you, contains many comas, and is verbose and long enough that it would cause ingest to fail if ingest failed when a header was more than 256 characters long. Really, it's just sadistic. Also to make matters worse, the space at the begining of this sentance was a special unicode space designed to make you angry." };
    VariableType[] expectedVariableTypes = { VariableType.NUMERIC, VariableType.CHARACTER, VariableType.CHARACTER, VariableType.CHARACTER, VariableType.CHARACTER, VariableType.CHARACTER, VariableType.NUMERIC, VariableType.NUMERIC, VariableType.CHARACTER, VariableType.CHARACTER };
    VariableInterval[] expectedVariableIntervals = { VariableInterval.DISCRETE, VariableInterval.DISCRETE, VariableInterval.DISCRETE, VariableInterval.DISCRETE, VariableInterval.DISCRETE, VariableInterval.DISCRETE, VariableInterval.CONTINUOUS, VariableInterval.CONTINUOUS, VariableInterval.DISCRETE, VariableInterval.DISCRETE };
    String[] expectedVariableFormatCategories = { null, null, "time", "time", "date", null, null, null, null, null };
    String[] expectedVariableFormats = { null, null, "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", null, null, null, null, null };
    // aka the number of lines in the TAB file produced by the ingest plugin
    Long expectedNumberOfCases = 7L;
    DataTable result = null;
    try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(testFile))) {
        CSVFileReader instance = new CSVFileReader(new CSVFileReaderSpi());
        result = instance.read(stream, null).getDataTable();
    } catch (IOException ex) {
        fail("" + ex);
    }
    assertNotNull(result);
    assertNotNull(result.getDataVariables());
    assertEquals(result.getVarQuantity(), new Long(result.getDataVariables().size()));
    assertEquals(result.getVarQuantity(), new Long(expectedVariableTypes.length));
    assertEquals(expectedNumberOfCases, result.getCaseQuantity());
    // OK, let's go through the individual variables:
    for (int i = 0; i < result.getVarQuantity(); i++) {
        assertEquals("variable " + i + ":", expectedVariableNames[i], result.getDataVariables().get(i).getName());
        assertEquals("variable " + i + ":", expectedVariableTypes[i], result.getDataVariables().get(i).getType());
        assertEquals("variable " + i + ":", expectedVariableIntervals[i], result.getDataVariables().get(i).getInterval());
        assertEquals("variable " + i + ":", expectedVariableFormatCategories[i], result.getDataVariables().get(i).getFormatCategory());
        assertEquals("variable " + i + ":", expectedVariableFormats[i], result.getDataVariables().get(i).getFormat());
    }
}
Also used : DataTable(edu.harvard.iq.dataverse.DataTable) VariableType(edu.harvard.iq.dataverse.datavariable.DataVariable.VariableType) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) VariableInterval(edu.harvard.iq.dataverse.datavariable.DataVariable.VariableInterval) Test(org.junit.Test)

Aggregations

DataTable (edu.harvard.iq.dataverse.DataTable)1 VariableInterval (edu.harvard.iq.dataverse.datavariable.DataVariable.VariableInterval)1 VariableType (edu.harvard.iq.dataverse.datavariable.DataVariable.VariableType)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Test (org.junit.Test)1