From c1bab7abd666d809aa01c330a66c53e1b6abe6c4 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Mon, 9 Jul 2012 09:27:48 +0000 Subject: Improve QueryKit's handling of quotes by making it on by default. Also, add a bunch more tests. --- .../QueryKit/Tests/QKSelectQueryGroupByTests.m | 35 +++++++++---- .../QueryKit/Tests/QKSelectQueryOrderByTests.m | 58 ++++++++++++++++++---- Frameworks/QueryKit/Tests/QKSelectQueryTests.m | 56 +++++++++++++++------ Frameworks/QueryKit/Tests/QKTestConstants.h | 41 +++++++++++++++ Frameworks/QueryKit/Tests/QKTestConstants.m | 41 +++++++++++++++ Frameworks/QueryKit/Tests/QKUpdateQueryTests.m | 39 +++++++++------ 6 files changed, 219 insertions(+), 51 deletions(-) create mode 100644 Frameworks/QueryKit/Tests/QKTestConstants.h create mode 100644 Frameworks/QueryKit/Tests/QKTestConstants.m (limited to 'Frameworks/QueryKit/Tests') diff --git a/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.m b/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.m index c34b586a..e9edbea1 100644 --- a/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.m +++ b/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.m @@ -29,16 +29,12 @@ // OTHER DEALINGS IN THE SOFTWARE. #import "QKSelectQueryGroupByTests.h" - -static NSString *QKTestTableName = @"test_table"; - -static NSString *QKTestFieldOne = @"test_field1"; -static NSString *QKTestFieldTwo = @"test_field2"; +#import "QKTestConstants.h" @implementation QKSelectQueryGroupByTests #pragma mark - -#pragma mark Setup & tear down +#pragma mark Setup - (void)setUp { @@ -53,26 +49,45 @@ static NSString *QKTestFieldTwo = @"test_field2"; - (void)testSelectQueryTypeIsCorrect { - STAssertTrue([[_query query] hasPrefix:@"SELECT"], @"query type"); + STAssertTrue([[_query query] hasPrefix:@"SELECT"], @"select query type"); } - (void)testSelectQueryGroupByIsCorrect { [_query groupByField:QKTestFieldOne]; + NSString *query = [NSString stringWithFormat:@"GROUP BY `%@`", QKTestFieldOne]; + + STAssertTrue([[_query query] hasSuffix:query], @"select query group by"); +} + +- (void)testSelectQueryGroupByWithoutQuotesIsCorrect +{ + [_query setUseQuotes:NO]; + [_query groupByField:QKTestFieldOne]; + NSString *query = [NSString stringWithFormat:@"GROUP BY %@", QKTestFieldOne]; - STAssertTrue([[_query query] hasSuffix:query], @"query group by"); + STAssertTrue([[_query query] hasSuffix:query], @"select query group by without quotes"); } - (void)testSelectQueryGroupByMultipleFieldsIsCorrect { [_query groupByFields:[NSArray arrayWithObjects:QKTestFieldOne, QKTestFieldTwo, nil]]; - NSString *query = [NSString stringWithFormat:@"GROUP BY %@, %@", QKTestFieldOne, QKTestFieldTwo]; + NSString *query = [NSString stringWithFormat:@"GROUP BY `%@`, `%@`", QKTestFieldOne, QKTestFieldTwo]; - STAssertTrue([[_query query] hasSuffix:query], @"query group by"); + STAssertTrue([[_query query] hasSuffix:query], @"select query group by multiple fields"); } +- (void)testSelectQueryGroupByMultipleFieldsWithoutQuotesIsCorrect +{ + [_query setUseQuotes:NO]; + [_query groupByFields:[NSArray arrayWithObjects:QKTestFieldOne, QKTestFieldTwo, nil]]; + + NSString *query = [NSString stringWithFormat:@"GROUP BY %@, %@", QKTestFieldOne, QKTestFieldTwo]; + + STAssertTrue([[_query query] hasSuffix:query], @"select query group by multiple fields without quotes"); +} @end diff --git a/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.m b/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.m index ac70f0fe..cb1a472f 100644 --- a/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.m +++ b/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.m @@ -29,16 +29,12 @@ // OTHER DEALINGS IN THE SOFTWARE. #import "QKSelectQueryOrderByTests.h" - -static NSString *QKTestTableName = @"test_table"; - -static NSString *QKTestFieldOne = @"test_field1"; -static NSString *QKTestFieldTwo = @"test_field2"; +#import "QKTestConstants.h" @implementation QKSelectQueryOrderByTests #pragma mark - -#pragma mark Setup & tear down +#pragma mark Setup - (void)setUp { @@ -53,43 +49,83 @@ static NSString *QKTestFieldTwo = @"test_field2"; - (void)testSelectQueryTypeIsCorrect { - STAssertTrue([[_query query] hasPrefix:@"SELECT"], @"query type"); + STAssertTrue([[_query query] hasPrefix:@"SELECT"], @"select query type"); } - (void)testSelectQueryOrderByAscendingIsCorrect { [_query orderByField:QKTestFieldOne descending:NO]; + NSString *query = [NSString stringWithFormat:@"ORDER BY `%@` ASC", QKTestFieldOne]; + + STAssertTrue([[_query query] hasSuffix:query], @"select query order by"); +} + +- (void)testSelectQueryOrderByAscendingWithoutQuotesIsCorrect +{ + [_query setUseQuotes:NO]; + [_query orderByField:QKTestFieldOne descending:NO]; + NSString *query = [NSString stringWithFormat:@"ORDER BY %@ ASC", QKTestFieldOne]; - STAssertTrue([[_query query] hasSuffix:query], @"query order by"); + STAssertTrue([[_query query] hasSuffix:query], @"select query order by without quotes"); } - (void)testSelectQueryOrderByMultipleFieldsAscendingIsCorrect { [_query orderByFields:[NSArray arrayWithObjects:QKTestFieldOne, QKTestFieldTwo, nil] descending:NO]; + NSString *query = [NSString stringWithFormat:@"ORDER BY `%@`, `%@` ASC", QKTestFieldOne, QKTestFieldTwo]; + + STAssertTrue([[_query query] hasSuffix:query], @"select query order by multiple fields ascending when quoted"); +} + +- (void)testSelectQueryOrderByMultipleFieldsAscendingWithoutQuotesIsCorrect +{ + [_query setUseQuotes:NO]; + [_query orderByFields:[NSArray arrayWithObjects:QKTestFieldOne, QKTestFieldTwo, nil] descending:NO]; + NSString *query = [NSString stringWithFormat:@"ORDER BY %@, %@ ASC", QKTestFieldOne, QKTestFieldTwo]; - STAssertTrue([[_query query] hasSuffix:query], @"query order by"); + STAssertTrue([[_query query] hasSuffix:query], @"select query order by multiple fields ascending without quotes"); } - (void)testSelectQueryOrderByDescendingIsCorrect { [_query orderByField:QKTestFieldOne descending:YES]; + NSString *query = [NSString stringWithFormat:@"ORDER BY `%@` DESC", QKTestFieldOne]; + + STAssertTrue([[_query query] hasSuffix:query], @"select query order by descending"); +} + +- (void)testSelectQueryOrderByDescendingWithoutQuotesIsCorrect +{ + [_query setUseQuotes:NO]; + [_query orderByField:QKTestFieldOne descending:YES]; + NSString *query = [NSString stringWithFormat:@"ORDER BY %@ DESC", QKTestFieldOne]; - STAssertTrue([[_query query] hasSuffix:query], @"query order by"); + STAssertTrue([[_query query] hasSuffix:query], @"select query order by descending without quotes"); } - (void)testSelectQueryOrderByMultipleFieldsDescendingIsCorrect { [_query orderByFields:[NSArray arrayWithObjects:QKTestFieldOne, QKTestFieldTwo, nil] descending:YES]; + NSString *query = [NSString stringWithFormat:@"ORDER BY `%@`, `%@` DESC", QKTestFieldOne, QKTestFieldTwo]; + + STAssertTrue([[_query query] hasSuffix:query], @"select query order by multiple fields descending"); +} + +- (void)testSelectQueryOrderByMultipleFieldsDescendingWithoutQuotesIsCorrect +{ + [_query setUseQuotes:NO]; + [_query orderByFields:[NSArray arrayWithObjects:QKTestFieldOne, QKTestFieldTwo, nil] descending:YES]; + NSString *query = [NSString stringWithFormat:@"ORDER BY %@, %@ DESC", QKTestFieldOne, QKTestFieldTwo]; - STAssertTrue([[_query query] hasSuffix:query], @"query order by"); + STAssertTrue([[_query query] hasSuffix:query], @"select query order by multiple fields descending without quotes"); } @end diff --git a/Frameworks/QueryKit/Tests/QKSelectQueryTests.m b/Frameworks/QueryKit/Tests/QKSelectQueryTests.m index cb2d3599..75cf7ed2 100644 --- a/Frameworks/QueryKit/Tests/QKSelectQueryTests.m +++ b/Frameworks/QueryKit/Tests/QKSelectQueryTests.m @@ -29,20 +29,12 @@ // OTHER DEALINGS IN THE SOFTWARE. #import "QKSelectQueryTests.h" - -static NSString *QKTestTableName = @"test_table"; - -static NSString *QKTestFieldOne = @"test_field1"; -static NSString *QKTestFieldTwo = @"test_field2"; -static NSString *QKTestFieldThree = @"test_field3"; -static NSString *QKTestFieldFour = @"test_field4"; - -static NSUInteger QKTestParameterOne = 10; +#import "QKTestConstants.h" @implementation QKSelectQueryTests #pragma mark - -#pragma mark Setup & tear down +#pragma mark Setup - (void)setUp { @@ -61,21 +53,55 @@ static NSUInteger QKTestParameterOne = 10; - (void)testSelectQueryTypeIsCorrect { - STAssertTrue([[_query query] hasPrefix:@"SELECT"], @"query type"); + STAssertTrue([[_query query] hasPrefix:@"SELECT"], @"select query type"); +} + +- (void)testSelectQueryFieldIsCorrect +{ + NSString *query = [NSString stringWithFormat:@"SELECT `%@`", QKTestFieldOne]; + + STAssertTrue([[_query query] hasPrefix:query], @"select query field"); +} + +- (void)testSelectQueryFieldWithoutQuotesIsCorrect +{ + [_query setUseQuotes:NO]; + + NSString *query = [NSString stringWithFormat:@"SELECT %@", QKTestFieldOne]; + + STAssertTrue([[_query query] hasPrefix:query], @"select query field without quotes"); +} + +- (void)testSelectQueryMultipleFieldsWhenQuotedAreCorrect +{ + NSString *query = [NSString stringWithFormat:@"SELECT `%@`, `%@`, `%@`, `%@`", QKTestFieldOne, QKTestFieldTwo, QKTestFieldThree, QKTestFieldFour]; + + STAssertTrue([[_query query] hasPrefix:query], @"select query multiple fields"); } -- (void)testSelectQueryFieldsAreCorrect +- (void)testSelectQueryMultipleFieldsWithoutQuotesAreCorrect { + [_query setUseQuotes:NO]; + NSString *query = [NSString stringWithFormat:@"SELECT %@, %@, %@, %@", QKTestFieldOne, QKTestFieldTwo, QKTestFieldThree, QKTestFieldFour]; - - STAssertTrue([[_query query] hasPrefix:query], @"query fields"); + + STAssertTrue([[_query query] hasPrefix:query], @"select query multiple fields without quotes"); } - (void)testSelectQueryConstraintsAreCorrect { + NSString *query = [NSString stringWithFormat:@"WHERE `%@` %@ %@", QKTestFieldOne, [QKQueryUtilities operatorRepresentationForType:QKEqualityOperator], [NSNumber numberWithUnsignedInteger:QKTestParameterOne]]; + + STAssertTrue(([[_query query] rangeOfString:query].location != NSNotFound), @"select query constraint"); +} + +- (void)testSelectQueryConstraintsWithoutQuotesAreCorrect +{ + [_query setUseQuotes:NO]; + NSString *query = [NSString stringWithFormat:@"WHERE %@ %@ %@", QKTestFieldOne, [QKQueryUtilities operatorRepresentationForType:QKEqualityOperator], [NSNumber numberWithUnsignedInteger:QKTestParameterOne]]; - STAssertTrue(([[_query query] rangeOfString:query].location != NSNotFound), @"query constraints"); + STAssertTrue(([[_query query] rangeOfString:query].location != NSNotFound), @"select query constraint without quotes"); } @end diff --git a/Frameworks/QueryKit/Tests/QKTestConstants.h b/Frameworks/QueryKit/Tests/QKTestConstants.h new file mode 100644 index 00000000..920ff060 --- /dev/null +++ b/Frameworks/QueryKit/Tests/QKTestConstants.h @@ -0,0 +1,41 @@ +// +// $Id$ +// +// QKTestConstants.h +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on July 8, 2012 +// Copyright (c) 2012 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. + +extern NSString *QKTestTableName; + +extern NSString *QKTestFieldOne; +extern NSString *QKTestFieldTwo; +extern NSString *QKTestFieldThree; +extern NSString *QKTestFieldFour; + +extern NSString *QKTestUpdateValueOne; +extern NSString *QKTestUpdateValueTwo; + +extern NSUInteger QKTestParameterOne; \ No newline at end of file diff --git a/Frameworks/QueryKit/Tests/QKTestConstants.m b/Frameworks/QueryKit/Tests/QKTestConstants.m new file mode 100644 index 00000000..419d2987 --- /dev/null +++ b/Frameworks/QueryKit/Tests/QKTestConstants.m @@ -0,0 +1,41 @@ +// +// $Id$ +// +// QKTestConstants.m +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on July 8, 2012 +// Copyright (c) 2012 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. + +NSString *QKTestTableName = @"test_table"; + +NSString *QKTestFieldOne = @"test_field1"; +NSString *QKTestFieldTwo = @"test_field2"; +NSString *QKTestFieldThree = @"test_field3"; +NSString *QKTestFieldFour = @"test_field4"; + +NSString *QKTestUpdateValueOne = @"update_one"; +NSString *QKTestUpdateValueTwo = @"update_two"; + +NSUInteger QKTestParameterOne = 10; \ No newline at end of file diff --git a/Frameworks/QueryKit/Tests/QKUpdateQueryTests.m b/Frameworks/QueryKit/Tests/QKUpdateQueryTests.m index fa3bb766..6a24881d 100644 --- a/Frameworks/QueryKit/Tests/QKUpdateQueryTests.m +++ b/Frameworks/QueryKit/Tests/QKUpdateQueryTests.m @@ -29,21 +29,12 @@ // OTHER DEALINGS IN THE SOFTWARE. #import "QKUpdateQueryTests.h" - -static NSString *QKTestTableName = @"test_table"; - -static NSString *QKTestFieldOne = @"test_field1"; -static NSString *QKTestFieldTwo = @"test_field2"; - -static NSString *QKTestUpdateValueOne = @"update_one"; -static NSString *QKTestUpdateValueTwo = @"update_two"; - -static NSUInteger QKTestParameterOne = 10; +#import "QKTestConstants.h" @implementation QKUpdateQueryTests #pragma mark - -#pragma mark Setup & tear down +#pragma mark Setup - (void)setUp { @@ -62,21 +53,39 @@ static NSUInteger QKTestParameterOne = 10; - (void)testUpdateQueryTypeIsCorrect { - STAssertTrue([[_query query] hasPrefix:@"UPDATE"], @"query type"); + STAssertTrue([[_query query] hasPrefix:@"UPDATE"], @"update query type"); } - (void)testUpdateQueryFieldsAreCorrect { + NSString *query = [NSString stringWithFormat:@"UPDATE `%@` SET `%@` = '%@', `%@` = '%@'", QKTestTableName, QKTestFieldOne, QKTestUpdateValueOne, QKTestFieldTwo, QKTestUpdateValueTwo]; + + STAssertTrue([[_query query] hasPrefix:query], @"update query fields"); +} + +- (void)testUpdateQueryFieldsWithoutQuotesAreCorrect +{ + [_query setUseQuotes:NO]; + NSString *query = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@', %@ = '%@'", QKTestTableName, QKTestFieldOne, QKTestUpdateValueOne, QKTestFieldTwo, QKTestUpdateValueTwo]; + + STAssertTrue([[_query query] hasPrefix:query], @"update query fields without quotes"); +} + +- (void)testUpdateQueryConstraintIsCorrect +{ + NSString *query = [NSString stringWithFormat:@"WHERE `%@` %@ %@", QKTestFieldOne, [QKQueryUtilities operatorRepresentationForType:QKEqualityOperator], [NSNumber numberWithUnsignedInteger:QKTestParameterOne]]; - STAssertTrue([[_query query] hasPrefix:query], @"query fields"); + STAssertTrue(([[_query query] rangeOfString:query].location != NSNotFound), @"update query constraint"); } -- (void)testUpdateQueryConstraintsAreCorrect +- (void)testUpdateQueryConstraintWithoutQuotesIsCorrect { + [_query setUseQuotes:NO]; + NSString *query = [NSString stringWithFormat:@"WHERE %@ %@ %@", QKTestFieldOne, [QKQueryUtilities operatorRepresentationForType:QKEqualityOperator], [NSNumber numberWithUnsignedInteger:QKTestParameterOne]]; - STAssertTrue(([[_query query] rangeOfString:query].location != NSNotFound), @"query constraints"); + STAssertTrue(([[_query query] rangeOfString:query].location != NSNotFound), @"update query constraint without quotes"); } @end -- cgit v1.2.3