SqlParameterTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // SqlParameterTest.cs - NUnit Test Cases for testing the
  3. // SqlParameter class
  4. // Author:
  5. // Senganal T ([email protected])
  6. // Amit Biswas ([email protected])
  7. //
  8. // Copyright (c) 2004 Novell Inc., and the individuals listed
  9. // on the ChangeLog entries.
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Data;
  32. using System.Data.Common;
  33. using System.Data.SqlClient;
  34. using System.Data.SqlTypes;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Data.SqlClient
  37. {
  38. [TestFixture]
  39. [Category ("sqlserver")]
  40. //FIXME : Add more testcases
  41. public class SqlParameterTest
  42. {
  43. //testcase for #77410
  44. [Test]
  45. public void ParameterNullTest ()
  46. {
  47. SqlParameter param = new SqlParameter ("param", SqlDbType.Decimal);
  48. Assert.AreEqual (0, param.Scale, "#1");
  49. param.Value = DBNull.Value;
  50. Assert.AreEqual (0, param.Scale, "#2");
  51. param = new SqlParameter ("param", SqlDbType.Int);
  52. Assert.AreEqual (0, param.Scale, "#3");
  53. param.Value = DBNull.Value;
  54. Assert.AreEqual (0, param.Scale, "#4");
  55. }
  56. [Test]
  57. public void ParameterType ()
  58. {
  59. // If Type is not set, then type is inferred from the value
  60. // assigned. The Type should be inferred everytime Value is assigned
  61. // If value is null or DBNull, then the current Type should be reset to NVarChar.
  62. SqlParameter param = new SqlParameter ();
  63. Assert.AreEqual (SqlDbType.NVarChar, param.SqlDbType, "#1");
  64. param.Value = DBNull.Value;
  65. Assert.AreEqual (SqlDbType.NVarChar, param.SqlDbType, "#2");
  66. param.Value = 1;
  67. Assert.AreEqual (SqlDbType.Int, param.SqlDbType, "#3");
  68. param.Value = DBNull.Value;
  69. Assert.AreEqual (SqlDbType.NVarChar, param.SqlDbType, "#4");
  70. param.Value = null;
  71. Assert.AreEqual (SqlDbType.NVarChar, param.SqlDbType, "#5");
  72. //If Type is set, then the Type should not inferred from the value
  73. //assigned.
  74. SqlParameter param1 = new SqlParameter ();
  75. param1.DbType = DbType.String;
  76. Assert.AreEqual (SqlDbType.NVarChar, param1.SqlDbType, "#6");
  77. param1.Value = 1;
  78. Assert.AreEqual (SqlDbType.NVarChar, param1.SqlDbType, "#7");
  79. SqlParameter param2 = new SqlParameter ();
  80. param2.SqlDbType = SqlDbType.NVarChar;
  81. Assert.AreEqual (SqlDbType.NVarChar, param2.SqlDbType, "#8");
  82. param2.Value = 1;
  83. Assert.AreEqual (SqlDbType.NVarChar, param2.SqlDbType, "#9");
  84. }
  85. [Test]
  86. public void SqlDbTypeTest ()
  87. {
  88. SqlParameter p1 = new SqlParameter ();
  89. Assert.AreEqual (null, p1.Value, "#1 Value of the parameter must be null by default");
  90. Assert.AreEqual (SqlDbType.NVarChar, p1.SqlDbType, "#2 parameters without any value or null value must have SqlDbtype as NVarChar");
  91. Assert.AreEqual (DbType.String, p1.DbType, "#3 parameters without any value must have DbType as NVarChar");
  92. SqlParameter p2 = new SqlParameter ("#4 p2Name", (Object)null);
  93. Assert.AreEqual (null, p2.Value, "#5 Value of the parameter must be null by default");
  94. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#6 parameters without any value or null value must have SqlDbtype as NVarChar");
  95. Assert.AreEqual (DbType.String, p2.DbType, "#7parameters without any value or null value must have DbType as String");
  96. p2.Value = Convert.ToInt32(42);
  97. Assert.AreEqual (42, p2.Value, "#8 Value of the parameter must be 42");
  98. Assert.AreEqual (SqlDbType.Int, p2.SqlDbType, "#9 parameter must have SqlDbtype as Int");
  99. Assert.AreEqual (DbType.Int32, p2.DbType, "#10 parameter must have Dbtype as Int32");
  100. p2.Value = DBNull.Value;
  101. Assert.AreEqual (DBNull.Value, p2.Value, "#11 Value of the parameter must be DBNull.Value");
  102. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#12 parameters without any value or null value must have SqlDbtype as NVarChar");
  103. Assert.AreEqual (DbType.String, p2.DbType, "#13 parameters without any value or null value must have DbType as String");
  104. }
  105. //testcase for #82170
  106. [Test]
  107. public void ParameterSizeTest ()
  108. {
  109. SqlConnection conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
  110. conn.Open ();
  111. string longstring = new String('x', 20480);
  112. SqlCommand cmd;
  113. SqlParameter prm;
  114. cmd = new SqlCommand ("create table #text1 (ID int not null, Val1 ntext)", conn);
  115. cmd.ExecuteNonQuery ();
  116. cmd.CommandText = "INSERT INTO #text1(ID,Val1) VALUES (@ID,@Val1)";
  117. prm = new SqlParameter ();
  118. prm.ParameterName = "@ID";
  119. prm.Value = 1;
  120. cmd.Parameters.Add (prm);
  121. prm = new SqlParameter ();
  122. prm.ParameterName = "@Val1";
  123. prm.Value = longstring;
  124. prm.SqlDbType = SqlDbType.NText; // Comment and enjoy the truncation
  125. cmd.Parameters.Add (prm);
  126. cmd.ExecuteNonQuery ();
  127. cmd = new SqlCommand ("select datalength(Val1) from #text1", conn);
  128. Assert.AreEqual (20480 * 2, cmd.ExecuteScalar (), "#1");
  129. cmd.CommandText = "INSERT INTO #text1(ID,Val1) VALUES (@ID,@Val1)";
  130. prm = new SqlParameter ();
  131. prm.ParameterName = "@ID";
  132. prm.Value = 1;
  133. cmd.Parameters.Add (prm);
  134. prm = new SqlParameter ();
  135. prm.ParameterName = "@Val1";
  136. prm.Value = longstring;
  137. //prm.SqlDbType = SqlDbType.NText;
  138. cmd.Parameters.Add (prm);
  139. cmd.ExecuteNonQuery ();
  140. cmd = new SqlCommand ("select datalength(Val1) from #text1", conn);
  141. Assert.AreEqual (20480 * 2, cmd.ExecuteScalar (), "#2");
  142. cmd.CommandText = "INSERT INTO #text1(ID,Val1) VALUES (@ID,@Val1)";
  143. prm = new SqlParameter ();
  144. prm.ParameterName = "@ID";
  145. prm.Value = 1;
  146. cmd.Parameters.Add (prm);
  147. prm = new SqlParameter ();
  148. prm.ParameterName = "@Val1";
  149. prm.Value = longstring;
  150. prm.SqlDbType = SqlDbType.VarChar;
  151. cmd.Parameters.Add (prm);
  152. cmd.ExecuteNonQuery ();
  153. cmd = new SqlCommand ("select datalength(Val1) from #text1", conn);
  154. Assert.AreEqual (20480 * 2, cmd.ExecuteScalar (), "#3");
  155. cmd = new SqlCommand ("drop table #text1", conn);
  156. cmd.ExecuteNonQuery ();
  157. conn.Close ();
  158. }
  159. #if NET_2_0
  160. [Test]
  161. public void ResetSqlDbTypeTest ()
  162. {
  163. //Parameter with an assigned value but no SqlDbType specified
  164. SqlParameter p1 = new SqlParameter ("foo", 42);
  165. Assert.AreEqual (42, p1.Value, "#1");
  166. Assert.AreEqual (DbType.Int32, p1.DbType, "#2");
  167. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#3");
  168. p1.ResetSqlDbType ();
  169. Assert.AreEqual (DbType.Int32, p1.DbType, "#4 The parameter with value 42 must have DbType as Int32");
  170. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#5 The parameter with value 42 must have SqlDbType as Int");
  171. p1.SqlDbType = SqlDbType.DateTime; //assigning a SqlDbType
  172. Assert.AreEqual (DbType.DateTime, p1.DbType, "#6");
  173. Assert.AreEqual (SqlDbType.DateTime, p1.SqlDbType, "#7");
  174. p1.ResetSqlDbType (); //Resetting SqlDbType
  175. Assert.AreEqual (DbType.Int32, p1.DbType, "#8 Resetting SqlDbType must infer the type from the value");
  176. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#9 Resetting SqlDbType must infer the type from the value");
  177. //Parameter with an assigned SqlDbType but no specified value
  178. SqlParameter p2 = new SqlParameter ("foo", SqlDbType.Int);
  179. Assert.AreEqual (null, p2.Value, "#10");
  180. Assert.AreEqual (DbType.Int32, p2.DbType, "#11");
  181. Assert.AreEqual (SqlDbType.Int, p2.SqlDbType, "#12");
  182. //Although a SqlDbType is specified, calling ResetSqlDbType resets
  183. //the SqlDbType and DbType properties to default values
  184. p2.ResetSqlDbType ();
  185. Assert.AreEqual (DbType.String, p2.DbType, "#13 Resetting SqlDbType must infer the type from the value");
  186. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#14 Resetting SqlDbType must infer the type from the value");
  187. p2.SqlDbType = SqlDbType.DateTime; //assigning a SqlDbType
  188. Assert.AreEqual (DbType.DateTime, p2.DbType, "#15");
  189. Assert.AreEqual (SqlDbType.DateTime, p2.SqlDbType, "#16");
  190. p2.ResetSqlDbType (); //Resetting SqlDbType
  191. Assert.AreEqual (DbType.String, p2.DbType, "#17 Resetting SqlDbType must infer the type from the value");
  192. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#18 Resetting SqlDbType must infer the type from the value");
  193. }
  194. [Test]
  195. public void ResetDbTypeTest ()
  196. {
  197. //Parameter with an assigned value but no DbType specified
  198. SqlParameter p1 = new SqlParameter ("foo", 42);
  199. Assert.AreEqual (42, p1.Value, "#1");
  200. Assert.AreEqual (DbType.Int32, p1.DbType, "#2");
  201. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#3");
  202. p1.ResetDbType ();
  203. Assert.AreEqual (DbType.Int32, p1.DbType, "#4 The parameter with value 42 must have DbType as Int32");
  204. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#5 The parameter with value 42 must have SqlDbType as Int32");
  205. p1.DbType = DbType.DateTime; //assigning a DbType
  206. Assert.AreEqual (DbType.DateTime, p1.DbType, "#6");
  207. Assert.AreEqual (SqlDbType.DateTime, p1.SqlDbType, "#7");
  208. p1.ResetDbType (); //Resetting DbType
  209. Assert.AreEqual (DbType.Int32, p1.DbType, "#8 Resetting DbType must infer the type from the value");
  210. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#9 Resetting DbType must infer the type from the value");
  211. //Parameter with an assigned SqlDbType but no specified value
  212. SqlParameter p2 = new SqlParameter ("foo", SqlDbType.Int);
  213. Assert.AreEqual (null, p2.Value, "#10");
  214. Assert.AreEqual (DbType.Int32, p2.DbType, "#11");
  215. Assert.AreEqual (SqlDbType.Int, p2.SqlDbType, "#12");
  216. //Although a SqlDbType is specified, calling ResetDbType resets
  217. //the SqlDbType and DbType properties to default values
  218. p2.ResetDbType ();
  219. Assert.AreEqual (DbType.String, p2.DbType, "#13 Resetting DbType must infer the type from the value");
  220. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#14 Resetting DbType must infer the type from the value");
  221. p2.DbType = DbType.DateTime; //assigning a SqlDbType
  222. Assert.AreEqual (DbType.DateTime, p2.DbType, "#15");
  223. Assert.AreEqual (SqlDbType.DateTime, p2.SqlDbType, "#16");
  224. p2.ResetDbType (); //Resetting DbType
  225. Assert.AreEqual (DbType.String, p2.DbType, "#17 Resetting DbType must infer the type from the value");
  226. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#18 Resetting DbType must infer the type from the value");
  227. }
  228. [Test]
  229. public void XmlSchemaTest ()
  230. {
  231. SqlParameter p1 = new SqlParameter ();
  232. //Testing default values
  233. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
  234. "#1 Default value for XmlSchemaCollectionDatabase is an empty string");
  235. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionName,
  236. "#2 Default value for XmlSchemaCollectionName is an empty string");
  237. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionOwningSchema,
  238. "#3 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  239. //Changing one property should not affect the remaining two properties
  240. p1.XmlSchemaCollectionDatabase = "database";
  241. Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase,
  242. "#4 Default value for XmlSchemaCollectionDatabase is an empty string");
  243. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionName,
  244. "#5 Default value for XmlSchemaCollectionName is an empty string");
  245. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionOwningSchema,
  246. "#6 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  247. p1.XmlSchemaCollectionName = "name";
  248. Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase,
  249. "#7 Default value for XmlSchemaCollectionDatabase is an empty string");
  250. Assert.AreEqual ("name", p1.XmlSchemaCollectionName,
  251. "#8 Default value for XmlSchemaCollectionName is an empty string");
  252. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionOwningSchema,
  253. "#9 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  254. p1.XmlSchemaCollectionOwningSchema = "schema";
  255. Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase,
  256. "#10 Default value for XmlSchemaCollectionDatabase is an empty string");
  257. Assert.AreEqual ("name", p1.XmlSchemaCollectionName,
  258. "#11 Default value for XmlSchemaCollectionName is an empty string");
  259. Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema,
  260. "#12 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  261. //assigning null value stores default empty string
  262. p1.XmlSchemaCollectionDatabase = null;
  263. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
  264. "#13 Default value for XmlSchemaCollectionDatabase is an empty string");
  265. Assert.AreEqual ("name", p1.XmlSchemaCollectionName,
  266. "#14 Default value for XmlSchemaCollectionName is an empty string");
  267. Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema,
  268. "#15 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  269. p1.XmlSchemaCollectionName = "";
  270. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
  271. "#16 Default value for XmlSchemaCollectionDatabase is an empty string");
  272. Assert.AreEqual ("", p1.XmlSchemaCollectionName,
  273. "#17 Default value for XmlSchemaCollectionName is an empty string");
  274. Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema,
  275. "#18 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  276. //values are not trimmed
  277. p1.XmlSchemaCollectionOwningSchema = " a ";
  278. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
  279. "#19 Default value for XmlSchemaCollectionDatabase is an empty string");
  280. Assert.AreEqual ("", p1.XmlSchemaCollectionName,
  281. "#20 Default value for XmlSchemaCollectionName is an empty string");
  282. Assert.AreEqual (" a ", p1.XmlSchemaCollectionOwningSchema,
  283. "#21 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  284. }
  285. [Test]
  286. public void SourceColumnNullMappingTest ()
  287. {
  288. SqlParameter p1 = new SqlParameter ();
  289. Assert.AreEqual (false, p1.SourceColumnNullMapping, "#1 SourceColumnNullMapping should be false by default");
  290. p1.SourceColumnNullMapping = true;
  291. Assert.AreEqual (true, p1.SourceColumnNullMapping, "#2 SourceColumnNullMapping should be false by default");
  292. }
  293. [Test]
  294. public void ctor7Test ()
  295. {
  296. SqlParameter p1 = new SqlParameter ("p1Name", SqlDbType.VarChar, 20,
  297. ParameterDirection.InputOutput, 0, 0,
  298. "srcCol", DataRowVersion.Original, false,
  299. "foo", "database", "schema", "name");
  300. Assert.AreEqual (DbType.AnsiString, p1.DbType, "#");
  301. Assert.AreEqual (ParameterDirection.InputOutput, p1.Direction, "#");
  302. Assert.AreEqual (false, p1.IsNullable, "#");
  303. //Assert.AreEqual (999, p1.LocaleId, "#");
  304. Assert.AreEqual ("p1Name", p1.ParameterName, "#");
  305. Assert.AreEqual (0, p1.Precision, "#");
  306. Assert.AreEqual (0, p1.Scale, "#");
  307. Assert.AreEqual (20, p1.Size, "#");
  308. Assert.AreEqual ("srcCol", p1.SourceColumn, "#");
  309. Assert.AreEqual (false, p1.SourceColumnNullMapping, "#");
  310. Assert.AreEqual (DataRowVersion.Original, p1.SourceVersion, "#");
  311. Assert.AreEqual (SqlDbType.VarChar, p1.SqlDbType, "#");
  312. //Assert.AreEqual (3210, p1.SqlValue, "#");
  313. Assert.AreEqual ("foo", p1.Value, "#");
  314. Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase, "#");
  315. Assert.AreEqual ("name", p1.XmlSchemaCollectionName, "#");
  316. Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema, "#");
  317. }
  318. [Test]
  319. public void CompareInfoTest ()
  320. {
  321. SqlParameter parameter = new SqlParameter ();
  322. Assert.AreEqual (SqlCompareOptions.None, parameter.CompareInfo, "#1 Default value should be System.Data.SqlTypes.SqlCompareOptions.None");
  323. parameter.CompareInfo = SqlCompareOptions.IgnoreNonSpace;
  324. Assert.AreEqual (SqlCompareOptions.IgnoreNonSpace, parameter.CompareInfo, "#2 It should return CompareOptions.IgnoreSpace after setting this value for the property");
  325. }
  326. [Test]
  327. public void LocaleIdTest ()
  328. {
  329. SqlParameter parameter = new SqlParameter ();
  330. Assert.AreEqual (0, parameter.LocaleId, "#1 Default value for the property should be 0");
  331. parameter.LocaleId = 15;
  332. Assert.AreEqual(15, parameter.LocaleId, "#2");
  333. }
  334. [Test]
  335. public void SqlValue ()
  336. {
  337. SqlParameter parameter = new SqlParameter ();
  338. Assert.AreEqual (null, parameter.SqlValue, "#1 Default value for the property should be Null");
  339. parameter.SqlValue = SqlDbType.Char.ToString ();
  340. Assert.AreEqual ("Char", parameter.SqlValue, "#1 The value for the property should be Char after setting SqlDbType to Char");
  341. }
  342. #endif
  343. }
  344. }