-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix SqlHierarchyId serialization and deserialization (#55)
* fix SqlHierarchyId serialization and deserialization * add proper HierarchyDbTests
- Loading branch information
1 parent
6e464be
commit c06d736
Showing
8 changed files
with
262 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
src/Microsoft.SqlServer.Types.Tests/HierarchyId/HierarchyDbTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
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 : IDisposable | ||
{ | ||
const string DataSource = @"Data Source=(localdb)\mssqllocaldb;Integrated Security=True;AttachDbFileName="; | ||
|
||
|
||
public static string DropTables = @"IF OBJECT_ID('dbo.TreeNode', 'U') IS NOT NULL DROP TABLE TreeNode;"; | ||
|
||
public static string CreateTables = @" | ||
-- hierarchy_tests | ||
CREATE TABLE [dbo].[TreeNode]([Id] [int] IDENTITY(1,1) NOT NULL, [Route] [hierarchyid] NOT NULL); | ||
"; | ||
|
||
private readonly SqlConnection _connection; | ||
private static string _path = null!; | ||
private static readonly object lockObj = new object(); | ||
static HierarchyDbTests() | ||
{ | ||
Init(); | ||
} | ||
|
||
private static void Init() | ||
{ | ||
lock (lockObj) | ||
{ | ||
if (_path == null) | ||
{ | ||
_path = Path.Combine(new FileInfo(typeof(HierarchyDbTests).Assembly.Location).Directory.FullName, "HierarchyUnitTestData.mdf"); | ||
DatabaseUtil.CreateSqlDatabase(_path); | ||
using (var conn = new SqlConnection(DataSource + _path)) | ||
{ | ||
conn.Open(); | ||
var cmd = conn.CreateCommand(); | ||
cmd.CommandText = DropTables; | ||
cmd.ExecuteNonQuery(); | ||
cmd.CommandText = CreateTables; | ||
cmd.ExecuteNonQuery(); | ||
conn.Close(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static string ConnectionString => DataSource + _path; | ||
|
||
public HierarchyDbTests() | ||
{ | ||
Init(); | ||
_connection = new SqlConnection(ConnectionString); | ||
_connection.Open(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_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; | ||
} | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
src/Microsoft.SqlServer.Types.Tests/Microsoft.SqlServer.Types.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.