PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Tests.Issues/RavenDB_1598_MappingComplexProperties.cs

https://github.com/barryhagan/ravendb
C# | 222 lines | 201 code | 16 blank | 5 comment | 1 complexity | f22381114c9686759106e5c3b2f7a14c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. // -----------------------------------------------------------------------
  2. // <copyright file="RavenDB_1598_MappingComplexProperties.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using Raven.Tests.Common;
  7. using Raven.Tests.Helpers;
  8. namespace Raven.Tests.Issues
  9. {
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Linq;
  14. using Raven.Client.Indexes;
  15. using Raven.Tests.Bundles.ScriptedIndexResults;
  16. using Xunit;
  17. public class RavenDB_1598_MappingComplexProperties : RavenTestBase
  18. {
  19. public class ResultTypes
  20. {
  21. public string Name { get; set; }
  22. public string NameJson { get; set; }
  23. public string Animal { get; set; }
  24. public string AnimalJson { get; set; }
  25. public string StringArray_ZeroItems { get; set; }
  26. public string StringArray_ZeroItemsJson { get; set; }
  27. public string StringArray_OneItem { get; set; }
  28. public string StringArray_OneItemJson { get; set; }
  29. public string StringArray_OneItem_First { get; set; }
  30. public string StringArray_TwoItems { get; set; }
  31. public string StringArray_TwoItemsJson { get; set; }
  32. public string StringArray_TwoItems_First { get; set; }
  33. public string ObjectArray_ZeroItems { get; set; }
  34. public string ObjectArray_ZeroItemsJson { get; set; }
  35. public string ObjectArray_OneItem { get; set; }
  36. public string ObjectArray_OneItemJson { get; set; }
  37. public string ObjectArray_OneItem_First { get; set; }
  38. public string ObjectArray_TwoItems { get; set; }
  39. public string ObjectArray_TwoItemsJson { get; set; }
  40. public string ObjectArray_TwoItems_First { get; set; }
  41. }
  42. public class AnimalMapNonsense_Index : AbstractIndexCreationTask<Animal, AnimalMapNonsense_Index.Result>
  43. {
  44. public class Result
  45. {
  46. public string Name { get; set; }
  47. public Animal Animal { get; set; }
  48. public string[] StringArray_ZeroItems { get; set; }
  49. public string[] StringArray_OneItem { get; set; }
  50. public string[] StringArray_TwoItems { get; set; }
  51. public Animal[] ObjectArray_ZeroItems { get; set; }
  52. public Animal[] ObjectArray_OneItem { get; set; }
  53. public Animal[] ObjectArray_TwoItems { get; set; }
  54. }
  55. public AnimalMapNonsense_Index()
  56. {
  57. Map = animals =>
  58. from animal in animals
  59. select new Result
  60. {
  61. Name = animal.Name,
  62. Animal = new Animal { Name = animal.Name, Type = animal.Type },
  63. StringArray_ZeroItems = new string[0],
  64. StringArray_OneItem = new[] { animal.Name },
  65. StringArray_TwoItems = new[] { animal.Name, animal.Name },
  66. ObjectArray_ZeroItems = new Animal[0],
  67. ObjectArray_OneItem = new[] { new Animal { Name = animal.Name, Type = animal.Type } },
  68. ObjectArray_TwoItems = new[] { new Animal { Name = animal.Name, Type = animal.Type }, new Animal { Name = animal.Name, Type = animal.Type } }
  69. };
  70. }
  71. }
  72. public class AnimalReduceNonsense_Index : AnimalMapNonsense_Index
  73. {
  74. public AnimalReduceNonsense_Index()
  75. {
  76. Reduce = animals => animals.GroupBy(_ => _.Name).Select(_ => new Result
  77. {
  78. Name = _.First().Name,
  79. Animal = _.First().Animal,
  80. StringArray_ZeroItems = _.First().StringArray_ZeroItems,
  81. StringArray_OneItem = _.First().StringArray_OneItem,
  82. StringArray_TwoItems = _.First().StringArray_TwoItems,
  83. ObjectArray_ZeroItems = _.First().ObjectArray_ZeroItems,
  84. ObjectArray_OneItem = _.First().ObjectArray_OneItem,
  85. ObjectArray_TwoItems = _.First().ObjectArray_TwoItems,
  86. });
  87. }
  88. }
  89. protected override void ModifyConfiguration(Database.Config.InMemoryRavenConfiguration configuration)
  90. {
  91. configuration.Settings["Raven/ActiveBundles"] = "ScriptedIndexResults";
  92. }
  93. [Fact]
  94. public void CheckTypesOnMap()
  95. {
  96. checkIndex(new AnimalMapNonsense_Index());
  97. }
  98. [Fact]
  99. public void CheckTypesOnReduce()
  100. {
  101. checkIndex(new AnimalReduceNonsense_Index());
  102. }
  103. private void checkIndex(AnimalMapNonsense_Index index)
  104. {
  105. using (var store = NewDocumentStore())
  106. {
  107. using (var s = store.OpenSession())
  108. {
  109. s.Store(new Raven.Abstractions.Data.ScriptedIndexResults
  110. {
  111. Id = Raven.Abstractions.Data.ScriptedIndexResults.IdPrefix + index.IndexName,
  112. IndexScript = @"
  113. var docId = 'ResultTypes/' + this.Name;
  114. var doc = {
  115. Name: Object.prototype.toString.call(this.Name),
  116. NameJson: JSON.stringify(this.Name),
  117. Animal: Object.prototype.toString.call( this.Animal),
  118. AnimalJson: JSON.stringify( this.Animal),
  119. StringArray_ZeroItems: Object.prototype.toString.call( this.StringArray_ZeroItems),
  120. StringArray_ZeroItemsJson: JSON.stringify( this.StringArray_ZeroItems),
  121. StringArray_OneItem: Object.prototype.toString.call( this.StringArray_OneItem),
  122. StringArray_OneItemJson: JSON.stringify( this.StringArray_OneItem),
  123. StringArray_OneItem_First: Object.prototype.toString.call( this.StringArray_OneItem[0]),
  124. StringArray_TwoItems: Object.prototype.toString.call( this.StringArray_TwoItems),
  125. StringArray_TwoItemsJson: JSON.stringify( this.StringArray_TwoItems),
  126. StringArray_TwoItems_First: Object.prototype.toString.call( this.StringArray_TwoItems[0]),
  127. ObjectArray_ZeroItems: Object.prototype.toString.call( this.ObjectArray_ZeroItems),
  128. ObjectArray_ZeroItemsJson: JSON.stringify( this.ObjectArray_ZeroItems),
  129. ObjectArray_OneItem: Object.prototype.toString.call( this.ObjectArray_OneItem),
  130. ObjectArray_OneItemJson: JSON.stringify( this.ObjectArray_OneItem),
  131. ObjectArray_OneItem_First: Object.prototype.toString.call( this.ObjectArray_OneItem[0]),
  132. ObjectArray_TwoItems: Object.prototype.toString.call( this.ObjectArray_TwoItems),
  133. ObjectArray_TwoItemsJson: JSON.stringify( this.ObjectArray_TwoItems),
  134. ObjectArray_TwoItems_First: Object.prototype.toString.call( this.ObjectArray_TwoItems[0])
  135. }
  136. PutDocument(docId, doc);",
  137. DeleteScript = @""
  138. });
  139. s.SaveChanges();
  140. }
  141. using (var s = store.OpenSession())
  142. {
  143. s.Store(new Animal
  144. {
  145. Name = "Arava",
  146. Type = "Dog"
  147. });
  148. s.SaveChanges();
  149. }
  150. index.Execute(store);
  151. WaitForIndexing(store, timeout: TimeSpan.FromSeconds(360));
  152. using (var s = store.OpenSession())
  153. {
  154. var actual = s.Load<ResultTypes>("ResultTypes/Arava");
  155. Assert.NotNull(actual);
  156. ResultTypes expected = new ResultTypes()
  157. {
  158. Name = "[object String]",
  159. Animal = "[object Object]",
  160. StringArray_ZeroItems = "[object Array]",
  161. StringArray_OneItem = "[object Array]",
  162. StringArray_OneItem_First = "[object String]",
  163. StringArray_TwoItems = "[object Array]",
  164. StringArray_TwoItems_First = "[object String]",
  165. ObjectArray_ZeroItems = "[object Array]",
  166. ObjectArray_OneItem = "[object Array]",
  167. ObjectArray_OneItem_First = "[object Object]",
  168. ObjectArray_TwoItems = "[object Array]",
  169. ObjectArray_TwoItems_First = "[object Object]",
  170. NameJson = @"""Arava""",
  171. AnimalJson = @"{""Name"":""Arava"",""Type"":""Dog""}",
  172. StringArray_ZeroItemsJson = @"[]",
  173. StringArray_OneItemJson = @"[""Arava""]",
  174. StringArray_TwoItemsJson = @"[""Arava"",""Arava""]",
  175. ObjectArray_ZeroItemsJson = @"[]",
  176. ObjectArray_OneItemJson = @"[{""Name"":""Arava"",""Type"":""Dog""}]",
  177. ObjectArray_TwoItemsJson = @"[{""Name"":""Arava"",""Type"":""Dog""},{""Name"":""Arava"",""Type"":""Dog""}]",
  178. };
  179. Console.WriteLine("Replaced \\\" with ' for better readability...");
  180. Console.WriteLine();
  181. List<string> failed = new List<string>();
  182. foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(typeof(ResultTypes)))
  183. {
  184. object actualP = property.GetValue(actual);
  185. object expectedP = property.GetValue(expected);
  186. var match = Equals(actualP, expectedP);
  187. if (!match) failed.Add(property.Name);
  188. if (expectedP is String) expectedP = ((string)expectedP).Replace("\\\"", "'");
  189. if (actualP is String) actualP = ((string)actualP).Replace("\\\"", "'");
  190. Console.WriteLine(property.Name.PadRight(30) + ": " + (match ? "OK " : "FAIL ") + "expected " + expectedP + ", was " + actualP);
  191. }
  192. Assert.True(failed.Count == 0, "Properties with type missmatch: " + String.Join(", ", failed));
  193. }
  194. }
  195. }
  196. }
  197. }