Skip to content

Commit

Permalink
Added tests for #13. Fixes #14 (use correct coordinate ordering when …
Browse files Browse the repository at this point in the history
…writing SqlGeography to WKT
  • Loading branch information
dotMorten committed Oct 29, 2018
1 parent 644b7b3 commit 16b9c3e
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<Compile Include="..\Microsoft.SqlServer.Types.Tests\AssemblyLoader.cs">
<Link>AssemblyLoader.cs</Link>
</Compile>
<Compile Include="..\Microsoft.SqlServer.Types.Tests\Geography\WktTests.cs">
<Link>Geography\WktTests.cs</Link>
</Compile>
<Compile Include="..\Microsoft.SqlServer.Types.Tests\Geometry\DBTests.cs">
<Link>Geometry\DBTests.cs</Link>
</Compile>
Expand Down Expand Up @@ -96,9 +99,7 @@
<Version>1.3.2</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Geography\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
78 changes: 78 additions & 0 deletions src/Microsoft.SqlServer.Types.Tests/Geography/WktTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Microsoft.SqlServer.Types.Tests.Geometry;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.SqlServer.Types.Tests.Geography
{
[TestClass]
[TestCategory("SqlGeography")]
[TestCategory("WKT")]
public class WktTests
{

[TestMethod]
[WorkItem(13)]
public void UserSubmittedIssue_WKT2()
{
using (var conn = new System.Data.SqlClient.SqlConnection(DBTests.ConnectionString))
{
conn.Open();
var id = SqlGeography.Parse("LINESTRING (-122.36 47.656, -122.343 47.656)");

using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT @p";
var p = cmd.CreateParameter();
cmd.Parameters.Add(p);

p.UdtTypeName = "geography";
p.ParameterName = "@p";
p.Value = id;

Assert.AreEqual(id.ToString(), cmd.ExecuteScalar().ToString());
}
}
}

[TestMethod]
[WorkItem(13)]
public void UserSubmittedIssue_WKT3()
{
using (var conn = new System.Data.SqlClient.SqlConnection(DBTests.ConnectionString))
{
conn.Open();
var id = SqlGeography.Parse("LINESTRING (-122.36 47.656, -122.343 47.656)");
var l = id.STPointN(1).Long;
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT Cast(geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326) as geography)";

Assert.AreEqual(id.ToString(), cmd.ExecuteScalar().ToString());
}
}
}

[TestMethod]
[WorkItem(14)]
public void UserSubmittedIssue_WKT4()
{
using (var conn = new System.Data.SqlClient.SqlConnection(DBTests.ConnectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT Cast(geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326) as geography)";
var result = cmd.ExecuteScalar();
Assert.IsInstanceOfType(result, typeof(SqlGeography));
SqlGeography g = (SqlGeography)result;
Assert.AreEqual("LineString", g.STGeometryType());
Assert.AreEqual(2, g.STNumPoints());
Assert.AreEqual(47.656, g.STPointN(1).Lat.Value);
Assert.AreEqual(-122.360, g.STPointN(1).Long.Value);
Assert.AreEqual(47.656, g.STPointN(2).Lat.Value);
Assert.AreEqual(-122.343, g.STPointN(2).Long.Value);
Assert.AreEqual("LINESTRING (-122.36 47.656, -122.343 47.656)", cmd.ExecuteScalar().ToString());
}
}
}
}
}
35 changes: 22 additions & 13 deletions src/Microsoft.SqlServer.Types.Tests/Geometry/DBTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,38 @@ public class DBTests : IDisposable

private System.Data.SqlClient.SqlConnection conn;
private static string path;
private static object lockObj = new object();
static DBTests()
{
path = Path.Combine(new FileInfo(typeof(DBTests).Assembly.Location).Directory.FullName, "UnitTestData.mdf");
CreateSqlDatabase(path);
using (var conn = new System.Data.SqlClient.SqlConnection(connstr + path))
Init();
}
public static void Init()
{
lock(lockObj)
if(path == null)
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = OgcConformanceMap.DropTables;
cmd.ExecuteNonQuery();
cmd.CommandText = OgcConformanceMap.CreateTables;
cmd.ExecuteNonQuery();
cmd.CommandText = OgcConformanceMap.CreateRows;
cmd.ExecuteNonQuery();
conn.Close();
path = Path.Combine(new FileInfo(typeof(DBTests).Assembly.Location).Directory.FullName, "UnitTestData.mdf");
CreateSqlDatabase(path);
using (var conn = new System.Data.SqlClient.SqlConnection(connstr + path))
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = OgcConformanceMap.DropTables;
cmd.ExecuteNonQuery();
cmd.CommandText = OgcConformanceMap.CreateTables;
cmd.ExecuteNonQuery();
cmd.CommandText = OgcConformanceMap.CreateRows;
cmd.ExecuteNonQuery();
conn.Close();
}
}
}

