FSTDocumentTests.mm 3.8 KB

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