diff options
author | Stephan Heilner <stephan@lds.org> | 2015-01-21 23:48:43 +0300 |
---|---|---|
committer | Stephan Heilner <stephan@lds.org> | 2015-01-22 21:35:01 +0300 |
commit | 52371b07bb745211b854d9b4e241cae3ff459e93 (patch) | |
tree | 228874629ec8267f635480f7d5a7dd1aa4fefb3a /Tests | |
parent | a8df75e87c9ee9e1b33365e226d4cc843fdc0b6a (diff) |
Added tests for FTS change
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/FMDatabaseFTS3Tests.m | 2 | ||||
-rw-r--r-- | Tests/FMDatabaseFTS3WithModuleNameTests.m | 83 |
2 files changed, 84 insertions, 1 deletions
diff --git a/Tests/FMDatabaseFTS3Tests.m b/Tests/FMDatabaseFTS3Tests.m index 339aa80..7984f39 100644 --- a/Tests/FMDatabaseFTS3Tests.m +++ b/Tests/FMDatabaseFTS3Tests.m @@ -27,7 +27,7 @@ static id<FMTokenizerDelegate> g_testTok = nil; // Create a tokenizer instance that will not be de-allocated when the method finishes. g_testTok = [[FMSimpleTokenizer alloc] initWithLocale:NULL]; - [FMDatabase registerTokenizer:g_testTok withName:@"testTok"]; + [FMDatabase registerTokenizer:g_testTok withKey:@"testTok"]; } - (void)setUp diff --git a/Tests/FMDatabaseFTS3WithModuleNameTests.m b/Tests/FMDatabaseFTS3WithModuleNameTests.m new file mode 100644 index 0000000..f48cc1e --- /dev/null +++ b/Tests/FMDatabaseFTS3WithModuleNameTests.m @@ -0,0 +1,83 @@ +// +// FMDatabaseFTS3WithKeyTests.m +// fmdb +// +// Created by Stephan Heilner on 1/21/15. +// +// + +#import "FMDBTempDBTests.h" +#import "FMDatabase+FTS3.h" +#import "FMTokenizers.h" + +@interface FMDatabaseFTS3WithModuleNameTests : FMDBTempDBTests + +@end + +static id<FMTokenizerDelegate> g_testTok = nil; + +@implementation FMDatabaseFTS3WithModuleNameTests + ++ (void)populateDatabase:(FMDatabase *)db +{ + [db executeUpdate:@"CREATE VIRTUAL TABLE mail USING fts3(subject, body)"]; + + [db executeUpdate:@"INSERT INTO mail VALUES('hello world', 'This message is a hello world message.')"]; + [db executeUpdate:@"INSERT INTO mail VALUES('urgent: serious', 'This mail is seen as a more serious mail')"]; + + // Create a tokenizer instance that will not be de-allocated when the method finishes. + g_testTok = [[FMSimpleTokenizer alloc] initWithLocale:NULL]; + [FMDatabase registerTokenizer:g_testTok]; +} + +- (void)setUp +{ + [super setUp]; + // Put setup code here. This method is called before the invocation of each test method in the class. +} + +- (void)tearDown +{ + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testOffsets +{ + FMResultSet *results = [self.db executeQuery:@"SELECT offsets(mail) FROM mail WHERE mail MATCH 'world'"]; + + if ([results next]) { + FMTextOffsets *offsets = [results offsetsForColumnIndex:0]; + + [offsets enumerateWithBlock:^(NSInteger columnNumber, NSInteger termNumber, NSRange matchRange) { + if (columnNumber == 0) { + XCTAssertEqual(termNumber, 0L); + XCTAssertEqual(matchRange.location, 6UL); + XCTAssertEqual(matchRange.length, 5UL); + } else if (columnNumber == 1) { + XCTAssertEqual(termNumber, 0L); + XCTAssertEqual(matchRange.location, 24UL); + XCTAssertEqual(matchRange.length, 5UL); + } + }]; + } +} + +- (void)testTokenizer +{ + [self.db installTokenizerModuleWithName:@"TestModuleName"]; + + BOOL ok = [self.db executeUpdate:@"CREATE VIRTUAL TABLE simple_fts USING fts3(tokenize=TestModuleName)"]; + XCTAssertTrue(ok, @"Failed to create virtual table: %@", [self.db lastErrorMessage]); + + // The FMSimpleTokenizer handles non-ASCII characters well, since it's based on CFStringTokenizer. + NSString *text = @"I like the band Queensrÿche. They are really great."; + + ok = [self.db executeUpdate:@"INSERT INTO simple_fts VALUES(?)", text]; + XCTAssertTrue(ok, @"Failed to insert data: %@", [self.db lastErrorMessage]); + + FMResultSet *results = [self.db executeQuery:@"SELECT * FROM simple_fts WHERE simple_fts MATCH ?", @"Queensrÿche"]; + XCTAssertTrue([results next], @"Failed to find result"); +} + +@end |