FSTDocumentTests.mm 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/Example/Tests/Util/FSTHelpers.h"
  22. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  23. namespace testutil = firebase::firestore::testutil;
  24. NS_ASSUME_NONNULL_BEGIN
  25. @interface FSTDocumentTests : XCTestCase
  26. @end
  27. @implementation FSTDocumentTests
  28. - (void)testConstructor {
  29. FSTDocumentKey *key = FSTTestDocKey(@"messages/first");
  30. FSTSnapshotVersion *version = FSTTestVersion(1);
  31. FSTObjectValue *data = FSTTestObjectValue(@{ @"a" : @1 });
  32. FSTDocument *doc =
  33. [FSTDocument documentWithData:data key:key version:version hasLocalMutations:NO];
  34. XCTAssertEqualObjects(doc.key, FSTTestDocKey(@"messages/first"));
  35. XCTAssertEqualObjects(doc.version, version);
  36. XCTAssertEqualObjects(doc.data, data);
  37. XCTAssertEqual(doc.hasLocalMutations, NO);
  38. }
  39. - (void)testExtractsFields {
  40. FSTDocumentKey *key = FSTTestDocKey(@"rooms/eros");
  41. FSTSnapshotVersion *version = FSTTestVersion(1);
  42. FSTObjectValue *data = FSTTestObjectValue(@{
  43. @"desc" : @"Discuss all the project related stuff",
  44. @"owner" : @{@"name" : @"Jonny", @"title" : @"scallywag"}
  45. });
  46. FSTDocument *doc =
  47. [FSTDocument documentWithData:data key:key version:version hasLocalMutations:NO];
  48. XCTAssertEqualObjects([doc fieldForPath:testutil::Field("desc")],
  49. [FSTStringValue stringValue:@"Discuss all the project related stuff"]);
  50. XCTAssertEqualObjects([doc fieldForPath:testutil::Field("owner.title")],
  51. [FSTStringValue stringValue:@"scallywag"]);
  52. }
  53. - (void)testIsEqual {
  54. XCTAssertEqualObjects(FSTTestDoc(@"messages/first", 1,
  55. @{ @"a" : @1 }, NO),
  56. FSTTestDoc(@"messages/first", 1,
  57. @{ @"a" : @1 }, NO));
  58. XCTAssertNotEqualObjects(FSTTestDoc(@"messages/first", 1,
  59. @{ @"a" : @1 }, NO),
  60. FSTTestDoc(@"messages/first", 1,
  61. @{ @"b" : @1 }, NO));
  62. XCTAssertNotEqualObjects(FSTTestDoc(@"messages/first", 1,
  63. @{ @"a" : @1 }, NO),
  64. FSTTestDoc(@"messages/second", 1,
  65. @{ @"b" : @1 }, NO));
  66. XCTAssertNotEqualObjects(FSTTestDoc(@"messages/first", 1,
  67. @{ @"a" : @1 }, NO),
  68. FSTTestDoc(@"messages/first", 2,
  69. @{ @"a" : @1 }, NO));
  70. XCTAssertNotEqualObjects(FSTTestDoc(@"messages/first", 1,
  71. @{ @"a" : @1 }, NO),
  72. FSTTestDoc(@"messages/first", 1,
  73. @{ @"a" : @1 }, YES));
  74. XCTAssertEqualObjects(FSTTestDoc(@"messages/first", 1,
  75. @{ @"a" : @1 }, YES),
  76. FSTTestDoc(@"messages/first", 1,
  77. @{ @"a" : @1 }, 5));
  78. }
  79. @end
  80. NS_ASSUME_NONNULL_END