Ninja-Squad / DbSetup

An API for populating a database in unit tests
http://dbsetup.ninja-squad.com/
212 stars 34 forks source link

Rows counter on Insert.Builder #33

Closed arey closed 9 years ago

arey commented 9 years ago

We are using DbSetup to initialize a H2 database with large data set. For each new use case of our integration tests, we have to add new rows. Our requirement is to know how much rows we have inserted on some tables. We need this count number for assertion. A getRowsCount() method on the Insert.Builder nested class would be welcome. What do you think about this improvement?

jnizet commented 9 years ago

Could you tell us more about why this method would be useful? Why isn't it possible to maintain a separate counter and increment it each time a row is inserted? If this counter is really useful/necessary, shouldn't it be on Insert rather than Insert.Builder? Insert is immutable, and its number of rows never changes. Whereas Insert.Builder is mutable, its number of rows changes, and we can even be inside the creation of a row (which would make getRowCount() ambiguous).

arey commented 9 years ago

We currently maintain a separate counter but the code is not pretty. See by yourself:

int rowCount = 
...
Insert.Builder builder = insertInto("MY_TABLE") .columns("ID", "DATE", "NAME");
Object[][] allValues = new Object[][]{ {1, "2015-02-16", "Name 1"},  /*...*/ }; 
rowCount = allValues.length; 
for (Object[] val : allValues) { 
    builder.values(val);
} 
return builder.build();

Thus a getRowCount() method on Insert would be useful.

jnizet commented 9 years ago

OK. That doesn't hurt. I'll do that soon (probably Friday).

jnizet commented 9 years ago

@arey I released version 1.4.0, containing the new method. It should appear on Maven central in the next hours.