Skip to content

Commit 5d76d20

Browse files
committed
KdbTable: Bug fix adding row after setting initial data set
- Add extra null checks to prevent NullPointerExceptions - Add more tests
1 parent 5ac4d8f commit 5d76d20

File tree

2 files changed

+203
-14
lines changed

2 files changed

+203
-14
lines changed

src/main/java/com/buabook/kdb/data/KdbTable.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ private void doSetOfInitialDataSet(Flip initalData, boolean overwrite) throws Da
112112
for(int cCount = 0; cCount < initalData.x.length; cCount++) {
113113
String colName = initalData.x[cCount];
114114
Object[] colData = Converters.arrayToObjectArray(initalData.y[cCount]);
115+
List<Object> objArray = new ArrayList<>(Arrays.asList(colData));
115116

116-
data.put(colName, Arrays.asList(colData));
117+
data.put(colName, objArray);
117118
}
118119

119120
this.rowCount = Converters.arrayToObjectArray(initalData.y[0]).length;
@@ -145,6 +146,9 @@ public void deleteColumn(String columnName) {
145146

146147
/** @see #addRow(Map) */
147148
public void addRow(KdbDict row) throws TableSchemaMismatchException {
149+
if(row == null)
150+
return;
151+
148152
addRow(row.getDataStoreWithStringKeys());
149153
}
150154

@@ -154,7 +158,7 @@ public void addRow(KdbDict row) throws TableSchemaMismatchException {
154158
* @throws TableSchemaMismatchException If there are any missing columns from the new row
155159
*/
156160
public void addRow(Map<String, Object> row) throws TableSchemaMismatchException {
157-
if(row == null)
161+
if(row == null || row.isEmpty())
158162
return;
159163

160164
if(! isEmpty())
@@ -178,17 +182,17 @@ else if(rowValue instanceof Enum)
178182
this.rowCount++;
179183
}
180184

181-
/**@throws TableSchemaMismatchException If the two table names or table schemas do not match */
185+
/**
186+
* Appends the specified table onto the current table (similar to the kdb+ <code>uj</code> function)
187+
* @throws TableSchemaMismatchException If the two table names or table schemas do not match
188+
*/
182189
public void append(KdbTable that) throws TableSchemaMismatchException {
183-
if(that == null)
190+
if(that == null || that.isEmpty())
184191
return;
185192

186193
if(! this.tableName.equals(that.tableName))
187194
throw new TableSchemaMismatchException("Table names are different");
188195

189-
if(that.isEmpty())
190-
return;
191-
192196
if(! isEmpty())
193197
if(! that.getTableData().keySet().containsAll(data.keySet()))
194198
throw new TableSchemaMismatchException("Tables have different schemas");
@@ -213,7 +217,8 @@ public Map<String, List<Object>> getTableData() {
213217

214218
/**
215219
* Converts the nice Java representation of a kdb table into the actual format ready for sending across
216-
* the wire to a kdb process.
220+
* the wire to a kdb process. <b>NOTE</b>: The columns of the generated table will <i>always</i> be in
221+
* alphabetical order.
217222
* @return The object that can be sent to a kdb process
218223
*/
219224
public Flip convertToFlip() {
@@ -239,7 +244,7 @@ public Integer getRowCount() {
239244
}
240245

241246
public Boolean isEmpty() {
242-
return data == null || rowCount == 0 || data.isEmpty();
247+
return rowCount == 0 || data.isEmpty();
243248
}
244249

245250
/**

src/test/java/com/buabook/kdb/data/test/KdbTableTest.java

Lines changed: 189 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.buabook.kdb.data.test;
22

3-
import static org.hamcrest.Matchers.equalTo;
4-
import static org.hamcrest.Matchers.hasKey;
5-
import static org.hamcrest.Matchers.is;
6-
import static org.hamcrest.Matchers.not;
7-
import static org.hamcrest.Matchers.nullValue;
3+
import static org.hamcrest.Matchers.*;
84
import static org.junit.Assert.assertThat;
5+
import static org.hamcrest.CoreMatchers.instanceOf;
96

107
import java.util.ArrayList;
8+
import java.util.HashMap;
119
import java.util.List;
10+
import java.util.Map;
1211

1312
import org.junit.Test;
1413

14+
import com.buabook.kdb.data.KdbDict;
1515
import com.buabook.kdb.data.KdbTable;
1616
import com.buabook.kdb.exceptions.DataOverwriteNotPermittedException;
1717
import com.buabook.kdb.exceptions.TableColumnAlreadyExistsException;
@@ -140,6 +140,186 @@ public void testDeleteColumnDeletesColumn() {
140140
assertThat(table.getTableData(), not(hasKey("key1")));
141141
}
142142

143+
// KdbTable.addRow(KdbDict)
144+
145+
@Test
146+
public void testAddRowKdbDictIgnoresNullRow() {
147+
KdbTable table = new KdbTable("my-test-table");
148+
table.addRow((KdbDict) null);
149+
150+
assertThat(table.isEmpty(), is(equalTo(true)));
151+
}
152+
153+
@Test
154+
public void testAddRowKdbDictAddsRow() {
155+
KdbTable table = new KdbTable("my-test-table");
156+
table.addRow(new KdbDict().add("col1", "val1"));
157+
158+
assertThat(table.getTableData(), hasKey("col1"));
159+
}
160+
161+
// KdbTable.addRow(Map)
162+
163+
@Test
164+
public void testAddRowMapIgnoresNullAndEmptyMaps() {
165+
KdbTable table = new KdbTable("my-test-table");
166+
table.addRow((Map<String, Object>) null);
167+
table.addRow(new HashMap<>());
168+
169+
assertThat(table.isEmpty(), is(equalTo(true)));
170+
}
171+
172+
@Test(expected=TableSchemaMismatchException.class)
173+
public void testAddRowMapThrowsExceptionIfRowMissingColumn() {
174+
KdbTable table = new KdbTable("my-test-table", getTable());
175+
KdbDict newRow = new KdbDict()
176+
.add("key1", 1.3)
177+
.add("key2", 10);
178+
179+
table.addRow(newRow);
180+
}
181+
182+
@Test
183+
public void testAddRowMapAddsNewRow() {
184+
KdbTable table = new KdbTable("my-test-table", getTable());
185+
KdbDict newRow = new KdbDict()
186+
.add("key1", 1.3)
187+
.add("key2", 10)
188+
.add("key3", "a");
189+
190+
table.addRow(newRow);
191+
192+
assertThat(table.getRowCount(), is(equalTo(4)));
193+
assertThat(table.getRow(3), is(equalTo(newRow)));
194+
}
195+
196+
@Test
197+
public void testAddRowMapAddsConvertsListAndEnumTypesCorrectly() {
198+
KdbTable table = new KdbTable("my-test-table");
199+
KdbDict newRow = new KdbDict()
200+
.add("nested-list", ImmutableList.of(1, 2, 3))
201+
.add("enum", TestEnum.VALUE_1);
202+
203+
table.addRow(newRow);
204+
205+
KdbDict addedRow = table.getRow(0);
206+
207+
assertThat(addedRow.get("nested-list"), is(instanceOf(Object[].class)));
208+
assertThat(addedRow.get("enum"), is(equalTo("VALUE_1")));
209+
}
210+
211+
// KdbTable.append
212+
213+
@Test
214+
public void testAppendIgnoresNullOrEmptyTableToAdd() {
215+
KdbTable table = new KdbTable("my-test-table");
216+
table.append(null);
217+
table.append(new KdbTable("my-test-table"));;
218+
219+
assertThat(table.isEmpty(), is(equalTo(true)));
220+
}
221+
222+
@Test(expected=TableSchemaMismatchException.class)
223+
public void testAppendThrowsExceptionIfTableNameOfNonEmptyTableMismatches() {
224+
KdbTable table = new KdbTable("my-test-table");
225+
KdbTable toAppend = new KdbTable("bad-table-name", getTable());
226+
227+
table.append(toAppend);
228+
}
229+
230+
@Test(expected=TableSchemaMismatchException.class)
231+
public void testAppendThrowsExceptionIfTableSchemaToAppendMismatches() {
232+
KdbTable table = new KdbTable("my-test-table", getTable());
233+
KdbTable toAppend = new KdbTable("my-test-table", getTable2());
234+
235+
table.append(toAppend);
236+
}
237+
238+
@Test
239+
public void testAppendAppendsTableToEndOfTable() {
240+
KdbTable table = new KdbTable("my-test-table", getTable());
241+
KdbTable toAppend = new KdbTable("my-test-table", getTable());
242+
243+
table.append(toAppend);
244+
245+
assertThat(table.getRowCount(), is(equalTo(6)));
246+
}
247+
248+
// KdbTable.getTableName
249+
250+
@Test
251+
public void testGetTableNameReturnsTableName() {
252+
KdbTable table = new KdbTable("my-test-table");
253+
assertThat(table.getTableName(), is(equalTo("my-test-table")));
254+
}
255+
256+
// KdbTable.getTableData
257+
258+
@Test
259+
public void testGetTableDataReturnsUnderlyingTableData() {
260+
KdbTable table = new KdbTable("my-test-table");
261+
assertThat(table.getTableData(), is(anEmptyMap()));
262+
}
263+
264+
// KdbTable.convertToFlip
265+
266+
@Test
267+
public void testConvertToFlipReturnsNullForEmptyTable() {
268+
KdbTable table = new KdbTable("my-test-table");
269+
assertThat(table.convertToFlip(), is(nullValue()));
270+
}
271+
272+
@Test
273+
public void testConvertToFlipReturnsFlip() {
274+
KdbTable table = new KdbTable("my-test-table", getTable());
275+
Flip converted = table.convertToFlip();
276+
277+
assertThat(converted.x, is(arrayWithSize(3)));
278+
assertThat(converted.x, is(arrayContainingInAnyOrder("key1", "key2", "key3")));
279+
}
280+
281+
// KdbTable.getRowCount()
282+
283+
@Test
284+
public void testGetRowCountReturns0ForEmptyTable() {
285+
KdbTable table = new KdbTable("my-test-table");
286+
assertThat(table.getRowCount(), is(equalTo(0)));
287+
}
288+
289+
@Test
290+
public void testGetRowCountReturnsRowCount() {
291+
KdbTable table = new KdbTable("my-test-table", getTable());
292+
assertThat(table.getRowCount(), is(equalTo(3)));
293+
}
294+
295+
// KdbTable.isEmpty
296+
297+
@Test
298+
public void testIsEmptyReturnsTrueForEmptyTable() {
299+
KdbTable table = new KdbTable("my-test-table");
300+
assertThat(table.isEmpty(), is(equalTo(false)));
301+
}
302+
303+
@Test
304+
public void testIsEmptyReturnsFalseForNonEmptyTable() {
305+
KdbTable table = new KdbTable("my-test-table", getTable());
306+
assertThat(table.isEmpty(), is(equalTo(false)));
307+
}
308+
309+
// KdbTable.changeTableName
310+
311+
@Test(expected=UnsupportedOperationException.class)
312+
public void testChangeTableNameThrowsExceptionIfNewNameIsNull() {
313+
new KdbTable("table-name").changeTableName(null);
314+
}
315+
316+
@Test
317+
public void testChangeTableNameChangesTableName() {
318+
KdbTable table = new KdbTable("table-name");
319+
table.changeTableName("some-other-table-name");
320+
321+
assertThat(table.getTableName(), is(equalTo("some-other-table-name")));
322+
}
143323

144324

145325
private Flip getTable() {
@@ -165,4 +345,8 @@ private Flip getTable2() {
165345

166346
return new Flip(new Dict(keys, cols));
167347
}
348+
349+
enum TestEnum {
350+
VALUE_1;
351+
}
168352
}

0 commit comments

Comments
 (0)