Skip to content

Commit

Permalink
fixes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Aug 21, 2016
1 parent 89335e7 commit 25987c8
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,14 @@ private ITableMetaData getMetaData(String tableName,
private void fillRow(DefaultTable table, Map<String, Object> row,
int rowIndex) {
try {
table.addRow();
// set the column values for the current row
for (Map.Entry<String, Object> column : row.entrySet()) {
table.setValue(rowIndex, column.getKey(), column.getValue());

if(!row.entrySet().isEmpty()){
table.addRow();
// set the column values for the current row
for (Map.Entry<String, Object> column : row.entrySet()) {
table.setValue(rowIndex, column.getKey(), column.getValue());
}
}

} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ public class YamlDataSet implements IDataSet {
public YamlDataSet(InputStream source) {
@SuppressWarnings("unchecked")
Map<String, List<Map<String, Object>>> data = (Map<String, List<Map<String, Object>>>) new Yaml().load(source);
for (Map.Entry<String, List<Map<String, Object>>> ent : data.entrySet()) {
String tableName = ent.getKey();
List<Map<String, Object>> rows = ent.getValue();
createTable(tableName.toUpperCase(), rows);
if(data != null){
for (Map.Entry<String, List<Map<String, Object>>> ent : data.entrySet()) {
String tableName = ent.getKey();
List<Map<String, Object>> rows = ent.getValue();
createTable(tableName.toUpperCase(), rows);
}
}

}

class MyTable implements ITable {
Expand All @@ -47,6 +50,8 @@ ITableMetaData createMeta(String name, List<String> columnNames) {
columns = new Column[columnNames.size()];
for (int i = 0; i < columnNames.size(); i++)
columns[i] = new Column(columnNames.get(i), DataType.UNKNOWN);
} else{
columns = new Column[0];
}
return new DefaultTableMetaData(name, columns);
}
Expand Down Expand Up @@ -80,9 +85,11 @@ Map<String, Object> convertMap(Map<String, Object> values) {
}

MyTable createTable(String name, List<Map<String, Object>> rows) {
MyTable table = new MyTable(name, rows.size() > 0 ? new ArrayList<String>(rows.get(0).keySet()) : null);
for (Map<String, Object> values : rows)
table.addRow(values);
MyTable table = new MyTable(name, (rows != null && rows.size() > 0) ? new ArrayList<String>(rows.get(0).keySet()) : null);
if(rows != null){
for (Map<String, Object> values : rows)
table.addRow(values);
}
tables.put(name.toUpperCase(), table);
return table;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ public void compareCurrentDataSetWith(DataSetModel expectedDataSetModel, String[
initDatabaseConnection();
}
current = databaseConnection.createDataSet();
current.getTable("user");
expected = loadDataSet(expectedDataSetModel.getName());
} catch (Exception e) {
throw new RuntimeException("Could not create dataset to compare.", e);
Expand All @@ -448,14 +447,14 @@ public void compareCurrentDataSetWith(DataSetModel expectedDataSetModel, String[
throw new RuntimeException("Could extract dataset table names.", e);
}

for (String tableName : tableNames) {
for (String tableName : expected.getTableNames()) {
ITable expectedTable = null;
ITable actualTable = null;
try {
expectedTable = expected.getTable(tableName);
actualTable = current.getTable(tableName);
} catch (DataSetException e) {
throw new RuntimeException("Could extract dataset table.", e);
throw new RuntimeException("DataSet comparison failed due to following exception: ", e);
}
ITable filteredActualTable = DefaultColumnFilter.includedColumnsTable(actualTable, expectedTable.getTableMetaData().getColumns());
DataSetAssertion.assertEqualsIgnoreCols(expectedTable, filteredActualTable, excludeCols);
Expand Down
40 changes: 40 additions & 0 deletions core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,44 @@ public void shouldMatchExpectedDataSetAfterSeedingDataBase() {
tx().commit();
}
// end::expectedWithSeeding[]

@Test
@DataSet(value = "yml/user.yml", disableConstraints = true)
@ExpectedDataSet(value = "yml/empty.yml")
public void shouldMatchEmptyYmlDataSet() {
tx().begin();
em().remove(em().find(User.class,1L));
em().remove(em().find(User.class,2L));
tx().commit();
}

@Test
@DataSet(value = "yml/user.yml", disableConstraints = true, transactional = true)
@ExpectedDataSet(value = "yml/empty.yml")
public void shouldMatchEmptyYmlDataSetWithTransaction() {
em().remove(em().find(User.class,1L));
em().remove(em().find(User.class,2L));
}


@Test
@DataSet(value = "json/user.json", disableConstraints = true)
@ExpectedDataSet(value = "json/empty.json")
public void shouldMatchEmptyJsonDataSet() {
tx().begin();
em().remove(em().find(User.class,1L));
em().remove(em().find(User.class,2L));
tx().commit();
em().createQuery("select u from User u").getResultList();
}

@Test
@DataSet(value = "xml/user.xml", disableConstraints = true)
@ExpectedDataSet(value = "xml/empty.xml")
public void shouldMatchEmptyXmlDataSet() {
tx().begin();
em().remove(em().find(User.class,1L));
em().remove(em().find(User.class,2L));
tx().commit();
}
}
14 changes: 14 additions & 0 deletions core/src/test/resources/datasets/json/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"USER": [
{
}
],
"TWEET": [
{
}
],
"FOLLOWER": [
{
}
]
}
12 changes: 12 additions & 0 deletions core/src/test/resources/datasets/json/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"USER": [
{
"id": 1,
"name": "@realpestano"
},
{
"id": 2,
"name": "@dbunit"
}
]
}
5 changes: 5 additions & 0 deletions core/src/test/resources/datasets/xml/empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<dataset>
<USER />
<FOLLOWER />
<TWEET />
</dataset>
4 changes: 4 additions & 0 deletions core/src/test/resources/datasets/xml/user.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<dataset>
<USER id="1" name="@realpestano" />
<USER id="2" name="@dbunit" />
</dataset>
5 changes: 5 additions & 0 deletions core/src/test/resources/datasets/yml/empty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
user:

follower:

tweet:

0 comments on commit 25987c8

Please sign in to comment.