FMDatabaseAdditionsTests.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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)testValidate {
  63. NSError *error;
  64. XCTAssert([self.db validateSQL:@"create table datetest (a double, b double, c double)" error:&error]);
  65. XCTAssertNil(error, @"There should be no error object");
  66. }
  67. - (void)testFailValidate {
  68. NSError *error;
  69. XCTAssertFalse([self.db validateSQL:@"blah blah blah" error:&error]);
  70. XCTAssert(error, @"There should be no error object");
  71. }
  72. - (void)testTableExists {
  73. XCTAssertTrue([self.db executeUpdate:@"create table t4 (a text, b text)"]);
  74. XCTAssertTrue([self.db tableExists:@"t4"]);
  75. XCTAssertFalse([self.db tableExists:@"thisdoesntexist"]);
  76. FMResultSet *rs = [self.db getSchema];
  77. while ([rs next]) {
  78. XCTAssertEqualObjects([rs stringForColumn:@"type"], @"table");
  79. }
  80. }
  81. - (void)testColumnExists {
  82. [self.db executeUpdate:@"create table nulltest (a text, b text)"];
  83. XCTAssertTrue([self.db columnExists:@"a" inTableWithName:@"nulltest"]);
  84. XCTAssertTrue([self.db columnExists:@"b" inTableWithName:@"nulltest"]);
  85. XCTAssertFalse([self.db columnExists:@"c" inTableWithName:@"nulltest"]);
  86. }
  87. - (void)testUserVersion {
  88. [[self db] setUserVersion:12];
  89. XCTAssertTrue([[self db] userVersion] == 12);
  90. }
  91. - (void)testApplicationID {
  92. #if SQLITE_VERSION_NUMBER >= 3007017
  93. uint32_t appID = NSHFSTypeCodeFromFileType(NSFileTypeForHFSTypeCode('fmdb'));
  94. [self.db setApplicationID:appID];
  95. uint32_t rAppID = [self.db applicationID];
  96. XCTAssertEqual(rAppID, appID);
  97. [self.db setApplicationIDString:@"acrn"];
  98. NSString *s = [self.db applicationIDString];
  99. XCTAssertEqualObjects(s, @"acrn");
  100. #else
  101. NSString *errorMessage = NSLocalizedStringFromTable(@"Application ID functions require SQLite 3.7.17", @"FMDB", nil);
  102. XCTFail("%@", errorMessage);
  103. if (self.db.logsErrors) NSLog(@"%@", errorMessage);
  104. #endif
  105. }
  106. @end