testutil.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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_mutation.h"
  38. #include "Firestore/core/src/model/transform_operation.h"
  39. #include "Firestore/core/src/model/unknown_document.h"
  40. #include "Firestore/core/src/model/verify_mutation.h"
  41. #include "Firestore/core/src/nanopb/byte_string.h"
  42. #include "Firestore/core/src/util/hard_assert.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. return model::FieldPath::FromServerFormat(std::string(field));
  108. }
  109. model::DatabaseId DbId(std::string project) {
  110. size_t slash = project.find('/');
  111. if (slash == std::string::npos) {
  112. return model::DatabaseId(std::move(project), model::DatabaseId::kDefault);
  113. } else {
  114. std::string database_id = project.substr(slash + 1);
  115. project = project.substr(0, slash);
  116. return model::DatabaseId(std::move(project), std::move(database_id));
  117. }
  118. }
  119. FieldValue Ref(std::string project, absl::string_view path) {
  120. return FieldValue::FromReference(DbId(std::move(project)), Key(path));
  121. }
  122. model::ResourcePath Resource(absl::string_view field) {
  123. return model::ResourcePath::FromString(std::string(field));
  124. }
  125. model::SnapshotVersion Version(int64_t version) {
  126. namespace chr = std::chrono;
  127. auto timepoint =
  128. chr::time_point<chr::system_clock>(chr::microseconds(version));
  129. return model::SnapshotVersion{Timestamp::FromTimePoint(timepoint)};
  130. }
  131. model::Document Doc(absl::string_view key,
  132. int64_t version,
  133. const model::FieldValue::Map& data) {
  134. return Doc(key, version, data, DocumentState::kSynced);
  135. }
  136. model::Document Doc(absl::string_view key,
  137. int64_t version,
  138. const model::FieldValue::Map& data,
  139. model::DocumentState document_state) {
  140. return model::Document(model::ObjectValue::FromMap(data), Key(key),
  141. Version(version), document_state);
  142. }
  143. model::Document Doc(absl::string_view key,
  144. int64_t version,
  145. const FieldValue& data) {
  146. return Doc(key, version, data, DocumentState::kSynced);
  147. }
  148. model::Document Doc(absl::string_view key,
  149. int64_t version,
  150. const FieldValue& data,
  151. model::DocumentState document_state) {
  152. return model::Document(model::ObjectValue(data), Key(key), Version(version),
  153. document_state);
  154. }
  155. model::NoDocument DeletedDoc(absl::string_view key,
  156. int64_t version,
  157. bool has_committed_mutations) {
  158. return model::NoDocument(Key(key), Version(version), has_committed_mutations);
  159. }
  160. model::NoDocument DeletedDoc(model::DocumentKey key,
  161. int64_t version,
  162. bool has_committed_mutations) {
  163. return model::NoDocument(std::move(key), Version(version),
  164. has_committed_mutations);
  165. }
  166. model::UnknownDocument UnknownDoc(absl::string_view key, int64_t version) {
  167. return model::UnknownDocument(Key(key), Version(version));
  168. }
  169. DocumentComparator DocComparator(absl::string_view field_path) {
  170. return Query("docs").AddingOrderBy(OrderBy(field_path)).Comparator();
  171. }
  172. DocumentSet DocSet(DocumentComparator comp, std::vector<Document> docs) {
  173. DocumentSet set{std::move(comp)};
  174. for (const Document& doc : docs) {
  175. set = set.insert(doc);
  176. }
  177. return set;
  178. }
  179. core::Filter::Operator OperatorFromString(absl::string_view s) {
  180. if (s == "<") {
  181. return core::Filter::Operator::LessThan;
  182. } else if (s == "<=") {
  183. return core::Filter::Operator::LessThanOrEqual;
  184. } else if (s == "==") {
  185. return core::Filter::Operator::Equal;
  186. } else if (s == "!=") {
  187. return core::Filter::Operator::NotEqual;
  188. } else if (s == ">") {
  189. return core::Filter::Operator::GreaterThan;
  190. } else if (s == ">=") {
  191. return core::Filter::Operator::GreaterThanOrEqual;
  192. // Both are accepted for compatibility with spec tests and existing
  193. // canonical ids.
  194. } else if (s == "array_contains" || s == "array-contains") {
  195. return core::Filter::Operator::ArrayContains;
  196. } else if (s == "in") {
  197. return core::Filter::Operator::In;
  198. } else if (s == "array-contains-any") {
  199. return core::Filter::Operator::ArrayContainsAny;
  200. } else if (s == "not-in") {
  201. return core::Filter::Operator::NotIn;
  202. } else {
  203. HARD_FAIL("Unknown operator: %s", s);
  204. }
  205. }
  206. core::FieldFilter Filter(absl::string_view key,
  207. absl::string_view op,
  208. FieldValue value) {
  209. return core::FieldFilter::Create(Field(key), OperatorFromString(op),
  210. std::move(value));
  211. }
  212. core::FieldFilter Filter(absl::string_view key,
  213. absl::string_view op,
  214. FieldValue::Map value) {
  215. return Filter(key, op, FieldValue::FromMap(std::move(value)));
  216. }
  217. core::FieldFilter Filter(absl::string_view key,
  218. absl::string_view op,
  219. std::nullptr_t) {
  220. return Filter(key, op, FieldValue::Null());
  221. }
  222. core::FieldFilter Filter(absl::string_view key,
  223. absl::string_view op,
  224. const char* value) {
  225. return Filter(key, op, FieldValue::FromString(value));
  226. }
  227. core::FieldFilter Filter(absl::string_view key,
  228. absl::string_view op,
  229. int value) {
  230. return Filter(key, op, FieldValue::FromInteger(value));
  231. }
  232. core::FieldFilter Filter(absl::string_view key,
  233. absl::string_view op,
  234. double value) {
  235. return Filter(key, op, FieldValue::FromDouble(value));
  236. }
  237. core::Direction Direction(absl::string_view direction) {
  238. if (direction == "asc") {
  239. return core::Direction::Ascending;
  240. } else if (direction == "desc") {
  241. return core::Direction::Descending;
  242. } else {
  243. HARD_FAIL("Unknown direction: %s (use \"asc\" or \"desc\")", direction);
  244. }
  245. }
  246. core::OrderBy OrderBy(absl::string_view key, absl::string_view direction) {
  247. return core::OrderBy(Field(key), Direction(direction));
  248. }
  249. core::OrderBy OrderBy(model::FieldPath field_path, core::Direction direction) {
  250. return core::OrderBy(std::move(field_path), direction);
  251. }
  252. core::Query Query(absl::string_view path) {
  253. return core::Query(Resource(path));
  254. }
  255. core::Query CollectionGroupQuery(absl::string_view collection_id) {
  256. return core::Query(model::ResourcePath::Empty(),
  257. std::make_shared<const std::string>(collection_id));
  258. }
  259. model::SetMutation SetMutation(absl::string_view path,
  260. const model::FieldValue::Map& values) {
  261. return model::SetMutation(Key(path), model::ObjectValue::FromMap(values),
  262. model::Precondition::None());
  263. }
  264. model::PatchMutation PatchMutation(
  265. absl::string_view path,
  266. FieldValue::Map values,
  267. // TODO(rsgowman): Investigate changing update_mask to a set.
  268. std::vector<model::FieldPath> update_mask) {
  269. ObjectValue object_value = ObjectValue::Empty();
  270. std::set<FieldPath> field_mask_paths;
  271. for (const auto& kv : values) {
  272. FieldPath field_path = Field(kv.first);
  273. field_mask_paths.insert(field_path);
  274. const FieldValue& value = kv.second;
  275. if (!value.is_string() || value.string_value() != kDeleteSentinel) {
  276. object_value = object_value.Set(field_path, value);
  277. }
  278. }
  279. bool merge = !update_mask.empty();
  280. Precondition precondition =
  281. merge ? Precondition::None() : Precondition::Exists(true);
  282. FieldMask mask(
  283. merge ? std::set<FieldPath>(update_mask.begin(), update_mask.end())
  284. : field_mask_paths);
  285. return model::PatchMutation(Key(path), std::move(object_value),
  286. std::move(mask), precondition);
  287. }
  288. model::TransformMutation TransformMutation(
  289. absl::string_view key,
  290. std::vector<std::pair<std::string, TransformOperation>> transforms) {
  291. std::vector<FieldTransform> field_transforms;
  292. for (auto&& pair : transforms) {
  293. auto path = Field(std::move(pair.first));
  294. TransformOperation&& op_ptr = std::move(pair.second);
  295. FieldTransform transform(std::move(path), std::move(op_ptr));
  296. field_transforms.push_back(std::move(transform));
  297. }
  298. return model::TransformMutation(Key(key), std::move(field_transforms));
  299. }
  300. std::pair<std::string, TransformOperation> Increment(std::string field,
  301. FieldValue operand) {
  302. model::NumericIncrementTransform transform(std::move(operand));
  303. return std::pair<std::string, TransformOperation>(std::move(field),
  304. std::move(transform));
  305. }
  306. std::pair<std::string, TransformOperation> ArrayUnion(
  307. std::string field, std::vector<FieldValue> operands) {
  308. model::ArrayTransform transform(TransformOperation::Type::ArrayUnion,
  309. std::move(operands));
  310. return std::pair<std::string, TransformOperation>(std::move(field),
  311. std::move(transform));
  312. }
  313. model::DeleteMutation DeleteMutation(absl::string_view path) {
  314. return model::DeleteMutation(Key(path), Precondition::None());
  315. }
  316. model::VerifyMutation VerifyMutation(absl::string_view path, int64_t version) {
  317. return model::VerifyMutation(Key(path),
  318. Precondition::UpdateTime(Version(version)));
  319. }
  320. model::MutationResult MutationResult(int64_t version) {
  321. return model::MutationResult(Version(version), absl::nullopt);
  322. }
  323. nanopb::ByteString ResumeToken(int64_t snapshot_version) {
  324. if (snapshot_version == 0) {
  325. // TODO(rsgowman): The other platforms return null here, though I'm not sure
  326. // if they ever rely on that. I suspect it'd be sufficient to return '{}'.
  327. // But for now, we'll just abort() until we hit a test case that actually
  328. // makes use of this.
  329. HARD_FAIL("Unsupported snapshot version %s", snapshot_version);
  330. }
  331. std::string snapshot_string =
  332. std::string("snapshot-") + std::to_string(snapshot_version);
  333. return nanopb::ByteString(snapshot_string);
  334. }
  335. } // namespace testutil
  336. } // namespace firestore
  337. } // namespace firebase