FSTDocumentSetTests.mm 4.7 KB

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