FSTDocumentTests.mm 3.7 KB

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