Skip to content

Commit 11cd36d

Browse files
committed
Add tests for GetMySqlGeometry.
1 parent 74544b3 commit 11cd36d

File tree

1 file changed

+58
-24
lines changed

1 file changed

+58
-24
lines changed

tests/SideBySide/DataTypes.cs

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
using GetValueWhenNullException = System.Data.SqlTypes.SqlNullValueException;
1919
using GetGuidWhenNullException = MySql.Data.MySqlClient.MySqlException;
2020
using GetBytesWhenNullException = System.NullReferenceException;
21+
using GetGeometryWhenNullException = System.Exception;
2122
#else
2223
using GetValueWhenNullException = System.InvalidCastException;
2324
using GetGuidWhenNullException = System.InvalidCastException;
2425
using GetBytesWhenNullException = System.InvalidCastException;
26+
using GetGeometryWhenNullException = System.InvalidCastException;
2527
#endif
2628

2729
namespace SideBySide
@@ -1009,23 +1011,50 @@ public void GetMySqlDateTime(string columnName)
10091011
}
10101012

10111013
[Fact]
1012-
public void Geometry()
1014+
public void QueryGeometry()
10131015
{
1014-
DoQuery("geometry", "Geometry", "GEOMETRY", new object[]
1016+
var geometryData = new byte[][]
10151017
{
10161018
null,
10171019
new byte[] { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 240, 63 },
10181020
new byte[] { 0, 0, 0, 0, 1, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 64 }
1019-
},
1021+
};
1022+
1023+
DoQuery("geometry", "Geometry", "GEOMETRY", geometryData.ToArray(),
10201024
#if !BASELINE
10211025
GetBytes
10221026
#else
10231027
// NOTE: Connector/NET returns 'null' for NULL so simulate an exception for the tests
10241028
x => x.IsDBNull(0) ? throw new GetValueWhenNullException() : x.GetValue(0)
1029+
#endif
1030+
);
1031+
1032+
DoQuery<GetGeometryWhenNullException>("geometry", "Geometry", "GEOMETRY", geometryData.Select(CreateGeometry).ToArray(),
1033+
reader => reader.GetMySqlGeometry(0),
1034+
matchesDefaultType: false,
1035+
#if BASELINE
1036+
omitGetFieldValueTest: true, // https://bugs.mysql.com/bug.php?id=96500
1037+
omitWhereTest: true, // https://bugs.mysql.com/bug.php?id=96498
1038+
#endif
1039+
#if BASELINE
1040+
assertEqual: (x, y) => Assert.Equal(((MySqlGeometry) x).Value, ((MySqlGeometry) y).Value)
1041+
#else
1042+
assertEqual: (x, y) => Assert.Equal(((MySqlGeometry) x)?.Value.ToArray(), ((MySqlGeometry) y)?.Value.ToArray())
10251043
#endif
10261044
);
10271045
}
10281046

1047+
private static object CreateGeometry(byte[] data)
1048+
{
1049+
if (data is null)
1050+
return null;
1051+
#if BASELINE
1052+
return new MySqlGeometry(MySqlDbType.Geometry, data);
1053+
#else
1054+
return MySqlGeometry.FromMySql(data);
1055+
#endif
1056+
}
1057+
10291058
#if !NETCOREAPP1_1_2
10301059
[Theory]
10311060
[InlineData("Bit1", "datatypes_bits", MySqlDbType.Bit, 1, typeof(ulong), "N", 0, 0)]
@@ -1364,9 +1393,10 @@ private void DoQuery(
13641393
bool matchesDefaultType = true,
13651394
MySqlConnection connection = null,
13661395
Action<object, object> assertEqual = null,
1367-
Type getFieldValueType = null)
1396+
Type getFieldValueType = null,
1397+
bool omitGetFieldValueTest = false)
13681398
{
1369-
DoQuery<GetValueWhenNullException>(table, column, dataTypeName, expected, getValue, baselineCoercedNullValue, omitWhereTest, matchesDefaultType, connection, assertEqual, getFieldValueType);
1399+
DoQuery<GetValueWhenNullException>(table, column, dataTypeName, expected, getValue, baselineCoercedNullValue, omitWhereTest, matchesDefaultType, connection, assertEqual, getFieldValueType, omitGetFieldValueTest);
13701400
}
13711401

