FMDatabaseFTS3WithModuleNameTests.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // FMDatabaseFTS3WithKeyTests.m
  3. // fmdb
  4. //
  5. // Created by Stephan Heilner on 1/21/15.
  6. //
  7. //
  8. #import "FMDBTempDBTests.h"
  9. #import "FMDatabase+FTS3.h"
  10. #import "FMTokenizers.h"
  11. @interface FMDatabaseFTS3WithModuleNameTests : FMDBTempDBTests
  12. @end
  13. static id<FMTokenizerDelegate> g_testTok = nil;
  14. @implementation FMDatabaseFTS3WithModuleNameTests
  15. + (void)populateDatabase:(FMDatabase *)db
  16. {
  17. [db executeUpdate:@"CREATE VIRTUAL TABLE mail USING fts3(subject, body)"];
  18. [db executeUpdate:@"INSERT INTO mail VALUES('hello world', 'This message is a hello world message.')"];
  19. [db executeUpdate:@"INSERT INTO mail VALUES('urgent: serious', 'This mail is seen as a more serious mail')"];
  20. // Create a tokenizer instance that will not be de-allocated when the method finishes.
  21. g_testTok = [[FMSimpleTokenizer alloc] initWithLocale:NULL];
  22. [FMDatabase registerTokenizer:g_testTok];
  23. }
  24. - (void)setUp
  25. {
  26. [super setUp];
  27. // Put setup code here. This method is called before the invocation of each test method in the class.
  28. }
  29. - (void)tearDown
  30. {
  31. // Put teardown code here. This method is called after the invocation of each test method in the class.
  32. [super tearDown];
  33. }
  34. - (void)testOffsets
  35. {
  36. FMResultSet *results = [self.db executeQuery:@"SELECT offsets(mail) FROM mail WHERE mail MATCH 'world'"];
  37. if ([results next]) {
  38. FMTextOffsets *offsets = [results offsetsForColumnIndex:0];
  39. [offsets enumerateWithBlock:^(NSInteger columnNumber, NSInteger termNumber, NSRange matchRange) {
  40. if (columnNumber == 0) {
  41. XCTAssertEqual(termNumber, 0L);
  42. XCTAssertEqual(matchRange.location, 6UL);
  43. XCTAssertEqual(matchRange.length, 5UL);
  44. } else if (columnNumber == 1) {
  45. XCTAssertEqual(termNumber, 0L);
  46. XCTAssertEqual(matchRange.location, 24UL);
  47. XCTAssertEqual(matchRange.length, 5UL);
  48. }
  49. }];
  50. }
  51. }
  52. - (void)testTokenizer
  53. {
  54. [self.db installTokenizerModuleWithName:@"TestModuleName"];
  55. BOOL ok = [self.db executeUpdate:@"CREATE VIRTUAL TABLE simple_fts USING fts3(tokenize=TestModuleName)"];
  56. XCTAssertTrue(ok, @"Failed to create virtual table: %@", [self.db lastErrorMessage]);
  57. // The FMSimpleTokenizer handles non-ASCII characters well, since it's based on CFStringTokenizer.
  58. NSString *text = @"I like the band Queensrÿche. They are really great.";
  59. ok = [self.db executeUpdate:@"INSERT INTO simple_fts VALUES(?)", text];
  60. XCTAssertTrue(ok, @"Failed to insert data: %@", [self.db lastErrorMessage]);
  61. FMResultSet *results = [self.db executeQuery:@"SELECT * FROM simple_fts WHERE simple_fts MATCH ?", @"Queensrÿche"];
  62. XCTAssertTrue([results next], @"Failed to find result");
  63. }
  64. @end