Skip to content

Commit

Permalink
Hierarchyid (#56)
Browse files Browse the repository at this point in the history
* fix SqlHierarchyId serialization and deserialization (#55)

* fix SqlHierarchyId serialization and deserialization

* add proper HierarchyDbTests

* Clean up test lifecycle

* Test clean up

Co-authored-by: Olmo del Corral <olmo@signumsoftware.com>
  • Loading branch information
dotMorten and olmobrutall committed Dec 11, 2020
1 parent e5625ea commit 3483ab4
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.SqlServer.Types.Tests/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static void Init()
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
private static System.Reflection.Assembly? CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name == "Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" ||
args.Name == "Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" ||
Expand Down
34 changes: 34 additions & 0 deletions src/Microsoft.SqlServer.Types.Tests/DatabaseUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Microsoft.SqlServer.Types.Tests
{
static class DatabaseUtil
{
internal static void CreateSqlDatabase(string filename)
{
string databaseName = System.IO.Path.GetFileNameWithoutExtension(filename);
if (File.Exists(filename))
File.Delete(filename);
if (File.Exists(filename.Replace(".mdf", "_log.ldf")))
File.Delete(filename.Replace(".mdf", "_log.ldf"));
using (var connection = new System.Data.SqlClient.SqlConnection(
@"Data Source=(localdb)\mssqllocaldb;Initial Catalog=master; Integrated Security=true;"))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText =
String.Format("CREATE DATABASE {0} ON PRIMARY (NAME={0}, FILENAME='{1}')", databaseName, filename);
command.ExecuteNonQuery();

command.CommandText =
String.Format("EXEC sp_detach_db '{0}', 'true'", databaseName);
command.ExecuteNonQuery();
}
}
}
}
}
77 changes: 21 additions & 56 deletions src/Microsoft.SqlServer.Types.Tests/Geometry/DBTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,37 @@ namespace Microsoft.SqlServer.Types.Tests.Geometry
[TestClass]
[TestCategory("Database")]
[TestCategory("SqlGeometry")]
public class DBTests : IDisposable
public class DBTests
{
const string connstr = @"Data Source=(localdb)\mssqllocaldb;Integrated Security=True;AttachDbFileName=";

private System.Data.SqlClient.SqlConnection conn;
#pragma warning disable CS8618 // Guaranteed to be initialized in class initialize
private static System.Data.SqlClient.SqlConnection conn;
private static string path;
private static object lockObj = new object();
static DBTests()
{
Init();
}
public static void Init()
{
lock(lockObj)
if(path == null)
{
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();
}
}
}

#pragma warning restore CS8618
public static string ConnectionString => connstr + path;

public DBTests()
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
Init();
conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
path = Path.Combine(new FileInfo(typeof(DBTests).Assembly.Location).Directory.FullName, "UnitTestData.mdf");
if (File.Exists(path))
File.Delete(path);
DatabaseUtil.CreateSqlDatabase(path);
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();
}

