IntegrationTests.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2024 Google LLC
  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
  15. import FirebaseCore
  16. @testable import FirebaseDataConnect
  17. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  18. final class IntegrationTests: XCTestCase {
  19. class func setupFirebaseApp() {
  20. if FirebaseApp.app() == nil {
  21. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  22. gcmSenderID: "00000000000000000-00000000000-000000000")
  23. options.projectID = "fdc-test"
  24. FirebaseApp.configure(options: options)
  25. }
  26. }
  27. override class func setUp() {
  28. setupFirebaseApp()
  29. DataConnect.kitchenSinkClient.useEmulator(port: 3628)
  30. }
  31. override func setUp(completion: @escaping ((any Error)?) -> Void) {
  32. Task {
  33. do {
  34. try await ProjectConfigurator.shared.configureProject()
  35. completion(nil)
  36. } catch {
  37. completion(error)
  38. }
  39. }
  40. }
  41. // test to confirm that we can assign an explicit UUID
  42. func testSpecifiedUUID() async throws {
  43. let specifiedUUID = UUID()
  44. let result = try await DataConnect.kitchenSinkClient.createTestIdMutationRef(id: specifiedUUID)
  45. .execute()
  46. XCTAssertEqual(result.data.testId_insert.id, specifiedUUID)
  47. }
  48. // test for an auto generated UUID assignment
  49. func testAutoId() async throws {
  50. let result = try await DataConnect.kitchenSinkClient.createTestAutoIdMutationRef().execute()
  51. _ = result.data.testAutoId_insert.id
  52. // if we get till here - we have successfully got a UUID and decoded it. So test is successful
  53. XCTAssert(true)
  54. }
  55. func testStandardScalar() async throws {
  56. let standardScalarUUID = UUID()
  57. let testText = "Hello Firebase World"
  58. let testDecimal = Double.random(in: 10.0 ... 999.0)
  59. let testInt = Int(Int32.random(in: Int32.min ... Int32.max))
  60. // The following fails since server returns a different value.
  61. // The value is outside the 32-bit range and GQL Int is 32-bits.
  62. // Tracked internally with issue - b/358198261
  63. // let testInt = -6196243450739521536
  64. let executeResult = try await DataConnect.kitchenSinkClient.createStandardScalarMutationRef(
  65. id: standardScalarUUID,
  66. number: testInt,
  67. text: testText,
  68. decimal: testDecimal
  69. ).execute()
  70. XCTAssertEqual(
  71. executeResult.data.standardScalars_insert.id,
  72. standardScalarUUID,
  73. "UUID mismatch between specified and returned"
  74. )
  75. let queryResult = try await DataConnect.kitchenSinkClient
  76. .getStandardScalarQueryRef(id: standardScalarUUID).execute()
  77. let returnedDecimal = queryResult.data.standardScalars?.decimal
  78. XCTAssertEqual(
  79. returnedDecimal,
  80. testDecimal,
  81. "Decimal value mismatch between sent \(testDecimal) and received \(String(describing: returnedDecimal))"
  82. )
  83. let returnedNumber = queryResult.data.standardScalars?.number
  84. XCTAssertEqual(
  85. returnedNumber,
  86. testInt,
  87. "Int value mismatch between sent \(testInt) and received \(String(describing: returnedNumber))"
  88. )
  89. let returnedText = queryResult.data.standardScalars?.text
  90. XCTAssertEqual(
  91. returnedText,
  92. testText,
  93. "String value mismatch between sent \(testText) and received \(String(describing: returnedText))"
  94. )
  95. }
  96. func testScalarBoundaries() async throws {
  97. let scalaryBoundaryUUID = UUID()
  98. let maxInt = Int(Int32.max)
  99. let minInt = Int(Int32.min)
  100. let maxFloat = Double.greatestFiniteMagnitude
  101. let minFloat = Double.leastNormalMagnitude
  102. _ = try await DataConnect.kitchenSinkClient.createScalarBoundaryMutationRef(
  103. id: scalaryBoundaryUUID,
  104. maxNumber: maxInt,
  105. minNumber: minInt,
  106. maxDecimal: maxFloat,
  107. minDecimal: minFloat
  108. ).execute()
  109. let queryResult = try await DataConnect.kitchenSinkClient
  110. .getScalarBoundaryQueryRef(id: scalaryBoundaryUUID).execute()
  111. let returnedMaxInt = queryResult.data.scalarBoundary?.maxNumber
  112. XCTAssertEqual(
  113. returnedMaxInt,
  114. maxInt,
  115. "Returned maxInt \(String(describing: returnedMaxInt)) is not same as sent \(maxInt)"
  116. )
  117. let returnedMinInt = queryResult.data.scalarBoundary?.minNumber
  118. XCTAssertEqual(
  119. returnedMinInt,
  120. minInt,
  121. "Returned minInt \(minInt) is not same as sent \(minInt)"
  122. )
  123. let returnedMaxFloat = queryResult.data.scalarBoundary?.maxDecimal
  124. XCTAssertEqual(
  125. returnedMaxFloat,
  126. maxFloat,
  127. "Returned maxFloat \(String(describing: returnedMaxFloat)) is not same as sent \(maxFloat)"
  128. )
  129. let returnedMinFloat = queryResult.data.scalarBoundary?.minDecimal
  130. XCTAssertEqual(
  131. returnedMinFloat,
  132. minFloat,
  133. "Returned minFloat \(String(describing: returnedMinFloat)) is not same as sent \(minFloat)"
  134. )
  135. }
  136. func testLargeNum() async throws {
  137. let largeNumUUID = UUID()
  138. let largeNum = Int64.random(in: Int64.min ... Int64.max)
  139. let largeNumMax = Int64.max
  140. let largeNumMin = Int64.min
  141. _ = try await DataConnect.kitchenSinkClient.createLargeNumMutationRef(
  142. id: largeNumUUID,
  143. num: largeNum,
  144. maxNum: largeNumMax,
  145. minNum: largeNumMin
  146. ).execute()
  147. let result = try await DataConnect.kitchenSinkClient.getLargeNumQueryRef(id: largeNumUUID)
  148. .execute()
  149. let returnedLargeNum = result.data.largeIntType?.num
  150. XCTAssertEqual(
  151. returnedLargeNum,
  152. largeNum,
  153. "Int64 returned \(String(describing: returnedLargeNum)) does not match sent \(largeNum)"
  154. )
  155. let returnedMax = result.data.largeIntType?.maxNum
  156. XCTAssertEqual(
  157. returnedMax,
  158. largeNumMax,
  159. "Int64 max returned \(String(describing: returnedMax)) does not match sent \(largeNumMax)"
  160. )
  161. let returnedMin = result.data.largeIntType?.minNum
  162. XCTAssertEqual(
  163. returnedMin,
  164. largeNumMin,
  165. "Int64 min returned \(String(describing: returnedMin)) does not match sent \(largeNumMin)"
  166. )
  167. }
  168. func testLocalDateSerialization() async throws {
  169. let localDateUUID = UUID()
  170. let ld = try LocalDate(localDateString: "2024-11-01")
  171. _ = try await DataConnect.kitchenSinkClient.createLocalDateMutationRef(
  172. id: localDateUUID,
  173. localDate: ld
  174. ).execute()
  175. let result = try await DataConnect.kitchenSinkClient.getLocalDateTypeQueryRef(id: localDateUUID)
  176. .execute()
  177. let returnedLd = result.data.localDateType?.localDate
  178. XCTAssertEqual(ld, returnedLd)
  179. }
  180. }