diff options
Diffstat (limited to 'UnitTests')
-rw-r--r-- | UnitTests/MCPKitTest.h | 11 | ||||
-rw-r--r-- | UnitTests/MCPKitTest.m | 88 |
2 files changed, 74 insertions, 25 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 |