13721402
// NOTE: baselineCoercedNullValue is to work around inconsistencies in mysql-connector-net; DBNull.Value will
@@ -1382,7 +1412,8 @@ private void DoQuery<TException>(
13821412
bool matchesDefaultType = true,
13831413
MySqlConnection connection = null,
13841414
Action<object, object> assertEqual = null,
1385-
Type getFieldValueType = null)
1415+
Type getFieldValueType = null,
1416+
bool omitGetFieldValueTest = false)
13861417
where TException : Exception
13871418
{
13881419
connection = connection ?? Connection;
@@ -1421,24 +1452,27 @@ private void DoQuery<TException>(
14211452
Assert.Equal(value.GetType(), reader.GetFieldType(column.Replace("`", "")));
14221453
}
14231454

1424-
// test `reader.GetFieldValue<value.GetType()>`
1425-
var syncMethod = typeof(MySqlDataReader)
1426-
.GetMethod("GetFieldValue")
1427-
.MakeGenericMethod(getFieldValueType ?? value.GetType());
1428-
assertEqual(value, syncMethod.Invoke(reader, new object[] { 0 }));
1429-
1430-
// test `reader.GetFieldValueAsync<value.GetType()>`
1431-
var asyncMethod = typeof(MySqlDataReader)
1432-
.GetMethod("GetFieldValueAsync", new[] { typeof(int) })
1433-
.MakeGenericMethod(getFieldValueType ?? value.GetType());
1434-
var asyncMethodValue = asyncMethod.Invoke(reader, new object[] { 0 });
1435-
var asyncMethodGetAwaiter = asyncMethodValue.GetType()
1436-
.GetMethod("GetAwaiter");
1437-
var asyncMethodGetAwaiterValue = asyncMethodGetAwaiter.Invoke(asyncMethodValue, new object[] { });
1438-
var asyncMethodGetResult = asyncMethodGetAwaiterValue.GetType()
1439-
.GetMethod("GetResult");
1440-
var asyncMethodGetResultValue = asyncMethodGetResult.Invoke(asyncMethodGetAwaiterValue, new object[] { });
1441-
assertEqual(value, asyncMethodGetResultValue);
1455+
if (!omitGetFieldValueTest)
1456+
{
1457+
// test `reader.GetFieldValue<value.GetType()>`
1458+
var syncMethod = typeof(MySqlDataReader)
1459+
.GetMethod("GetFieldValue")
1460+
.MakeGenericMethod(getFieldValueType ?? value.GetType());
1461+
assertEqual(value, syncMethod.Invoke(reader, new object[] { 0 }));
1462+
1463+
// test `reader.GetFieldValueAsync<value.GetType()>`
1464+
var asyncMethod = typeof(MySqlDataReader)
1465+
.GetMethod("GetFieldValueAsync", new[] { typeof(int) })
1466+
.MakeGenericMethod(getFieldValueType ?? value.GetType());
1467+
var asyncMethodValue = asyncMethod.Invoke(reader, new object[] { 0 });
1468+
var asyncMethodGetAwaiter = asyncMethodValue.GetType()
1469+
.GetMethod("GetAwaiter");
1470+
var asyncMethodGetAwaiterValue = asyncMethodGetAwaiter.Invoke(asyncMethodValue, new object[] { });
1471+
var asyncMethodGetResult = asyncMethodGetAwaiterValue.GetType()
1472+
.GetMethod("GetResult");
1473+
var asyncMethodGetResultValue = asyncMethodGetResult.Invoke(asyncMethodGetAwaiterValue, new object[] { });
1474+
assertEqual(value, asyncMethodGetResultValue);
1475+
}
14421476
}
14431477
}
14441478
Assert.False(reader.Read());

0 commit comments

Comments
 (0)