testutil.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright 2018 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. #include "Firestore/core/test/unit/testutil/testutil.h"
  17. #include <algorithm>
  18. #include <chrono> // NOLINT(build/c++11)
  19. #include <set>
  20. #include "Firestore/core/include/firebase/firestore/geo_point.h"
  21. #include "Firestore/core/include/firebase/firestore/timestamp.h"
  22. #include "Firestore/core/src/core/direction.h"
  23. #include "Firestore/core/src/core/field_filter.h"
  24. #include "Firestore/core/src/core/order_by.h"
  25. #include "Firestore/core/src/core/query.h"
  26. #include "Firestore/core/src/model/delete_mutation.h"
  27. #include "Firestore/core/src/model/document.h"
  28. #include "Firestore/core/src/model/document_key.h"
  29. #include "Firestore/core/src/model/document_set.h"
  30. #include "Firestore/core/src/model/field_mask.h"
  31. #include "Firestore/core/src/model/field_path.h"
  32. #include "Firestore/core/src/model/field_transform.h"
  33. #include "Firestore/core/src/model/mutable_document.h"
  34. #include "Firestore/core/src/model/patch_mutation.h"
  35. #include "Firestore/core/src/model/precondition.h"
  36. #include "Firestore/core/src/model/set_mutation.h"
  37. #include "Firestore/core/src/model/transform_operation.h"
  38. #include "Firestore/core/src/model/value_util.h"
  39. #include "Firestore/core/src/model/verify_mutation.h"
  40. #include "Firestore/core/src/nanopb/byte_string.h"
  41. #include "Firestore/core/src/nanopb/message.h"
  42. #include "Firestore/core/src/nanopb/nanopb_util.h"
  43. #include "Firestore/core/src/util/hard_assert.h"
  44. #include "Firestore/core/src/util/statusor.h"
  45. #include "Firestore/core/src/util/string_format.h"
  46. #include "absl/memory/memory.h"
  47. namespace firebase {
  48. namespace firestore {
  49. namespace testutil {
  50. using model::DeepClone;
  51. using model::Document;
  52. using model::DocumentComparator;
  53. using model::DocumentKey;
  54. using model::DocumentSet;
  55. using model::FieldMask;
  56. using model::FieldPath;
  57. using model::FieldTransform;
  58. using model::MutableDocument;
  59. using model::NullValue;
  60. using model::ObjectValue;
  61. using model::Precondition;
  62. using model::TransformOperation;
  63. using nanopb::ByteString;
  64. using nanopb::Message;
  65. using nanopb::SetRepeatedField;
  66. using nanopb::SharedMessage;
  67. using util::StringFormat;
  68. /**
  69. * A string sentinel that can be used with PatchMutation() to mark a field for
  70. * deletion.
  71. */
  72. constexpr const char* kDeleteSentinel = "<DELETE>";
  73. // We use a canonical NaN bit pattern that's common for both Objective-C and
  74. // Java. Specifically:
  75. //
  76. // - sign: 0
  77. // - exponent: 11 bits, all 1
  78. // - significand: 52 bits, MSB=1, rest=0
  79. //
  80. // This matches the Firestore backend which uses Double.doubleToLongBits from
  81. // the JDK (which is defined to normalize all NaNs to this value). This also
  82. // happens to be a common value for NAN in C++, but C++ does not require this
  83. // specific NaN value to be used, so we normalize.
  84. const uint64_t kCanonicalNanBits = 0x7ff8000000000000ULL;
  85. namespace details {
  86. Message<google_firestore_v1_Value> BlobValue(
  87. std::initializer_list<uint8_t> octets) {
  88. nanopb::ByteString contents{octets};
  89. Message<google_firestore_v1_Value> result;
  90. result->which_value_type = google_firestore_v1_Value_bytes_value_tag;
  91. result->bytes_value = nanopb::MakeBytesArray(octets.begin(), octets.size());
  92. return result;
  93. }
  94. } // namespace details
  95. ByteString Bytes(std::initializer_list<uint8_t> octets) {
  96. return ByteString(octets);
  97. }
  98. Message<google_firestore_v1_Value> Value(std::nullptr_t) {
  99. return DeepClone(NullValue());
  100. }
  101. Message<google_firestore_v1_Value> Value(double value) {
  102. Message<google_firestore_v1_Value> result;
  103. result->which_value_type = google_firestore_v1_Value_double_value_tag;
  104. result->double_value = value;
  105. return result;
  106. }
  107. Message<google_firestore_v1_Value> Value(Timestamp value) {
  108. Message<google_firestore_v1_Value> result;
  109. result->which_value_type = google_firestore_v1_Value_timestamp_value_tag;
  110. result->timestamp_value.seconds = value.seconds();
  111. result->timestamp_value.nanos = value.nanoseconds();
  112. return result;
  113. }
  114. Message<google_firestore_v1_Value> Value(const char* value) {
  115. Message<google_firestore_v1_Value> result;
  116. result->which_value_type = google_firestore_v1_Value_string_value_tag;
  117. result->string_value = nanopb::MakeBytesArray(value);
  118. return result;
  119. }
  120. Message<google_firestore_v1_Value> Value(const std::string& value) {
  121. Message<google_firestore_v1_Value> result;
  122. result->which_value_type = google_firestore_v1_Value_string_value_tag;
  123. result->string_value = nanopb::MakeBytesArray(value);
  124. return result;
  125. }
  126. Message<google_firestore_v1_Value> Value(const nanopb::ByteString& value) {
  127. Message<google_firestore_v1_Value> result;
  128. result->which_value_type = google_firestore_v1_Value_bytes_value_tag;
  129. result->bytes_value = nanopb::MakeBytesArray(value.begin(), value.size());
  130. return result;
  131. }
  132. Message<google_firestore_v1_Value> Value(const GeoPoint& value) {
  133. Message<google_firestore_v1_Value> result;
  134. result->which_value_type = google_firestore_v1_Value_geo_point_value_tag;
  135. result->geo_point_value.latitude = value.latitude();
  136. result->geo_point_value.longitude = value.longitude();
  137. return result;
  138. }
  139. Message<google_firestore_v1_Value> Value(
  140. Message<google_firestore_v1_Value> value) {
  141. return value;
  142. }
  143. Message<google_firestore_v1_Value> Value(
  144. Message<google_firestore_v1_MapValue> value) {
  145. Message<google_firestore_v1_Value> result;
  146. result->which_value_type = google_firestore_v1_Value_map_value_tag;
  147. result->map_value = *value.release();
  148. return result;
  149. }
  150. Message<google_firestore_v1_Value> Value(
  151. Message<google_firestore_v1_ArrayValue> value) {
  152. Message<google_firestore_v1_Value> result;
  153. result->which_value_type = google_firestore_v1_Value_array_value_tag;
  154. result->array_value = *value.release();
  155. return result;
  156. }
  157. Message<google_firestore_v1_Value> Value(const model::ObjectValue& value) {
  158. return DeepClone(value.Get());
  159. }
  160. ObjectValue WrapObject(Message<google_firestore_v1_Value> value) {
  161. return ObjectValue{std::move(value)};
  162. }
  163. model::DocumentKey Key(absl::string_view path) {
  164. return model::DocumentKey::FromPathString(std::string(path));
  165. }
  166. model::FieldPath Field(absl::string_view field) {
  167. auto path = model::FieldPath::FromServerFormat(std::string(field));
  168. return path.ConsumeValueOrDie();
  169. }
  170. model::DatabaseId DbId(std::string project) {
  171. size_t slash = project.find('/');
  172. if (slash == std::string::npos) {
  173. return model::DatabaseId(std::move(project), model::DatabaseId::kDefault);
  174. } else {
  175. std::string database_id = project.substr(slash + 1);
  176. project = project.substr(0, slash);
  177. return model::DatabaseId(std::move(project), std::move(database_id));
  178. }
  179. }
  180. Message<google_firestore_v1_Value> Ref(std::string project,
  181. absl::string_view path) {
  182. model::DatabaseId database_id = DbId(std::move(project));
  183. Message<google_firestore_v1_Value> result;
  184. result->which_value_type = google_firestore_v1_Value_reference_value_tag;
  185. result->string_value = nanopb::MakeBytesArray(
  186. StringFormat("projects/%s/databases/%s/documents/%s",
  187. database_id.project_id(), database_id.database_id(), path));
  188. return result;
  189. }
  190. model::ResourcePath Resource(absl::string_view field) {
  191. return model::ResourcePath::FromString(std::string(field));
  192. }
  193. model::SnapshotVersion Version(int64_t version) {
  194. namespace chr = std::chrono;
  195. auto timepoint =
  196. chr::time_point<chr::system_clock>(chr::microseconds(version));
  197. return model::SnapshotVersion{Timestamp::FromTimePoint(timepoint)};
  198. }
  199. model::MutableDocument Doc(absl::string_view key,
  200. int64_t version,
  201. Message<google_firestore_v1_Value> data) {
  202. return MutableDocument::FoundDocument(Key(key), Version(version),
  203. ObjectValue{std::move(data)});
  204. }
  205. model::MutableDocument Doc(absl::string_view key, int64_t version) {
  206. return MutableDocument::FoundDocument(Key(key), Version(version),
  207. ObjectValue{});
  208. }
  209. model::MutableDocument DeletedDoc(absl::string_view key, int64_t version) {
  210. return MutableDocument::NoDocument(Key(key), Version(version));
  211. }
  212. model::MutableDocument DeletedDoc(DocumentKey key, int64_t version) {
  213. return MutableDocument::NoDocument(std::move(key), Version(version));
  214. }
  215. model::MutableDocument UnknownDoc(absl::string_view key, int64_t version) {
  216. return MutableDocument::UnknownDocument(Key(key), Version(version));
  217. }
  218. model::MutableDocument InvalidDoc(absl::string_view key) {
  219. return MutableDocument::InvalidDocument(Key(key));
  220. }
  221. DocumentComparator DocComparator(absl::string_view field_path) {
  222. return Query("docs").AddingOrderBy(OrderBy(field_path)).Comparator();
  223. }
  224. DocumentSet DocSet(DocumentComparator comp, std::vector<Document> docs) {
  225. DocumentSet set{std::move(comp)};
  226. for (const Document& doc : docs) {
  227. set = set.insert(doc);
  228. }
  229. return set;
  230. }
  231. core::FieldFilter::Operator OperatorFromString(absl::string_view s) {
  232. if (s == "<") {
  233. return core::FieldFilter::Operator::LessThan;
  234. } else if (s == "<=") {
  235. return core::FieldFilter::Operator::LessThanOrEqual;
  236. } else if (s == "==") {
  237. return core::FieldFilter::Operator::Equal;
  238. } else if (s == "!=") {
  239. return core::FieldFilter::Operator::NotEqual;
  240. } else if (s == ">") {
  241. return core::FieldFilter::Operator::GreaterThan;
  242. } else if (s == ">=") {
  243. return core::FieldFilter::Operator::GreaterThanOrEqual;
  244. // Both are accepted for compatibility with spec tests and existing
  245. // canonical ids.
  246. } else if (s == "array_contains" || s == "array-contains") {
  247. return core::FieldFilter::Operator::ArrayContains;
  248. } else if (s == "in") {
  249. return core::FieldFilter::Operator::In;
  250. } else if (s == "array-contains-any") {
  251. return core::FieldFilter::Operator::ArrayContainsAny;
  252. } else if (s == "not-in") {
  253. return core::FieldFilter::Operator::NotIn;
  254. } else {
  255. HARD_FAIL("Unknown operator: %s", s);
  256. }
  257. }
  258. core::FieldFilter Filter(absl::string_view key,
  259. absl::string_view op,
  260. Message<google_firestore_v1_Value> value) {
  261. return core::FieldFilter::Create(Field(key), OperatorFromString(op),
  262. std::move(value));
  263. }
  264. core::FieldFilter Filter(absl::string_view key,
  265. absl::string_view op,
  266. Message<google_firestore_v1_ArrayValue> value) {
  267. return core::FieldFilter::Create(Field(key), OperatorFromString(op),
  268. Value(std::move(value)));
  269. }
  270. core::FieldFilter Filter(absl::string_view key,
  271. absl::string_view op,
  272. std::nullptr_t) {
  273. return Filter(key, op, DeepClone(NullValue()));
  274. }
  275. core::FieldFilter Filter(absl::string_view key,
  276. absl::string_view op,
  277. const char* value) {
  278. return Filter(key, op, Value(value));
  279. }
  280. core::FieldFilter Filter(absl::string_view key,
  281. absl::string_view op,
  282. int value) {
  283. return Filter(key, op, Value(value));
  284. }
  285. core::FieldFilter Filter(absl::string_view key,
  286. absl::string_view op,
  287. double value) {
  288. return Filter(key, op, Value(value));
  289. }
  290. core::Direction Direction(absl::string_view direction) {
  291. if (direction == "asc") {
  292. return core::Direction::Ascending;
  293. } else if (direction == "desc") {
  294. return core::Direction::Descending;
  295. } else {
  296. HARD_FAIL("Unknown direction: %s (use \"asc\" or \"desc\")", direction);
  297. }
  298. }
  299. core::OrderBy OrderBy(absl::string_view key, absl::string_view direction) {
  300. return core::OrderBy(Field(key), Direction(direction));
  301. }
  302. core::OrderBy OrderBy(model::FieldPath field_path, core::Direction direction) {
  303. return core::OrderBy(std::move(field_path), direction);
  304. }
  305. core::Query Query(absl::string_view path) {
  306. return core::Query(Resource(path));
  307. }
  308. core::Query CollectionGroupQuery(absl::string_view collection_id) {
  309. return core::Query(model::ResourcePath::Empty(),
  310. std::make_shared<const std::string>(collection_id));
  311. }
  312. // TODO(chenbrian): Rewrite SetMutation to allow parsing of field
  313. // transforms directly in the `values` parameter once the UserDataReader/
  314. // UserDataWriter changes are ported from Web and Android.
  315. model::SetMutation SetMutation(
  316. absl::string_view path,
  317. Message<google_firestore_v1_Value> values,
  318. std::vector<std::pair<std::string, TransformOperation>> transforms) {
  319. std::vector<FieldTransform> field_transforms;
  320. for (auto&& pair : transforms) {
  321. auto field_path = Field(std::move(pair.first));
  322. TransformOperation&& op_ptr = std::move(pair.second);
  323. FieldTransform transform(std::move(field_path), std::move(op_ptr));
  324. field_transforms.push_back(std::move(transform));
  325. }
  326. return model::SetMutation(Key(path), model::ObjectValue{std::move(values)},
  327. model::Precondition::None(),
  328. std::move(field_transforms));
  329. }
  330. // TODO(chenbrian): Rewrite PatchMutation to allow parsing of field
  331. // transforms directly in the `values` parameter once the UserDataReader/
  332. // UserDataWriter changes are ported from Web and Android.
  333. model::PatchMutation PatchMutation(
  334. absl::string_view path,
  335. Message<google_firestore_v1_Value> values,
  336. // TODO(rsgowman): Investigate changing update_mask to a set.
  337. std::vector<std::pair<std::string, TransformOperation>> transforms) {
  338. return PatchMutationHelper(path, std::move(values), std::move(transforms),
  339. Precondition::Exists(true), absl::nullopt);
  340. }
  341. // TODO(chenbrian): Rewrite MergeMutation to allow parsing of field
  342. // transforms directly in the `values` parameter once the UserDataReader/
  343. // UserDataWriter changes are ported from Web and Android.
  344. model::PatchMutation MergeMutation(
  345. absl::string_view path,
  346. Message<google_firestore_v1_Value> values,
  347. const std::vector<model::FieldPath>& update_mask,
  348. std::vector<std::pair<std::string, TransformOperation>> transforms) {
  349. return PatchMutationHelper(path, std::move(values), std::move(transforms),
  350. Precondition::None(), update_mask);
  351. }
  352. model::PatchMutation PatchMutationHelper(
  353. absl::string_view path,
  354. Message<google_firestore_v1_Value> values,
  355. std::vector<std::pair<std::string, TransformOperation>> transforms,
  356. Precondition precondition,
  357. const absl::optional<std::vector<model::FieldPath>>& update_mask) {
  358. ObjectValue object_value{};
  359. std::set<FieldPath> field_mask_paths;
  360. std::vector<FieldTransform> field_transforms;
  361. for (auto&& pair : transforms) {
  362. auto field_path = Field(std::move(pair.first));
  363. TransformOperation&& op_ptr = std::move(pair.second);
  364. FieldTransform transform(std::move(field_path), std::move(op_ptr));
  365. field_transforms.push_back(std::move(transform));
  366. }
  367. for (pb_size_t i = 0; i < values->map_value.fields_count; ++i) {
  368. FieldPath field_path =
  369. Field(nanopb::MakeStringView(values->map_value.fields[i].key));
  370. field_mask_paths.insert(field_path);
  371. const google_firestore_v1_Value& value = values->map_value.fields[i].value;
  372. if (value.which_value_type != google_firestore_v1_Value_string_value_tag ||
  373. nanopb::MakeStringView(value.string_value) != kDeleteSentinel) {
  374. object_value.Set(field_path, DeepClone(value));
  375. } else if (nanopb::MakeStringView(value.string_value) == kDeleteSentinel) {
  376. object_value.Delete(field_path);
  377. }
  378. }
  379. FieldMask mask(
  380. update_mask.has_value()
  381. ? std::set<FieldPath>(update_mask->begin(), update_mask->end())
  382. : field_mask_paths);
  383. return model::PatchMutation(Key(path), std::move(object_value),
  384. std::move(mask), precondition,
  385. std::move(field_transforms));
  386. }
  387. std::pair<std::string, TransformOperation> Increment(
  388. std::string field, Message<google_firestore_v1_Value> operand) {
  389. model::NumericIncrementTransform transform(std::move(operand));
  390. return std::pair<std::string, TransformOperation>(std::move(field),
  391. std::move(transform));
  392. }
  393. std::pair<std::string, TransformOperation> ArrayUnion(
  394. std::string field,
  395. const std::vector<Message<google_firestore_v1_Value>>& operands) {
  396. Message<google_firestore_v1_ArrayValue> array_value;
  397. SetRepeatedField(&array_value->values, &array_value->values_count,
  398. operands.begin(), operands.end(),
  399. [](const Message<google_firestore_v1_Value>& value) {
  400. return *DeepClone(*value).release();
  401. });
  402. model::ArrayTransform transform(TransformOperation::Type::ArrayUnion,
  403. std::move(array_value));
  404. return std::pair<std::string, TransformOperation>(std::move(field),
  405. std::move(transform));
  406. }
  407. model::DeleteMutation DeleteMutation(absl::string_view path) {
  408. return model::DeleteMutation(Key(path), Precondition::None());
  409. }
  410. model::VerifyMutation VerifyMutation(absl::string_view path, int64_t version) {
  411. return model::VerifyMutation(Key(path),
  412. Precondition::UpdateTime(Version(version)));
  413. }
  414. model::MutationResult MutationResult(int64_t version) {
  415. return model::MutationResult(Version(version), Array());
  416. }
  417. nanopb::ByteString ResumeToken(int64_t snapshot_version) {
  418. if (snapshot_version == 0) {
  419. // TODO(rsgowman): The other platforms return null here, though I'm not sure
  420. // if they ever rely on that. I suspect it'd be sufficient to return '{}'.
  421. // But for now, we'll just abort() until we hit a test case that actually
  422. // makes use of this.
  423. HARD_FAIL("Unsupported snapshot version %s", snapshot_version);
  424. }
  425. std::string snapshot_string =
  426. std::string("snapshot-") + std::to_string(snapshot_version);
  427. return nanopb::ByteString(snapshot_string);
  428. }
  429. model::FieldIndex MakeFieldIndex(const std::string& collection_group) {
  430. return {-1, collection_group, {}, model::FieldIndex::InitialState()};
  431. }
  432. model::FieldIndex MakeFieldIndex(const std::string& collection_group,
  433. int32_t index_id,
  434. model::IndexState state) {
  435. return {index_id, collection_group, {}, state};
  436. }
  437. model::FieldIndex MakeFieldIndex(const std::string& collection_group,
  438. const std::string& field,
  439. model::Segment::Kind kind) {
  440. return {-1,
  441. collection_group,
  442. {model::Segment{Field(field), kind}},
  443. model::FieldIndex::InitialState()};
  444. }
  445. model::FieldIndex MakeFieldIndex(const std::string& collection_group,
  446. const std::string& field_1,
  447. model::Segment::Kind kind_1,
  448. const std::string& field_2,
  449. model::Segment::Kind kind_2) {
  450. return {-1,
  451. collection_group,
  452. {model::Segment{Field(field_1), kind_1},
  453. model::Segment{Field(field_2), kind_2}},
  454. model::FieldIndex::InitialState()};
  455. }
  456. model::FieldIndex MakeFieldIndex(const std::string& collection_group,
  457. const std::string& field_1,
  458. model::Segment::Kind kind_1,
  459. const std::string& field_2,
  460. model::Segment::Kind kind_2,
  461. const std::string& field_3,
  462. model::Segment::Kind kind_3) {
  463. return {-1,
  464. collection_group,
  465. {model::Segment{Field(field_1), kind_1},
  466. model::Segment{Field(field_2), kind_2},
  467. model::Segment{Field(field_3), kind_3}},
  468. model::FieldIndex::InitialState()};
  469. }
  470. model::FieldIndex MakeFieldIndex(const std::string& collection_group,
  471. int32_t index_id,
  472. model::IndexState state,
  473. const std::string& field_1,
  474. model::Segment::Kind kind_1) {
  475. return {index_id,
  476. collection_group,
  477. {model::Segment{Field(field_1), kind_1}},
  478. state};
  479. }
  480. } // namespace testutil
  481. } // namespace firestore
  482. } // namespace firebase