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