FIRArrayTransformTests.mm 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/API/FIRFieldValue+Internal.h"
  19. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  20. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  21. /**
  22. * Note: Transforms are tested pretty thoroughly in FIRServerTimestampTests (via set, update,
  23. * transactions, nested in documents, multiple transforms together, etc.) and so these tests
  24. * mostly focus on the array transform semantics.
  25. */
  26. @interface FIRArrayTransformTests : FSTIntegrationTestCase
  27. @end
  28. @implementation FIRArrayTransformTests {
  29. // A document reference to read and write to.
  30. FIRDocumentReference *_docRef;
  31. // Accumulator used to capture events during the test.
  32. FSTEventAccumulator<FIRDocumentSnapshot *> *_accumulator;
  33. // Listener registration for a listener maintained during the course of the test.
  34. id<FIRListenerRegistration> _listenerRegistration;
  35. }
  36. - (void)setUp {
  37. [super setUp];
  38. _docRef = [self documentRef];
  39. _accumulator = [FSTEventAccumulator accumulatorForTest:self];
  40. _listenerRegistration =
  41. [_docRef addSnapshotListenerWithIncludeMetadataChanges:YES
  42. listener:_accumulator.valueEventHandler];
  43. // Wait for initial nil snapshot to avoid potential races.
  44. FIRDocumentSnapshot *initialSnapshot = [_accumulator awaitEventWithName:@"initial event"];
  45. XCTAssertFalse(initialSnapshot.exists);
  46. }
  47. - (void)tearDown {
  48. [_listenerRegistration remove];
  49. [super tearDown];
  50. }
  51. #pragma mark - Test Helpers
  52. /** Writes some initial data and consumes the events generated. */
  53. - (void)writeInitialData:(NSDictionary<NSString *, id> *)data {
  54. [self writeDocumentRef:_docRef data:data];
  55. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, data);
  56. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, data);
  57. }
  58. #pragma mark - Test Cases
  59. - (void)testCreateDocumentWithArrayUnion {
  60. [self writeDocumentRef:_docRef
  61. data:@{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]}];
  62. id expected = @{@"array" : @[ @1, @2 ]};
  63. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  64. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  65. }
  66. - (void)testAppendToArrayViaUpdate {
  67. [self writeInitialData:@{@"array" : @[ @1, @3 ]}];
  68. [self updateDocumentRef:_docRef
  69. data:@{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @1, @4 ]]}];
  70. id expected = @{@"array" : @[ @1, @3, @2, @4 ]};
  71. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  72. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  73. }
  74. @end