SEGDatabaseManagerTests.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "FirebaseSegmentation/Sources/SEGDatabaseManager.h"
  16. #import <OCMock/OCMock.h>
  17. @interface SEGDatabaseManager (Test)
  18. - (NSString *)pathForSegmentationDatabase;
  19. @end
  20. @interface SEGDatabaseManagerTests : XCTestCase {
  21. long _expectationTimeout;
  22. }
  23. @property(nonatomic) id databaseManagerMock;
  24. @end
  25. @implementation SEGDatabaseManagerTests
  26. - (void)setUp {
  27. // Override the database path to create a test database.
  28. self.databaseManagerMock = OCMClassMock([SEGDatabaseManager class]);
  29. OCMStub([self.databaseManagerMock pathForSegmentationDatabase])
  30. .andReturn([self pathForSegmentationTestDatabase]);
  31. // Expectation timeout for each test.
  32. _expectationTimeout = 2;
  33. }
  34. - (void)tearDown {
  35. [self.databaseManagerMock stopMocking];
  36. self.databaseManagerMock = nil;
  37. }
  38. - (void)testDatabaseCreateOrOpen {
  39. XCTestExpectation *expectation = [self expectationWithDescription:@"testDatabaseLoad"];
  40. // Initialize the database manager.
  41. SEGDatabaseManager *databaseManager = [[SEGDatabaseManager alloc] init];
  42. XCTAssertNotNil(databaseManager);
  43. // Load all data from the database.
  44. [databaseManager createOrOpenDatabaseWithCompletion:^(BOOL success, NSDictionary *result) {
  45. XCTAssertTrue(success);
  46. XCTAssertTrue(result.count == 0);
  47. [expectation fulfill];
  48. }];
  49. [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
  50. }
  51. - (void)testDatabaseInsertAndRead {
  52. XCTestExpectation *expectation = [self expectationWithDescription:@"testDatabaseLoad"];
  53. // Initialize the database manager.
  54. SEGDatabaseManager *databaseManager = [[SEGDatabaseManager alloc] init];
  55. XCTAssertNotNil(databaseManager);
  56. // Load all data from the database.
  57. [databaseManager createOrOpenDatabaseWithCompletion:^(BOOL success, NSDictionary *result) {
  58. XCTAssertTrue(success);
  59. XCTAssertTrue(result.count == 0, "Result was %@", result);
  60. // Insert data.
  61. [databaseManager
  62. insertMainTableApplicationNamed:@"firebase_test_app"
  63. customInstanceIdentifier:@"custom-123"
  64. firebaseInstanceIdentifier:@"firebase-123"
  65. associationStatus:kSEGAssociationStatusPending
  66. completionHandler:^(BOOL success, NSDictionary *result) {
  67. XCTAssertTrue(success);
  68. XCTAssertNil(result);
  69. // Read data.
  70. [databaseManager loadMainTableWithCompletion:^(BOOL success,
  71. NSDictionary *result) {
  72. XCTAssertTrue(success);
  73. XCTAssertEqual(result.count, 1);
  74. NSDictionary *associations = [result objectForKey:@"firebase_test_app"];
  75. XCTAssertNotNil(associations);
  76. XCTAssertEqualObjects(
  77. [associations objectForKey:kSEGCustomInstallationIdentifierKey],
  78. @"custom-123");
  79. XCTAssertEqualObjects(
  80. [associations objectForKey:kSEGFirebaseInstallationIdentifierKey],
  81. @"firebase-123");
  82. XCTAssertEqualObjects(
  83. [associations objectForKey:kSEGAssociationStatusKey],
  84. kSEGAssociationStatusPending);
  85. [expectation fulfill];
  86. }];
  87. }];
  88. }];
  89. [self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
  90. }
  91. #pragma mark Helpers
  92. - (NSString *)pathForSegmentationTestDatabase {
  93. #if TARGET_OS_TV
  94. NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  95. #else
  96. NSArray *dirPaths =
  97. NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  98. #endif
  99. NSString *storageDir = dirPaths.firstObject;
  100. NSString *databaseName =
  101. [NSString stringWithFormat:@"FirebaseSegmentation-test-%d.sqlite3", (arc4random() % 100)];
  102. NSArray *components = @[ storageDir, @"Google/FirebaseSegmentation", databaseName ];
  103. NSString *dbPath = [NSString pathWithComponents:components];
  104. NSLog(@"Created test database at: %@", dbPath);
  105. return dbPath;
  106. }
  107. @end