FSTUserDataReaderTests.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 2019 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/API/FSTUserDataReader.h"
  17. #import <FirebaseFirestore/FIRFieldValue.h>
  18. #import <FirebaseFirestore/FIRGeoPoint.h>
  19. #import <FirebaseFirestore/FIRTimestamp.h>
  20. #import <XCTest/XCTest.h>
  21. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  22. #import "Firestore/Source/API/converters.h"
  23. #include "Firestore/core/include/firebase/firestore/geo_point.h"
  24. #include "Firestore/core/src/model/database_id.h"
  25. #include "Firestore/core/src/model/patch_mutation.h"
  26. #include "Firestore/core/src/model/set_mutation.h"
  27. #include "Firestore/core/src/model/transform_operation.h"
  28. #include "Firestore/core/src/nanopb/message.h"
  29. #include "Firestore/core/src/nanopb/nanopb_util.h"
  30. #include "Firestore/core/test/unit/testutil/testutil.h"
  31. namespace util = firebase::firestore::util;
  32. namespace nanopb = firebase::firestore::nanopb;
  33. using firebase::Timestamp;
  34. using firebase::firestore::GeoPoint;
  35. using firebase::firestore::api::MakeGeoPoint;
  36. using firebase::firestore::api::MakeTimestamp;
  37. using firebase::firestore::google_firestore_v1_ArrayValue;
  38. using firebase::firestore::google_firestore_v1_Value;
  39. using firebase::firestore::model::ArrayTransform;
  40. using firebase::firestore::model::DatabaseId;
  41. using firebase::firestore::model::FieldPath;
  42. using firebase::firestore::model::FieldTransform;
  43. using firebase::firestore::model::GetTypeOrder;
  44. using firebase::firestore::model::IsArray;
  45. using firebase::firestore::model::IsNullValue;
  46. using firebase::firestore::model::IsNumber;
  47. using firebase::firestore::model::ObjectValue;
  48. using firebase::firestore::model::PatchMutation;
  49. using firebase::firestore::model::RefValue;
  50. using firebase::firestore::model::SetMutation;
  51. using firebase::firestore::model::TransformOperation;
  52. using firebase::firestore::model::TypeOrder;
  53. using firebase::firestore::nanopb::MakeNSData;
  54. using firebase::firestore::nanopb::Message;
  55. using firebase::firestore::testutil::Array;
  56. using firebase::firestore::testutil::Field;
  57. using firebase::firestore::testutil::Map;
  58. using firebase::firestore::testutil::Value;
  59. using firebase::firestore::testutil::WrapObject;
  60. @interface FSTUserDataReaderTests : XCTestCase
  61. @end
  62. @implementation FSTUserDataReaderTests
  63. - (void)testConvertsIntegers {
  64. NSArray<NSNumber *> *values = @[
  65. @(INT_MIN), @(-1), @0, @1, @2, @(UCHAR_MAX), @(INT_MAX), // Standard integers
  66. @(LONG_MIN), @(LONG_MAX), @(LLONG_MIN), @(LLONG_MAX) // Larger values
  67. ];
  68. for (NSNumber *value in values) {
  69. Message<google_firestore_v1_Value> wrapped = FSTTestFieldValue(value);
  70. XCTAssertTrue(IsNumber(*wrapped));
  71. XCTAssertEqual(wrapped->integer_value, [value longLongValue]);
  72. }
  73. }
  74. - (void)testConvertsDoubles {
  75. // Note that 0x1.0p-1074 is a hex floating point literal representing the minimum subnormal
  76. // number: <https://en.wikipedia.org/wiki/Denormal_number>.
  77. NSArray<NSNumber *> *values = @[
  78. @(-INFINITY), @(-DBL_MAX), @(LLONG_MIN * -1.0), @(-1.1), @(-0x1.0p-1074), @(-0.0), @(0.0),
  79. @(0x1.0p-1074), @(DBL_MIN), @(1.1), @(LLONG_MAX * 1.0), @(DBL_MAX), @(INFINITY)
  80. ];
  81. for (NSNumber *value in values) {
  82. Message<google_firestore_v1_Value> wrapped = FSTTestFieldValue(value);
  83. XCTAssertTrue(IsNumber(*wrapped));
  84. XCTAssertEqual(wrapped->double_value, [value doubleValue]);
  85. }
  86. }
  87. - (void)testConvertsNilAndNSNull {
  88. Message<google_firestore_v1_Value> nullValue = Value(nullptr);
  89. XCTAssertTrue(IsNullValue(*nullValue));
  90. XCTAssertEqual(*FSTTestFieldValue(nil), *nullValue);
  91. XCTAssertEqual(*FSTTestFieldValue([NSNull null]), *nullValue);
  92. }
  93. - (void)testConvertsBooleans {
  94. NSArray<NSNumber *> *values = @[ @YES, @NO ];
  95. for (NSNumber *value in values) {
  96. Message<google_firestore_v1_Value> wrapped = FSTTestFieldValue(value);
  97. XCTAssertEqual(GetTypeOrder(*wrapped), TypeOrder::kBoolean);
  98. XCTAssertEqual(wrapped->boolean_value, [value boolValue]);
  99. }
  100. }
  101. - (void)testConvertsUnsignedCharToInteger {
  102. // See comments in FSTUserDataReader regarding handling of signed char. Essentially, signed
  103. // char has to be treated as boolean. Unsigned chars could conceivably be handled consistently
  104. // with signed chars but on arm64 these end up being stored as signed shorts. This forces us to
  105. // choose, and it's more useful to support shorts as Integers than it is to treat unsigned char as
  106. // Boolean.
  107. Message<google_firestore_v1_Value> wrapped =
  108. FSTTestFieldValue([NSNumber numberWithUnsignedChar:1]);
  109. XCTAssertEqual(*wrapped, *Value(1));
  110. }
  111. union DoubleBits {
  112. double d;
  113. uint64_t bits;
  114. };
  115. - (void)testConvertsStrings {
  116. NSArray<NSString *> *values = @[ @"", @"abc" ];
  117. for (id value in values) {
  118. Message<google_firestore_v1_Value> wrapped = FSTTestFieldValue(value);
  119. XCTAssertEqual(GetTypeOrder(*wrapped), TypeOrder::kString);
  120. XCTAssertEqual(nanopb::MakeString(wrapped->string_value), util::MakeString(value));
  121. }
  122. }
  123. - (void)testConvertsDates {
  124. NSArray<NSDate *> *values =
  125. @[ FSTTestDate(1900, 12, 1, 1, 20, 30), FSTTestDate(2017, 4, 24, 13, 20, 30) ];
  126. for (NSDate *value in values) {
  127. Message<google_firestore_v1_Value> wrapped = FSTTestFieldValue(value);
  128. XCTAssertEqual(GetTypeOrder(*wrapped), TypeOrder::kTimestamp);
  129. Timestamp timestamp = MakeTimestamp(value);
  130. XCTAssertEqual(wrapped->timestamp_value.nanos, timestamp.nanoseconds());
  131. XCTAssertEqual(wrapped->timestamp_value.seconds, timestamp.seconds());
  132. }
  133. }
  134. - (void)testConvertsGeoPoints {
  135. NSArray<FIRGeoPoint *> *values = @[ FSTTestGeoPoint(1.24, 4.56), FSTTestGeoPoint(-20, 100) ];
  136. for (FIRGeoPoint *value in values) {
  137. Message<google_firestore_v1_Value> wrapped = FSTTestFieldValue(value);
  138. XCTAssertEqual(GetTypeOrder(*wrapped), TypeOrder::kGeoPoint);
  139. GeoPoint geo_point = MakeGeoPoint(value);
  140. XCTAssertEqual(wrapped->geo_point_value.longitude, geo_point.longitude());
  141. XCTAssertEqual(wrapped->geo_point_value.latitude, geo_point.latitude());
  142. }
  143. }
  144. - (void)testConvertsBlobs {
  145. NSArray<NSData *> *values = @[ FSTTestData(1, 2, 3, -1), FSTTestData(1, 2, -1) ];
  146. for (NSData *value in values) {
  147. Message<google_firestore_v1_Value> wrapped = FSTTestFieldValue(value);
  148. XCTAssertEqual(GetTypeOrder(*wrapped), TypeOrder::kBlob);
  149. XCTAssertEqualObjects(MakeNSData(wrapped->bytes_value), value);
  150. }
  151. }
  152. - (void)testConvertsResourceNames {
  153. NSArray<FSTDocumentKeyReference *> *values = @[
  154. FSTTestRef("project", DatabaseId::kDefault, @"foo/bar"),
  155. FSTTestRef("project", DatabaseId::kDefault, @"foo/baz")
  156. ];
  157. for (FSTDocumentKeyReference *value in values) {
  158. Message<google_firestore_v1_Value> wrapped = FSTTestFieldValue(value);
  159. XCTAssertEqual(GetTypeOrder(*wrapped), TypeOrder::kReference);
  160. Message<google_firestore_v1_Value> expected = RefValue(value.databaseID, value.key);
  161. XCTAssertEqual(*wrapped, *expected);
  162. }
  163. }
  164. - (void)testConvertsEmptyObjects {
  165. XCTAssertTrue(ObjectValue(FSTTestFieldValue(@{})) == ObjectValue{});
  166. XCTAssertEqual(GetTypeOrder(*FSTTestFieldValue(@{})), TypeOrder::kMap);
  167. }
  168. - (void)testConvertsSimpleObjects {
  169. ObjectValue actual =
  170. FSTTestObjectValue(@{@"a" : @"foo", @"b" : @(1L), @"c" : @YES, @"d" : [NSNull null]});
  171. ObjectValue expected = WrapObject("a", "foo", "b", 1, "c", true, "d", nullptr);
  172. XCTAssertEqual(actual, expected);
  173. }
  174. - (void)testConvertsNestedObjects {
  175. ObjectValue actual = FSTTestObjectValue(@{@"a" : @{@"b" : @{@"c" : @"foo"}, @"d" : @YES}});
  176. ObjectValue expected = WrapObject("a", Map("b", Map("c", "foo"), "d", true));
  177. XCTAssertEqual(actual, expected);
  178. }
  179. - (void)testConvertsArrays {
  180. Message<google_firestore_v1_Value> expected = Value(Array("value", true));
  181. Message<google_firestore_v1_Value> actual = FSTTestFieldValue(@[ @"value", @YES ]);
  182. XCTAssertEqual(*actual, *expected);
  183. XCTAssertTrue(IsArray(*actual));
  184. }
  185. - (void)testNSDatesAreConvertedToTimestamps {
  186. NSDate *date = [NSDate date];
  187. Timestamp timestamp = MakeTimestamp(date);
  188. id input = @{@"array" : @[ @1, date ], @"obj" : @{@"date" : date, @"string" : @"hi"}};
  189. ObjectValue value = FSTTestObjectValue(input);
  190. {
  191. auto array = value.Get(Field("array"));
  192. XCTAssertTrue(array.has_value());
  193. XCTAssertEqual(GetTypeOrder(*array), TypeOrder::kArray);
  194. const google_firestore_v1_Value &actual = array->array_value.values[1];
  195. XCTAssertEqual(GetTypeOrder(actual), TypeOrder::kTimestamp);
  196. XCTAssertEqual(actual.timestamp_value.seconds, timestamp.seconds());
  197. XCTAssertEqual(actual.timestamp_value.nanos, timestamp.nanoseconds());
  198. }
  199. {
  200. auto found = value.Get(Field("obj.date"));
  201. XCTAssertTrue(found.has_value());
  202. XCTAssertEqual(GetTypeOrder(*found), TypeOrder::kTimestamp);
  203. XCTAssertEqual(found->timestamp_value.seconds, timestamp.seconds());
  204. XCTAssertEqual(found->timestamp_value.nanos, timestamp.nanoseconds());
  205. }
  206. }
  207. - (void)testCreatesArrayUnionTransforms {
  208. PatchMutation patchMutation = FSTTestPatchMutation(@"collection/key", @{
  209. @"foo" : [FIRFieldValue fieldValueForArrayUnion:@[ @"tag" ]],
  210. @"bar.baz" :
  211. [FIRFieldValue fieldValueForArrayUnion:@[ @YES, @{@"nested" : @{@"a" : @[ @1, @2 ]}} ]]
  212. },
  213. {});
  214. XCTAssertEqual(patchMutation.field_transforms().size(), 2u);
  215. SetMutation setMutation = FSTTestSetMutation(@"collection/key", @{
  216. @"foo" : [FIRFieldValue fieldValueForArrayUnion:@[ @"tag" ]],
  217. @"bar" : [FIRFieldValue fieldValueForArrayUnion:@[ @YES, @{@"nested" : @{@"a" : @[ @1, @2 ]}} ]]
  218. });
  219. XCTAssertEqual(setMutation.field_transforms().size(), 2u);
  220. const FieldTransform &patchFirst = patchMutation.field_transforms()[0];
  221. XCTAssertEqual(patchFirst.path(), FieldPath({"foo"}));
  222. const FieldTransform &setFirst = setMutation.field_transforms()[0];
  223. XCTAssertEqual(setFirst.path(), FieldPath({"foo"}));
  224. {
  225. Message<google_firestore_v1_ArrayValue> expectedElements = Array(FSTTestFieldValue(@"tag"));
  226. ArrayTransform expected(TransformOperation::Type::ArrayUnion, std::move(expectedElements));
  227. XCTAssertEqual(static_cast<const ArrayTransform &>(patchFirst.transformation()), expected);
  228. XCTAssertEqual(static_cast<const ArrayTransform &>(setFirst.transformation()), expected);
  229. }
  230. const FieldTransform &patchSecond = patchMutation.field_transforms()[1];
  231. XCTAssertEqual(patchSecond.path(), FieldPath({"bar", "baz"}));
  232. const FieldTransform &setSecond = setMutation.field_transforms()[1];
  233. XCTAssertEqual(setSecond.path(), FieldPath({"bar"}));
  234. {
  235. Message<google_firestore_v1_ArrayValue> expectedElements =
  236. Array(FSTTestFieldValue(@YES), FSTTestFieldValue(
  237. @{@"nested" : @{@"a" : @[ @1, @2 ]}}));
  238. ArrayTransform expected(TransformOperation::Type::ArrayUnion, std::move(expectedElements));
  239. XCTAssertEqual(static_cast<const ArrayTransform &>(patchSecond.transformation()), expected);
  240. XCTAssertEqual(static_cast<const ArrayTransform &>(setSecond.transformation()), expected);
  241. }
  242. }
  243. - (void)testCreatesArrayRemoveTransforms {
  244. PatchMutation patchMutation = FSTTestPatchMutation(@"collection/key", @{
  245. @"foo" : [FIRFieldValue fieldValueForArrayRemove:@[ @"tag" ]],
  246. },
  247. {});
  248. XCTAssertEqual(patchMutation.field_transforms().size(), 1u);
  249. SetMutation setMutation = FSTTestSetMutation(@"collection/key", @{
  250. @"foo" : [FIRFieldValue fieldValueForArrayRemove:@[ @"tag" ]],
  251. });
  252. XCTAssertEqual(patchMutation.field_transforms().size(), 1u);
  253. const FieldTransform &patchFirst = patchMutation.field_transforms()[0];
  254. XCTAssertEqual(patchFirst.path(), FieldPath({"foo"}));
  255. const FieldTransform &setFirst = setMutation.field_transforms()[0];
  256. XCTAssertEqual(setFirst.path(), FieldPath({"foo"}));
  257. {
  258. Message<google_firestore_v1_ArrayValue> expectedElements = Array(FSTTestFieldValue(@"tag"));
  259. const ArrayTransform expected(TransformOperation::Type::ArrayRemove,
  260. std::move(expectedElements));
  261. XCTAssertEqual(static_cast<const ArrayTransform &>(patchFirst.transformation()), expected);
  262. XCTAssertEqual(static_cast<const ArrayTransform &>(setFirst.transformation()), expected);
  263. }
  264. }
  265. @end