FSTDocumentSetTests.mm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2017 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 <XCTest/XCTest.h>
  17. #include <vector>
  18. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  19. #import "Firestore/Source/Model/FSTDocument.h"
  20. // TODO(wilhuff) move to first include once this test filename matches
  21. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  22. #include "Firestore/core/test/firebase/firestore/testutil/xcgmock.h"
  23. using firebase::firestore::model::DocumentSet;
  24. using testing::ElementsAre;
  25. NS_ASSUME_NONNULL_BEGIN
  26. @interface FSTDocumentSetTests : XCTestCase
  27. @end
  28. @implementation FSTDocumentSetTests {
  29. NSComparator _comp;
  30. FSTDocument *_doc1;
  31. FSTDocument *_doc2;
  32. FSTDocument *_doc3;
  33. }
  34. - (void)setUp {
  35. [super setUp];
  36. _comp = FSTTestDocComparator("sort");
  37. _doc1 = FSTTestDoc("docs/1", 0, @{@"sort" : @2}, FSTDocumentStateSynced);
  38. _doc2 = FSTTestDoc("docs/2", 0, @{@"sort" : @3}, FSTDocumentStateSynced);
  39. _doc3 = FSTTestDoc("docs/3", 0, @{@"sort" : @1}, FSTDocumentStateSynced);
  40. }
  41. - (void)testCount {
  42. XCTAssertEqual(FSTTestDocSet(_comp, @[]).size(), 0);
  43. XCTAssertEqual(FSTTestDocSet(_comp, @[ _doc1, _doc2, _doc3 ]).size(), 3);
  44. }
  45. - (void)testHasKey {
  46. DocumentSet set = FSTTestDocSet(_comp, @[ _doc1, _doc2 ]);
  47. XCTAssertTrue(set.ContainsKey(_doc1.key));
  48. XCTAssertTrue(set.ContainsKey(_doc2.key));
  49. XCTAssertFalse(set.ContainsKey(_doc3.key));
  50. }
  51. - (void)testDocumentForKey {
  52. DocumentSet set = FSTTestDocSet(_comp, @[ _doc1, _doc2 ]);
  53. XCTAssertEqualObjects(set.GetDocument(_doc1.key), _doc1);
  54. XCTAssertEqualObjects(set.GetDocument(_doc2.key), _doc2);
  55. XCTAssertNil(set.GetDocument(_doc3.key));
  56. }
  57. - (void)testFirstAndLastDocument {
  58. DocumentSet set = FSTTestDocSet(_comp, @[]);
  59. XCTAssertNil(set.GetFirstDocument());
  60. XCTAssertNil(set.GetLastDocument());
  61. set = FSTTestDocSet(_comp, @[ _doc1, _doc2, _doc3 ]);
  62. XCTAssertEqualObjects(set.GetFirstDocument(), _doc3);
  63. XCTAssertEqualObjects(set.GetLastDocument(), _doc2);
  64. }
  65. - (void)testKeepsDocumentsInTheRightOrder {
  66. DocumentSet set = FSTTestDocSet(_comp, @[ _doc1, _doc2, _doc3 ]);
  67. XC_ASSERT_THAT(set, ElementsAre(_doc3, _doc1, _doc2));
  68. }
  69. - (void)testDeletes {
  70. DocumentSet set = FSTTestDocSet(_comp, @[ _doc1, _doc2, _doc3 ]);
  71. DocumentSet setWithoutDoc1 = set.erase(_doc1.key);
  72. XC_ASSERT_THAT(setWithoutDoc1, ElementsAre(_doc3, _doc2));
  73. XCTAssertEqual(setWithoutDoc1.size(), 2);
  74. // Original remains unchanged
  75. XC_ASSERT_THAT(set, ElementsAre(_doc3, _doc1, _doc2));
  76. DocumentSet setWithoutDoc3 = setWithoutDoc1.erase(_doc3.key);
  77. XC_ASSERT_THAT(setWithoutDoc3, ElementsAre(_doc2));
  78. XCTAssertEqual(setWithoutDoc3.size(), 1);
  79. }
  80. - (void)testUpdates {
  81. DocumentSet set = FSTTestDocSet(_comp, @[ _doc1, _doc2, _doc3 ]);
  82. FSTDocument *doc2Prime = FSTTestDoc("docs/2", 0, @{@"sort" : @9}, FSTDocumentStateSynced);
  83. set = set.insert(doc2Prime);
  84. XCTAssertEqual(set.size(), 3);
  85. XCTAssertEqualObjects(set.GetDocument(doc2Prime.key), doc2Prime);
  86. XC_ASSERT_THAT(set, ElementsAre(_doc3, _doc1, doc2Prime));
  87. }
  88. - (void)testAddsDocsWithEqualComparisonValues {
  89. FSTDocument *doc4 = FSTTestDoc("docs/4", 0, @{@"sort" : @2}, FSTDocumentStateSynced);
  90. DocumentSet set = FSTTestDocSet(_comp, @[ _doc1, doc4 ]);
  91. XC_ASSERT_THAT(set, ElementsAre(_doc1, doc4));
  92. }
  93. - (void)testIsEqual {
  94. DocumentSet empty{FSTDocumentComparatorByKey};
  95. DocumentSet set1 = FSTTestDocSet(FSTDocumentComparatorByKey, @[ _doc1, _doc2, _doc3 ]);
  96. DocumentSet set2 = FSTTestDocSet(FSTDocumentComparatorByKey, @[ _doc1, _doc2, _doc3 ]);
  97. XCTAssertEqual(set1, set1);
  98. XCTAssertEqual(set1, set2);
  99. XCTAssertNotEqual(set1, empty);
  100. DocumentSet sortedSet1 = FSTTestDocSet(_comp, @[ _doc1, _doc2, _doc3 ]);
  101. DocumentSet sortedSet2 = FSTTestDocSet(_comp, @[ _doc1, _doc2, _doc3 ]);
  102. XCTAssertEqual(sortedSet1, sortedSet1);
  103. XCTAssertEqual(sortedSet1, sortedSet2);
  104. XCTAssertNotEqual(sortedSet1, empty);
  105. DocumentSet shortSet = FSTTestDocSet(FSTDocumentComparatorByKey, @[ _doc1, _doc2 ]);
  106. XCTAssertNotEqual(set1, shortSet);
  107. XCTAssertNotEqual(set1, sortedSet1);
  108. }
  109. @end
  110. NS_ASSUME_NONNULL_END