FSTDocumentTests.mm 3.8 KB

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