/test/SlowTests/Bugs/Indexing/DynamicFields.cs

https://github.com/fitzchak/ravendb · C# · 292 lines · 252 code · 40 blank · 0 comment · 0 complexity · 4b8caa6c73544ce2d71bd924910b9a8c MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FastTests;
  5. using Lucene.Net.Documents;
  6. using Raven.Client.Documents.Indexes;
  7. using Xunit;
  8. namespace SlowTests.Bugs.Indexing
  9. {
  10. public class DynamicFields : RavenTestBase
  11. {
  12. private class Product
  13. {
  14. public string Id { get; set; }
  15. public List<Attribute> Attributes { get; set; }
  16. }
  17. private class Attribute
  18. {
  19. public string Name { get; set; }
  20. public string Value { get; set; }
  21. public decimal NumericValue { get; set; }
  22. public int IntValue { get; set; }
  23. }
  24. private class Product_ByAttribute : AbstractIndexCreationTask<Product>
  25. {
  26. public Product_ByAttribute()
  27. {
  28. Map = products =>
  29. from p in products
  30. select new
  31. {
  32. _ = p.Attributes.Select(attribute => new Field(attribute.Name, attribute.Value, Field.Store.NO, Field.Index.ANALYZED))
  33. };
  34. }
  35. }
  36. private class Product_ByAttributeStored : AbstractIndexCreationTask<Product>
  37. {
  38. public Product_ByAttributeStored()
  39. {
  40. Map = products =>
  41. from p in products
  42. select new
  43. {
  44. _ = p.Attributes.Select(attribute => new Field(attribute.Name, attribute.Value, Field.Store.YES, Field.Index.ANALYZED))
  45. };
  46. }
  47. }
  48. private class Product_ByNumericAttribute : AbstractIndexCreationTask<Product>
  49. {
  50. public Product_ByNumericAttribute()
  51. {
  52. Map = products =>
  53. from p in products
  54. select new
  55. {
  56. _ = p.Attributes.Select(attribute => new NumericField(attribute.Name + "_D_Range", Field.Store.NO, true).SetDoubleValue((double)attribute.NumericValue))
  57. };
  58. }
  59. }
  60. private class Product_ByNumericAttributeUsingField : AbstractIndexCreationTask<Product>
  61. {
  62. public Product_ByNumericAttributeUsingField()
  63. {
  64. Map = products =>
  65. from p in products
  66. select new
  67. {
  68. _ = p.Attributes.Select(attribute => new Field(attribute.Name, attribute.NumericValue.ToString("#.#"), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS))
  69. };
  70. }
  71. }
  72. private class Product_ByIntAttribute : AbstractIndexCreationTask<Product>
  73. {
  74. public Product_ByIntAttribute()
  75. {
  76. Map = products =>
  77. from p in products
  78. select new
  79. {
  80. _ = p.Attributes.Select(attribute => new NumericField(attribute.Name + "_L_Range", Field.Store.NO, true).SetLongValue(attribute.IntValue))
  81. };
  82. }
  83. }
  84. [Fact]
  85. public void CanCreateCompletelyDynamicFields()
  86. {
  87. using (var store = GetDocumentStore())
  88. {
  89. new Product_ByAttribute().Execute(store);
  90. using (var session = store.OpenSession())
  91. {
  92. session.Store(new Product
  93. {
  94. Attributes = new List<Attribute>
  95. {
  96. new Attribute{Name = "Color", Value = "Red"}
  97. }
  98. });
  99. session.SaveChanges();
  100. }
  101. using (var session = store.OpenSession())
  102. {
  103. var products = session.Advanced.DocumentQuery<Product>("Product/ByAttribute")
  104. .WhereEquals("Color", "Red")
  105. .WaitForNonStaleResults(TimeSpan.FromMinutes(3))
  106. .ToList();
  107. Assert.NotEmpty(products);
  108. }
  109. }
  110. }
  111. [Fact]
  112. public void CanCreateCompletelyDynamicFieldsWithProjection()
  113. {
  114. using (var store = GetDocumentStore())
  115. {
  116. new Product_ByAttributeStored().Execute(store);
  117. using (var session = store.OpenSession())
  118. {
  119. session.Store(new Product
  120. {
  121. Attributes = new List<Attribute>
  122. {
  123. new Attribute{Name = "Color", Value = "Red"}
  124. }
  125. });
  126. session.SaveChanges();
  127. }
  128. using (var session = store.OpenSession())
  129. {
  130. var products = session.Advanced
  131. .RawQuery<dynamic>(@"from index 'Product/ByAttributeStored' as product
  132. where product.Color='Red'
  133. select {
  134. Color: product.Color
  135. }")
  136. .WaitForNonStaleResults(TimeSpan.FromMinutes(3))
  137. .ToList();
  138. Assert.NotEmpty(products);
  139. Assert.Equal("Red",products.First().Color.ToString());
  140. }
  141. }
  142. }
  143. [Fact]
  144. public void CanCreateCompletelyDynamicNumericFields()
  145. {
  146. using (var store = GetDocumentStore())
  147. {
  148. new Product_ByNumericAttribute().Execute(store);
  149. using (var session = store.OpenSession())
  150. {
  151. session.Store(new Product
  152. {
  153. Attributes = new List<Attribute>
  154. {
  155. new Attribute{Name = "Color", Value = "Red", NumericValue = 30}
  156. }
  157. });
  158. session.SaveChanges();
  159. }
  160. using (var session = store.OpenSession())
  161. {
  162. var products = session.Advanced.DocumentQuery<Product, Product_ByNumericAttribute>()
  163. .WhereGreaterThan("Color", 20d)
  164. .WaitForNonStaleResults(TimeSpan.FromMinutes(3))
  165. .ToList();
  166. Assert.NotEmpty(products);
  167. }
  168. }
  169. }
  170. [Fact]
  171. public void CanCreateCompletelyDynamicNumericFieldsUsingField()
  172. {
  173. using (var store = GetDocumentStore())
  174. {
  175. new Product_ByNumericAttributeUsingField().Execute(store);
  176. using (var session = store.OpenSession())
  177. {
  178. session.Store(new Product
  179. {
  180. Attributes = new List<Attribute>
  181. {
  182. new Attribute{Name = "Color", Value = "Red", NumericValue = 30}
  183. }
  184. });
  185. session.SaveChanges();
  186. }
  187. using (var session = store.OpenSession())
  188. {
  189. var products = session.Advanced.DocumentQuery<Product, Product_ByNumericAttributeUsingField>()
  190. .WhereEquals("Color", "30")
  191. .WaitForNonStaleResults(TimeSpan.FromMinutes(3))
  192. .ToList();
  193. Assert.NotEmpty(products);
  194. }
  195. }
  196. }
  197. [Fact]
  198. public void CanQueryCompletelyDynamicNumericFieldsWithNegativeRangeUsingInt()
  199. {
  200. using (var store = GetDocumentStore())
  201. {
  202. new Product_ByIntAttribute().Execute(store);
  203. using (var session = store.OpenSession())
  204. {
  205. session.Store(new Product
  206. {
  207. Attributes = new List<Attribute>
  208. {
  209. new Attribute{Name = "Color", Value = "Red", IntValue = 30}
  210. }
  211. });
  212. session.SaveChanges();
  213. }
  214. using (var session = store.OpenSession())
  215. {
  216. var products = session.Advanced.DocumentQuery<Product, Product_ByIntAttribute>()
  217. .WhereGreaterThan("Color", -1)
  218. .WaitForNonStaleResults(TimeSpan.FromMinutes(3))
  219. .ToList();
  220. Assert.NotEmpty(products);
  221. }
  222. }
  223. }
  224. [Fact]
  225. public void CanQueryCompletelyDynamicNumericFieldsWithNegativeRange()
  226. {
  227. using (var store = GetDocumentStore())
  228. {
  229. new Product_ByNumericAttribute().Execute(store);
  230. using (var session = store.OpenSession())
  231. {
  232. session.Store(new Product
  233. {
  234. Attributes = new List<Attribute>
  235. {
  236. new Attribute{Name = "Color", Value = "Red", NumericValue = 30}
  237. }
  238. });
  239. session.SaveChanges();
  240. }
  241. using (var session = store.OpenSession())
  242. {
  243. var products = session.Advanced.DocumentQuery<Product, Product_ByNumericAttribute>()
  244. .WhereGreaterThan("Color", -1d)
  245. .WaitForNonStaleResults(TimeSpan.FromMinutes(3))
  246. .ToList();
  247. Assert.NotEmpty(products);
  248. }
  249. }
  250. }
  251. }
  252. }