11package 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 .*;
84import static org .junit .Assert .assertThat ;
5+ import static org .hamcrest .CoreMatchers .instanceOf ;
96
107import java .util .ArrayList ;
8+ import java .util .HashMap ;
119import java .util .List ;
10+ import java .util .Map ;
1211
1312import org .junit .Test ;
1413
14+ import com .buabook .kdb .data .KdbDict ;
1515import com .buabook .kdb .data .KdbTable ;
1616import com .buabook .kdb .exceptions .DataOverwriteNotPermittedException ;
1717import 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