FSTReferenceSetTests.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "Firestore/Source/Local/FSTReferenceSet.h"
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/Model/FSTDocumentKey.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @interface FSTReferenceSetTests : XCTestCase
  21. @end
  22. @implementation FSTReferenceSetTests
  23. - (void)testAddOrRemoveReferences {
  24. FSTDocumentKey *key = [FSTDocumentKey keyWithPathString:@"foo/bar"];
  25. FSTReferenceSet *referenceSet = [[FSTReferenceSet alloc] init];
  26. XCTAssertTrue([referenceSet isEmpty]);
  27. XCTAssertFalse([referenceSet containsKey:key]);
  28. [referenceSet addReferenceToKey:key forID:1];
  29. XCTAssertTrue([referenceSet containsKey:key]);
  30. XCTAssertFalse([referenceSet isEmpty]);
  31. [referenceSet addReferenceToKey:key forID:2];
  32. XCTAssertTrue([referenceSet containsKey:key]);
  33. [referenceSet removeReferenceToKey:key forID:1];
  34. XCTAssertTrue([referenceSet containsKey:key]);
  35. [referenceSet removeReferenceToKey:key forID:3];
  36. XCTAssertTrue([referenceSet containsKey:key]);
  37. [referenceSet removeReferenceToKey:key forID:2];
  38. XCTAssertFalse([referenceSet containsKey:key]);
  39. XCTAssertTrue([referenceSet isEmpty]);
  40. }
  41. - (void)testRemoveAllReferencesForTargetID {
  42. FSTDocumentKey *key1 = [FSTDocumentKey keyWithPathString:@"foo/bar"];
  43. FSTDocumentKey *key2 = [FSTDocumentKey keyWithPathString:@"foo/baz"];
  44. FSTDocumentKey *key3 = [FSTDocumentKey keyWithPathString:@"foo/blah"];
  45. FSTReferenceSet *referenceSet = [[FSTReferenceSet alloc] init];
  46. [referenceSet addReferenceToKey:key1 forID:1];
  47. [referenceSet addReferenceToKey:key2 forID:1];
  48. [referenceSet addReferenceToKey:key3 forID:2];
  49. XCTAssertFalse([referenceSet isEmpty]);
  50. XCTAssertTrue([referenceSet containsKey:key1]);
  51. XCTAssertTrue([referenceSet containsKey:key2]);
  52. XCTAssertTrue([referenceSet containsKey:key3]);
  53. [referenceSet removeReferencesForID:1];
  54. XCTAssertFalse([referenceSet isEmpty]);
  55. XCTAssertFalse([referenceSet containsKey:key1]);
  56. XCTAssertFalse([referenceSet containsKey:key2]);
  57. XCTAssertTrue([referenceSet containsKey:key3]);
  58. [referenceSet removeReferencesForID:2];
  59. XCTAssertTrue([referenceSet isEmpty]);
  60. XCTAssertFalse([referenceSet containsKey:key1]);
  61. XCTAssertFalse([referenceSet containsKey:key2]);
  62. XCTAssertFalse([referenceSet containsKey:key3]);
  63. }
  64. @end
  65. NS_ASSUME_NONNULL_END