TestBaseRealm.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import XCTest
  6. @testable import NextcloudTalk
  7. class TestBaseRealm: XCTestCase {
  8. static var fakeAccountId = "fakeAccountId"
  9. var realm: RLMRealm!
  10. override func setUpWithError() throws {
  11. // Setup in memory database
  12. let config = RLMRealmConfiguration()
  13. // Use a UUID to create a new/empty database for each test
  14. config.inMemoryIdentifier = UUID().uuidString
  15. RLMRealmConfiguration.setDefault(config)
  16. realm = RLMRealm.default()
  17. createFakeActiveAccount()
  18. }
  19. override func tearDownWithError() throws {
  20. // Make sure we correctly remove the fake account again, to clear the capability cache in NCDatabaseManager
  21. NCDatabaseManager.sharedInstance().removeAccount(withAccountId: TestBaseRealm.fakeAccountId)
  22. }
  23. func createFakeActiveAccount() {
  24. let account = TalkAccount()
  25. account.accountId = TestBaseRealm.fakeAccountId
  26. account.active = true
  27. account.user = TestConstants.username
  28. account.server = TestConstants.server
  29. try? realm.transaction {
  30. realm.add(account)
  31. }
  32. }
  33. func updateCapabilities(updateBlock: @escaping (ServerCapabilities) -> Void) {
  34. try? realm.transaction {
  35. var capabilities = ServerCapabilities()
  36. capabilities.accountId = TestBaseRealm.fakeAccountId
  37. if let storedCapabilities = ServerCapabilities.object(forPrimaryKey: TestBaseRealm.fakeAccountId) {
  38. capabilities = storedCapabilities
  39. }
  40. updateBlock(capabilities)
  41. realm.addOrUpdate(capabilities)
  42. }
  43. }
  44. @discardableResult
  45. func addRoom(withToken roomToken: String, withName roomName: String = "", withAccountId accountId: String = fakeAccountId, updateBlock: ((NCRoom) -> Void)? = nil) -> NCRoom {
  46. let room = NCRoom()
  47. room.token = roomToken
  48. room.name = roomName
  49. room.accountId = accountId
  50. room.internalId = "\(roomToken)@\(accountId)"
  51. updateBlock?(room)
  52. try? realm.transaction {
  53. realm.add(room)
  54. }
  55. return room
  56. }
  57. }