public static string ConnectionString => connstr + path;


public DBTests()
{
Init();
conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
conn.Open();
}
Expand Down
24 changes: 21 additions & 3 deletions src/Microsoft.SqlServer.Types.Tests/Geometry/WktTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Microsoft.SqlServer.Types.Tests.Geometry
{
[TestClass]
[TestCategory("SqlGeometry")]
[TestCategory("WKT")]
public class WktTests
{
public object StreamExtensions { get; private set; }

[TestMethod]
public void TestNullToString()
{
Expand Down Expand Up @@ -45,6 +45,7 @@ public void TestLineStringToString()
var str = g.ToString();
Assert.AreEqual("LINESTRING (0 1 1, 3 2 2, 4 5)", str);
}

[TestMethod]
public void TestLineStringFromString()
{
Expand Down Expand Up @@ -78,6 +79,23 @@ public void TestLineStringFromString()
Assert.IsTrue(p3.M.IsNull);
}


[TestMethod]
[WorkItem(13)]
public void UserSubmittedIssue_WKT1()
{
using (var conn = new System.Data.SqlClient.SqlConnection(DBTests.ConnectionString))
{
conn.Open();
var id = SqlGeometry.Parse("LINESTRING (100 100, 20 180, 180 180)");
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT @p";
var p =cmd.Parameters.Add("@p", System.Data.SqlDbType.Udt);
p.UdtTypeName = "geometry";
p.Value = id;
Assert.AreEqual(id.ToString(), cmd.ExecuteScalar().ToString());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,4 @@
</Compile>
</ItemGroup>

<ItemGroup>
<Folder Include="Geography\" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions src/Microsoft.SqlServer.Types/SqlGeography.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public static SqlGeography STGeomFromText(SqlChars geometryTaggedText, int srid)
{
if (geometryTaggedText.IsNull)
return SqlGeography.Null;
var data = Wkt.WktReader.Parse(geometryTaggedText.ToString(), Wkt.WktReader.CoordinateOrder.LatLong);
var data = Wkt.WktReader.Parse(geometryTaggedText.ToString(), Wkt.CoordinateOrder.LatLong);
return new SqlGeography(data, srid);
}

Expand All @@ -433,7 +433,7 @@ public static SqlGeography Parse(SqlString s)
{
if (s.IsNull)
return SqlGeography.Null;
var data = Wkt.WktReader.Parse(s.ToString(), Wkt.WktReader.CoordinateOrder.LatLong);
var data = Wkt.WktReader.Parse(s.ToString(), Wkt.CoordinateOrder.LatLong);
return new SqlGeography(data, 4326);
}

Expand Down Expand Up @@ -495,6 +495,6 @@ public SqlBytes Serialize()
}
}

public override string ToString() => WktWriter.Write(_geometry);
public override string ToString() => Wkt.WktWriter.Write(_geometry, Wkt.CoordinateOrder.LatLong);
}
}
6 changes: 3 additions & 3 deletions src/Microsoft.SqlServer.Types/SqlGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public static SqlGeometry STGeomFromText(SqlChars geometryTaggedText, int srid)
{
if (geometryTaggedText.IsNull)
return SqlGeometry.Null;
var data = Wkt.WktReader.Parse(geometryTaggedText.ToString(), Wkt.WktReader.CoordinateOrder.XY);
var data = Wkt.WktReader.Parse(geometryTaggedText.ToString(), Wkt.CoordinateOrder.XY);
return new SqlGeometry(data, srid);
}

Expand All @@ -432,7 +432,7 @@ public static SqlGeometry Parse(SqlString s)
{
if (s.IsNull)
return SqlGeometry.Null;
var data = Wkt.WktReader.Parse(s.ToString(), Wkt.WktReader.CoordinateOrder.XY);
var data = Wkt.WktReader.Parse(s.ToString(), Wkt.CoordinateOrder.XY);
return new SqlGeometry(data, 0);
}

Expand All @@ -449,6 +449,6 @@ public void Write(BinaryWriter w)
_geometry.Write(w);
}

public override string ToString() => WktWriter.Write(_geometry);
public override string ToString() => Wkt.WktWriter.Write(_geometry, Wkt.CoordinateOrder.XY);
}
}
12 changes: 6 additions & 6 deletions src/Microsoft.SqlServer.Types/Wkt/WktReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

namespace Microsoft.SqlServer.Types.Wkt
{
internal enum CoordinateOrder
{
XY,
LatLong
}

internal class WktReader
{
private int length = 0;
Expand All @@ -23,12 +29,6 @@ internal class WktReader
List<Shape> _shapes;
CoordinateOrder _order;

public enum CoordinateOrder
{
XY,
LatLong
}

private WktReader(string str)
{
if (string.IsNullOrEmpty(str))
Expand Down
Loading

0 comments on commit 16b9c3e

Please sign in to comment.