FMDatabaseAdditionsTests.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. @end