PageRenderTime 44ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/Provider/ProviderTests/SpatialServicesTests.cs

http://entityframework.codeplex.com
C# | 348 lines | 294 code | 53 blank | 1 comment | 2 complexity | 43b9efd8d48067fa2561e6e2eedb8a2a MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Spatial;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml.Linq;
  10. using SampleEntityFrameworkProvider;
  11. using Xunit;
  12. namespace ProviderTests
  13. {
  14. public class SpatialServicesTests
  15. {
  16. private readonly DbSpatialServices spatialServices = SpatialServices.Instance;
  17. private const string PointWKT = "POINT (1 2)";
  18. private readonly byte[] PointWKB =
  19. new byte[]
  20. {
  21. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  22. 0x00, 0x00, 0x00, 0xF0, 0x3F, 0x00, 0x00, 0x00,
  23. 0x00, 0x00, 0x00, 0x00, 0x40,
  24. };
  25. private readonly XDocument GeographyPointGml =
  26. XDocument.Parse("<Point xmlns=\"http://www.opengis.net/gml\"><pos>2 1</pos></Point>");
  27. private readonly XDocument GeometryPointGml =
  28. XDocument.Parse("<Point xmlns=\"http://www.opengis.net/gml\"><pos>1 2</pos></Point>");
  29. private const string PolygonWKT = "POLYGON ((0 0, 5 0, 5 5, 0 5, 0 0))";
  30. private const int DefaultCoordinateSystemId = 4326;
  31. #region DbGeography tests
  32. [Fact]
  33. public void Verify_DbGeography_AsBinary_method()
  34. {
  35. Assert.True(spatialServices.GeographyFromText(PointWKT).AsBinary().SequenceEqual(PointWKB));
  36. }
  37. [Fact]
  38. public void Verify_DbGeography_AsGml_method()
  39. {
  40. var gml = XDocument.Parse(spatialServices.GeographyFromText(PointWKT).AsGml());
  41. Assert.True(XDocument.DeepEquals(gml, GeographyPointGml));
  42. }
  43. [Fact]
  44. public void Verify_DbGeography_AsText_method()
  45. {
  46. Assert.Equal(
  47. PointWKT,
  48. spatialServices.GeographyFromText(PointWKT).AsText());
  49. }
  50. [Fact]
  51. public void Verify_DbGeography_Buffer_method()
  52. {
  53. const string line = "LINESTRING (-122.36 47.656, -122.343 47.656)";
  54. Assert.Equal(
  55. line,
  56. spatialServices.GeographyFromText(line).Buffer(0).AsText());
  57. }
  58. [Fact]
  59. public void Verify_DbGeography_CreateProviderValue_WKT_method()
  60. {
  61. var geographyWellKnownValue = new DbGeographyWellKnownValue()
  62. {
  63. CoordinateSystemId = DefaultCoordinateSystemId,
  64. WellKnownBinary = null,
  65. WellKnownText = PointWKT
  66. };
  67. dynamic providerValue = spatialServices.CreateProviderValue(geographyWellKnownValue);
  68. Assert.Equal(PointWKT, providerValue.ToString());
  69. }
  70. [Fact]
  71. public void Verify_DbGeography_CreateProviderValue_WKB_method()
  72. {
  73. var geographyWellKnownValue = new DbGeographyWellKnownValue()
  74. {
  75. CoordinateSystemId = DefaultCoordinateSystemId,
  76. WellKnownBinary = PointWKB,
  77. WellKnownText = null
  78. };
  79. dynamic providerValue = spatialServices.CreateProviderValue(geographyWellKnownValue);
  80. Assert.Equal(PointWKT, providerValue.ToString());
  81. }
  82. [Fact]
  83. public void Verify_DbGeography_Distance_method()
  84. {
  85. var distance = spatialServices.Distance(
  86. spatialServices.GeographyFromText("POINT (1 2)"),
  87. spatialServices.GeographyFromText("POINT (2 1)"));
  88. Assert.True(distance > 156876.149075896D && distance < 156876.149075897D);
  89. }
  90. [Fact]
  91. public void Verify_DbGeography_FromBinary_method()
  92. {
  93. Assert.Equal(
  94. PointWKT,
  95. spatialServices.GeographyFromBinary(PointWKB, DefaultCoordinateSystemId).AsText());
  96. }
  97. [Fact]
  98. public void Verify_DbGeography_FromGml_method()
  99. {
  100. Assert.Equal(
  101. PointWKT,
  102. spatialServices.GeographyFromGml(GeographyPointGml.ToString(), DefaultCoordinateSystemId).AsText());
  103. }
  104. [Fact]
  105. public void Verify_DbGeography_FromProviderValue_method()
  106. {
  107. var providerValue = spatialServices.GeographyFromText(PointWKT).ProviderValue;
  108. Assert.Equal(
  109. PointWKT,
  110. spatialServices.GeographyFromProviderValue(providerValue).AsText());
  111. }
  112. [Fact]
  113. public void Verify_DbGeography_FromText_method()
  114. {
  115. Assert.Equal(
  116. PointWKT,
  117. spatialServices.GeographyFromText(PointWKT).AsText());
  118. }
  119. [Fact]
  120. public void Verify_DbGeography_GetCoordinateSystemID_method()
  121. {
  122. Assert.Equal(
  123. DefaultCoordinateSystemId,
  124. spatialServices.GetCoordinateSystemId(spatialServices.GeographyFromText(PointWKT)));
  125. }
  126. [Fact]
  127. public void Verify_DbGeography_GetLatitude_method()
  128. {
  129. Assert.Equal(
  130. 2,
  131. spatialServices.GetLatitude(spatialServices.GeographyFromText(PointWKT)));
  132. }
  133. [Fact]
  134. public void Verify_DbGeography_GetLongitude_method()
  135. {
  136. Assert.Equal(
  137. 1,
  138. spatialServices.GetLongitude(spatialServices.GeographyFromText(PointWKT)));
  139. }
  140. [Fact]
  141. public void Verify_DbGeography_SpatialEquals_returns_true_for_equal_types()
  142. {
  143. Assert.True(
  144. spatialServices.SpatialEquals(
  145. spatialServices.GeographyFromText(PointWKT),
  146. spatialServices.GeographyFromText(PointWKT)));
  147. }
  148. [Fact]
  149. public void Verify_DbGeography_SpatialEquals_returns_false_for_not_equal_types()
  150. {
  151. Assert.False(
  152. spatialServices.SpatialEquals(
  153. spatialServices.GeographyFromText("POINT(2 1)"),
  154. spatialServices.GeographyFromText(PointWKT)));
  155. }
  156. [Fact]
  157. public void Verify_DbGeography_Area_method()
  158. {
  159. var area = spatialServices.GetArea(spatialServices.GeographyFromText(PolygonWKT));
  160. Assert.True(area > 307540516837.287 && area < 307540516837.289);
  161. }
  162. #endregion
  163. #region DbGeometry tests
  164. [Fact]
  165. public void Verify_DbGeometry_AsBinary_method()
  166. {
  167. Assert.True(spatialServices.GeometryFromText(PointWKT).AsBinary().SequenceEqual(PointWKB));
  168. }
  169. [Fact]
  170. public void Verify_DbGeometry_AsGml_method()
  171. {
  172. var gml = XDocument.Parse(spatialServices.GeometryFromText(PointWKT).AsGml());
  173. Assert.True(XDocument.DeepEquals(gml, GeometryPointGml));
  174. }
  175. [Fact]
  176. public void Verify_DbGeometry_AsText_method()
  177. {
  178. Assert.Equal(
  179. PointWKT,
  180. spatialServices.GeometryFromText(PointWKT).AsText());
  181. }
  182. [Fact]
  183. public void Verify_DbGeometry_Buffer_method()
  184. {
  185. const string line = "LINESTRING (-122.36 47.656, -122.343 47.656)";
  186. Assert.Equal(
  187. line,
  188. spatialServices.GeometryFromText(line).Buffer(0).AsText());
  189. }
  190. [Fact]
  191. public void Verify_DbGeometry_CreateProviderValue_WKT_method()
  192. {
  193. var geometryWellKnownValue = new DbGeometryWellKnownValue()
  194. {
  195. CoordinateSystemId = DefaultCoordinateSystemId,
  196. WellKnownBinary = null,
  197. WellKnownText = PointWKT
  198. };
  199. dynamic providerValue = spatialServices.CreateProviderValue(geometryWellKnownValue);
  200. Assert.Equal(PointWKT, providerValue.ToString());
  201. }
  202. [Fact]
  203. public void Verify_DbGeometry_CreateProviderValue_WKB_method()
  204. {
  205. var geometryWellKnownValue = new DbGeometryWellKnownValue()
  206. {
  207. CoordinateSystemId = DefaultCoordinateSystemId,
  208. WellKnownBinary = PointWKB,
  209. WellKnownText = null
  210. };
  211. dynamic providerValue = spatialServices.CreateProviderValue(geometryWellKnownValue);
  212. Assert.Equal(PointWKT, providerValue.ToString());
  213. }
  214. [Fact]
  215. public void Verify_DbGeometry_Distance_method()
  216. {
  217. var distance = spatialServices.Distance(
  218. spatialServices.GeometryFromText("POINT (0 0)"),
  219. spatialServices.GeometryFromText("POINT (3 4)"));
  220. Assert.Equal(5, distance);
  221. }
  222. [Fact]
  223. public void Verify_DbGeometry_FromBinary_method()
  224. {
  225. Assert.Equal(
  226. PointWKT,
  227. spatialServices.GeometryFromBinary(PointWKB, DefaultCoordinateSystemId).AsText());
  228. }
  229. [Fact]
  230. public void Verify_DbGeometry_FromGml_method()
  231. {
  232. Assert.Equal(
  233. PointWKT,
  234. spatialServices.GeometryFromGml(GeometryPointGml.ToString(), DefaultCoordinateSystemId).AsText());
  235. }
  236. [Fact]
  237. public void Verify_DbGeometry_FromProviderValue_method()
  238. {
  239. var providerValue = spatialServices.GeometryFromText(PointWKT).ProviderValue;
  240. Assert.Equal(
  241. PointWKT,
  242. spatialServices.GeometryFromProviderValue(providerValue).AsText());
  243. }
  244. [Fact]
  245. public void Verify_DbGeometry_FromText_method()
  246. {
  247. Assert.Equal(
  248. PointWKT,
  249. spatialServices.GeometryFromText(PointWKT).AsText());
  250. }
  251. [Fact]
  252. public void Verify_DbGeometry_GetCoordinateSystemID_method()
  253. {
  254. Assert.Equal(
  255. DefaultCoordinateSystemId,
  256. spatialServices.GetCoordinateSystemId(spatialServices.GeometryFromText(PointWKT)));
  257. }
  258. [Fact]
  259. public void Verify_DbGeometry_GetXCoordinate_method()
  260. {
  261. Assert.Equal(
  262. 1,
  263. spatialServices.GetXCoordinate(spatialServices.GeometryFromText(PointWKT)));
  264. }
  265. [Fact]
  266. public void Verify_DbGeometry_GetYCoordinate_method()
  267. {
  268. Assert.Equal(
  269. 2,
  270. spatialServices.GetYCoordinate(spatialServices.GeometryFromText(PointWKT)));
  271. }
  272. [Fact]
  273. public void Verify_DbGeometry_SpatialEquals_returns_true_for_equal_types()
  274. {
  275. Assert.True(
  276. spatialServices.SpatialEquals(
  277. spatialServices.GeometryFromText(PointWKT),
  278. spatialServices.GeometryFromText(PointWKT)));
  279. }
  280. [Fact]
  281. public void Verify_DbGeometry_SpatialEquals_returns_false_for_not_equal_types()
  282. {
  283. Assert.False(
  284. spatialServices.SpatialEquals(
  285. spatialServices.GeometryFromText("POINT(2 1)"),
  286. spatialServices.GeometryFromText(PointWKT)));
  287. }
  288. [Fact]
  289. public void Verify_DbGeometry_Area_method()
  290. {
  291. Assert.Equal(25, spatialServices.GetArea(spatialServices.GeometryFromText(PolygonWKT)));
  292. }
  293. #endregion
  294. }
  295. }