aboutsummaryrefslogtreecommitdiffstats
path: root/UnitTests
diff options
context:
space:
mode:
authorAbhi Beckert <abhi@abhibeckert.com>2016-01-29 06:40:18 +1000
committerAbhi Beckert <abhi@abhibeckert.com>2016-01-29 06:40:18 +1000
commitf4c4300dca0c416c12b3992204778ef9f37981ec (patch)
treee76a4747bf1c3f82bc037990bf84e3f13062601e /UnitTests
parent465062dacc96c2c84bfe9cff1ded58c14d01bdcd (diff)
parent3aadea1be33212ca50e7faffcd0620ea976f9d59 (diff)
downloadsequelpro-f4c4300dca0c416c12b3992204778ef9f37981ec.tar.gz
sequelpro-f4c4300dca0c416c12b3992204778ef9f37981ec.tar.bz2
sequelpro-f4c4300dca0c416c12b3992204778ef9f37981ec.zip
Merge remote-tracking branch 'sequelpro/master'
Diffstat (limited to 'UnitTests')
-rw-r--r--UnitTests/SPDataAdditionsTests.m103
-rw-r--r--UnitTests/SPStringAdditionsTests.m21
-rw-r--r--UnitTests/SPTableCopyTest.m70
3 files changed, 164 insertions, 30 deletions
diff --git a/UnitTests/SPDataAdditionsTests.m b/UnitTests/SPDataAdditionsTests.m
index 812eb45d..988267d2 100644
--- a/UnitTests/SPDataAdditionsTests.m
+++ b/UnitTests/SPDataAdditionsTests.m
@@ -40,6 +40,7 @@
- (void)testDataEncryptedWithKeyIV;
- (void)testDataDecryptedWithPassword;
- (void)testDataDecryptedWithKey;
+- (void)testEnumerateLinesBreakingAt_withBlock;
@end
@@ -281,4 +282,106 @@
}
}
+- (void)testEnumerateLinesBreakingAt_withBlock
+{
+ //simple empty data
+ {
+ __block NSUInteger invocations = 0;
+ NSData *data = [NSData data];
+ [data enumerateLinesBreakingAt:SPLineTerminatorAny withBlock:^(NSRange line, BOOL *stop) {
+ invocations++;
+ }];
+ STAssertTrue(invocations==0, @"Empty data never invokes block");
+ }
+ //simple unix file
+ {
+ const char inp[] = "Two\nLines\n";
+ __block NSUInteger invocations = 0;
+ NSData *data = [NSData dataWithBytes:inp length:strlen(inp)];
+ [data enumerateLinesBreakingAt:SPLineTerminatorAny withBlock:^(NSRange line, BOOL *stop) {
+ switch (invocations) {
+ case 0:
+ STAssertEquals(line, NSMakeRange(0, 3), @"range of first line");
+ break;
+ case 1:
+ STAssertEquals(line, NSMakeRange(4, 5), @"range of second line");
+ break;
+ }
+ invocations++;
+ }];
+ STAssertTrue(invocations==2, @"File with two lines, terminated with empty line");
+ }
+ //simple windows file without ending empty line
+ {
+ const char inp[] = "A\r\nWindows\r\nfile";
+ __block NSUInteger invocations = 0;
+ NSData *data = [NSData dataWithBytes:inp length:strlen(inp)];
+ [data enumerateLinesBreakingAt:SPLineTerminatorAny withBlock:^(NSRange line, BOOL *stop) {
+ switch (invocations) {
+ case 0:
+ STAssertEquals(line, NSMakeRange(0, 1), @"range of first line");
+ break;
+ case 1:
+ STAssertEquals(line, NSMakeRange(3, 7), @"range of second line");
+ break;
+ case 2:
+ STAssertEquals(line, NSMakeRange(12, 4), @"range of third line");
+ break;
+ }
+ invocations++;
+ }];
+ STAssertTrue(invocations==3, @"File with three lines, CRLF, terminated with empty line");
+ }
+ //empty lines with all 3 endings
+ {
+ const char inp[] = "\n\r\n\r";
+ __block NSUInteger invocations = 0;
+ NSData *data = [NSData dataWithBytes:inp length:strlen(inp)];
+ [data enumerateLinesBreakingAt:SPLineTerminatorAny withBlock:^(NSRange line, BOOL *stop) {
+ switch (invocations) {
+ case 0:
+ STAssertEquals(line, NSMakeRange(0, 0), @"range of first line");
+ break;
+ case 1:
+ STAssertEquals(line, NSMakeRange(1, 0), @"range of second line");
+ break;
+ case 2:
+ STAssertEquals(line, NSMakeRange(3, 0), @"range of third line");
+ break;
+ }
+ invocations++;
+ }];
+ STAssertTrue(invocations==3, @"LF, CRLF and CR mixed");
+ }
+ //looking for specific line breaks only
+ {
+ const char inp[] = "foo\nbar\r\nbaz\r";
+ __block NSUInteger invocations = 0;
+ NSData *data = [NSData dataWithBytes:inp length:strlen(inp)];
+ [data enumerateLinesBreakingAt:SPLineTerminatorCRLF withBlock:^(NSRange line, BOOL *stop) {
+ switch (invocations) {
+ case 0:
+ STAssertEquals(line, NSMakeRange(0, 7), @"range of first line");
+ break;
+ case 1:
+ STAssertEquals(line, NSMakeRange(9, 4), @"range of second line");
+ break;
+ }
+ invocations++;
+ }];
+ STAssertTrue(invocations==2, @"other line breaks when only CRLF is expected");
+ }
+ //stopping early
+ {
+ const char inp[] = "Two\nLines\n";
+ __block NSUInteger invocations = 0;
+ NSData *data = [NSData dataWithBytes:inp length:strlen(inp)];
+ [data enumerateLinesBreakingAt:SPLineTerminatorAny withBlock:^(NSRange line, BOOL *stop) {
+ invocations++;
+ *stop = YES;
+ }];
+ STAssertTrue(invocations==1, @"File with two lines, stopped after first");
+ }
+}
+
@end
diff --git a/UnitTests/SPStringAdditionsTests.m b/UnitTests/SPStringAdditionsTests.m
index 23ec82e9..00cbdc14 100644
--- a/UnitTests/SPStringAdditionsTests.m
+++ b/UnitTests/SPStringAdditionsTests.m
@@ -39,6 +39,7 @@
- (void)testStringWithNewUUID;
- (void)testCreateViewSyntaxPrettifier;
- (void)testNonConsecutivelySearchStringMatchingRanges;
+- (void)testStringByReplacingCharactersInSetWithString;
@end
@@ -195,6 +196,26 @@ static NSRange RangeFromArray(NSArray *a,NSUInteger idx);
}
+- (void)testStringByReplacingCharactersInSetWithString
+{
+ {
+ //test against empty string
+ STAssertEqualObjects([@"" stringByReplacingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] withString:@"x"], @"", @"replacement on empty string must result in empty string");
+ }
+ {
+ //test match at begin, middle, end / consecutive matches
+ STAssertEqualObjects([@" ab c " stringByReplacingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] withString:@"_"], @"_ab__c_", @"Testing matches at both end, replacement of consecutive matches");
+ }
+ {
+ //test replacement of different characters
+ STAssertEqualObjects([@"ab\r\ncd" stringByReplacingCharactersInSet:[NSCharacterSet newlineCharacterSet] withString:@"*"], @"ab**cd", @"Testing replacement of different characters in set");
+ }
+ {
+ // nil for replacement char
+ STAssertEqualObjects([@"ab\r\ncd" stringByReplacingCharactersInSet:[NSCharacterSet newlineCharacterSet] withString:nil], @"abcd", @"testing replacement with nil");
+ }
+}
+
@end
NSRange RangeFromArray(NSArray *a,NSUInteger idx)
diff --git a/UnitTests/SPTableCopyTest.m b/UnitTests/SPTableCopyTest.m
index 76a2fe45..05ab4a76 100644
--- a/UnitTests/SPTableCopyTest.m
+++ b/UnitTests/SPTableCopyTest.m
@@ -39,52 +39,62 @@
@interface SPTableCopyTest : SenTestCase
- (void)testCopyTableFromToWithData;
+- (void)testCopyTableFromTo_NoPermissions;
@end
@implementation SPTableCopyTest
-- (id)mockConnection
-{
- return [[OCMockObject niceMockForClass:[SPMySQLConnection class]] autorelease];
-}
-
-- (id)mockResult
-{
- return [[OCMockObject niceMockForClass:[SPMySQLResult class]] autorelease];
-}
-
- (void)testCopyTableFromToWithData
{
- id mockResult = [self mockResult];
+ id mockResult = OCMClassMock([SPMySQLResult class]);
- unsigned long long varOne = 1;
- NSValue *valueOne = [NSValue value:&varOne withObjCType:@encode(__typeof__(varOne))];
- BOOL varNo = NO;
-
- NSValue *valueNo = [NSValue value:&varNo withObjCType:@encode(BOOL)];
NSArray *resultArray = [[NSArray alloc] initWithObjects:@"", @"CREATE TABLE `table_name` ()", nil];
- id mockConnection = [self mockConnection];
+ id mockConnection = OCMClassMock([SPMySQLConnection class]);
- [(SPMySQLResult *)[[mockResult expect] andReturn:valueOne] numberOfRows];
- [[[mockResult expect] andReturn:resultArray] getRowAsArray];
+ OCMExpect([mockResult numberOfRows]).andReturn(1);
+ OCMExpect([mockResult getRowAsArray]).andReturn(resultArray);
- [[[mockConnection expect] andReturn:mockResult] queryString:@"SHOW CREATE TABLE `source_db`.`table_name`"];
- [[mockConnection expect] queryString:@"CREATE TABLE `target_db`.`table_name` ()"];
- [[mockConnection expect] queryString:@"INSERT INTO `target_db`.`table_name` SELECT * FROM `source_db`.`table_name`"];
- [[[mockConnection stub] andReturnValue:valueNo] queryErrored];
+ OCMExpect([mockConnection queryString:@"SHOW CREATE TABLE `source_db`.`table_name`"]).andReturn(mockResult);
+ OCMExpect([mockConnection queryString:@"CREATE TABLE `target_db`.`table_name` ()"]);
+ OCMExpect([mockConnection queryString:@"INSERT INTO `target_db`.`table_name` SELECT * FROM `source_db`.`table_name`"]);
+ OCMStub([mockConnection queryErrored]).andReturn(NO);
- SPTableCopy *tableCopy = [[SPTableCopy alloc] init];
-
- [tableCopy setConnection:mockConnection];
- [tableCopy copyTable:@"table_name" from:@"source_db" to:@"target_db" withContent:YES];
+ {
+ SPTableCopy *tableCopy = [[SPTableCopy alloc] init];
+
+ [tableCopy setConnection:mockConnection];
+ [tableCopy copyTable:@"table_name" from:@"source_db" to:@"target_db" withContent:YES];
+
+ [tableCopy release];
+ }
- [mockResult verify];
- [mockConnection verify];
+ OCMVerifyAll(mockResult);
+ OCMVerifyAll(mockConnection);
- [tableCopy release];
[resultArray release];
}
+- (void)testCopyTableFromTo_NoPermissions
+{
+ id mockConnection = OCMStrictClassMock([SPMySQLConnection class]);
+
+ OCMExpect([mockConnection queryString:@"SHOW CREATE TABLE `source_db`.`table_name`"]).andReturn(nil);
+ OCMStub([mockConnection queryErrored]).andReturn(YES);
+ OCMStub([mockConnection lastErrorMessage]).andReturn(@"SHOW command denied to user 'alice'@'localhost' for table 'table_name'");
+ OCMStub([mockConnection lastErrorID]).andReturn(1142);
+ OCMStub([mockConnection lastSqlstate]).andReturn(@"42000");
+
+ {
+ SPTableCopy *tableCopy = [[SPTableCopy alloc] init];
+
+ STAssertFalse([tableCopy copyTable:@"table_name" from:@"source_db" to:@"target_db"],@"copy operation must fail.");
+
+ [tableCopy release];
+ }
+
+ [mockConnection verify];
+}
+
@end