FMDatabaseAdditionsTests.m 3.5 KB

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