From cc4ddd112c2f838f4995c462b1a93f64f0d43ba1 Mon Sep 17 00:00:00 2001 From: Jesper Jepsen Date: Mon, 7 Oct 2013 18:56:49 +0200 Subject: [PATCH] Added test cases for cursor. --- src/tightdb/objc/test/Makefile | 4 +- src/tightdb/objc/test/functional.m | 279 +++++++++++++++++++++++++++++ 2 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 src/tightdb/objc/test/functional.m diff --git a/src/tightdb/objc/test/Makefile b/src/tightdb/objc/test/Makefile index b696c0b1a4..95d53d3c40 100644 --- a/src/tightdb/objc/test/Makefile +++ b/src/tightdb/objc/test/Makefile @@ -4,9 +4,9 @@ XCODE_HOME = $(shell xcode-select --print-path) unit_tests_SOURCES = \ data_type.mm table.m group.m group_misc_2.m table_delete_all.m tutorial.m enumerator.m \ -get_subtable.m subtable.m mixed.m query.m shared_group.m err_handling.mm +get_subtable.m subtable.m mixed.m query.m shared_group.m err_handling.mm functional.m -#unit_tests_SOURCES = err_handling.mm +#unit_tests_SOURCES = functional.m unit_tests_CFLAGS = -Wno-comment -fobjc-arc -fobjc-abi-version=2 -F$(XCODE_HOME)/Library/Frameworks unit_tests_LDFLAGS = -bundle -fobjc-link-runtime -F$(XCODE_HOME)/Library/Frameworks -framework Cocoa -framework SenTestingKit diff --git a/src/tightdb/objc/test/functional.m b/src/tightdb/objc/test/functional.m new file mode 100644 index 0000000000..01dcd81edc --- /dev/null +++ b/src/tightdb/objc/test/functional.m @@ -0,0 +1,279 @@ +// functional.m +// TightDB +// +// This test is aimed at verifying functionallity added by the binding. + + +#import +#import +#import + +TIGHTDB_TABLE_3(FuncPeopleTable, + Name, String, + Age, Int, + Hired, Bool) + +#define TABLE_SIZE 1000 // must be even number +#define INSERT_ROW 5 + +@interface MACtestFunctional: SenTestCase +@end +@implementation MACtestFunctional + +- (void)testTypedCursor +{ + + /* + * Cursor in a table. + */ + + FuncPeopleTable *table = [[FuncPeopleTable alloc] init]; + + FuncPeopleTable_Cursor *cursor; + + // Add rows + for (int i = 0; i < TABLE_SIZE; i++) { + cursor = [table addRow]; + cursor.Name = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",i]]; + cursor.Age = i; + cursor.Hired = i%2 == 0; + }; + + // Check the values + int i= 0; + for (cursor in table) { + NSString *expected = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",i]]; + STAssertEquals([[NSString stringWithString:cursor.Name] isEqual:expected], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:cursor.Age] isEqual:[NSNumber numberWithInt:i]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:cursor.Hired] isEqual:[NSNumber numberWithBool:i%2 == 0]], YES, @"Check hired"); + i++; + } + + // Insert a row + cursor = [table insertRowAtIndex:INSERT_ROW]; + cursor.Name = @"Person_Inserted"; + cursor.Age = 99; + cursor.Hired = YES; + + // Check inserted row + cursor = [table cursorAtIndex:INSERT_ROW]; + STAssertEquals([[NSString stringWithString:cursor.Name] isEqual:@"Person_Inserted"], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:cursor.Age] isEqual:[NSNumber numberWithInt:99]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:cursor.Hired] isEqual:[NSNumber numberWithBool:YES]], YES, @"Check hired"); + + // Check row before + cursor = [table cursorAtIndex:INSERT_ROW-1]; + NSString *expected = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",INSERT_ROW-1]]; + STAssertEquals([[NSString stringWithString:cursor.Name] isEqual:expected], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:cursor.Age] isEqual:[NSNumber numberWithInt:INSERT_ROW-1]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:cursor.Hired] isEqual:[NSNumber numberWithBool:(INSERT_ROW-1)%2 == 0]], YES, @"Check hired"); + + // Check row after (should be equal to the previous row at index INSERT_ROW). + cursor = [table cursorAtIndex:INSERT_ROW+1]; + NSString *expected2 = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",INSERT_ROW]]; + STAssertEquals([[NSString stringWithString:cursor.Name] isEqual:expected2], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:cursor.Age] isEqual:[NSNumber numberWithInt:INSERT_ROW]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:cursor.Hired] isEqual:[NSNumber numberWithBool:(INSERT_ROW)%2 == 0]], YES, @"Check hired"); + + // Get a cursor at the last index + cursor = [table cursorAtLastIndex]; + NSString *expected3 = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",TABLE_SIZE-1]]; + STAssertEquals([[NSString stringWithString:cursor.Name] isEqual:expected3], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:cursor.Age] isEqual:[NSNumber numberWithInt:TABLE_SIZE-1]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:cursor.Hired] isEqual:[NSNumber numberWithBool:(TABLE_SIZE-1)%2 == 0]], YES, @"Check hired"); + + // Remove the inserted. The query test check that the row was + // removed correctly (that we're back to the original table). + [table removeRowAtIndex:INSERT_ROW]; + [table removeLastRow]; + [table removeLastRow]; + STAssertEquals([NSNumber numberWithLong:[table count]], [NSNumber numberWithLong:TABLE_SIZE-2], @"Check the size"); + + // TODO: InsertRowAtIndex.. out-of-bounds check (depends on error handling strategy) + // TODO: CursorAtIndex.. out-of-bounds check (depends onerror handling strategy + + /* + * Cursor in a query. + */ + + FuncPeopleTable_Query *query = [[table where].Name columnIsNotEqualTo:@"Nothing is equal to this"]; // dummy query required right now + STAssertEquals([query count], [NSNumber numberWithLong:(TABLE_SIZE-2)], @"Check the size"); + + i=0; + for (cursor in query) { + NSString *expected = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",i]]; + STAssertEquals([[NSString stringWithString:cursor.Name] isEqual:expected], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:cursor.Age] isEqual:[NSNumber numberWithInt:i]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:cursor.Hired] isEqual:[NSNumber numberWithBool:i%2 == 0]], YES, @"Check hired"); + i++; + } + + /* + * Cursor in table view. + */ + + FuncPeopleTable_View *view = [[query.Hired columnIsEqualTo:YES] findAll]; + STAssertEquals([query count], [NSNumber numberWithLong:(TABLE_SIZE-2)/2], @"Check the size"); + + i=0; + for (cursor in view) { + NSString *expected = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",i]]; + STAssertEquals([[NSString stringWithString:cursor.Name] isEqual:expected], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:cursor.Age] isEqual:[NSNumber numberWithInt:i]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:cursor.Hired] isEqual:[NSNumber numberWithBool:YES]], YES, @"Check hired"); + i = i + 2; // note: +2 + } + + // Modify a row in the view + + cursor = [view cursorAtIndex:[view count]-1]; // last row in view (Hired = all YES) + cursor.Name = @"Modified by view"; + + // Check the effect on the table + + cursor = [table cursorAtIndex:[table count]-2]; // the second last row in the view (Hired = .....YES, NO, YES, NO) + STAssertEquals([[NSString stringWithString:cursor.Name] isEqual:@"Modified by view"], YES, @"Check mod by view"); + + // Now delete that row + + [view removeRowAtIndex:[view count]-1]; // last row in view (Hired = all YES) + + // And check it's gone. + + STAssertEquals([NSNumber numberWithLong:[table count]], [NSNumber numberWithLong:TABLE_SIZE-3], @"Check the size"); + +} + +- (void)testDynamicCursor +{ + + + /* + * Cursor in a table. + */ + + TightdbTable *table = [[TightdbTable alloc] init]; + + size_t const NAME = [table addColumnWithType:tightdb_String andName:@"Name"]; + size_t const AGE = [table addColumnWithType:tightdb_Int andName:@"Age"]; + size_t const HIRED = [table addColumnWithType:tightdb_Bool andName:@"Hired"]; + + TightdbCursor *cursor; + + // Add rows + for (int i = 0; i < TABLE_SIZE; i++) { + cursor = [table addRow]; + [cursor setString:[@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",i]] inColumn:NAME]; + [cursor setInt:i inColumn:AGE]; + [cursor setBool:i%2 == 0 inColumn:HIRED]; + }; + + // Check the values + int i= 0; + for (cursor in table) { + NSString *expected = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",i]]; + STAssertEquals([[NSString stringWithString:[cursor getStringInColumn:NAME]] isEqual:expected], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:[cursor getIntInColumn:AGE]] isEqual:[NSNumber numberWithInt:i]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:[cursor getBoolInColumn:HIRED]] isEqual:[NSNumber numberWithBool:i%2 == 0]], YES, @"Check hired"); + i++; + } + + // Insert a row + cursor = [table insertRowAtIndex:INSERT_ROW]; + [cursor setString:@"Person_Inserted" inColumn:NAME]; + [cursor setInt:99 inColumn:AGE]; + [cursor setBool:YES inColumn:HIRED]; + + // Check inserted row + cursor = [table cursorAtIndex:INSERT_ROW]; + STAssertEquals([[NSString stringWithString:[cursor getStringInColumn:NAME]] isEqual:@"Person_Inserted"], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:[cursor getIntInColumn:AGE]] isEqual:[NSNumber numberWithInt:99]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:[cursor getBoolInColumn:HIRED]] isEqual:[NSNumber numberWithBool:YES]], YES, @"Check hired"); + + // Check row before + cursor = [table cursorAtIndex:INSERT_ROW-1]; + NSString *expected = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",INSERT_ROW-1]]; + STAssertEquals([[NSString stringWithString:[cursor getStringInColumn:NAME]] isEqual:expected], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:[cursor getIntInColumn:AGE]] isEqual:[NSNumber numberWithInt:INSERT_ROW-1]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:[cursor getBoolInColumn:HIRED]] isEqual:[NSNumber numberWithBool:(INSERT_ROW-1)%2 == 0]], YES, @"Check hired"); + + // Check row after (should be equal to the previous row at index INSERT_ROW). + cursor = [table cursorAtIndex:INSERT_ROW+1]; + NSString *expected2 = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",INSERT_ROW]]; + STAssertEquals([[NSString stringWithString:[cursor getStringInColumn:NAME]] isEqual:expected2], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:[cursor getIntInColumn:AGE]] isEqual:[NSNumber numberWithInt:INSERT_ROW]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:[cursor getBoolInColumn:HIRED]] isEqual:[NSNumber numberWithBool:(INSERT_ROW)%2 == 0]], YES, @"Check hired"); + + // Get a cursor at the last index + cursor = [table cursorAtLastIndex]; + NSString *expected3 = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",TABLE_SIZE-1]]; + STAssertEquals([[NSString stringWithString:[cursor getStringInColumn:NAME]] isEqual:expected3], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:[cursor getIntInColumn:AGE]] isEqual:[NSNumber numberWithInt:TABLE_SIZE-1]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:[cursor getBoolInColumn:HIRED]] isEqual:[NSNumber numberWithBool:(TABLE_SIZE-1)%2 == 0]], YES, @"Check hired"); + + // Remove the inserted. The query test check that the row was + // removed correctly (that we're back to the original table). + [table removeRowAtIndex:INSERT_ROW]; + [table removeLastRow]; + [table removeLastRow]; + STAssertEquals([NSNumber numberWithLong:[table count]], [NSNumber numberWithLong:TABLE_SIZE-2], @"Check the size"); + + // TODO: InsertRowAtIndex.. out-of-bounds check (depends on error handling strategy) + // TODO: CursorAtIndex.. out-of-bounds check (depends onerror handling strategy + + /* + * Cursor in a query. + */ + + TightdbQuery *query = [[table where] column:NAME isNotEqualToString:@"Nothing is equal to this"]; // dummy query required right now + STAssertEquals([query count], [NSNumber numberWithLong:(TABLE_SIZE-2)], @"Check the size"); + + i=0; + for (cursor in query) { + NSString *expected = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",i]]; + STAssertEquals([[NSString stringWithString:[cursor getStringInColumn:NAME]] isEqual:expected], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:[cursor getIntInColumn:AGE]] isEqual:[NSNumber numberWithInt:i]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:[cursor getBoolInColumn:HIRED]] isEqual:[NSNumber numberWithBool:i%2 == 0]], YES, @"Check hired"); + i++; + } + + /* + * Cursor in table view. + */ + + TightdbView *view = [[query column:HIRED isEqualToBool:YES] findAll]; + STAssertEquals([query count], [NSNumber numberWithLong:(TABLE_SIZE-2)/2], @"Check the size"); + + i=0; + for (cursor in view) { + NSString *expected = [@"Person_" stringByAppendingString: [NSString stringWithFormat:@"%d",i]]; + STAssertEquals([[NSString stringWithString:[cursor getStringInColumn:NAME]] isEqual:expected], YES, @"Check name"); + STAssertEquals([[NSNumber numberWithLong:[cursor getIntInColumn:AGE]] isEqual:[NSNumber numberWithInt:i]], YES, @"Check age"); + STAssertEquals([[NSNumber numberWithBool:[cursor getBoolInColumn:HIRED]] isEqual:[NSNumber numberWithBool:YES]], YES, @"Check hired"); + i = i + 2; // note: +2 + } + + // Modify a row in the view + + cursor = [view cursorAtIndex:[view count]-1]; // last row in view (Hired = all YES) + [cursor setString:@"Modified by view" inColumn:NAME]; + + // Check the effect on the table + + cursor = [table cursorAtIndex:[table count]-2]; // the second last row in the view (Hired = .....YES, NO, YES, NO) + STAssertEquals([[NSString stringWithString:[cursor getStringInColumn:NAME]] isEqual:@"Modified by view"], YES, @"Check mod by view"); + + // Now delete that row + + [view removeRowAtIndex:[view count]-1]; // last row in view (Hired = all YES) + + // And check it's gone. + + STAssertEquals([NSNumber numberWithLong:[table count]], [NSNumber numberWithLong:TABLE_SIZE-3], @"Check the size"); + +} + + + + +@end