private static void CreateSqlDatabase(string filename)
{
string databaseName = System.IO.Path.GetFileNameWithoutExtension(filename);
if (File.Exists(filename))
File.Delete(filename);
if (File.Exists(filename.Replace(".mdf","_log.ldf")))
File.Delete(filename.Replace(".mdf", "_log.ldf"));
using (var connection = new System.Data.SqlClient.SqlConnection(
@"Data Source=(localdb)\mssqllocaldb;Initial Catalog=master; Integrated Security=true;"))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText =
String.Format("CREATE DATABASE {0} ON PRIMARY (NAME={0}, FILENAME='{1}')", databaseName, filename);
command.ExecuteNonQuery();

command.CommandText =
String.Format("EXEC sp_detach_db '{0}', 'true'", databaseName);
command.ExecuteNonQuery();
}
}
}
public void Dispose()
[ClassCleanup]
public static void ClassCleanup()
{
conn.Close();
conn.Dispose();
Expand Down Expand Up @@ -198,7 +163,7 @@ public void QueryPolygons()
var geomValue = reader.GetValue(geomColumn) as SqlGeometry;
//var geomValue = SqlGeometry.Deserialize(reader.GetSqlBytes(geomColumn));
Assert.IsInstanceOfType(geomValue, typeof(SqlGeometry));
var g = geomValue as SqlGeometry;
var g = (SqlGeometry)geomValue!;
Assert.IsFalse(g.IsNull);
Assert.AreEqual(101, g.STSrid);
Assert.AreEqual(1, g.STNumGeometries().Value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using Microsoft.SqlServer.Types;
using Microsoft.SqlServer.Types.SqlHierarchy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Data.SqlClient;
using System.IO;
using System.Text;

namespace Microsoft.SqlServer.Types.Tests.HierarchyId
{
Expand All @@ -11,7 +15,7 @@ namespace Microsoft.SqlServer.Types.Tests.HierarchyId
[TestClass]
[TestCategory("Deserialize")]
[TestCategory("SqlHierarchyId")]
public class DeserializeFromBinary
public class DeserializeFromBinaryTests
{

[TestMethod]
Expand Down Expand Up @@ -40,13 +44,13 @@ public void TestSqlHiarchy2()
// The first three fields are the same as in the first example.That is, the first two bits(01) are the L1 field, the second two bits(01) are the O1 field, and the fifth bit(1) is the F1 field.This encodes the / 1 / portion of the logical representation.
// The next 5 bits(00111) are the L2 field, so the next integer is between - 8 and - 1.The following 3 bits(111) are the O2 field, representing the offset 7 from the beginning of this range.Thus, the L2 and O2 fields together encode the integer - 1.The next bit(0) is the F2 field.Because it is 0(zero), this level is fake, and 1 has to be subtracted from the integer yielded by the L2 and O2 fields. Therefore, the L2, O2, and F2 fields together represent -2 in the logical representation of this node.
// The next 3 bits(110) are the L3 field, so the next integer is between 16 and 79.The subsequent 8 bits(00001010) are the L4 field. Removing the anti - ambiguity bits from there(the third bit(0) and the fifth bit(1)) leaves 000010, which is the binary representation of 2.Thus, the integer encoded by the L3 and O3 fields is 16 + 2, which is 18.The next bit(1) is the F3 field, representing the slash(/) after the 18 in the logical representation.The final 6 bits(000000) are the W field, padding the physical representation to the nearest byte.
byte[] bytes = { 0x59,0xFB,0x05,0x40 }; //01011001 11111011 00000101 01000000
byte[] bytes = { 0x59, 0xFB, 0x05, 0x40 }; //01011001 11111011 00000101 01000000
var hid = new Microsoft.SqlServer.Types.SqlHierarchyId();
using (var r = new BinaryReader(new MemoryStream(bytes)))
{
hid.Read(r);
}
Assert.AreEqual("/1/-2.18/", hid.ToString());
}
}
}
}
166 changes: 166 additions & 0 deletions src/Microsoft.SqlServer.Types.Tests/HierarchyId/HierarchyDbTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using Microsoft.SqlServer.Types.SqlHierarchy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Text;

namespace Microsoft.SqlServer.Types.Tests.HierarchyId
{
[TestCategory("Database")]
[TestCategory("SqlHierarchyId")]
[TestClass]
public class HierarchyDbTests
{
const string DataSource = @"Data Source=(localdb)\mssqllocaldb;Integrated Security=True;AttachDbFileName=";

#pragma warning disable CS8618 // Guaranteed to be initialized in class initialize
private static SqlConnection _connection;
private static string _path;
#pragma warning restore CS8618
private static string ConnectionString => DataSource + _path;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
_path = Path.Combine(new FileInfo(typeof(HierarchyDbTests).Assembly.Location).Directory.FullName, "HierarchyUnitTestData.mdf");
if (File.Exists(_path))
File.Delete(_path);
DatabaseUtil.CreateSqlDatabase(_path);
using (var conn = new SqlConnection(DataSource + _path))
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "CREATE TABLE [dbo].[TreeNode]([Id] [int] IDENTITY(1,1) NOT NULL, [Route] [hierarchyid] NOT NULL);";
cmd.ExecuteNonQuery();
conn.Close();
}

_connection = new SqlConnection(ConnectionString);
_connection.Open();
}

[ClassCleanup]
public static void ClassCleanup()
{
_connection.Close();
_connection.Dispose();
}

