FMDatabaseAdditionsTests.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // FMDatabaseAdditionsTests.m
  3. // fmdb
  4. //
  5. // Created by Graham Dennis on 24/11/2013.
  6. //
  7. //
  8. #import <XCTest/XCTest.h>
  9. #import "FMDatabaseAdditions.h"
  10. #if FMDB_SQLITE_STANDALONE
  11. #import <sqlite3/sqlite3.h>
  12. #else
  13. #import <sqlite3.h>
  14. #endif
  15. @interface FMDatabaseAdditionsTests : FMDBTempDBTests
  16. @end
  17. @implementation FMDatabaseAdditionsTests
  18. - (void)setUp
  19. {
  20. [super setUp];
  21. // Put setup code here. This method is called before the invocation of each test method in the class.
  22. }
  23. - (void)tearDown
  24. {
  25. // Put teardown code here. This method is called after the invocation of each test method in the class.
  26. [super tearDown];
  27. }
  28. - (void)testFunkyTableNames
  29. {
  30. [self.db executeUpdate:@"create table '234 fds' (foo text)"];
  31. XCTAssertFalse([self.db hadError], @"table creation should have succeeded");
  32. FMResultSet *rs = [self.db getTableSchema:@"234 fds"];
  33. XCTAssertTrue([rs next], @"Schema should have succeded");
  34. [rs close];
  35. XCTAssertFalse([self.db hadError], @"There shouldn't be any errors");
  36. }
  37. - (void)testBoolForQuery
  38. {
  39. BOOL result = [self.db boolForQuery:@"SELECT ? not null", @""];
  40. XCTAssertTrue(result, @"Empty strings should be considered true");
  41. result = [self.db boolForQuery:@"SELECT ? not null", [NSMutableData data]];
  42. XCTAssertTrue(result, @"Empty mutable data should be considered true");
  43. result = [self.db boolForQuery:@"SELECT ? not null", [NSData data]];
  44. XCTAssertTrue(result, @"Empty data should be considered true");
  45. }
  46. - (void)testIntForQuery
  47. {
  48. [self.db executeUpdate:@"create table t1 (a integer)"];
  49. [self.db executeUpdate:@"insert into t1 values (?)", [NSNumber numberWithInt:5]];
  50. XCTAssertEqual([self.db changes], 1, @"There should only be one change");
  51. int ia = [self.db intForQuery:@"select a from t1 where a = ?", [NSNumber numberWithInt:5]];
  52. XCTAssertEqual(ia, 5, @"foo");
  53. }
  54. - (void)testDateForQuery
  55. {
  56. NSDate *date = [NSDate date];
  57. [self.db executeUpdate:@"create table datetest (a double, b double, c double)"];
  58. [self.db executeUpdate:@"insert into datetest (a, b, c) values (?, ?, 0)" , [NSNull null], date];
  59. NSDate *foo = [self.db dateForQuery:@"select b from datetest where c = 0"];
  60. XCTAssertEqualWithAccuracy([foo timeIntervalSinceDate:date], 0.0, 1.0, @"Dates should be the same to within a second");
  61. }
  62. - (void)testTableExists
  63. {
  64. XCTAssertTrue([self.db executeUpdate:@"create table t4 (a text, b text)"]);
  65. XCTAssertTrue([self.db tableExists:@"t4"]);
  66. XCTAssertFalse([self.db tableExists:@"thisdoesntexist"]);
  67. FMResultSet *rs = [self.db getSchema];
  68. while ([rs next]) {
  69. XCTAssertEqualObjects([rs stringForColumn:@"type"], @"table");
  70. }
  71. }
  72. - (void)testColumnExists
  73. {
  74. [self.db executeUpdate:@"create table nulltest (a text, b text)"];
  75. XCTAssertTrue([self.db columnExists:@"a" inTableWithName:@"nulltest"]);
  76. XCTAssertTrue([self.db columnExists:@"b" inTableWithName:@"nulltest"]);
  77. XCTAssertFalse([self.db columnExists:@"c" inTableWithName:@"nulltest"]);
  78. }
  79. - (void)testUserVersion {
  80. [[self db] setUserVersion:12];
  81. XCTAssertTrue([[self db] userVersion] == 12);
  82. }
  83. - (void)testApplicationID {
  84. #if SQLITE_VERSION_NUMBER >= 3007017
  85. uint32_t appID = NSHFSTypeCodeFromFileType(NSFileTypeForHFSTypeCode('fmdb'));
  86. [self.db setApplicationID:appID];
  87. uint32_t rAppID = [self.db applicationID];
  88. XCTAssertEqual(rAppID, appID);
  89. [self.db setApplicationIDString:@"acrn"];
  90. NSString *s = [self.db applicationIDString];
  91. XCTAssertEqualObjects(s, @"acrn");
  92. #else
  93. NSString *errorMessage = NSLocalizedString(@"Application ID functions require SQLite 3.7.17", nil);
  94. XCTFail("%@", errorMessage);
  95. if (self.db.logsErrors) NSLog(@"%@", errorMessage);
  96. #endif
  97. }
  98. @end