testutil.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #ifndef FIRESTORE_CORE_TEST_UNIT_TESTUTIL_TESTUTIL_H_
  17. #define FIRESTORE_CORE_TEST_UNIT_TESTUTIL_TESTUTIL_H_
  18. #include <cstdint>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include "Firestore/core/src/core/core_fwd.h"
  24. #include "Firestore/core/src/model/field_value.h"
  25. #include "Firestore/core/src/model/model_fwd.h"
  26. #include "absl/strings/string_view.h"
  27. namespace firebase {
  28. class Timestamp;
  29. namespace firestore {
  30. class GeoPoint;
  31. namespace nanopb {
  32. class ByteString;
  33. } // namespace nanopb
  34. namespace testutil {
  35. namespace details {
  36. model::FieldValue BlobValue(std::initializer_list<uint8_t>);
  37. } // namespace details
  38. // Convenience methods for creating instances for tests.
  39. nanopb::ByteString Bytes(std::initializer_list<uint8_t>);
  40. model::FieldValue Value(std::nullptr_t);
  41. /**
  42. * A type definition that evaluates to type V only if T is exactly type `bool`.
  43. */
  44. template <typename T, typename V>
  45. using EnableForExactlyBool =
  46. typename std::enable_if<std::is_same<bool, T>::value, V>::type;
  47. /**
  48. * A type definition that evaluates to type V only if T is an integral type but
  49. * not `bool`.
  50. */
  51. template <typename T, typename V>
  52. using EnableForInts = typename std::enable_if<std::is_integral<T>::value &&
  53. !std::is_same<bool, T>::value,
  54. V>::type;
  55. /**
  56. * Creates a boolean FieldValue.
  57. *
  58. * @tparam T A type that must be exactly bool. Any T that is not bool causes
  59. * this declaration to be disabled.
  60. * @param bool_value A boolean value that disallows implicit conversions.
  61. */
  62. template <typename T>
  63. EnableForExactlyBool<T, model::FieldValue> Value(T bool_value) {
  64. return model::FieldValue::FromBoolean(bool_value);
  65. }
  66. /**
  67. * Creates an integer FieldValue.
  68. *
  69. * This is defined as a template to capture all integer literals. Just defining
  70. * this as taking int64_t would make integer literals ambiguous because int64_t
  71. * and double are equally good choices according to the standard.
  72. *
  73. * @tparam T Any integral type (but not bool). Types larger than int64_t will
  74. * likely generate a warning.
  75. * @param value An integer value.
  76. */
  77. template <typename T>
  78. EnableForInts<T, model::FieldValue> Value(T value) {
  79. return model::FieldValue::FromInteger(value);
  80. }
  81. model::FieldValue Value(double value);
  82. model::FieldValue Value(Timestamp value);
  83. model::FieldValue Value(const char* value);
  84. model::FieldValue Value(const std::string& value);
  85. model::FieldValue Value(const GeoPoint& value);
  86. template <typename... Ints>
  87. model::FieldValue BlobValue(Ints... octets) {
  88. return details::BlobValue({static_cast<uint8_t>(octets)...});
  89. }
  90. // This overload allows Object() to appear as a value (along with any explicitly
  91. // constructed FieldValues).
  92. model::FieldValue Value(const model::FieldValue& value);
  93. model::FieldValue Value(const model::ObjectValue& value);
  94. model::FieldValue Value(const model::FieldValue::Map& value);
  95. namespace details {
  96. /**
  97. * Recursive base case for AddPairs, below. Returns the map.
  98. */
  99. inline model::FieldValue::Map AddPairs(const model::FieldValue::Map& prior) {
  100. return prior;
  101. }
  102. /**
  103. * Inserts the given key-value pair into the map, and then recursively calls
  104. * AddPairs to add any remaining arguments.
  105. *
  106. * @param prior A map into which the values should be inserted.
  107. * @param key The key naming the field to insert.
  108. * @param value A value to wrap with a call to Value(), above.
  109. * @param rest Any remaining arguments
  110. *
  111. * @return The resulting map.
  112. */
  113. template <typename ValueType, typename... Args>
  114. model::FieldValue::Map AddPairs(const model::FieldValue::Map& prior,
  115. const std::string& key,
  116. const ValueType& value,
  117. Args... rest) {
  118. return AddPairs(prior.insert(key, Value(value)), rest...);
  119. }
  120. /**
  121. * Creates an immutable sorted map from the given key/value pairs.
  122. *
  123. * @param key_value_pairs Alternating strings naming keys and values that can
  124. * be passed to Value().
  125. */
  126. template <typename... Args>
  127. model::FieldValue::Map MakeMap(Args... key_value_pairs) {
  128. return AddPairs(model::FieldValue::Map(), key_value_pairs...);
  129. }
  130. } // namespace details
  131. template <typename... Args>
  132. model::FieldValue Array(Args... values) {
  133. std::vector<model::FieldValue> contents{Value(values)...};
  134. return model::FieldValue::FromArray(std::move(contents));
  135. }
  136. /** Wraps an immutable sorted map into an ObjectValue. */
  137. model::ObjectValue WrapObject(const model::FieldValue::Map& value);
  138. /**
  139. * Creates an ObjectValue from the given key/value pairs.
  140. *
  141. * @param key_value_pairs Alternating strings naming keys and values that can
  142. * be passed to Value().
  143. */
  144. template <typename... Args>
  145. model::ObjectValue WrapObject(Args... key_value_pairs) {
  146. return WrapObject(details::MakeMap(key_value_pairs...));
  147. }
  148. /**
  149. * Creates an ObjectValue from the given key/value pairs with Type::Object.
  150. *
  151. * @param key_value_pairs Alternating strings naming keys and values that can
  152. * be passed to Value().
  153. */
  154. template <typename... Args>
  155. model::FieldValue::Map Map(Args... key_value_pairs) {
  156. return details::MakeMap(key_value_pairs...);
  157. }
  158. model::DocumentKey Key(absl::string_view path);
  159. model::FieldPath Field(absl::string_view field);
  160. model::DatabaseId DbId(std::string project = "project/(default)");
  161. model::FieldValue Ref(std::string project, absl::string_view path);
  162. model::ResourcePath Resource(absl::string_view field);
  163. /**
  164. * Creates a snapshot version from the given version timestamp.
  165. *
  166. * @param version a timestamp in microseconds since the epoch.
  167. */
  168. model::SnapshotVersion Version(int64_t version);
  169. model::Document Doc(
  170. absl::string_view key,
  171. int64_t version = 0,
  172. const model::FieldValue::Map& data = model::FieldValue::Map());
  173. model::Document Doc(absl::string_view key,
  174. int64_t version,
  175. const model::FieldValue::Map& data,
  176. model::DocumentState document_state);
  177. model::Document Doc(absl::string_view key,
  178. int64_t version,
  179. const model::FieldValue& data);
  180. model::Document Doc(absl::string_view key,
  181. int64_t version,
  182. const model::FieldValue& data,
  183. model::DocumentState document_state);
  184. /** A convenience method for creating deleted docs for tests. */
  185. model::NoDocument DeletedDoc(absl::string_view key,
  186. int64_t version = 0,
  187. bool has_committed_mutations = false);
  188. /** A convenience method for creating deleted docs for tests. */
  189. model::NoDocument DeletedDoc(model::DocumentKey key,
  190. int64_t version = 0,
  191. bool has_committed_mutations = false);
  192. /** A convenience method for creating unknown docs for tests. */
  193. model::UnknownDocument UnknownDoc(absl::string_view key, int64_t version);
  194. /**
  195. * Creates a DocumentComparator that will compare Documents by the given
  196. * field_path string then by key.
  197. */
  198. model::DocumentComparator DocComparator(absl::string_view field_path);
  199. /**
  200. * Creates a DocumentSet based on the given comparator, initially containing the
  201. * given documents.
  202. */
  203. model::DocumentSet DocSet(model::DocumentComparator comp,
  204. std::vector<model::Document> docs);
  205. core::FieldFilter Filter(absl::string_view key,
  206. absl::string_view op,
  207. model::FieldValue value);
  208. core::FieldFilter Filter(absl::string_view key,
  209. absl::string_view op,
  210. model::FieldValue::Map value);
  211. core::FieldFilter Filter(absl::string_view key,
  212. absl::string_view op,
  213. std::nullptr_t);
  214. core::FieldFilter Filter(absl::string_view key,
  215. absl::string_view op,
  216. const char* value);
  217. template <typename T>
  218. EnableForExactlyBool<T, core::FieldFilter> Filter(absl::string_view key,
  219. absl::string_view op,
  220. T value) {
  221. return Filter(key, op, Value(value));
  222. }
  223. core::FieldFilter Filter(absl::string_view key,
  224. absl::string_view op,
  225. int value);
  226. core::FieldFilter Filter(absl::string_view key,
  227. absl::string_view op,
  228. double value);
  229. core::Direction Direction(absl::string_view direction);
  230. core::OrderBy OrderBy(absl::string_view key,
  231. absl::string_view direction = "asc");
  232. core::OrderBy OrderBy(model::FieldPath field_path, core::Direction direction);
  233. core::Query Query(absl::string_view path);
  234. core::Query CollectionGroupQuery(absl::string_view collection_id);
  235. model::SetMutation SetMutation(
  236. absl::string_view path,
  237. const model::FieldValue::Map& values = model::FieldValue::Map());
  238. model::PatchMutation PatchMutation(
  239. absl::string_view path,
  240. model::FieldValue::Map values = model::FieldValue::Map(),
  241. std::vector<model::FieldPath> update_mask = {});
  242. model::TransformMutation TransformMutation(
  243. absl::string_view path,
  244. std::vector<std::pair<std::string, model::TransformOperation>> transforms);
  245. /**
  246. * Creates a pair of field name, TransformOperation that represents a numeric
  247. * increment on the given field, suitable for passing to TransformMutation,
  248. * above.
  249. */
  250. std::pair<std::string, model::TransformOperation> Increment(
  251. std::string field, model::FieldValue operand);
  252. /**
  253. * Creates a pair of field name, TransformOperation that represents an array
  254. * union on the given field, suitable for passing to TransformMutation,
  255. * above.
  256. */
  257. std::pair<std::string, model::TransformOperation> ArrayUnion(
  258. std::string field, std::vector<model::FieldValue> operands);
  259. model::DeleteMutation DeleteMutation(absl::string_view path);
  260. model::VerifyMutation VerifyMutation(absl::string_view path, int64_t version);
  261. model::MutationResult MutationResult(int64_t version);
  262. nanopb::ByteString ResumeToken(int64_t snapshot_version);
  263. template <typename T, typename... Ts>
  264. std::vector<T> Vector(T&& arg1, Ts&&... args) {
  265. return {std::forward<T>(arg1), std::forward<Ts>(args)...};
  266. }
  267. // Degenerate case to end recursion of `MoveIntoVector`.
  268. template <typename T>
  269. void MoveIntoVector(std::vector<std::unique_ptr<T>>*) {
  270. }
  271. template <typename T, typename Head, typename... Tail>
  272. void MoveIntoVector(std::vector<std::unique_ptr<T>>* result,
  273. Head head,
  274. Tail... tail) {
  275. result->push_back(std::move(head));
  276. MoveIntoVector(result, std::move(tail)...);
  277. }
  278. // Works around the fact that move-only types (in this case, `unique_ptr`) don't
  279. // work with `initializer_list`. Desired (doesn't work):
  280. //
  281. // std::unique_ptr<int> x, y;
  282. // std::vector<std::unique_ptr>> foo{std::move(x), std::move(y)};
  283. //
  284. // Actual:
  285. //
  286. // std::unique_ptr<int> x, y;
  287. // std::vector<std::unique_ptr<int>> foo = Changes(std::move(x),
  288. // std::move(y));
  289. template <typename T, typename... Elems>
  290. std::vector<std::unique_ptr<T>> VectorOfUniquePtrs(Elems... elems) {
  291. std::vector<std::unique_ptr<T>> result;
  292. MoveIntoVector<T>(&result, std::move(elems)...);
  293. return result;
  294. }
  295. } // namespace testutil
  296. } // namespace firestore
  297. } // namespace firebase
  298. #endif // FIRESTORE_CORE_TEST_UNIT_TESTUTIL_TESTUTIL_H_