[DataTestMethod]
[DataRow("/-4294971464/")]
[DataRow("/4294972495/")]
[DataRow("/3.2725686107/")]
[DataRow("/0/")]
[DataRow("/1/")]
[DataRow("/1.0.2/")]
[DataRow("/1.1.2/")]
[DataRow("/1.2.2/")]
[DataRow("/1.3.2/")]
[DataRow("/3.0/")]
public void SerializeDeserialize(string route)
{
var parsed = SqlHierarchyId.Parse(route);
var ms = new MemoryStream();
parsed.Write(new BinaryWriter(ms));
ms.Position = 0;
var dumMem = Dump(ms);
ms.Position = 0;
var roundTrip = new Microsoft.SqlServer.Types.SqlHierarchyId();
roundTrip.Read(new BinaryReader(ms));
if (parsed != roundTrip)
Assert.AreEqual(parsed, roundTrip); //breakpoint here

var id = new SqlCommand($"INSERT INTO [dbo].[TreeNode] (Route) output INSERTED.ID VALUES ('{route}') ", _connection).ExecuteScalar();

using (var reader = new SqlCommand($"SELECT Route FROM [dbo].[TreeNode] WHERE ID = " + id, _connection).ExecuteReader())
{
while (reader.Read())
{
var sqlRoundTrip = new Microsoft.SqlServer.Types.SqlHierarchyId();
var dumSql = Dump(reader.GetStream(0));
Assert.AreEqual(dumMem, dumSql);
sqlRoundTrip.Read(new BinaryReader(reader.GetStream(0)));
if (parsed != sqlRoundTrip)
Assert.AreEqual(parsed, sqlRoundTrip); //breakpoint here
}
}
}

[DataTestMethod]
[DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
public void SerializeDeserializeRandom(string route)
{
SerializeDeserialize(route);
}

private const int CountOfGeneratedCases = 1000;
public static IEnumerable<object[]> GetData()
{
Random r = new Random();
for (var i = 0; i < CountOfGeneratedCases; i++)
{
yield return new object[] { RandomHierarhyId(r)};
}
}

public static string RandomHierarhyId(Random random)
{
StringBuilder sb = new StringBuilder();
sb.Append("/");
var levels = random.Next(4);
for (int i = 0; i < levels; i++)
{
var subLevels = random.Next(1, 4);
for (int j = 0; j < subLevels; j++)
{
var pattern = KnownPatterns.RandomPattern(random);
sb.Append(random.NextLong(pattern.MinValue, pattern.MaxValue + 1).ToString());
if (j < subLevels - 1)
sb.Append(".");
}
sb.Append("/");
}

return sb.ToString();
}

static string Dump(Stream ms)
{
return new BitReader(new BinaryReader(ms)).ToString();
}
}

public static class RandomExtensionMethods
{
/// <summary>
/// Returns a random long from min (inclusive) to max (exclusive)
/// </summary>
/// <param name="random">The given random instance</param>
/// <param name="min">The inclusive minimum bound</param>
/// <param name="max">The exclusive maximum bound. Must be greater than min</param>
public static long NextLong(this Random random, long min, long max)
{
if (max <= min)
throw new ArgumentOutOfRangeException("max", "max must be > min!");

//Working with ulong so that modulo works correctly with values > long.MaxValue
ulong uRange = (ulong)(max - min);

//Prevent a modolo bias; see https://stackoverflow.com/a/10984975/238419
//for more information.
//In the worst case, the expected number of calls is 2 (though usually it's
//much closer to 1) so this loop doesn't really hurt performance at all.
ulong ulongRand;
do
{
byte[] buf = new byte[8];
random.NextBytes(buf);
ulongRand = (ulong)BitConverter.ToInt64(buf, 0);
} while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange);

return (long)(ulongRand % uRange) + min;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<Project Sdk="MSBuild.Sdk.Extras/2.0.54">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
<IsPackable>false</IsPackable>
<nullable>enable</nullable>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Microsoft.SqlServer.Types\SqlHierarchy\BitPattern.cs" Link="HierarchyId\BitPattern.cs" />
<Compile Include="..\Microsoft.SqlServer.Types\SqlHierarchy\BitReader.cs" Link="HierarchyId\BitReader.cs" />
<Compile Include="..\Microsoft.SqlServer.Types\SqlHierarchy\BitWriter.cs" Link="HierarchyId\BitWriter.cs" />
<Compile Include="..\Microsoft.SqlServer.Types\SqlHierarchy\KnownPatterns.cs" Link="HierarchyId\KnownPatterns.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 3483ab4

Please sign in to comment.