/test/SlowTests/Bugs/Stacey/Aspects.cs

https://github.com/fitzchak/ravendb · C# · 279 lines · 194 code · 50 blank · 35 comment · 1 complexity · c3db43c357d8691cacce4028af4582d4 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FastTests;
  5. using Newtonsoft.Json;
  6. using Raven.Client.Documents.Indexes;
  7. using Raven.Client.Documents.Linq.Indexing;
  8. using Raven.Client.Documents.Operations.Indexes;
  9. using Xunit;
  10. namespace SlowTests.Bugs.Stacey
  11. {
  12. public class Aspects : RavenTestBase
  13. {
  14. private readonly Options _options = new Options
  15. {
  16. ModifyDocumentStore = documentStore =>
  17. documentStore.Conventions.CustomizeJsonSerializer = serializer => serializer.TypeNameHandling = TypeNameHandling.All
  18. };
  19. [Fact]
  20. public void Aspects_Can_Be_Installed()
  21. {
  22. using (var store = GetDocumentStore(_options))
  23. {
  24. // currency
  25. var currency = new[]
  26. {
  27. new Currency {Name = "Money"},
  28. new Currency {Name = "Points"},
  29. };
  30. // assert
  31. using (var session = store.OpenSession())
  32. {
  33. // ensure optimistic concurrency
  34. session.Advanced.UseOptimisticConcurrency = true;
  35. // try to insert the apertures into the database
  36. session.Store(currency[0], "currencies/1");
  37. session.Store(currency[1], "currencies/2");
  38. session.SaveChanges();
  39. }
  40. // arrange
  41. var aspects = new[]
  42. {
  43. new Aspect {Name = "Strength", Kind = Kind.Attribute, Category = Category.Physical},
  44. new Aspect {Name = "Stamina", Kind = Kind.Attribute, Category = Category.Physical},
  45. };
  46. // assert
  47. using (var session = store.OpenSession())
  48. {
  49. // ensure optimistic concurrency
  50. session.Advanced.UseOptimisticConcurrency = true;
  51. // try to insert the aspects into the database
  52. session.Store(aspects[0], "aspects/1");
  53. session.Store(aspects[1], "aspects/2");
  54. session.SaveChanges();
  55. // try to query each of the newly inserted aspects.
  56. var query = session.Query<Aspect>().ToList();
  57. // unit test each aspect.
  58. foreach (var aspect in query)
  59. {
  60. Assert.NotNull(aspect.Id);
  61. Assert.NotNull(aspect.Name);
  62. Assert.NotNull(aspect.Kind);
  63. Assert.NotNull(aspect.Category);
  64. }
  65. }
  66. // update
  67. using (var session = store.OpenSession())
  68. {
  69. // ensure optimistic concurrency
  70. session.Advanced.UseOptimisticConcurrency = true;
  71. // create an array to hold the results.
  72. var results = new Aspect[2];
  73. // try to query each of the newly inserted aspects.
  74. results[0] = session.Load<Aspect>("aspects/1");
  75. results[1] = session.Load<Aspect>("aspects/2");
  76. // load the flexskill points currency.
  77. var points = session.Load<Currency>("currencies/2");
  78. results[0].Path = new Path
  79. {
  80. Steps = new List<Step>
  81. {
  82. new Step
  83. {
  84. Cost = 5,
  85. Number = 1,
  86. Currency = points.Id,
  87. Requirements = new List<Requirement>
  88. {
  89. new Requirement
  90. {
  91. What = results[1].Id,
  92. Value = 2
  93. },
  94. new Requirement
  95. {
  96. What = points.Id,
  97. Value = 5
  98. }
  99. }
  100. }
  101. }
  102. };
  103. session.SaveChanges();
  104. }
  105. // assert
  106. using (var session = store.OpenSession())
  107. {
  108. // ensure optimistic concurrency
  109. session.Advanced.UseOptimisticConcurrency = true;
  110. // create an array to hold the results.
  111. // try to query each of the newly inserted aspects.
  112. var results = session.Include("Path.Steps,Requirements,What").Load<Aspect>("aspects/1");
  113. //var results = session.Include<Aspect>(aspect => aspect.Path.Steps[0].Requirements[0].What).Load<Aspect>("aspects/1");
  114. // the first requirement should be an aspect
  115. var requirements = new Entity[2];
  116. requirements[0] = session.Load<Aspect>(results.Path.Steps[0].Requirements[0].What);
  117. requirements[1] = session.Load<Currency>(results.Path.Steps[0].Requirements[1].What);
  118. Assert.IsType<Aspect>(requirements[0]);
  119. Assert.IsType<Currency>(requirements[1]);
  120. //Assert.Equal(1, session.Advanced.NumberOfRequests);
  121. session.SaveChanges();
  122. }
  123. }
  124. }
  125. [Fact]
  126. public void Index_can_be_queried()
  127. {
  128. using (var store = GetDocumentStore())
  129. {
  130. store.Maintenance.Send(new PutIndexesOperation(new IndexDefinitionBuilder<Aspect>("AspectsByName")
  131. {
  132. Map = orders => from order in orders
  133. select new { order.Name }
  134. }.ToIndexDefinition(store.Conventions)));
  135. store.Maintenance.Send(new PutIndexesOperation(new IndexDefinitionBuilder<Entity>("test")
  136. {
  137. Map = docs => from i in docs.WhereEntityIs<Entity>("Aspects", "Currencies")
  138. select new { i.Name }
  139. }.ToIndexDefinition(store.Conventions)));
  140. // arrange
  141. var aspects = new[]
  142. {
  143. new Aspect {Name = "Strength", Kind = Kind.Attribute, Category = Category.Physical},
  144. new Aspect {Name = "Stamina", Kind = Kind.Attribute, Category = Category.Physical},
  145. };
  146. // assert
  147. using (var session = store.OpenSession())
  148. {
  149. // ensure optimistic concurrency
  150. session.Advanced.UseOptimisticConcurrency = true;
  151. // try to insert the aspects into the database
  152. session.Store(aspects[0]);
  153. session.Store(aspects[1]);
  154. session.SaveChanges();
  155. // try to query each of the newly inserted aspects.
  156. var query = session.Query<Aspect>().ToList();
  157. // unit test each aspect.
  158. foreach (var aspect in query)
  159. {
  160. Assert.NotNull(aspect.Id);
  161. Assert.NotNull(aspect.Name);
  162. Assert.NotNull(aspect.Kind);
  163. Assert.NotNull(aspect.Category);
  164. }
  165. }
  166. // assert
  167. using (var session = store.OpenSession())
  168. {
  169. // ensure optimistic concurrency
  170. session.Advanced.UseOptimisticConcurrency = true;
  171. // create an array to hold the results.
  172. // try to query each of the newly inserted aspects.
  173. var results = session.Query<Aspect>("AspectsByName")
  174. .Customize(n => n.WaitForNonStaleResults())
  175. .Where(n => n.Name == "Strength")
  176. .ToList();
  177. Assert.NotEmpty(results);
  178. }
  179. }
  180. }
  181. private abstract class Entity
  182. {
  183. public string Id { get; set; }
  184. public string Name { get; set; }
  185. }
  186. private class Aspect : Entity
  187. {
  188. public Kind Kind { get; set; }
  189. public Category Category { get; set; }
  190. public Path Path { get; set; }
  191. }
  192. /// <summary>
  193. /// Denotes the aspect's polymorphed form.
  194. /// </summary>
  195. private enum Kind
  196. {
  197. None,
  198. Statistic,
  199. Attribute,
  200. Skill
  201. }
  202. private enum Category
  203. {
  204. None = 0,
  205. Physical = 1,
  206. Mental = 2,
  207. Spirit = 3
  208. }
  209. private class Step
  210. {
  211. public int Cost { get; set; }
  212. public int Number { get; set; }
  213. public string Currency { get; set; }
  214. public List<Requirement> Requirements { get; set; }
  215. }
  216. private class Currency : Entity
  217. {
  218. }
  219. private class Path
  220. {
  221. public List<Step> Steps { get; set; }
  222. }
  223. private class Requirement
  224. {
  225. public string What { get; set; }
  226. public int Value { get; set; }
  227. }
  228. }
  229. }