diff options
Diffstat (limited to 'UnitTests')
-rw-r--r-- | UnitTests/MCPKitTest.h | 11 | ||||
-rw-r--r-- | UnitTests/MCPKitTest.m | 88 | ||||
-rw-r--r-- | UnitTests/SPStringAdditionsTest.m | 39 |
3 files changed, 95 insertions, 43 deletions
diff --git a/UnitTests/MCPKitTest.h b/UnitTests/MCPKitTest.h index d4d1ffec..926780fa 100644 --- a/UnitTests/MCPKitTest.h +++ b/UnitTests/MCPKitTest.h @@ -27,6 +27,17 @@ @class MCPConnection; +/** + * @class MCPKitTest MCPKitTest.h + * + * MCPKit test case class. + * + * Note that this test case class uses the 'sakila' database to perform it's tests. It is available from: + * + * http://downloads.mysql.com/docs/sakila-db.zip + * + * You must also create a user called 'sp_tester' with no password and all permissions on the database 'sakila'. + */ @interface MCPKitTest : SenTestCase { MCPConnection *connection; diff --git a/UnitTests/MCPKitTest.m b/UnitTests/MCPKitTest.m index e347d9da..4480b0a1 100644 --- a/UnitTests/MCPKitTest.m +++ b/UnitTests/MCPKitTest.m @@ -27,59 +27,97 @@ #import "MCPKitTest.h" +static const NSString *SPTestDatabaseHost = @"127.0.0.1"; +static const NSString *SPTestDatabaseName = @"sakila"; +static const NSString *SPTestDatabaseUser = @"sp_tester"; +static const NSString *SPTestDatabasePassword = @""; + +static const NSInteger SPTestDatabasePort = 3306; + @implementation MCPKitTest +#pragma mark - +#pragma mark Setup & tear down + +/** + * Sets up the connection for use in the test cases. + */ - (void)setUp { - // For now, we try an find the following database in the local connection. - // If the connection fails for any reasons, tests are not run. - // http://downloads.mysql.com/docs/sakila-db.zip - // - // Set up a user called 'sakila' with no password that has all privs on the - // database 'sakila'. + connection = [[MCPConnection alloc] initToHost:SPTestDatabaseHost withLogin:SPTestDatabaseUser usingPort:SPTestDatabasePort]; + + [connection setPassword:SPTestDatabasePassword]; - connection = [[MCPConnection alloc] initToSocket:@"/var/mysql/mysql.sock" withLogin:@"sakila"]; + [connection setConnectionTimeout:10]; + [connection setUseKeepAlive:1]; + [connection setKeepAliveInterval:60]; - // Set the 'sakila' user's password - [connection setPassword:@""]; + [connection connect]; if (![connection isConnected]) { - [connection dealloc]; - connection = nil; + [connection release], connection = nil; + STFail(@"Error connecting to database server. No tests were run."); } else { - if (![connection selectDB:@"sakila"]) { - [connection dealloc]; - connection = nil; - STFail(@"Error selecting database 'sakila'. No tests were run."); + if (![connection selectDB:SPTestDatabaseName]) { + [connection release], connection = nil; + + STFail(@"Error selecting database '%@'. No tests were run.", SPTestDatabaseName); } } } +/** + * Disconnects the connection if connected. + */ - (void)tearDown { - if (connection != nil) { + if (connection && [connection isConnected]) { [connection disconnect]; - [connection dealloc]; } + + [connection release], connection = nil; } -- (void)testServerVersion +#pragma mark - +#pragma mark Tests + +/** + * Tests the connection's major version number. + */ +- (void)testServerMajorVersion { - if (connection == nil) return; + if ((!connection) || (![connection isConnected])) return; - STAssertTrue([connection serverMajorVersion] != 0, @"server version"); - STAssertTrue([connection serverMajorVersion] != 0, @"server version"); + STAssertTrue(([connection serverMajorVersion] != 0), @"server major version"); } -- (void)testTableList +/** + * Tests the connection's version string. + */ +- (void)testServerVersionString { - if (connection == nil) return; + if ((!connection) || (![connection isConnected])) return; + + STAssertTrue(([[connection serverVersionString] length] > 0), @"server version string"); +} + +/** + * Tests the connection query execution. + */ +- (void)testQueryExexution +{ + if ((!connection) || (![connection isConnected])) return; - MCPResult *queryResult = [connection queryString:@"SELECT * FROM actor"]; + MCPResult *result = [connection queryString:@"SELECT * FROM actor"]; - STAssertEquals([queryResult numOfRows], (my_ulonglong)200, @"actors table count"); + if ([connection queryErrored]) { + STFail(@"Query execution failed with error: %@", [connection getLastErrorMessage]); + } + else { + STAssertEquals([result numOfRows], (my_ulonglong)200, @"'actors' table count"); + } } @end diff --git a/UnitTests/SPStringAdditionsTest.m b/UnitTests/SPStringAdditionsTest.m index 208381a8..8fce7e07 100644 --- a/UnitTests/SPStringAdditionsTest.m +++ b/UnitTests/SPStringAdditionsTest.m @@ -26,32 +26,35 @@ #import "SPStringAdditionsTest.h" #import "SPStringAdditions.h" -@implementation SPStringAdditionsTest - -- (void)setUp -{ - -} +static const NSString *SPASCIITestString = @"this is a big, crazy test st'ring with som'e random spaces and quot'es"; +static const NSString *SPUTFTestString = @"In der Kürze liegt die Würz"; -- (void)tearDown -{ - -} +@implementation SPStringAdditionsTest +/** + * stringByRemovingCharactersInSet test case. + */ - (void)testStringByRemovingCharactersInSet { - NSCharacterSet *junk = [NSCharacterSet characterSetWithCharactersInString:@"abc',ü"]; + NSString *charsToRemove = @"abc',ü"; + + NSCharacterSet *junk = [NSCharacterSet characterSetWithCharactersInString:charsToRemove]; - NSString *s = @"this is big, crazy st'ring"; - NSString *expect = @"this is ig rzy string"; + NSString *actualUTFString = SPUTFTestString; + NSString *actualASCIIString = SPASCIITestString; - STAssertEqualObjects([s stringByRemovingCharactersInSet:junk], expect, @"stringByRemovingCharactersInSet"); + NSString *expectedUTFString = @"In der Krze liegt die Wrz"; + NSString *expectedASCIIString = @"this is ig rzy test string with some rndom spes nd quotes"; - // Check UTF - s = @"In der Kürze liegt die Würz"; - expect = @"In der Krze liegt die Wrz"; + STAssertEqualObjects([actualASCIIString stringByRemovingCharactersInSet:junk], + expectedASCIIString, + @"The following characters should have been removed %@", + charsToRemove); - STAssertEqualObjects([s stringByRemovingCharactersInSet:junk], expect, @"stringByRemovingCharactersInSet"); + STAssertEqualObjects([actualUTFString stringByRemovingCharactersInSet:junk], + expectedUTFString, + @"The following characters should have been removed %@", + charsToRemove); } @end |