/test/SlowTests/Tests/Spatial/TwoLocations.cs

https://github.com/fitzchak/ravendb
C# | 274 lines | 227 code | 42 blank | 5 comment | 0 complexity | 5a6e6304390ef3356fbdaaf5372a3d89 MD5 | raw file
  1. // -----------------------------------------------------------------------
  2. // <copyright file="TwoLocations.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System.Linq;
  7. using FastTests;
  8. using Raven.Client.Documents;
  9. using Raven.Client.Documents.Indexes;
  10. using Raven.Client.Documents.Indexes.Spatial;
  11. using Xunit;
  12. namespace SlowTests.Tests.Spatial
  13. {
  14. public class TwoLocations : RavenTestBase
  15. {
  16. private class Event
  17. {
  18. public string Name;
  19. public Location[] Locations;
  20. public class Location
  21. {
  22. public double Lng, Lat;
  23. }
  24. }
  25. private static void Setup(IDocumentStore store)
  26. {
  27. using (var session = store.OpenSession())
  28. {
  29. session.Store(new Event
  30. {
  31. Name = "Trial",
  32. Locations = new[]
  33. {
  34. new Event.Location
  35. {
  36. Lat =32.1067536,
  37. Lng = 34.8357353
  38. },
  39. new Event.Location
  40. {
  41. Lat = 32.0624912,
  42. Lng = 34.7700725
  43. },
  44. }
  45. });
  46. session.SaveChanges();
  47. }
  48. }
  49. [Fact]
  50. public void CanQueryByMultipleLocations()
  51. {
  52. using (var store = GetDocumentStore())
  53. {
  54. new MultiLocations().Execute(store);
  55. Setup(store);
  56. using (var session = store.OpenSession())
  57. {
  58. var list = session.Query<Event, MultiLocations>()
  59. .Spatial("Location", factory => factory.WithinRadius(1, 32.0590291, 34.7707401))
  60. .Customize(x => x.WaitForNonStaleResults())
  61. .ToList();
  62. RavenTestHelper.AssertNoIndexErrors(store);
  63. Assert.NotEmpty(list);
  64. }
  65. using (var session = store.OpenSession())
  66. {
  67. var list = session.Query<Event, MultiLocations>()
  68. .Spatial("Location", factory => factory.WithinRadius(1, 32.1104641, 34.8417456))
  69. .Customize(x => x.WaitForNonStaleResults())
  70. .ToList();
  71. RavenTestHelper.AssertNoIndexErrors(store);
  72. Assert.NotEmpty(list);
  73. }
  74. }
  75. }
  76. [Fact]
  77. public void CanQueryByMultipleLocations2()
  78. {
  79. using (var store = GetDocumentStore())
  80. {
  81. new MultiLocationsCustomFieldName().Execute(store);
  82. Setup(store);
  83. using (var session = store.OpenSession())
  84. {
  85. var list = session.Query<Event, MultiLocationsCustomFieldName>()
  86. .Spatial("someField", factory => factory.WithinRadius(1, 32.0590291, 34.7707401))
  87. .Customize(x => x.WaitForNonStaleResults())
  88. .ToList();
  89. RavenTestHelper.AssertNoIndexErrors(store);
  90. Assert.NotEmpty(list);
  91. }
  92. using (var session = store.OpenSession())
  93. {
  94. var list = session.Query<Event, MultiLocationsCustomFieldName>()
  95. .Spatial("someField", factory => factory.WithinRadius(1, 32.1104641, 34.8417456))
  96. .Customize(x => x.WaitForNonStaleResults())
  97. .ToList();
  98. RavenTestHelper.AssertNoIndexErrors(store);
  99. Assert.NotEmpty(list);
  100. }
  101. }
  102. }
  103. [Fact]
  104. public void CanQueryByMultipleLocationsOverHttp()
  105. {
  106. using (var store = GetDocumentStore())
  107. {
  108. new MultiLocations().Execute(store);
  109. Setup(store);
  110. using (var session = store.OpenSession())
  111. {
  112. var list = session.Query<Event, MultiLocations>()
  113. .Spatial("Location", factory => factory.WithinRadius(1, 32.0590291, 34.7707401))
  114. .Customize(x => x.WaitForNonStaleResults())
  115. .ToList();
  116. Assert.NotEmpty(list);
  117. }
  118. using (var session = store.OpenSession())
  119. {
  120. var list = session.Query<Event, MultiLocations>()
  121. .Spatial("Location", factory => factory.WithinRadius(1, 32.1104641, 34.8417456))
  122. .Customize(x => x.WaitForNonStaleResults())
  123. .ToList();
  124. Assert.NotEmpty(list);
  125. }
  126. }
  127. }
  128. [Fact]
  129. public void CanQueryByMultipleLocationsHttp2()
  130. {
  131. using (var store = GetDocumentStore())
  132. {
  133. new MultiLocationsCustomFieldName().Execute(store);
  134. Setup(store);
  135. using (var session = store.OpenSession())
  136. {
  137. var list = session.Query<Event, MultiLocationsCustomFieldName>()
  138. .Spatial("someField", factory => factory.WithinRadius(1, 32.0590291, 34.7707401))
  139. .Customize(x => x.WaitForNonStaleResults())
  140. .ToList();
  141. Assert.NotEmpty(list);
  142. }
  143. using (var session = store.OpenSession())
  144. {
  145. var list = session.Query<Event, MultiLocationsCustomFieldName>()
  146. .Spatial("someField", factory => factory.WithinRadius(1, 32.1104641, 34.8417456))
  147. .Customize(x => x.WaitForNonStaleResults())
  148. .ToList();
  149. Assert.NotEmpty(list);
  150. }
  151. }
  152. }
  153. [Fact]
  154. public void CanQueryByMultipleLocationsRaw()
  155. {
  156. using (var store = GetDocumentStore())
  157. {
  158. new MultiLocationsCustomFieldName().Execute(store);
  159. Setup(store);
  160. using (var session = store.OpenSession())
  161. {
  162. var list = session.Query<Event, MultiLocationsCustomFieldName>()
  163. .Spatial("someField", factory => factory.RelatesToShape("Circle(34.770740 32.059029 d=1.000000)", SpatialRelation.Within))
  164. .Customize(x => x.WaitForNonStaleResults())
  165. .ToList();
  166. RavenTestHelper.AssertNoIndexErrors(store);
  167. Assert.NotEmpty(list);
  168. }
  169. using (var session = store.OpenSession())
  170. {
  171. var list = session.Query<Event, MultiLocationsCustomFieldName>()
  172. .Spatial("someField", factory => factory.RelatesToShape("Circle(34.770740 32.059029 d=1.000000)", SpatialRelation.Within))
  173. .Customize(x => x.WaitForNonStaleResults())
  174. .ToList();
  175. RavenTestHelper.AssertNoIndexErrors(store);
  176. Assert.NotEmpty(list);
  177. }
  178. }
  179. }
  180. [Fact]
  181. public void CanQueryByMultipleLocationsRawOverHttp()
  182. {
  183. using (var store = GetDocumentStore())
  184. {
  185. new MultiLocationsCustomFieldName().Execute(store);
  186. Setup(store);
  187. using (var session = store.OpenSession())
  188. {
  189. var list = session.Query<Event, MultiLocationsCustomFieldName>()
  190. .Spatial("someField", factory => factory.RelatesToShape("Circle(34.770740 32.059029 d=1.000000)", SpatialRelation.Within))
  191. .Customize(x => x.WaitForNonStaleResults())
  192. .ToList();
  193. Assert.NotEmpty(list);
  194. }
  195. using (var session = store.OpenSession())
  196. {
  197. var list = session.Query<Event, MultiLocationsCustomFieldName>()
  198. .Spatial("someField", factory => factory.RelatesToShape("Circle(34.770740 32.059029 d=1.000000)", SpatialRelation.Within))
  199. .Customize(x => x.WaitForNonStaleResults())
  200. .ToList();
  201. Assert.NotEmpty(list);
  202. }
  203. }
  204. }
  205. private class MultiLocations : AbstractIndexCreationTask<Event>
  206. {
  207. public MultiLocations()
  208. {
  209. Map = events =>
  210. from e in events
  211. select new
  212. {
  213. e.Name,
  214. Location = e.Locations.Select(x => CreateSpatialField(x.Lat, x.Lng))
  215. };
  216. }
  217. }
  218. private class MultiLocationsCustomFieldName : AbstractIndexCreationTask<Event>
  219. {
  220. public MultiLocationsCustomFieldName()
  221. {
  222. Map = events =>
  223. from e in events
  224. select new
  225. {
  226. e.Name,
  227. someField = e.Locations.Select(x => CreateSpatialField(x.Lat, x.Lng))
  228. };
  229. }
  230. }
  231. }
  232. }