PageRenderTime 76ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NHibernate.Spatial/src/Tests.NHibernate.Spatial/CriteriaFixture.cs

https://bitbucket.org/dabide/nhcontrib
C# | 190 lines | 164 code | 25 blank | 1 comment | 8 complexity | ca3b3626b8c049e3a34474723e831865 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Apache-2.0, LGPL-3.0, LGPL-2.1
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using GeoAPI.Geometries;
  5. using NetTopologySuite.Geometries;
  6. using NetTopologySuite.IO;
  7. using NHibernate;
  8. using NHibernate.Criterion;
  9. using NHibernate.Linq;
  10. using NHibernate.Spatial.Criterion;
  11. using NHibernate.Spatial.Linq;
  12. using NUnit.Framework;
  13. using Tests.NHibernate.Spatial.Model;
  14. using Tests.NHibernate.Spatial.OgcSfSql11Compliance.Model;
  15. namespace Tests.NHibernate.Spatial
  16. {
  17. public abstract class CriteriaFixture : AbstractFixture
  18. {
  19. protected override Type[] Mappings
  20. {
  21. get
  22. {
  23. return new Type[] {
  24. typeof(Simple)
  25. };
  26. }
  27. }
  28. private ISession session;
  29. protected override void OnSetUp()
  30. {
  31. session = sessions.OpenSession();
  32. session.Save(new Simple("a point", new Point(12, 45)));
  33. session.Save(new Simple("a null", null));
  34. session.Save(new Simple("a collection empty 1", Wkt.Read("GEOMETRYCOLLECTION EMPTY")));
  35. session.Save(new Simple("a collection empty 2", GeometryCollection.Empty));
  36. session.Flush();
  37. }
  38. protected override void OnTearDown()
  39. {
  40. DeleteMappings(session);
  41. session.Close();
  42. }
  43. [Test]
  44. public void IsDirty()
  45. {
  46. Assert.IsFalse(session.IsDirty());
  47. var simple = session.CreateCriteria<Simple>()
  48. .SetMaxResults(1)
  49. .UniqueResult<Simple>();
  50. Assert.NotNull(simple);
  51. // New instance of the same geometry
  52. var wkt = simple.Geometry.AsText();
  53. var geometry = new WKTReader().Read(wkt);
  54. geometry.SRID = simple.Geometry.SRID;
  55. simple.Geometry = geometry;
  56. Assert.IsFalse(session.IsDirty());
  57. simple.Geometry.SRID = 12345;
  58. Assert.IsTrue(session.IsDirty());
  59. }
  60. [Test]
  61. public void RowCount()
  62. {
  63. IList results = session.CreateCriteria(typeof(Simple))
  64. .SetProjection(Projections.ProjectionList()
  65. .Add(Projections.RowCount())
  66. )
  67. .List();
  68. Assert.AreEqual(4, (int)(results[0]));
  69. }
  70. [Test]
  71. public void CountNullOrSpatialEmpty()
  72. {
  73. IList results = session.CreateCriteria(typeof (Simple))
  74. .Add(Restrictions.Or(
  75. Restrictions.IsNull("Geometry"),
  76. SpatialRestrictions.IsEmpty("Geometry")
  77. ))
  78. .List();
  79. Assert.AreEqual(3, results.Count);
  80. foreach (Simple item in results)
  81. {
  82. if (item.Geometry != null)
  83. {
  84. Assert.IsTrue(item.Geometry.IsEmpty);
  85. }
  86. }
  87. }
  88. [Test]
  89. public void CountNullOrSpatialEmptyLambda()
  90. {
  91. IList results = session.CreateCriteria(typeof (Simple))
  92. .Add(Restrictions.Or(
  93. Restrictions.On<Simple>(o => o.Geometry).IsNull,
  94. SpatialRestrictions.On<Simple>(o => o.Geometry).IsEmpty
  95. ))
  96. .List();
  97. Assert.AreEqual(3, results.Count);
  98. foreach (Simple item in results)
  99. {
  100. if (item.Geometry != null)
  101. {
  102. Assert.IsTrue(item.Geometry.IsEmpty);
  103. }
  104. }
  105. }
  106. [Test]
  107. public void CountNullOrSpatialEmptyLinq()
  108. {
  109. bool x = session.Query<Simple>().Select(s => s.Geometry.IsEmpty).First();
  110. IList results =
  111. session.Query<Simple>()
  112. .Where(s => s.Geometry == null || s.Geometry.IsEmpty)
  113. .ToList();
  114. Assert.AreEqual(3, results.Count);
  115. foreach (Simple item in results)
  116. {
  117. if (item.Geometry != null)
  118. {
  119. Assert.IsTrue(item.Geometry.IsEmpty);
  120. }
  121. }
  122. }
  123. [Test]
  124. public void CountNull()
  125. {
  126. IList results = session.CreateCriteria(typeof(Simple))
  127. .Add(Restrictions.IsNull("Geometry"))
  128. .List();
  129. Assert.AreEqual(1, results.Count);
  130. foreach (Simple item in results)
  131. {
  132. Assert.IsNull(item.Geometry);
  133. }
  134. }
  135. [Test]
  136. [ExpectedException(typeof(MappingException))]
  137. public void CountEmpty()
  138. {
  139. IList results = session.CreateCriteria(typeof(Simple))
  140. .Add(Restrictions.IsEmpty("Geometry"))
  141. .List();
  142. Assert.AreEqual(0, results.Count);
  143. }
  144. [Test]
  145. public void CountSpatialEmpty()
  146. {
  147. IList results = session.CreateCriteria(typeof(Simple))
  148. .Add(SpatialRestrictions.IsEmpty("Geometry"))
  149. .List();
  150. Assert.AreEqual(2, results.Count);
  151. foreach (Simple item in results)
  152. {
  153. Assert.IsTrue(item.Geometry.IsEmpty);
  154. }
  155. }
  156. [Test]
  157. public void CountSpatialEmpty_()
  158. {
  159. IGeometry geom = new Point(10, 10).Buffer(100);
  160. ICriteria crit = session.CreateCriteria(typeof (Simple))
  161. .Add(SpatialRestrictions.Intersects("Geometry", geom));
  162. var result = crit.List<Simple>();
  163. }
  164. }
  165. }