testutil.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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::GreaterThan;
  188. } else if (s == ">=") {
  189. return core::Filter::Operator::GreaterThanOrEqual;
  190. // Both are accepted for compatibility with spec tests and existing
  191. // canonical ids.
  192. } else if (s == "array_contains" || s == "array-contains") {
  193. return core::Filter::Operator::ArrayContains;
  194. } else if (s == "in") {
  195. return core::Filter::Operator::In;
  196. } else if (s == "array-contains-any") {
  197. return core::Filter::Operator::ArrayContainsAny;
  198. } else {
  199. HARD_FAIL("Unknown operator: %s", s);
  200. }
  201. }
  202. core::FieldFilter Filter(absl::string_view key,
  203. absl::string_view op,
  204. FieldValue value) {
  205. return core::FieldFilter::Create(Field(key), OperatorFromString(op),
  206. std::move(value));
  207. }
  208. core::FieldFilter Filter(absl::string_view key,
  209. absl::string_view op,
  210. FieldValue::Map value) {
  211. return Filter(key, op, FieldValue::FromMap(std::move(value)));
  212. }
  213. core::FieldFilter Filter(absl::string_view key,
  214. absl::string_view op,
  215. std::nullptr_t) {
  216. return Filter(key, op, FieldValue::Null());
  217. }
  218. core::FieldFilter Filter(absl::string_view key,
  219. absl::string_view op,
  220. const char* value) {
  221. return Filter(key, op, FieldValue::FromString(value));
  222. }
  223. core::FieldFilter Filter(absl::string_view key,
  224. absl::string_view op,
  225. int value) {
  226. return Filter(key, op, FieldValue::FromInteger(value));
  227. }
  228. core::FieldFilter Filter(absl::string_view key,
  229. absl::string_view op,
  230. double value) {
  231. return Filter(key, op, FieldValue::FromDouble(value));
  232. }
  233. core::Direction Direction(absl::string_view direction) {
  234. if (direction == "asc") {
  235. return core::Direction::Ascending;
  236. } else if (direction == "desc") {
  237. return core::Direction::Descending;
  238. } else {
  239. HARD_FAIL("Unknown direction: %s (use \"asc\" or \"desc\")", direction);
  240. }
  241. }
  242. core::OrderBy OrderBy(absl::string_view key, absl::string_view direction) {
  243. return core::OrderBy(Field(key), Direction(direction));
  244. }
  245. core::OrderBy OrderBy(model::FieldPath field_path, core::Direction direction) {
  246. return core::OrderBy(std::move(field_path), direction);
  247. }
  248. core::Query Query(absl::string_view path) {
  249. return core::Query(Resource(path));
  250. }
  251. core::Query CollectionGroupQuery(absl::string_view collection_id) {
  252. return core::Query(model::ResourcePath::Empty(),
  253. std::make_shared<const std::string>(collection_id));
  254. }
  255. model::SetMutation SetMutation(absl::string_view path,
  256. const model::FieldValue::Map& values) {
  257. return model::SetMutation(Key(path), model::ObjectValue::FromMap(values),
  258. model::Precondition::None());
  259. }
  260. model::PatchMutation PatchMutation(
  261. absl::string_view path,
  262. FieldValue::Map values,
  263. // TODO(rsgowman): Investigate changing update_mask to a set.
  264. std::vector<model::FieldPath> update_mask) {
  265. ObjectValue object_value = ObjectValue::Empty();
  266. std::set<FieldPath> field_mask_paths;
  267. for (const auto& kv : values) {
  268. FieldPath field_path = Field(kv.first);
  269. field_mask_paths.insert(field_path);
  270. const FieldValue& value = kv.second;
  271. if (!value.is_string() || value.string_value() != kDeleteSentinel) {
  272. object_value = object_value.Set(field_path, value);
  273. }
  274. }
  275. bool merge = !update_mask.empty();
  276. Precondition precondition =
  277. merge ? Precondition::None() : Precondition::Exists(true);
  278. FieldMask mask(
  279. merge ? std::set<FieldPath>(update_mask.begin(), update_mask.end())
  280. : field_mask_paths);
  281. return model::PatchMutation(Key(path), std::move(object_value),
  282. std::move(mask), precondition);
  283. }
  284. model::TransformMutation TransformMutation(
  285. absl::string_view key,
  286. std::vector<std::pair<std::string, TransformOperation>> transforms) {
  287. std::vector<FieldTransform> field_transforms;
  288. for (auto&& pair : transforms) {
  289. auto path = Field(std::move(pair.first));
  290. TransformOperation&& op_ptr = std::move(pair.second);
  291. FieldTransform transform(std::move(path), std::move(op_ptr));
  292. field_transforms.push_back(std::move(transform));
  293. }
  294. return model::TransformMutation(Key(key), std::move(field_transforms));
  295. }
  296. std::pair<std::string, TransformOperation> Increment(std::string field,
  297. FieldValue operand) {
  298. model::NumericIncrementTransform transform(std::move(operand));
  299. return std::pair<std::string, TransformOperation>(std::move(field),
  300. std::move(transform));
  301. }
  302. std::pair<std::string, TransformOperation> ArrayUnion(
  303. std::string field, std::vector<FieldValue> operands) {
  304. model::ArrayTransform transform(TransformOperation::Type::ArrayUnion,
  305. std::move(operands));
  306. return std::pair<std::string, TransformOperation>(std::move(field),
  307. std::move(transform));
  308. }
  309. model::DeleteMutation DeleteMutation(absl::string_view path) {
  310. return model::DeleteMutation(Key(path), Precondition::None());
  311. }
  312. model::VerifyMutation VerifyMutation(absl::string_view path, int64_t version) {
  313. return model::VerifyMutation(Key(path),
  314. Precondition::UpdateTime(Version(version)));
  315. }
  316. model::MutationResult MutationResult(int64_t version) {
  317. return model::MutationResult(Version(version), absl::nullopt);
  318. }
  319. nanopb::ByteString ResumeToken(int64_t snapshot_version) {
  320. if (snapshot_version == 0) {
  321. // TODO(rsgowman): The other platforms return null here, though I'm not sure
  322. // if they ever rely on that. I suspect it'd be sufficient to return '{}'.
  323. // But for now, we'll just abort() until we hit a test case that actually
  324. // makes use of this.
  325. HARD_FAIL("Unsupported snapshot version %s", snapshot_version);
  326. }
  327. std::string snapshot_string =
  328. std::string("snapshot-") + std::to_string(snapshot_version);
  329. return nanopb::ByteString(snapshot_string);
  330. }
  331. } // namespace testutil
  332. } // namespace firestore
  333. } // namespace firebase