final String name = "RowIdUpsert" + UUID.randomUUID();
final String description = name + "-Description";
//Import a CSV and set the rowIdentifier to be ID
final DatasetInfo dataset = importer.createViewFromCsv(name, description, CRIMES_CSV_HEADER, "ID");
TestCase.assertNotNull(dataset);
TestCase.assertNotNull(dataset.getId());
importer.publish(dataset.getId());
try {
//
//Verify the row we expect is really there.
final SoqlQuery lookupTestRow = new SoqlQueryBuilder()
.setWhereClause("id='8880962'")
.build();
final List queryResults = producer.query(dataset.getId(), lookupTestRow, Soda2Producer.HASH_RETURN_TYPE);
TestCase.assertEquals(1, queryResults.size());
final Map result = (Map) queryResults.get(0);
TestCase.assertEquals("8880962", result.get("id"));
TestCase.assertEquals("THEFT", result.get("primary_type"));
//
// Update the dataset by uploading a CSV stream
final InputStream csvStream = getClass().getResourceAsStream("/testCrimesHeader2.csv");
final UpsertResult results = producer.upsertStream(dataset.getId(), HttpLowLevel.CSV_TYPE, csvStream);
TestCase.assertEquals(1, results.getRowsCreated());
TestCase.assertEquals(0, results.errorCount());
TestCase.assertEquals(0, results.getRowsDeleted());
TestCase.assertEquals(2, results.getRowsUpdated());
//
// Verify an overwrite happened, and not just an append.
final List queryResults2 = producer.query(dataset.getId(), lookupTestRow, Soda2Producer.HASH_RETURN_TYPE);
TestCase.assertEquals(1, queryResults.size());
final Map result2 = (Map) queryResults2.get(0);
TestCase.assertEquals("8880962", result2.get("id"));
TestCase.assertEquals("BATTERY", result2.get("primary_type"));
//
// Test adding a stream that has an invalid row in it
final InputStream csvStreamInvalid = getClass().getResourceAsStream("/testCrimesWithInvalidCrime.csv");
final UpsertResult resultsInvalid = producer.upsertStream(dataset.getId(), HttpLowLevel.CSV_TYPE, csvStreamInvalid);
TestCase.assertEquals(0, resultsInvalid.getRowsCreated());
TestCase.assertEquals(1, resultsInvalid.errorCount());
TestCase.assertEquals(0, resultsInvalid.getRowsDeleted());
TestCase.assertEquals(2, resultsInvalid.getRowsUpdated());
TestCase.assertEquals(1, resultsInvalid.getErrors().get(0).getIndex());
TestCase.assertEquals("", resultsInvalid.getErrors().get(0).getPrimaryKey());
} finally {
importer.deleteDataset(dataset.getId());
}
}