Skip to content

Commit dea490c

Browse files
committed
Move prepared command tests to new class.
1 parent b365445 commit dea490c

File tree

2 files changed

+108
-94
lines changed

2 files changed

+108
-94
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using MySql.Data.MySqlClient;
4+
using Xunit;
5+
6+
namespace SideBySide
7+
{
8+
public class PreparedCommandTests : IClassFixture<DatabaseFixture>
9+
{
10+
public PreparedCommandTests(DatabaseFixture database)
11+
{
12+
}
13+
14+
[Theory]
15+
[MemberData(nameof(GetInsertAndQueryData))]
16+
public void InsertAndQuery(bool isPrepared, string dataType, object dataValue)
17+
{
18+
var csb = new MySqlConnectionStringBuilder(AppConfig.ConnectionString)
19+
{
20+
IgnorePrepare = !isPrepared,
21+
};
22+
using (var connection = new MySqlConnection(csb.ConnectionString))
23+
{
24+
connection.Open();
25+
using (var command = new MySqlCommand($@"DROP TABLE IF EXISTS prepared_command_test;
26+
CREATE TABLE prepared_command_test(rowid INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, data {dataType});", connection))
27+
{
28+
command.ExecuteNonQuery();
29+
}
30+
31+
using (var command = new MySqlCommand("INSERT INTO prepared_command_test(data) VALUES(@null), (@data);", connection))
32+
{
33+
command.Parameters.AddWithValue("@null", null);
34+
command.Parameters.AddWithValue("@data", dataValue);
35+
if (isPrepared)
36+
command.Prepare();
37+
Assert.Equal(isPrepared, command.IsPrepared);
38+
command.ExecuteNonQuery();
39+
}
40+
41+
using (var command = new MySqlCommand("SELECT data FROM prepared_command_test ORDER BY rowid;", connection))
42+
{
43+
if (isPrepared)
44+
command.Prepare();
45+
Assert.Equal(isPrepared, command.IsPrepared);
46+
47+
using (var reader = command.ExecuteReader())
48+
{
49+
Assert.True(reader.Read());
50+
Assert.True(reader.IsDBNull(0));
51+
52+
Assert.True(reader.Read());
53+
Assert.False(reader.IsDBNull(0));
54+
Assert.Equal(dataValue, reader.GetValue(0));
55+
56+
Assert.False(reader.Read());
57+
Assert.False(reader.NextResult());
58+
}
59+
}
60+
}
61+
}
62+
63+
public static IEnumerable<object[]> GetInsertAndQueryData()
64+
{
65+
foreach (var isPrepared in new[] { false, true })
66+
{
67+
yield return new object[] { isPrepared, "TINYINT", (sbyte) -123 };
68+
yield return new object[] { isPrepared, "TINYINT UNSIGNED", (byte) 123 };
69+
yield return new object[] { isPrepared, "SMALLINT", (short) -12345 };
70+
yield return new object[] { isPrepared, "SMALLINT UNSIGNED", (ushort) 12345 };
71+
yield return new object[] { isPrepared, "MEDIUMINT", -1234567 };
72+
yield return new object[] { isPrepared, "MEDIUMINT UNSIGNED", 1234567u };
73+
yield return new object[] { isPrepared, "INT", -123456789 };
74+
yield return new object[] { isPrepared, "INT UNSIGNED", 123456789u };
75+
yield return new object[] { isPrepared, "BIGINT", -1234567890123456789L };
76+
yield return new object[] { isPrepared, "BIGINT UNSIGNED", 1234567890123456789UL };
77+
yield return new object[] { isPrepared, "BIT(10)", 1000UL };
78+
yield return new object[] { isPrepared, "BINARY(5)", new byte[] { 5, 6, 7, 8, 9 } };
79+
yield return new object[] { isPrepared, "VARBINARY(100)", new byte[] { 7, 8, 9, 10 } };
80+
yield return new object[] { isPrepared, "BLOB", new byte[] { 5, 4, 3, 2, 1 } };
81+
yield return new object[] { isPrepared, "CHAR(36)", new Guid("00112233-4455-6677-8899-AABBCCDDEEFF") };
82+
yield return new object[] { isPrepared, "FLOAT", 12.375f };
83+
yield return new object[] { isPrepared, "DOUBLE", 14.21875 };
84+
yield return new object[] { isPrepared, "DECIMAL(9,3)", 123.45m };
85+
yield return new object[] { isPrepared, "VARCHAR(100)", "test;@'; -- " };
86+
yield return new object[] { isPrepared, "TEXT", "testing testing" };
87+
yield return new object[] { isPrepared, "DATE", new DateTime(2018, 7, 23) };
88+
yield return new object[] { isPrepared, "DATETIME(3)", new DateTime(2018, 7, 23, 20, 46, 52, 123) };
89+
yield return new object[] { isPrepared, "ENUM('small', 'medium', 'large')", "medium" };
90+
yield return new object[] { isPrepared, "SET('one','two','four','eight')", "two,eight" };
91+
92+
#if !BASELINE
93+
// https://bugs.mysql.com/bug.php?id=78917
94+
yield return new object[] { isPrepared, "BOOL", true };
95+
96+
// https://bugs.mysql.com/bug.php?id=91770
97+
yield return new object[] { isPrepared, "TIME(3)", TimeSpan.Zero.Subtract(new TimeSpan(15, 10, 34, 56, 789)) };
98+
99+
// https://bugs.mysql.com/bug.php?id=91751
100+
yield return new object[] { isPrepared, "YEAR", 2134 };
101+
#endif
102+
103+
if (AppConfig.SupportsJson)
104+
yield return new object[] { isPrepared, "JSON", "{\"test\": true}" };
105+
}
106+
}
107+
}
108+
}

tests/SideBySide/QueryTests.cs

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -989,100 +989,6 @@ enum TestLongEnum : long
989989
Value = long.MaxValue,
990990
}
991991

992-
[Theory]
993-
[MemberData(nameof(GetPreparedCommandsData))]
994-
public void PreparedCommands(bool isPrepared, string dataType, object dataValue)
995-
{
996-
var csb = new MySqlConnectionStringBuilder(AppConfig.ConnectionString)
997-
{
998-
IgnorePrepare = !isPrepared,
999-
};
1000-
using (var connection = new MySqlConnection(csb.ConnectionString))
1001-
{
1002-
connection.Open();
1003-
using (var command = new MySqlCommand($@"DROP TABLE IF EXISTS prepared_command_test;
1004-
CREATE TABLE prepared_command_test(rowid INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, data {dataType});", connection))
1005-
{
1006-
command.ExecuteNonQuery();
1007-
}
1008-
1009-
using (var command = new MySqlCommand("INSERT INTO prepared_command_test(data) VALUES(@null), (@data);", connection))
1010-
{
1011-
command.Parameters.AddWithValue("@null", null);
1012-
command.Parameters.AddWithValue("@data", dataValue);
1013-
if (isPrepared)
1014-
command.Prepare();
1015-
Assert.Equal(isPrepared, command.IsPrepared);
1016-
command.ExecuteNonQuery();
1017-
}
1018-
1019-
using (var command = new MySqlCommand("SELECT data FROM prepared_command_test ORDER BY rowid;", connection))
1020-
{
1021-
if (isPrepared)
1022-
command.Prepare();
1023-
Assert.Equal(isPrepared, command.IsPrepared);
1024-
1025-
using (var reader = command.ExecuteReader())
1026-
{
1027-
Assert.True(reader.Read());
1028-
Assert.True(reader.IsDBNull(0));
1029-
1030-
Assert.True(reader.Read());
1031-
Assert.False(reader.IsDBNull(0));
1032-
Assert.Equal(dataValue, reader.GetValue(0));
1033-
1034-
Assert.False(reader.Read());
1035-
Assert.False(reader.NextResult());
1036-
}
1037-
}
1038-
}
1039-
}
1040-
1041-
public static IEnumerable<object[]> GetPreparedCommandsData()
1042-
{
1043-
foreach (var isPrepared in new[] { false, true })
1044-
{
1045-
yield return new object[] { isPrepared, "TINYINT", (sbyte) -123 };
1046-
yield return new object[] { isPrepared, "TINYINT UNSIGNED", (byte) 123 };
1047-
yield return new object[] { isPrepared, "SMALLINT", (short) -12345 };
1048-
yield return new object[] { isPrepared, "SMALLINT UNSIGNED", (ushort) 12345 };
1049-
yield return new object[] { isPrepared, "MEDIUMINT", -1234567 };
1050-
yield return new object[] { isPrepared, "MEDIUMINT UNSIGNED", 1234567u };
1051-
yield return new object[] { isPrepared, "INT", -123456789 };
1052-
yield return new object[] { isPrepared, "INT UNSIGNED", 123456789u };
1053-
yield return new object[] { isPrepared, "BIGINT", -1234567890123456789L };
1054-
yield return new object[] { isPrepared, "BIGINT UNSIGNED", 1234567890123456789UL };
1055-
yield return new object[] { isPrepared, "BIT(10)", 1000UL };
1056-
yield return new object[] { isPrepared, "BINARY(5)", new byte[] { 5, 6, 7, 8, 9 } };
1057-
yield return new object[] { isPrepared, "VARBINARY(100)", new byte[] { 7, 8, 9, 10 } };
1058-
yield return new object[] { isPrepared, "BLOB", new byte[] { 5, 4, 3, 2, 1 } };
1059-
yield return new object[] { isPrepared, "CHAR(36)", new Guid("00112233-4455-6677-8899-AABBCCDDEEFF") };
1060-
yield return new object[] { isPrepared, "FLOAT", 12.375f };
1061-
yield return new object[] { isPrepared, "DOUBLE", 14.21875 };
1062-
yield return new object[] { isPrepared, "DECIMAL(9,3)", 123.45m };
1063-
yield return new object[] { isPrepared, "VARCHAR(100)", "test;@'; -- " };
1064-
yield return new object[] { isPrepared, "TEXT", "testing testing" };
1065-
yield return new object[] { isPrepared, "DATE", new DateTime(2018, 7, 23) };
1066-
yield return new object[] { isPrepared, "DATETIME(3)", new DateTime(2018, 7, 23, 20, 46, 52, 123) };
1067-
yield return new object[] { isPrepared, "ENUM('small', 'medium', 'large')", "medium" };
1068-
yield return new object[] { isPrepared, "SET('one','two','four','eight')", "two,eight" };
1069-
1070-
#if !BASELINE
1071-
// https://bugs.mysql.com/bug.php?id=78917
1072-
yield return new object[] { isPrepared, "BOOL", true };
1073-
1074-
// https://bugs.mysql.com/bug.php?id=91770
1075-
yield return new object[] { isPrepared, "TIME(3)", TimeSpan.Zero.Subtract(new TimeSpan(15, 10, 34, 56, 789)) };
1076-
1077-
// https://bugs.mysql.com/bug.php?id=91751
1078-
yield return new object[] { isPrepared, "YEAR", 2134 };
1079-
#endif
1080-
1081-
if (AppConfig.SupportsJson)
1082-
yield return new object[] { isPrepared, "JSON", "{\"test\": true}" };
1083-
}
1084-
}
1085-
1086992
readonly DatabaseFixture m_database;
1087993
}
1088994
}

0 commit comments

Comments
 (0)