testutil.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_set.h"
  29. #include "Firestore/core/src/model/field_mask.h"
  30. #include "Firestore/core/src/model/field_path.h"
  31. #include "Firestore/core/src/model/field_transform.h"
  32. #include "Firestore/core/src/model/field_value.h"
  33. #include "Firestore/core/src/model/no_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/unknown_document.h"
  39. #include "Firestore/core/src/model/verify_mutation.h"
  40. #include "Firestore/core/src/nanopb/byte_string.h"
  41. #include "Firestore/core/src/util/hard_assert.h"
  42. #include "Firestore/core/src/util/statusor.h"
  43. #include "absl/memory/memory.h"
  44. namespace firebase {
  45. namespace firestore {
  46. namespace testutil {
  47. using model::Document;
  48. using model::DocumentComparator;
  49. using model::DocumentSet;
  50. using model::DocumentState;
  51. using model::FieldMask;
  52. using model::FieldPath;
  53. using model::FieldTransform;
  54. using model::FieldValue;
  55. using model::ObjectValue;
  56. using model::Precondition;
  57. using model::TransformOperation;
  58. using nanopb::ByteString;
  59. /**
  60. * A string sentinel that can be used with PatchMutation() to mark a field for
  61. * deletion.
  62. */
  63. constexpr const char* kDeleteSentinel = "<DELETE>";
  64. namespace details {
  65. FieldValue BlobValue(std::initializer_list<uint8_t> octets) {
  66. nanopb::ByteString contents{octets};
  67. return FieldValue::FromBlob(std::move(contents));
  68. }
  69. } // namespace details
  70. ByteString Bytes(std::initializer_list<uint8_t> octets) {
  71. return ByteString(octets);
  72. }
  73. FieldValue Value(std::nullptr_t) {
  74. return FieldValue::Null();
  75. }
  76. FieldValue Value(double value) {
  77. return FieldValue::FromDouble(value);
  78. }
  79. FieldValue Value(Timestamp value) {
  80. return FieldValue::FromTimestamp(value);
  81. }
  82. FieldValue Value(const char* value) {
  83. return FieldValue::FromString(value);
  84. }
  85. FieldValue Value(const std::string& value) {
  86. return FieldValue::FromString(value);
  87. }
  88. FieldValue Value(const GeoPoint& value) {
  89. return FieldValue::FromGeoPoint(value);
  90. }
  91. FieldValue Value(const FieldValue& value) {
  92. return value;
  93. }
  94. FieldValue Value(const model::ObjectValue& value) {
  95. return value.AsFieldValue();
  96. }
  97. FieldValue Value(const FieldValue::Map& value) {
  98. return Value(model::ObjectValue::FromMap(value));
  99. }
  100. model::ObjectValue WrapObject(const model::FieldValue::Map& value) {
  101. return model::ObjectValue::FromMap(value);
  102. }
  103. model::DocumentKey Key(absl::string_view path) {
  104. return model::DocumentKey::FromPathString(std::string(path));
  105. }
  106. model::FieldPath Field(absl::string_view field) {
  107. auto path = model::FieldPath::FromServerFormat(std::string(field));
  108. return path.ConsumeValueOrDie();
  109. }
  110. model::DatabaseId DbId(std::string project) {
  111. size_t slash = project.find('/');
  112. if (slash == std::string::npos) {
  113. return model::DatabaseId(std::move(project), model::DatabaseId::kDefault);
  114. } else {
  115. std::string database_id = project.substr(slash + 1);
  116. project = project.substr(0, slash);
  117. return model::DatabaseId(std::move(project), std::move(database_id));
  118. }
  119. }
  120. FieldValue Ref(std::string project, absl::string_view path) {
  121. return FieldValue::FromReference(DbId(std::move(project)), Key(path));
  122. }
  123. model::ResourcePath Resource(absl::string_view field) {
  124. return model::ResourcePath::FromString(std::string(field));
  125. }
  126. model::SnapshotVersion Version(int64_t version) {
  127. namespace chr = std::chrono;
  128. auto timepoint =
  129. chr::time_point<chr::system_clock>(chr::microseconds(version));
  130. return model::SnapshotVersion{Timestamp::FromTimePoint(timepoint)};
  131. }
  132. model::Document Doc(absl::string_view key,
  133. int64_t version,
  134. const model::FieldValue::Map& data) {
  135. return Doc(key, version, data, DocumentState::kSynced);
  136. }
  137. model::Document Doc(absl::string_view key,
  138. int64_t version,
  139. const model::FieldValue::Map& data,
  140. model::DocumentState document_state) {
  141. return model::Document(model::ObjectValue::FromMap(data), Key(key),
  142. Version(version), document_state);
  143. }
  144. model::Document Doc(absl::string_view key,
  145. int64_t version,
  146. const FieldValue& data) {
  147. return Doc(key, version, data, DocumentState::kSynced);
  148. }
  149. model::Document Doc(absl::string_view key,
  150. int64_t version,
  151. const FieldValue& data,
  152. model::DocumentState document_state) {
  153. return model::Document(model::ObjectValue(data), Key(key), Version(version),
  154. document_state);
  155. }
  156. model::NoDocument DeletedDoc(absl::string_view key,
  157. int64_t version,
  158. bool has_committed_mutations) {
  159. return model::NoDocument(Key(key), Version(version), has_committed_mutations);
  160. }
  161. model::NoDocument DeletedDoc(model::DocumentKey key,
  162. int64_t version,
  163. bool has_committed_mutations) {
  164. return model::NoDocument(std::move(key), Version(version),
  165. has_committed_mutations);
  166. }
  167. model::UnknownDocument UnknownDoc(absl::string_view key, int64_t version) {
  168. return model::UnknownDocument(Key(key), Version(version));
  169. }
  170. DocumentComparator DocComparator(absl::string_view field_path) {
  171. return Query("docs").AddingOrderBy(OrderBy(field_path)).Comparator();
  172. }
  173. DocumentSet DocSet(DocumentComparator comp, std::vector<Document> docs) {
  174. DocumentSet set{std::move(comp)};
  175. for (const Document& doc : docs) {
  176. set = set.insert(doc);
  177. }
  178. return set;
  179. }
  180. core::Filter::Operator OperatorFromString(absl::string_view s) {
  181. if (s == "<") {
  182. return core::Filter::Operator::LessThan;
  183. } else if (s == "<=") {
  184. return core::Filter::Operator::LessThanOrEqual;
  185. } else if (s == "==") {
  186. return core::Filter::Operator::Equal;
  187. } else if (s == "!=") {
  188. return core::Filter::Operator::NotEqual;
  189. } else if (s == ">") {
  190. return core::Filter::Operator::GreaterThan;
  191. } else if (s == ">=") {
  192. return core::Filter::Operator::GreaterThanOrEqual;
  193. // Both are accepted for compatibility with spec tests and existing
  194. // canonical ids.
  195. } else if (s == "array_contains" || s == "array-contains") {
  196. return core::Filter::Operator::ArrayContains;
  197. } else if (s == "in") {
  198. return core::Filter::Operator::In;
  199. } else if (s == "array-contains-any") {
  200. return core::Filter::Operator::ArrayContainsAny;
  201. } else if (s == "not-in") {
  202. return core::Filter::Operator::NotIn;
  203. } else {
  204. HARD_FAIL("Unknown operator: %s", s);
  205. }
  206. }
  207. core::FieldFilter Filter(absl::string_view key,
  208. absl::string_view op,
  209. FieldValue value) {
  210. return core::FieldFilter::Create(Field(key), OperatorFromString(op),
  211. std::move(value));
  212. }
  213. core::FieldFilter Filter(absl::string_view key,
  214. absl::string_view op,
  215. FieldValue::Map value) {
  216. return Filter(key, op, FieldValue::FromMap(std::move(value)));
  217. }
  218. core::FieldFilter Filter(absl::string_view key,
  219. absl::string_view op,
  220. std::nullptr_t) {
  221. return Filter(key, op, FieldValue::Null());
  222. }
  223. core::FieldFilter Filter(absl::string_view key,
  224. absl::string_view op,
  225. const char* value) {
  226. return Filter(key, op, FieldValue::FromString(value));
  227. }
  228. core::FieldFilter Filter(absl::string_view key,
  229. absl::string_view op,
  230. int value) {
  231. return Filter(key, op, FieldValue::FromInteger(value));
  232. }
  233. core::FieldFilter Filter(absl::string_view key,
  234. absl::string_view op,
  235. double value) {
  236. return Filter(key, op, FieldValue::FromDouble(value));
  237. }
  238. core::Direction Direction(absl::string_view direction) {
  239. if (direction == "asc") {
  240. return core::Direction::Ascending;
  241. } else if (direction == "desc") {
  242. return core::Direction::Descending;
  243. } else {
  244. HARD_FAIL("Unknown direction: %s (use \"asc\" or \"desc\")", direction);
  245. }
  246. }
  247. core::OrderBy OrderBy(absl::string_view key, absl::string_view direction) {
  248. return core::OrderBy(Field(key), Direction(direction));
  249. }
  250. core::OrderBy OrderBy(model::FieldPath field_path, core::Direction direction) {
  251. return core::OrderBy(std::move(field_path), direction);
  252. }
  253. core::Query Query(absl::string_view path) {
  254. return core::Query(Resource(path));
  255. }
  256. core::Query CollectionGroupQuery(absl::string_view collection_id) {
  257. return core::Query(model::ResourcePath::Empty(),
  258. std::make_shared<const std::string>(collection_id));
  259. }
  260. // TODO(chenbrian): Rewrite SetMutation to allow parsing of field
  261. // transforms directly in the `values` parameter once the UserDataReader/
  262. // UserDataWriter changes are ported from Web and Android.
  263. model::SetMutation SetMutation(
  264. absl::string_view path,
  265. const model::FieldValue::Map& values,
  266. std::vector<std::pair<std::string, TransformOperation>> transforms) {
  267. std::vector<FieldTransform> field_transforms;
  268. for (auto&& pair : transforms) {
  269. auto field_path = Field(std::move(pair.first));
  270. TransformOperation&& op_ptr = std::move(pair.second);
  271. FieldTransform transform(std::move(field_path), std::move(op_ptr));
  272. field_transforms.push_back(std::move(transform));
  273. }
  274. return model::SetMutation(Key(path), model::ObjectValue::FromMap(values),
  275. model::Precondition::None(),
  276. std::move(field_transforms));
  277. }
  278. // TODO(chenbrian): Rewrite PatchMutation to allow parsing of field
  279. // transforms directly in the `values` parameter once the UserDataReader/
  280. // UserDataWriter changes are ported from Web and Android.
  281. model::PatchMutation PatchMutation(
  282. absl::string_view path,
  283. const FieldValue::Map& values,
  284. // TODO(rsgowman): Investigate changing update_mask to a set.
  285. std::vector<std::pair<std::string, TransformOperation>> transforms) {
  286. return PatchMutationHelper(path, values, transforms,
  287. Precondition::Exists(true), absl::nullopt);
  288. }
  289. // TODO(chenbrian): Rewrite MergeMutation to allow parsing of field
  290. // transforms directly in the `values` parameter once the UserDataReader/
  291. // UserDataWriter changes are ported from Web and Android.
  292. model::PatchMutation MergeMutation(
  293. absl::string_view path,
  294. const FieldValue::Map& values,
  295. const std::vector<model::FieldPath>& update_mask,
  296. std::vector<std::pair<std::string, TransformOperation>> transforms) {
  297. return PatchMutationHelper(path, values, transforms, Precondition::None(),
  298. update_mask);
  299. }
  300. model::PatchMutation PatchMutationHelper(
  301. absl::string_view path,
  302. const FieldValue::Map& values,
  303. std::vector<std::pair<std::string, TransformOperation>> transforms,
  304. Precondition precondition,
  305. const absl::optional<std::vector<model::FieldPath>>& update_mask) {
  306. ObjectValue object_value = ObjectValue::Empty();
  307. std::set<FieldPath> field_mask_paths;
  308. std::vector<FieldTransform> field_transforms;
  309. for (auto&& pair : transforms) {
  310. auto field_path = Field(std::move(pair.first));
  311. TransformOperation&& op_ptr = std::move(pair.second);
  312. FieldTransform transform(std::move(field_path), std::move(op_ptr));
  313. field_transforms.push_back(std::move(transform));
  314. }
  315. for (const auto& kv : values) {
  316. FieldPath field_path = Field(kv.first);
  317. field_mask_paths.insert(field_path);
  318. const FieldValue& value = kv.second;
  319. if (!value.is_string() || value.string_value() != kDeleteSentinel) {
  320. object_value = object_value.Set(field_path, value);
  321. } else if (value.string_value() == kDeleteSentinel) {
  322. object_value =
  323. object_value.Set(field_path, object_value.Delete(field_path));
  324. }
  325. }
  326. FieldMask mask(
  327. update_mask.has_value()
  328. ? std::set<FieldPath>(update_mask->begin(), update_mask->end())
  329. : field_mask_paths);
  330. return model::PatchMutation(Key(path), std::move(object_value),
  331. std::move(mask), precondition,
  332. std::move(field_transforms));
  333. }
  334. std::pair<std::string, TransformOperation> Increment(std::string field,
  335. FieldValue operand) {
  336. model::NumericIncrementTransform transform(std::move(operand));
  337. return std::pair<std::string, TransformOperation>(std::move(field),
  338. std::move(transform));
  339. }
  340. std::pair<std::string, TransformOperation> ArrayUnion(
  341. std::string field, std::vector<FieldValue> operands) {
  342. model::ArrayTransform transform(TransformOperation::Type::ArrayUnion,
  343. std::move(operands));
  344. return std::pair<std::string, TransformOperation>(std::move(field),
  345. std::move(transform));
  346. }
  347. model::DeleteMutation DeleteMutation(absl::string_view path) {
  348. return model::DeleteMutation(Key(path), Precondition::None());
  349. }
  350. model::VerifyMutation VerifyMutation(absl::string_view path, int64_t version) {
  351. return model::VerifyMutation(Key(path),
  352. Precondition::UpdateTime(Version(version)));
  353. }
  354. model::MutationResult MutationResult(int64_t version) {
  355. return model::MutationResult(Version(version), absl::nullopt);
  356. }
  357. nanopb::ByteString ResumeToken(int64_t snapshot_version) {
  358. if (snapshot_version == 0) {
  359. // TODO(rsgowman): The other platforms return null here, though I'm not sure
  360. // if they ever rely on that. I suspect it'd be sufficient to return '{}'.
  361. // But for now, we'll just abort() until we hit a test case that actually
  362. // makes use of this.
  363. HARD_FAIL("Unsupported snapshot version %s", snapshot_version);
  364. }
  365. std::string snapshot_string =
  366. std::string("snapshot-") + std::to_string(snapshot_version);
  367. return nanopb::ByteString(snapshot_string);
  368. }
  369. } // namespace testutil
  370. } // namespace firestore
  371. } // namespace firebase