SqlParameterTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #if NET_2_0
  106. [Test]
  107. public void ResetSqlDbTypeTest ()
  108. {
  109. //Parameter with an assigned value but no SqlDbType specified
  110. SqlParameter p1 = new SqlParameter ("foo", 42);
  111. Assert.AreEqual (42, p1.Value, "#1");
  112. Assert.AreEqual (DbType.Int32, p1.DbType, "#2");
  113. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#3");
  114. p1.ResetSqlDbType ();
  115. Assert.AreEqual (DbType.Int32, p1.DbType, "#4 The parameter with value 42 must have DbType as Int32");
  116. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#5 The parameter with value 42 must have SqlDbType as Int");
  117. p1.SqlDbType = SqlDbType.DateTime; //assigning a SqlDbType
  118. Assert.AreEqual (DbType.DateTime, p1.DbType, "#6");
  119. Assert.AreEqual (SqlDbType.DateTime, p1.SqlDbType, "#7");
  120. p1.ResetSqlDbType (); //Resetting SqlDbType
  121. Assert.AreEqual (DbType.Int32, p1.DbType, "#8 Resetting SqlDbType must infer the type from the value");
  122. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#9 Resetting SqlDbType must infer the type from the value");
  123. //Parameter with an assigned SqlDbType but no specified value
  124. SqlParameter p2 = new SqlParameter ("foo", SqlDbType.Int);
  125. Assert.AreEqual (null, p2.Value, "#10");
  126. Assert.AreEqual (DbType.Int32, p2.DbType, "#11");
  127. Assert.AreEqual (SqlDbType.Int, p2.SqlDbType, "#12");
  128. //Although a SqlDbType is specified, calling ResetSqlDbType resets
  129. //the SqlDbType and DbType properties to default values
  130. p2.ResetSqlDbType ();
  131. Assert.AreEqual (DbType.String, p2.DbType, "#13 Resetting SqlDbType must infer the type from the value");
  132. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#14 Resetting SqlDbType must infer the type from the value");
  133. p2.SqlDbType = SqlDbType.DateTime; //assigning a SqlDbType
  134. Assert.AreEqual (DbType.DateTime, p2.DbType, "#15");
  135. Assert.AreEqual (SqlDbType.DateTime, p2.SqlDbType, "#16");
  136. p2.ResetSqlDbType (); //Resetting SqlDbType
  137. Assert.AreEqual (DbType.String, p2.DbType, "#17 Resetting SqlDbType must infer the type from the value");
  138. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#18 Resetting SqlDbType must infer the type from the value");
  139. }
  140. [Test]
  141. public void ResetDbTypeTest ()
  142. {
  143. //Parameter with an assigned value but no DbType specified
  144. SqlParameter p1 = new SqlParameter ("foo", 42);
  145. Assert.AreEqual (42, p1.Value, "#1");
  146. Assert.AreEqual (DbType.Int32, p1.DbType, "#2");
  147. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#3");
  148. p1.ResetDbType ();
  149. Assert.AreEqual (DbType.Int32, p1.DbType, "#4 The parameter with value 42 must have DbType as Int32");
  150. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#5 The parameter with value 42 must have SqlDbType as Int32");
  151. p1.DbType = DbType.DateTime; //assigning a DbType
  152. Assert.AreEqual (DbType.DateTime, p1.DbType, "#6");
  153. Assert.AreEqual (SqlDbType.DateTime, p1.SqlDbType, "#7");
  154. p1.ResetDbType (); //Resetting DbType
  155. Assert.AreEqual (DbType.Int32, p1.DbType, "#8 Resetting DbType must infer the type from the value");
  156. Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#9 Resetting DbType must infer the type from the value");
  157. //Parameter with an assigned SqlDbType but no specified value
  158. SqlParameter p2 = new SqlParameter ("foo", SqlDbType.Int);
  159. Assert.AreEqual (null, p2.Value, "#10");
  160. Assert.AreEqual (DbType.Int32, p2.DbType, "#11");
  161. Assert.AreEqual (SqlDbType.Int, p2.SqlDbType, "#12");
  162. //Although a SqlDbType is specified, calling ResetDbType resets
  163. //the SqlDbType and DbType properties to default values
  164. p2.ResetDbType ();
  165. Assert.AreEqual (DbType.String, p2.DbType, "#13 Resetting DbType must infer the type from the value");
  166. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#14 Resetting DbType must infer the type from the value");
  167. p2.DbType = DbType.DateTime; //assigning a SqlDbType
  168. Assert.AreEqual (DbType.DateTime, p2.DbType, "#15");
  169. Assert.AreEqual (SqlDbType.DateTime, p2.SqlDbType, "#16");
  170. p2.ResetDbType (); //Resetting DbType
  171. Assert.AreEqual (DbType.String, p2.DbType, "#17 Resetting DbType must infer the type from the value");
  172. Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#18 Resetting DbType must infer the type from the value");
  173. }
  174. [Test]
  175. public void XmlSchemaTest ()
  176. {
  177. SqlParameter p1 = new SqlParameter ();
  178. //Testing default values
  179. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
  180. "#1 Default value for XmlSchemaCollectionDatabase is an empty string");
  181. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionName,
  182. "#2 Default value for XmlSchemaCollectionName is an empty string");
  183. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionOwningSchema,
  184. "#3 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  185. //Changing one property should not affect the remaining two properties
  186. p1.XmlSchemaCollectionDatabase = "database";
  187. Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase,
  188. "#4 Default value for XmlSchemaCollectionDatabase is an empty string");
  189. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionName,
  190. "#5 Default value for XmlSchemaCollectionName is an empty string");
  191. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionOwningSchema,
  192. "#6 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  193. p1.XmlSchemaCollectionName = "name";
  194. Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase,
  195. "#7 Default value for XmlSchemaCollectionDatabase is an empty string");
  196. Assert.AreEqual ("name", p1.XmlSchemaCollectionName,
  197. "#8 Default value for XmlSchemaCollectionName is an empty string");
  198. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionOwningSchema,
  199. "#9 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  200. p1.XmlSchemaCollectionOwningSchema = "schema";
  201. Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase,
  202. "#10 Default value for XmlSchemaCollectionDatabase is an empty string");
  203. Assert.AreEqual ("name", p1.XmlSchemaCollectionName,
  204. "#11 Default value for XmlSchemaCollectionName is an empty string");
  205. Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema,
  206. "#12 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  207. //assigning null value stores default empty string
  208. p1.XmlSchemaCollectionDatabase = null;
  209. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
  210. "#13 Default value for XmlSchemaCollectionDatabase is an empty string");
  211. Assert.AreEqual ("name", p1.XmlSchemaCollectionName,
  212. "#14 Default value for XmlSchemaCollectionName is an empty string");
  213. Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema,
  214. "#15 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  215. p1.XmlSchemaCollectionName = "";
  216. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
  217. "#16 Default value for XmlSchemaCollectionDatabase is an empty string");
  218. Assert.AreEqual ("", p1.XmlSchemaCollectionName,
  219. "#17 Default value for XmlSchemaCollectionName is an empty string");
  220. Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema,
  221. "#18 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  222. //values are not trimmed
  223. p1.XmlSchemaCollectionOwningSchema = " a ";
  224. Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
  225. "#19 Default value for XmlSchemaCollectionDatabase is an empty string");
  226. Assert.AreEqual ("", p1.XmlSchemaCollectionName,
  227. "#20 Default value for XmlSchemaCollectionName is an empty string");
  228. Assert.AreEqual (" a ", p1.XmlSchemaCollectionOwningSchema,
  229. "#21 Default value for XmlSchemaCollectionOwningSchema is an empty string");
  230. }
  231. [Test]
  232. public void SourceColumnNullMappingTest ()
  233. {
  234. SqlParameter p1 = new SqlParameter ();
  235. Assert.AreEqual (false, p1.SourceColumnNullMapping, "#1 SourceColumnNullMapping should be false by default");
  236. p1.SourceColumnNullMapping = true;
  237. Assert.AreEqual (true, p1.SourceColumnNullMapping, "#2 SourceColumnNullMapping should be false by default");
  238. }
  239. [Test]
  240. public void ctor7Test ()
  241. {
  242. SqlParameter p1 = new SqlParameter ("p1Name", SqlDbType.VarChar, 20,
  243. ParameterDirection.InputOutput, 0, 0,
  244. "srcCol", DataRowVersion.Original, false,
  245. "foo", "database", "schema", "name");
  246. Assert.AreEqual (DbType.AnsiString, p1.DbType, "#");
  247. Assert.AreEqual (ParameterDirection.InputOutput, p1.Direction, "#");
  248. Assert.AreEqual (false, p1.IsNullable, "#");
  249. //Assert.AreEqual (999, p1.LocaleId, "#");
  250. Assert.AreEqual ("p1Name", p1.ParameterName, "#");
  251. Assert.AreEqual (0, p1.Precision, "#");
  252. Assert.AreEqual (0, p1.Scale, "#");
  253. Assert.AreEqual (20, p1.Size, "#");
  254. Assert.AreEqual ("srcCol", p1.SourceColumn, "#");
  255. Assert.AreEqual (false, p1.SourceColumnNullMapping, "#");
  256. Assert.AreEqual (DataRowVersion.Original, p1.SourceVersion, "#");
  257. Assert.AreEqual (SqlDbType.VarChar, p1.SqlDbType, "#");
  258. //Assert.AreEqual (3210, p1.SqlValue, "#");
  259. Assert.AreEqual ("foo", p1.Value, "#");
  260. Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase, "#");
  261. Assert.AreEqual ("name", p1.XmlSchemaCollectionName, "#");
  262. Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema, "#");
  263. }
  264. [Test]
  265. public void CompareInfoTest ()
  266. {
  267. SqlParameter parameter = new SqlParameter ();
  268. Assert.AreEqual (SqlCompareOptions.None, parameter.CompareInfo, "#1 Default value should be System.Data.SqlTypes.SqlCompareOptions.None");
  269. parameter.CompareInfo = SqlCompareOptions.IgnoreNonSpace;
  270. Assert.AreEqual (SqlCompareOptions.IgnoreNonSpace, parameter.CompareInfo, "#2 It should return CompareOptions.IgnoreSpace after setting this value for the property");
  271. }
  272. [Test]
  273. public void LocaleIdTest ()
  274. {
  275. SqlParameter parameter = new SqlParameter ();
  276. Assert.AreEqual (0, parameter.LocaleId, "#1 Default value for the property should be 0");
  277. parameter.LocaleId = 15;
  278. Assert.AreEqual(15, parameter.LocaleId, "#2");
  279. }
  280. [Test]
  281. public void SqlValue ()
  282. {
  283. SqlParameter parameter = new SqlParameter ();
  284. Assert.AreEqual (null, parameter.SqlValue, "#1 Default value for the property should be Null");
  285. parameter.SqlValue = SqlDbType.Char.ToString ();
  286. Assert.AreEqual ("Char", parameter.SqlValue, "#1 The value for the property should be Char after setting SqlDbType to Char");
  287. }
  288. #endif
  289. }
  290. }