FSTDocumentTests.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/Model/FSTDocument.h"
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/Core/FSTSnapshotVersion.h"
  19. #import "Firestore/Source/Model/FSTDocumentKey.h"
  20. #import "Firestore/Source/Model/FSTFieldValue.h"
  21. #import "Firestore/Source/Model/FSTPath.h"
  22. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. @interface FSTDocumentTests : XCTestCase
  25. @end
  26. @implementation FSTDocumentTests
  27. - (void)testConstructor {
  28. FSTDocumentKey *key = [FSTDocumentKey keyWithPathString:@"messages/first"];
  29. FSTSnapshotVersion *version = FSTTestVersion(1);
  30. FSTObjectValue *data = FSTTestObjectValue(@{ @"a" : @1 });
  31. FSTDocument *doc =
  32. [FSTDocument documentWithData:data key:key version:version hasLocalMutations:NO];
  33. XCTAssertEqualObjects(doc.key, [FSTDocumentKey keyWithPathString:@"messages/first"]);
  34. XCTAssertEqualObjects(doc.version, version);
  35. XCTAssertEqualObjects(doc.data, data);
  36. XCTAssertEqual(doc.hasLocalMutations, NO);
  37. }
  38. - (void)testExtractsFields {
  39. FSTDocumentKey *key = [FSTDocumentKey keyWithPathString:@"rooms/eros"];
  40. FSTSnapshotVersion *version = FSTTestVersion(1);
  41. FSTObjectValue *data = FSTTestObjectValue(@{
  42. @"desc" : @"Discuss all the project related stuff",
  43. @"owner" : @{@"name" : @"Jonny", @"title" : @"scallywag"}
  44. });
  45. FSTDocument *doc =
  46. [FSTDocument documentWithData:data key:key version:version hasLocalMutations:NO];
  47. XCTAssertEqualObjects([doc fieldForPath:FSTTestFieldPath(@"desc")],
  48. [FSTStringValue stringValue:@"Discuss all the project related stuff"]);
  49. XCTAssertEqualObjects([doc fieldForPath:FSTTestFieldPath(@"owner.title")],
  50. [FSTStringValue stringValue:@"scallywag"]);
  51. }
  52. - (void)testIsEqual {
  53. FSTDocumentKey *key1 = [FSTDocumentKey keyWithPathString:@"messages/first"];
  54. FSTDocumentKey *key2 = [FSTDocumentKey keyWithPathString:@"messages/second"];
  55. FSTObjectValue *data1 = FSTTestObjectValue(@{ @"a" : @1 });
  56. FSTObjectValue *data2 = FSTTestObjectValue(@{ @"b" : @1 });
  57. FSTSnapshotVersion *version1 = FSTTestVersion(1);
  58. FSTDocument *doc1 =
  59. [FSTDocument documentWithData:data1 key:key1 version:version1 hasLocalMutations:NO];
  60. FSTDocument *doc2 =
  61. [FSTDocument documentWithData:data1 key:key1 version:version1 hasLocalMutations:NO];
  62. XCTAssertEqualObjects(doc1, doc2);
  63. XCTAssertEqualObjects(
  64. doc1, [FSTDocument documentWithData:FSTTestObjectValue(
  65. @{ @"a" : @1 })
  66. key:[FSTDocumentKey keyWithPathString:@"messages/first"]
  67. version:version1
  68. hasLocalMutations:NO]);
  69. FSTSnapshotVersion *version2 = FSTTestVersion(2);
  70. XCTAssertNotEqualObjects(
  71. doc1, [FSTDocument documentWithData:data2 key:key1 version:version1 hasLocalMutations:NO]);
  72. XCTAssertNotEqualObjects(
  73. doc1, [FSTDocument documentWithData:data1 key:key2 version:version1 hasLocalMutations:NO]);
  74. XCTAssertNotEqualObjects(
  75. doc1, [FSTDocument documentWithData:data1 key:key1 version:version2 hasLocalMutations:NO]);
  76. XCTAssertNotEqualObjects(
  77. doc1, [FSTDocument documentWithData:data1 key:key1 version:version1 hasLocalMutations:YES]);
  78. XCTAssertEqualObjects(
  79. [FSTDocument documentWithData:data1 key:key1 version:version1 hasLocalMutations:YES],
  80. [FSTDocument documentWithData:data1 key:key1 version:version1 hasLocalMutations:5]);
  81. }
  82. @end
  83. NS_ASSUME_NONNULL_END