Skip to content

Commit

Permalink
Implemented CodeRabbit requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Riku Virtanen committed Nov 26, 2024
1 parent 0b6b901 commit 5fd4b44
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,59 +145,50 @@ public async Task TestWithGeographyData()
ThrowErrorOnFailure = true
};

var createInput = new Input
var input = new Input
{
ConnectionString = _connString,
Query = $"IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{table}') BEGIN CREATE TABLE {table} ( Id int IDENTITY(1, 1), GeogCol1 geography, GeogCol2 AS GeogCol1.STAsText()); END",
ExecuteType = ExecuteTypes.Auto,
Parameters = null
};

var create = await MicrosoftSQL.ExecuteQuery(createInput, options, default);
Assert.IsTrue(create.Success, "Create table");

var insert1Input = new Input
try
{
ConnectionString = _connString,
Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));",
ExecuteType = ExecuteTypes.Auto,
Parameters = null
};
var create = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(create.Success, "Create table");

var insert1 = await MicrosoftSQL.ExecuteQuery(insert1Input, options, default);
Assert.IsTrue(insert1.Success, "First insert");
input.Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));";

var insert2Input = new Input
{
ConnectionString = _connString,
Query = $"INSERT INTO {table} (GeogCol1) VALUES(geography::STGeomFromText('POLYGON((-122.358 47.653 , -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326));",
ExecuteType = ExecuteTypes.Auto,
Parameters = null
};
var insert1 = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(insert1.Success, "First insert");

var insert2 = await MicrosoftSQL.ExecuteQuery(insert2Input, options, default);
Assert.IsTrue(insert2.Success, "Second insert");
input.Query = $"INSERT INTO {table} (GeogCol1) VALUES(geography::STGeomFromText('POLYGON((-122.358 47.653 , -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326));";

var selectInput = new Input
{
ConnectionString = _connString,
Query = $"SELECT * From {table}",
ExecuteType = ExecuteTypes.Auto,
Parameters = null
};
var insert2 = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(insert2.Success, "Second insert");

input.Query = $"SELECT * From {table}";

var select = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(select.Success, "Select");
Assert.AreEqual(typeof(JArray), select.Data.GetType());
Assert.AreEqual(2, select.Data.Count);

var select = await MicrosoftSQL.ExecuteQuery(selectInput, options, default);
Assert.IsTrue(select.Success, "Select");
// Verify first row (LINESTRING)
Assert.IsNotNull(select.Data[0]["GeogCol1"]);
Assert.IsTrue(select.Data[0]["GeogCol2"].ToString().StartsWith("LINESTRING"));

var dropInput = new Input
// Verify second row (POLYGON)
Assert.IsNotNull(select.Data[1]["GeogCol1"]);
Assert.IsTrue(select.Data[1]["GeogCol2"].ToString().StartsWith("POLYGON"));
}
finally
{
ConnectionString = _connString,
Query = $"DROP TABLE {table}",
ExecuteType = ExecuteTypes.Auto,
Parameters = null
};
input.Query = $"DROP TABLE {table}";

var drop = await MicrosoftSQL.ExecuteQuery(dropInput, options, default);
Assert.IsTrue(drop.Success, "Drop");
var drop = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(drop.Success, "Drop");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Frends.MicrosoftSQL.ExecuteQuery.Definitions;
using Frends.MicrosoftSQL.ExecuteQuery.Tests.Lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;

namespace Frends.MicrosoftSQL.ExecuteQuery.Tests;

Expand Down Expand Up @@ -151,29 +152,37 @@ public async Task TestWithGeographyData_Scalar()
Parameters = null
};

var create = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(create.Success, "Create table");

input.Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));";
try
{
var create = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(create.Success, "Create table");

var insert1 = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(insert1.Success, "First insert");
input.Query = $"INSERT INTO {table} (GeogCol1) VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));";

input.Query = $"INSERT INTO {table} (GeogCol1) VALUES(geography::STGeomFromText('POLYGON((-122.358 47.653 , -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326));";
var insert1 = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(insert1.Success, "First insert");

var insert2 = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(insert2.Success, "Second insert");
input.Query = $"INSERT INTO {table} (GeogCol1) VALUES(geography::STGeomFromText('POLYGON((-122.358 47.653 , -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326));";

input.Query = $"SELECT GeogCol1 From {table}";
input.ExecuteType = ExecuteTypes.Scalar;
var insert2 = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(insert2.Success, "Second insert");

var select = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(select.Success, "Select");
input.Query = $"SELECT GeogCol1 From {table}";
input.ExecuteType = ExecuteTypes.Scalar;

input.Query = $"DROP TABLE {table}";
input.ExecuteType = ExecuteTypes.Auto;
var select = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(select.Success, "Select");
Assert.IsNotNull(select.Data["Value"], "Selected data should not be null");
Assert.IsInstanceOfType(select.Data["Value"], typeof(JValue), "Geography data should be converted to string");
Assert.IsTrue(((string)select.Data["Value"]).StartsWith("LINESTRING"), "First row should be a LINESTRING");
}
finally
{
input.Query = $"DROP TABLE {table}";
input.ExecuteType = ExecuteTypes.Auto;

var drop = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(drop.Success, "Drop");
var drop = await MicrosoftSQL.ExecuteQuery(input, options, default);
Assert.IsTrue(drop.Success, "Drop");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ private static async Task<JToken> LoadData(SqlDataReader reader, CancellationTok
var row = new JObject();
for (var i = 0; i < reader.FieldCount; i++)
{
dynamic value;
if (reader.GetValue(i).GetType() == typeof(DBNull))
object fieldValue = reader.GetValue(i);
object value;
if (fieldValue == DBNull.Value)
value = null;
else if (reader.GetValue(i).GetType() == typeof(SqlGeography))
value = reader.GetValue(i).ToString();
else if (fieldValue is SqlGeography geography)
value = geography.ToString();
else
value = reader.GetValue(i);
value = fieldValue;

row.Add(new JProperty(reader.GetName(i), value));
}
Expand Down

0 comments on commit 5fd4b44

Please sign in to comment.