PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/SlowTests/Issues/RavenDB-13695.cs

https://github.com/arekpalinski/ravendb
C# | 460 lines | 385 code | 69 blank | 6 comment | 7 complexity | 22694dd54a40a86e393ee594188683aa MD5 | raw file
Possible License(s): GPL-3.0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using FastTests;
  4. using Raven.Client.Documents.Indexes;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. namespace SlowTests.Issues
  8. {
  9. public class RavenDB_13695 : RavenTestBase
  10. {
  11. public RavenDB_13695(ITestOutputHelper output) : base(output)
  12. {
  13. }
  14. private class NewsDocument
  15. {
  16. public List<string> AuthorNames { get; set; }
  17. }
  18. private class DocumentInfo
  19. {
  20. }
  21. private class NewsDocumentIndex : AbstractIndexCreationTask<NewsDocument, NewsDocumentIndex.Info>
  22. {
  23. public static string StaticIndexName = "ETIS/NewsDocumentIndex";
  24. public override string IndexName
  25. {
  26. get { return StaticIndexName; }
  27. }
  28. public class Info : DocumentInfo
  29. {
  30. public string AuthorNamesStr { get; set; }
  31. }
  32. public NewsDocumentIndex()
  33. {
  34. Map = docs => from d in docs
  35. select new Info
  36. {
  37. AuthorNamesStr = d.AuthorNames != null ? string.Join(", ", d.AuthorNames.OrderBy(x => x)) : string.Empty,
  38. };
  39. AdditionalSources = new Dictionary<string, string>()
  40. {
  41. ["DateTimeExtension4"] = @"
  42. using System;
  43. using System.Collections.Generic;
  44. using System.Linq;
  45. namespace ETIS
  46. {
  47. public static class DateTimeExtension4
  48. {
  49. public static IEnumerable<int> GetYearsRepresented(List<DateTime?> startTimes, IEnumerable<DateTime?> endTimes)
  50. {
  51. if (startTimes == null || endTimes == null)
  52. return null;
  53. var years = new List<int>();
  54. var tuples = startTimes.Zip(endTimes, (x, y) => new Tuple<DateTime?, DateTime?>(x, y)).ToList();
  55. foreach (var pair in tuples)
  56. {
  57. if (!pair.Item1.HasValue)
  58. return null;
  59. if (!pair.Item2.HasValue)
  60. continue;
  61. var startYear = pair.Item1.Value.Year;
  62. var endYear = pair.Item2.Value.Year;
  63. if (endYear - startYear < 0)
  64. continue;
  65. years.AddRange(Enumerable.Range(startYear, endYear - startYear + 1));
  66. }
  67. return years;
  68. }
  69. }
  70. }
  71. "
  72. };
  73. }
  74. }
  75. [Fact]
  76. public void CanCompileAdditionalSource()
  77. {
  78. using (var store = GetDocumentStore())
  79. {
  80. using (var session = store.OpenSession())
  81. {
  82. session.Store(new NewsDocument
  83. {
  84. AuthorNames = new List<string>
  85. {
  86. "garcia", "weir"
  87. }
  88. });
  89. session.Store(new NewsDocument());
  90. session.SaveChanges();
  91. }
  92. // should not throw
  93. new NewsDocumentIndex().Execute(store);
  94. WaitForIndexing(store);
  95. using (var session = store.OpenSession())
  96. {
  97. var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
  98. .Where(doc => doc.AuthorNamesStr == "garcia, weir")
  99. .ToList();
  100. Assert.Single(q);
  101. }
  102. }
  103. }
  104. [Fact]
  105. public void CanCompileAdditionalSource_List()
  106. {
  107. using (var store = GetDocumentStore())
  108. {
  109. using (var session = store.OpenSession())
  110. {
  111. session.Store(new NewsDocument
  112. {
  113. AuthorNames = new List<string>
  114. {
  115. "garcia", "weir"
  116. }
  117. });
  118. session.Store(new NewsDocument());
  119. session.SaveChanges();
  120. }
  121. // should not throw
  122. var index = new NewsDocumentIndex
  123. {
  124. AdditionalSources = new Dictionary<string, string>
  125. {
  126. ["DateTimeExtension"] = @"
  127. using System;
  128. using System.Collections.Generic;
  129. using System.Linq;
  130. namespace ETIS
  131. {
  132. public static class DateTimeExtension
  133. {
  134. public static bool Foo(List<DateTime?> startTimes)
  135. {
  136. if (startTimes == null)
  137. return false;
  138. return startTimes.Count(x => x.Value.Year > 2000) < 10;
  139. }
  140. }
  141. }
  142. "
  143. }
  144. };
  145. index.Execute(store);
  146. WaitForIndexing(store);
  147. using (var session = store.OpenSession())
  148. {
  149. var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
  150. .Where(doc => doc.AuthorNamesStr == "garcia, weir")
  151. .ToList();
  152. Assert.Single(q);
  153. }
  154. }
  155. }
  156. [Fact]
  157. public void CanCompileAdditionalSource_IList()
  158. {
  159. using (var store = GetDocumentStore())
  160. {
  161. using (var session = store.OpenSession())
  162. {
  163. session.Store(new NewsDocument
  164. {
  165. AuthorNames = new List<string>
  166. {
  167. "garcia", "weir"
  168. }
  169. });
  170. session.Store(new NewsDocument());
  171. session.SaveChanges();
  172. }
  173. // should not throw
  174. var index = new NewsDocumentIndex
  175. {
  176. AdditionalSources = new Dictionary<string, string>
  177. {
  178. ["DateTimeExtension"] = @"
  179. using System;
  180. using System.Collections.Generic;
  181. using System.Linq;
  182. namespace ETIS
  183. {
  184. public static class DateTimeExtension
  185. {
  186. public static bool Foo(IList<DateTime?> startTimes)
  187. {
  188. if (startTimes == null)
  189. return false;
  190. return startTimes.Count(x => x.Value.Year > 2000) < 10;
  191. }
  192. }
  193. }
  194. "
  195. }
  196. };
  197. index.Execute(store);
  198. WaitForIndexing(store);
  199. using (var session = store.OpenSession())
  200. {
  201. var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
  202. .Where(doc => doc.AuthorNamesStr == "garcia, weir")
  203. .ToList();
  204. Assert.Single(q);
  205. }
  206. }
  207. }
  208. [Fact]
  209. public void CanCompileAdditionalSource_ICollection()
  210. {
  211. using (var store = GetDocumentStore())
  212. {
  213. using (var session = store.OpenSession())
  214. {
  215. session.Store(new NewsDocument
  216. {
  217. AuthorNames = new List<string>
  218. {
  219. "garcia", "weir"
  220. }
  221. });
  222. session.Store(new NewsDocument());
  223. session.SaveChanges();
  224. }
  225. // should not throw
  226. var index = new NewsDocumentIndex
  227. {
  228. AdditionalSources = new Dictionary<string, string>
  229. {
  230. ["DateTimeExtension"] = @"
  231. using System;
  232. using System.Collections.Generic;
  233. using System.Linq;
  234. namespace ETIS
  235. {
  236. public static class DateTimeExtension
  237. {
  238. public static bool Foo(ICollection<string> names)
  239. {
  240. if (names == null)
  241. return false;
  242. return names.Count(x => x != ""ayende"") < 10;
  243. }
  244. }
  245. }
  246. "
  247. }
  248. };
  249. index.Execute(store);
  250. WaitForIndexing(store);
  251. using (var session = store.OpenSession())
  252. {
  253. var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
  254. .Where(doc => doc.AuthorNamesStr == "garcia, weir")
  255. .ToList();
  256. Assert.Single(q);
  257. }
  258. }
  259. }
  260. [Fact]
  261. public void CanCompileAdditionalSource_Hashset()
  262. {
  263. using (var store = GetDocumentStore())
  264. {
  265. using (var session = store.OpenSession())
  266. {
  267. session.Store(new NewsDocument
  268. {
  269. AuthorNames = new List<string>
  270. {
  271. "garcia", "weir"
  272. }
  273. });
  274. session.Store(new NewsDocument());
  275. session.SaveChanges();
  276. }
  277. // should not throw
  278. var index = new NewsDocumentIndex
  279. {
  280. AdditionalSources = new Dictionary<string, string>
  281. {
  282. ["DateTimeExtension"] = @"
  283. using System;
  284. using System.Collections.Generic;
  285. using System.Linq;
  286. namespace ETIS
  287. {
  288. public static class DateTimeExtension
  289. {
  290. public static bool Foo(HashSet<long> numbers)
  291. {
  292. if (numbers == null)
  293. return false;
  294. return numbers.Count(x => x > 100) < 10;
  295. }
  296. }
  297. }
  298. "
  299. }
  300. };
  301. index.Execute(store);
  302. WaitForIndexing(store);
  303. using (var session = store.OpenSession())
  304. {
  305. var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
  306. .Where(doc => doc.AuthorNamesStr == "garcia, weir")
  307. .ToList();
  308. Assert.Single(q);
  309. }
  310. }
  311. }
  312. [Fact]
  313. public void CanCompileAdditionalSource_Dictionary()
  314. {
  315. using (var store = GetDocumentStore())
  316. {
  317. using (var session = store.OpenSession())
  318. {
  319. session.Store(new NewsDocument
  320. {
  321. AuthorNames = new List<string>
  322. {
  323. "garcia", "weir"
  324. }
  325. });
  326. session.Store(new NewsDocument());
  327. session.SaveChanges();
  328. }
  329. // should not throw
  330. var index = new NewsDocumentIndex
  331. {
  332. AdditionalSources = new Dictionary<string, string>
  333. {
  334. ["DateTimeExtension"] = @"
  335. using System;
  336. using System.Collections.Generic;
  337. using System.Linq;
  338. namespace ETIS
  339. {
  340. public static class DateTimeExtension
  341. {
  342. public static bool Foo(Dictionary<string, long> dictionary)
  343. {
  344. if (dictionary == null)
  345. return false;
  346. return dictionary.Count(x => x.Value > 100) < 10;
  347. }
  348. }
  349. }
  350. "
  351. }
  352. };
  353. index.Execute(store);
  354. WaitForIndexing(store);
  355. using (var session = store.OpenSession())
  356. {
  357. var q = session.Query<NewsDocumentIndex.Info, NewsDocumentIndex>()
  358. .Where(doc => doc.AuthorNamesStr == "garcia, weir")
  359. .ToList();
  360. Assert.Single(q);
  361. }
  362. }
  363. }
  364. }
  365. }