FSTDocumentTests.mm 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 = FSTTestDocKey(@"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, FSTTestDocKey(@"messages/first"));
  34. XCTAssertEqualObjects(doc.version, version);
  35. XCTAssertEqualObjects(doc.data, data);
  36. XCTAssertEqual(doc.hasLocalMutations, NO);
  37. }
  38. - (void)testExtractsFields {
  39. FSTDocumentKey *key = FSTTestDocKey(@"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. XCTAssertEqualObjects(FSTTestDoc(@"messages/first", 1,
  54. @{ @"a" : @1 }, NO),
  55. FSTTestDoc(@"messages/first", 1,
  56. @{ @"a" : @1 }, NO));
  57. XCTAssertNotEqualObjects(FSTTestDoc(@"messages/first", 1,
  58. @{ @"a" : @1 }, NO),
  59. FSTTestDoc(@"messages/first", 1,
  60. @{ @"b" : @1 }, NO));
  61. XCTAssertNotEqualObjects(FSTTestDoc(@"messages/first", 1,
  62. @{ @"a" : @1 }, NO),
  63. FSTTestDoc(@"messages/second", 1,
  64. @{ @"b" : @1 }, NO));
  65. XCTAssertNotEqualObjects(FSTTestDoc(@"messages/first", 1,
  66. @{ @"a" : @1 }, NO),
  67. FSTTestDoc(@"messages/first", 2,
  68. @{ @"a" : @1 }, NO));
  69. XCTAssertNotEqualObjects(FSTTestDoc(@"messages/first", 1,
  70. @{ @"a" : @1 }, NO),
  71. FSTTestDoc(@"messages/first", 1,
  72. @{ @"a" : @1 }, YES));
  73. XCTAssertEqualObjects(FSTTestDoc(@"messages/first", 1,
  74. @{ @"a" : @1 }, YES),
  75. FSTTestDoc(@"messages/first", 1,
  76. @{ @"a" : @1 }, 5));
  77. }
  78. @end
  79. NS_ASSUME_NONNULL_END