From 209b7db55ffd4b096b5d55402cd0ce989000c45b Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Tue, 21 May 2013 19:54:00 +0000 Subject: PostgresKit: Implement -description on PGPostgresResult and add an associated test. --- Frameworks/PostgresKit/Source/PGPostgresResult.m | 23 ++++ .../PostgresKit/Tests/PGPostgresResultTests.h | 40 +++++++ .../PostgresKit/Tests/PGPostgresResultTests.m | 120 +++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 Frameworks/PostgresKit/Tests/PGPostgresResultTests.h create mode 100644 Frameworks/PostgresKit/Tests/PGPostgresResultTests.m diff --git a/Frameworks/PostgresKit/Source/PGPostgresResult.m b/Frameworks/PostgresKit/Source/PGPostgresResult.m index 1bf1db06..cd62f17e 100644 --- a/Frameworks/PostgresKit/Source/PGPostgresResult.m +++ b/Frameworks/PostgresKit/Source/PGPostgresResult.m @@ -289,6 +289,29 @@ return handler; } +#pragma mark - +#pragma mark Other + +- (NSString *)description +{ + NSMutableString *description = [[[NSMutableString alloc] init] autorelease]; + + if (![self numberOfRows]) return @""; + + PGPostgresResultRowType rowType = _defaultRowType; + + [self setDefaultRowType:PGPostgresResultRowAsDictionary]; + + for (NSDictionary *row in self) + { + [description appendString:[row description]]; + } + + [self setDefaultRowType:rowType]; + + return description; +} + #pragma mark - -(void)dealloc diff --git a/Frameworks/PostgresKit/Tests/PGPostgresResultTests.h b/Frameworks/PostgresKit/Tests/PGPostgresResultTests.h new file mode 100644 index 00000000..e519ebeb --- /dev/null +++ b/Frameworks/PostgresKit/Tests/PGPostgresResultTests.h @@ -0,0 +1,40 @@ +// +// $Id$ +// +// PGPostgresResultTests.h +// PostgresKit +// +// Created by Stuart Connolly (stuconnolly.com) on May 20, 2013. +// Copyright (c) 2013 Stuart Connolly. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#import +#import + +@interface PGPostgresResultTests : SenTestCase +{ + PGPostgresResult *_result; + PGPostgresConnection *_connection; +} + +@end diff --git a/Frameworks/PostgresKit/Tests/PGPostgresResultTests.m b/Frameworks/PostgresKit/Tests/PGPostgresResultTests.m new file mode 100644 index 00000000..046b4ce6 --- /dev/null +++ b/Frameworks/PostgresKit/Tests/PGPostgresResultTests.m @@ -0,0 +1,120 @@ +// +// $Id$ +// +// PGPostgresResultTests.m +// PostgresKit +// +// Created by Stuart Connolly (stuconnolly.com) on May 20, 2013. +// Copyright (c) 2013 Stuart Connolly. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#import "PGPostgresResultTests.h" + +static NSString *PGTestDatabaseHost = @"localhost"; +static NSString *PGTestDatabaseUser = @"pgkit_test"; +static NSString *PGTestDatabaseName = @"pgkit_test"; +static NSString *PGTestDatabasePassword = @"pgkit"; + +static NSUInteger PGTestDatabasePort = 5432; + +@interface PGPostgresResultTests () + +- (void)_establishConnection; + +@end + +@implementation PGPostgresResultTests + +#pragma mark - +#pragma mark Setup & Teardown + +- (void)setUp +{ + _connection = [[PGPostgresConnection alloc] init]; + + [self _establishConnection]; + + _result = [_connection execute:@"SELECT * FROM \"data_types\""]; +} + +#pragma mark - +#pragma mark Tests + +- (void)testResultDescriptionIsCorrect +{ + // Compare the output after getting rid of newlines and spaces + STAssertTrue([[[[_result description] stringByReplacingOccurrencesOfString:@"\n" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@"{" + "\"bigint_field\" = 123456789;" + "\"bool_field\" = 1;" + "\"char_field\" = CHAR;" + "\"date_field\" = \"1987-04-08 00:00:00 +0100\";" + "\"float_field\" = \"12345.68\";" + "\"int_field\" = 12345;" + "\"numeric_field\" = \"12345.678\";" + "\"smallint_field\" = 2;" + "\"time_field\" = \"2000-01-01 02:02:02 +0000\";" + "\"timestamp_field\" = \"1987-04-08 03:02:02 +0100\";" + "\"timestamptz_field\" = \"8 Apr 1987 03:02:02 GMT+01:00\";" + "\"timetz_field\" = \"02:02:02 GMT+10:00\";" + "\"varchar_field\" = VARCHAR;" + "}"], nil); +} + +#pragma mark - +#pragma mark Private API + +- (void)_establishConnection +{ + [_connection setHost:PGTestDatabaseHost]; + [_connection setUser:PGTestDatabaseUser]; + [_connection setPort:PGTestDatabasePort]; + [_connection setDatabase:PGTestDatabaseName]; + [_connection setPassword:PGTestDatabasePassword]; + + if (![_connection connect]) { + STFail(@"Request to establish connection to local database failed."); + + exit(1); + } + + do { + sleep(0.1); + } + while (![_connection isConnected]); +} + +#pragma mark - + +- (void)dealloc +{ + if (_connection && [_connection isConnected]) { + [_connection disconnect]; + } + + if (_result) [_result release], _result = nil; + if (_connection) [_connection release], _connection = nil; + + [super dealloc]; +} + +@end -- cgit v1.2.3