FieldMaskTreeTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System.Collections.Generic;
  33. using Google.Protobuf.Collections;
  34. using Google.Protobuf.TestProtos;
  35. using NUnit.Framework;
  36. using Google.Protobuf.WellKnownTypes;
  37. namespace Google.Protobuf
  38. {
  39. public class FieldMaskTreeTest
  40. {
  41. [Test]
  42. public void AddFieldPath()
  43. {
  44. FieldMaskTree tree = new FieldMaskTree();
  45. RepeatedField<string> paths = tree.ToFieldMask().Paths;
  46. Assert.AreEqual(0, paths.Count);
  47. tree.AddFieldPath("");
  48. paths = tree.ToFieldMask().Paths;
  49. Assert.AreEqual(1, paths.Count);
  50. Assert.Contains("", paths);
  51. // New branch.
  52. tree.AddFieldPath("foo");
  53. paths = tree.ToFieldMask().Paths;
  54. Assert.AreEqual(2, paths.Count);
  55. Assert.Contains("foo", paths);
  56. // Redundant path.
  57. tree.AddFieldPath("foo");
  58. paths = tree.ToFieldMask().Paths;
  59. Assert.AreEqual(2, paths.Count);
  60. // New branch.
  61. tree.AddFieldPath("bar.baz");
  62. paths = tree.ToFieldMask().Paths;
  63. Assert.AreEqual(3, paths.Count);
  64. Assert.Contains("bar.baz", paths);
  65. // Redundant sub-path.
  66. tree.AddFieldPath("foo.bar");
  67. paths = tree.ToFieldMask().Paths;
  68. Assert.AreEqual(3, paths.Count);
  69. // New branch from a non-root node.
  70. tree.AddFieldPath("bar.quz");
  71. paths = tree.ToFieldMask().Paths;
  72. Assert.AreEqual(4, paths.Count);
  73. Assert.Contains("bar.quz", paths);
  74. // A path that matches several existing sub-paths.
  75. tree.AddFieldPath("bar");
  76. paths = tree.ToFieldMask().Paths;
  77. Assert.AreEqual(3, paths.Count);
  78. Assert.Contains("foo", paths);
  79. Assert.Contains("bar", paths);
  80. }
  81. [Test]
  82. public void MergeFromFieldMask()
  83. {
  84. FieldMaskTree tree = new FieldMaskTree();
  85. tree.MergeFromFieldMask(new FieldMask
  86. {
  87. Paths = {"foo", "bar.baz", "bar.quz"}
  88. });
  89. RepeatedField<string> paths = tree.ToFieldMask().Paths;
  90. Assert.AreEqual(3, paths.Count);
  91. Assert.Contains("foo", paths);
  92. Assert.Contains("bar.baz", paths);
  93. Assert.Contains("bar.quz", paths);
  94. tree.MergeFromFieldMask(new FieldMask
  95. {
  96. Paths = {"foo.bar", "bar"}
  97. });
  98. paths = tree.ToFieldMask().Paths;
  99. Assert.AreEqual(2, paths.Count);
  100. Assert.Contains("foo", paths);
  101. Assert.Contains("bar", paths);
  102. }
  103. [Test]
  104. public void IntersectFieldPath()
  105. {
  106. FieldMaskTree tree = new FieldMaskTree();
  107. FieldMaskTree result = new FieldMaskTree();
  108. tree.MergeFromFieldMask(new FieldMask
  109. {
  110. Paths = {"foo", "bar.baz", "bar.quz"}
  111. });
  112. // Empty path.
  113. tree.IntersectFieldPath("", result);
  114. RepeatedField<string> paths = result.ToFieldMask().Paths;
  115. Assert.AreEqual(0, paths.Count);
  116. // Non-exist path.
  117. tree.IntersectFieldPath("quz", result);
  118. paths = result.ToFieldMask().Paths;
  119. Assert.AreEqual(0, paths.Count);
  120. // Sub-path of an existing leaf.
  121. tree.IntersectFieldPath("foo.bar", result);
  122. paths = result.ToFieldMask().Paths;
  123. Assert.AreEqual(1, paths.Count);
  124. Assert.Contains("foo.bar", paths);
  125. // Match an existing leaf node.
  126. tree.IntersectFieldPath("foo", result);
  127. paths = result.ToFieldMask().Paths;
  128. Assert.AreEqual(1, paths.Count);
  129. Assert.Contains("foo", paths);
  130. // Non-exist path.
  131. tree.IntersectFieldPath("bar.foo", result);
  132. paths = result.ToFieldMask().Paths;
  133. Assert.AreEqual(1, paths.Count);
  134. Assert.Contains("foo", paths);
  135. // Match a non-leaf node.
  136. tree.IntersectFieldPath("bar", result);
  137. paths = result.ToFieldMask().Paths;
  138. Assert.AreEqual(3, paths.Count);
  139. Assert.Contains("foo", paths);
  140. Assert.Contains("bar.baz", paths);
  141. Assert.Contains("bar.quz", paths);
  142. }
  143. private void Merge(FieldMaskTree tree, IMessage source, IMessage destination, FieldMask.MergeOptions options, bool useDynamicMessage)
  144. {
  145. if (useDynamicMessage)
  146. {
  147. var newSource = source.Descriptor.Parser.CreateTemplate();
  148. newSource.MergeFrom(source.ToByteString());
  149. var newDestination = source.Descriptor.Parser.CreateTemplate();
  150. newDestination.MergeFrom(destination.ToByteString());
  151. tree.Merge(newSource, newDestination, options);
  152. // Clear before merging:
  153. foreach (var fieldDescriptor in destination.Descriptor.Fields.InFieldNumberOrder())
  154. {
  155. fieldDescriptor.Accessor.Clear(destination);
  156. }
  157. destination.MergeFrom(newDestination.ToByteString());
  158. }
  159. else
  160. {
  161. tree.Merge(source, destination, options);
  162. }
  163. }
  164. [Test]
  165. [TestCase(true)]
  166. [TestCase(false)]
  167. public void Merge(bool useDynamicMessage)
  168. {
  169. TestAllTypes value = new TestAllTypes
  170. {
  171. SingleInt32 = 1234,
  172. SingleNestedMessage = new TestAllTypes.Types.NestedMessage {Bb = 5678},
  173. RepeatedInt32 = {4321},
  174. RepeatedNestedMessage = {new TestAllTypes.Types.NestedMessage {Bb = 8765}}
  175. };
  176. NestedTestAllTypes source = new NestedTestAllTypes
  177. {
  178. Payload = value,
  179. Child = new NestedTestAllTypes {Payload = value}
  180. };
  181. // Now we have a message source with the following structure:
  182. // [root] -+- payload -+- single_int32
  183. // | +- single_nested_message
  184. // | +- repeated_int32
  185. // | +- repeated_nested_message
  186. // |
  187. // +- child --- payload -+- single_int32
  188. // +- single_nested_message
  189. // +- repeated_int32
  190. // +- repeated_nested_message
  191. FieldMask.MergeOptions options = new FieldMask.MergeOptions();
  192. // Test merging each individual field.
  193. NestedTestAllTypes destination = new NestedTestAllTypes();
  194. Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"),
  195. source, destination, options, useDynamicMessage);
  196. NestedTestAllTypes expected = new NestedTestAllTypes
  197. {
  198. Payload = new TestAllTypes
  199. {
  200. SingleInt32 = 1234
  201. }
  202. };
  203. Assert.AreEqual(expected, destination);
  204. destination = new NestedTestAllTypes();
  205. Merge(new FieldMaskTree().AddFieldPath("payload.single_nested_message"),
  206. source, destination, options, useDynamicMessage);
  207. expected = new NestedTestAllTypes
  208. {
  209. Payload = new TestAllTypes
  210. {
  211. SingleNestedMessage = new TestAllTypes.Types.NestedMessage {Bb = 5678}
  212. }
  213. };
  214. Assert.AreEqual(expected, destination);
  215. destination = new NestedTestAllTypes();
  216. Merge(new FieldMaskTree().AddFieldPath("payload.repeated_int32"),
  217. source, destination, options, useDynamicMessage);
  218. expected = new NestedTestAllTypes
  219. {
  220. Payload = new TestAllTypes
  221. {
  222. RepeatedInt32 = {4321}
  223. }
  224. };
  225. Assert.AreEqual(expected, destination);
  226. destination = new NestedTestAllTypes();
  227. Merge(new FieldMaskTree().AddFieldPath("payload.repeated_nested_message"),
  228. source, destination, options, useDynamicMessage);
  229. expected = new NestedTestAllTypes
  230. {
  231. Payload = new TestAllTypes
  232. {
  233. RepeatedNestedMessage = {new TestAllTypes.Types.NestedMessage {Bb = 8765}}
  234. }
  235. };
  236. Assert.AreEqual(expected, destination);
  237. destination = new NestedTestAllTypes();
  238. Merge(
  239. new FieldMaskTree().AddFieldPath("child.payload.single_int32"),
  240. source,
  241. destination,
  242. options,
  243. useDynamicMessage);
  244. expected = new NestedTestAllTypes
  245. {
  246. Child = new NestedTestAllTypes
  247. {
  248. Payload = new TestAllTypes
  249. {
  250. SingleInt32 = 1234
  251. }
  252. }
  253. };
  254. Assert.AreEqual(expected, destination);
  255. destination = new NestedTestAllTypes();
  256. Merge(
  257. new FieldMaskTree().AddFieldPath("child.payload.single_nested_message"),
  258. source,
  259. destination,
  260. options,
  261. useDynamicMessage);
  262. expected = new NestedTestAllTypes
  263. {
  264. Child = new NestedTestAllTypes
  265. {
  266. Payload = new TestAllTypes
  267. {
  268. SingleNestedMessage = new TestAllTypes.Types.NestedMessage {Bb = 5678}
  269. }
  270. }
  271. };
  272. Assert.AreEqual(expected, destination);
  273. destination = new NestedTestAllTypes();
  274. Merge(new FieldMaskTree().AddFieldPath("child.payload.repeated_int32"),
  275. source, destination, options, useDynamicMessage);
  276. expected = new NestedTestAllTypes
  277. {
  278. Child = new NestedTestAllTypes
  279. {
  280. Payload = new TestAllTypes
  281. {
  282. RepeatedInt32 = {4321}
  283. }
  284. }
  285. };
  286. Assert.AreEqual(expected, destination);
  287. destination = new NestedTestAllTypes();
  288. Merge(new FieldMaskTree().AddFieldPath("child.payload.repeated_nested_message"),
  289. source, destination, options, useDynamicMessage);
  290. expected = new NestedTestAllTypes
  291. {
  292. Child = new NestedTestAllTypes
  293. {
  294. Payload = new TestAllTypes
  295. {
  296. RepeatedNestedMessage = {new TestAllTypes.Types.NestedMessage {Bb = 8765}}
  297. }
  298. }
  299. };
  300. Assert.AreEqual(expected, destination);
  301. destination = new NestedTestAllTypes();
  302. Merge(new FieldMaskTree().AddFieldPath("child").AddFieldPath("payload"),
  303. source, destination, options, useDynamicMessage);
  304. Assert.AreEqual(source, destination);
  305. // Test repeated options.
  306. destination = new NestedTestAllTypes
  307. {
  308. Payload = new TestAllTypes
  309. {
  310. RepeatedInt32 = { 1000 }
  311. }
  312. };
  313. Merge(new FieldMaskTree().AddFieldPath("payload.repeated_int32"),
  314. source, destination, options, useDynamicMessage);
  315. // Default behavior is to append repeated fields.
  316. Assert.AreEqual(2, destination.Payload.RepeatedInt32.Count);
  317. Assert.AreEqual(1000, destination.Payload.RepeatedInt32[0]);
  318. Assert.AreEqual(4321, destination.Payload.RepeatedInt32[1]);
  319. // Change to replace repeated fields.
  320. options.ReplaceRepeatedFields = true;
  321. Merge(new FieldMaskTree().AddFieldPath("payload.repeated_int32"),
  322. source, destination, options, useDynamicMessage);
  323. Assert.AreEqual(1, destination.Payload.RepeatedInt32.Count);
  324. Assert.AreEqual(4321, destination.Payload.RepeatedInt32[0]);
  325. // Test message options.
  326. destination = new NestedTestAllTypes
  327. {
  328. Payload = new TestAllTypes
  329. {
  330. SingleInt32 = 1000,
  331. SingleUint32 = 2000
  332. }
  333. };
  334. Merge(new FieldMaskTree().AddFieldPath("payload"),
  335. source, destination, options, useDynamicMessage);
  336. // Default behavior is to merge message fields.
  337. Assert.AreEqual(1234, destination.Payload.SingleInt32);
  338. Assert.AreEqual(2000, destination.Payload.SingleUint32);
  339. // Test merging unset message fields.
  340. NestedTestAllTypes clearedSource = source.Clone();
  341. clearedSource.Payload = null;
  342. destination = new NestedTestAllTypes();
  343. Merge(new FieldMaskTree().AddFieldPath("payload"),
  344. clearedSource, destination, options, useDynamicMessage);
  345. Assert.IsNull(destination.Payload);
  346. // Skip a message field if they are unset in both source and target.
  347. destination = new NestedTestAllTypes();
  348. Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"),
  349. clearedSource, destination, options, useDynamicMessage);
  350. Assert.IsNull(destination.Payload);
  351. // Change to replace message fields.
  352. options.ReplaceMessageFields = true;
  353. destination = new NestedTestAllTypes
  354. {
  355. Payload = new TestAllTypes
  356. {
  357. SingleInt32 = 1000,
  358. SingleUint32 = 2000
  359. }
  360. };
  361. Merge(new FieldMaskTree().AddFieldPath("payload"),
  362. source, destination, options, useDynamicMessage);
  363. Assert.AreEqual(1234, destination.Payload.SingleInt32);
  364. Assert.AreEqual(0, destination.Payload.SingleUint32);
  365. // Test merging unset message fields.
  366. destination = new NestedTestAllTypes
  367. {
  368. Payload = new TestAllTypes
  369. {
  370. SingleInt32 = 1000,
  371. SingleUint32 = 2000
  372. }
  373. };
  374. Merge(new FieldMaskTree().AddFieldPath("payload"),
  375. clearedSource, destination, options, useDynamicMessage);
  376. Assert.IsNull(destination.Payload);
  377. // Test merging unset primitive fields.
  378. destination = source.Clone();
  379. destination.Payload.SingleInt32 = 0;
  380. NestedTestAllTypes sourceWithPayloadInt32Unset = destination;
  381. destination = source.Clone();
  382. Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"),
  383. sourceWithPayloadInt32Unset, destination, options, useDynamicMessage);
  384. Assert.AreEqual(0, destination.Payload.SingleInt32);
  385. // Change to clear unset primitive fields.
  386. options.ReplacePrimitiveFields = true;
  387. destination = source.Clone();
  388. Merge(new FieldMaskTree().AddFieldPath("payload.single_int32"),
  389. sourceWithPayloadInt32Unset, destination, options, useDynamicMessage);
  390. Assert.IsNotNull(destination.Payload);
  391. }
  392. }
  393. }