DataReaderTest.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // DataReaderTest.cs - NUnit Test Cases for testing the
  2. // DataReader family of classes
  3. //
  4. // Authors:
  5. // Sureshkumar T ([email protected])
  6. //
  7. // Copyright (c) 2004 Novell Inc., and the individuals listed on the
  8. // ChangeLog entries.
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person
  12. // obtaining a copy of this software and associated documentation
  13. // files (the "Software"), to deal in the Software without
  14. // restriction, including without limitation the rights to use, copy,
  15. // modify, merge, publish, distribute, sublicense, and/or sell copies
  16. // of the Software, and to permit persons to whom the Software is
  17. // furnished to do so, subject to 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
  26. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. // SOFTWARE.
  30. using System;
  31. using System.Data;
  32. using System.Data.Common;
  33. using Mono.Data;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Data
  36. {
  37. [TestFixture]
  38. [Category ("odbc"), Category ("sqlserver")]
  39. public class DataReaderTest
  40. {
  41. [Test]
  42. public void GetSchemaTableTest ()
  43. {
  44. IDbConnection conn = ConnectionManager.Singleton.Connection;
  45. try {
  46. ConnectionManager.Singleton.OpenConnection ();
  47. IDbCommand cmd = conn.CreateCommand ();
  48. cmd.CommandText = "select id, fname, id + 20 as plustwenty from employee";
  49. IDataReader reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);
  50. DataTable schema = reader.GetSchemaTable ();
  51. reader.Close ();
  52. Assert.AreEqual (3, schema.Rows.Count, "#1");
  53. // Primary Key Test
  54. DataRow pkRow = schema.Select ("ColumnName = 'id'") [0];
  55. Assert.AreEqual (false, pkRow.IsNull ("IsKey"), "#2");
  56. Assert.AreEqual (true, (bool) pkRow ["IsKey"], "#3");
  57. } finally {
  58. ConnectionManager.Singleton.CloseConnection ();
  59. }
  60. }
  61. [Test]
  62. public void GetNameTest ()
  63. {
  64. IDbConnection conn = ConnectionManager.Singleton.Connection;
  65. try {
  66. ConnectionManager.Singleton.OpenConnection ();
  67. IDbCommand dbcmd = conn.CreateCommand ();
  68. string sql = "SELECT type_tinyint from numeric_family";
  69. dbcmd.CommandText = sql;
  70. using (IDataReader reader = dbcmd.ExecuteReader ()) {
  71. Assert.AreEqual ("type_tinyint", reader.GetName (0), "#1 GetName failes");
  72. }
  73. } finally {
  74. ConnectionManager.Singleton.CloseConnection ();
  75. }
  76. }
  77. [Test]
  78. public void NumericTest()
  79. {
  80. IDbConnection conn = ConnectionManager.Singleton.Connection;
  81. try {
  82. ConnectionManager.Singleton.OpenConnection ();
  83. IDbCommand dbCommand = conn.CreateCommand ();
  84. dbCommand.CommandText = "select type_numeric from numeric_family where id = 1;";
  85. using(IDataReader reader = dbCommand.ExecuteReader ()) {
  86. if (reader.Read()) {
  87. object value = reader.GetValue(0);
  88. Assert.AreEqual (typeof (decimal), value.GetType(), "#1 wrong type");
  89. Assert.AreEqual ( (decimal) 1000, value, "#2 value is wrong");
  90. } else
  91. Assert.Fail ("#3 does not have test data");
  92. }
  93. } finally {
  94. ConnectionManager.Singleton.CloseConnection ();
  95. }
  96. }
  97. [Test]
  98. public void TinyIntTest ()
  99. {
  100. IDbConnection conn = ConnectionManager.Singleton.Connection;
  101. try {
  102. ConnectionManager.Singleton.OpenConnection ();
  103. IDbCommand dbCommand = conn.CreateCommand ();
  104. dbCommand.CommandText = "select type_tinyint from numeric_family where id = 1;";
  105. using(IDataReader reader = dbCommand.ExecuteReader ()) {
  106. if (reader.Read()) {
  107. object value = reader.GetValue (0);
  108. Assert.AreEqual (typeof (byte), value.GetType(), "#1 wrong type");
  109. Assert.AreEqual (255, value, "#2 value is wrong");
  110. } else
  111. Assert.Fail ("#3 does not have test data");
  112. }
  113. } finally {
  114. ConnectionManager.Singleton.CloseConnection ();
  115. }
  116. }
  117. [Test]
  118. public void GetByteTest ()
  119. {
  120. IDbConnection conn = ConnectionManager.Singleton.Connection;
  121. try {
  122. ConnectionManager.Singleton.OpenConnection ();
  123. IDbCommand cmd = conn.CreateCommand ();
  124. string query = "select type_tinyint from numeric_family where id = 1";
  125. cmd.CommandText = query;
  126. using (IDataReader reader = cmd.ExecuteReader ()) {
  127. if (reader.Read ()) {
  128. byte b = reader.GetByte (0);
  129. Assert.AreEqual (255, b, "GetByte returns wrong result");
  130. } else // This should not happen while testing
  131. Assert.Fail ("test table does not have a test data!");
  132. }
  133. } finally {
  134. ConnectionManager.Singleton.CloseConnection ();
  135. }
  136. }
  137. [Test]
  138. public void GetValueBinaryTest ()
  139. {
  140. IDbConnection conn = ConnectionManager.Singleton.Connection;
  141. try {
  142. ConnectionManager.Singleton.OpenConnection ();
  143. IDbCommand cmd = conn.CreateCommand ();
  144. string sql = "select type_binary from binary_family where id = 1";
  145. cmd.CommandText = sql;
  146. using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
  147. if (reader.Read ()) {
  148. object ob = reader.GetValue (0);
  149. Assert.AreEqual (typeof (byte []), ob.GetType (), "#1 Type of binary column is wrong!");
  150. } else
  151. Assert.Fail ("#2 test data not available");
  152. }
  153. } finally {
  154. ConnectionManager.Singleton.CloseConnection ();
  155. }
  156. }
  157. [Test]
  158. public void GetBytesNullBufferTest ()
  159. {
  160. IDbConnection conn = ConnectionManager.Singleton.Connection;
  161. try {
  162. ConnectionManager.Singleton.OpenConnection ();
  163. IDbCommand cmd = conn.CreateCommand ();
  164. // 4th row is null
  165. string sql = "SELECT type_blob FROM binary_family where id = 1 or id = 4 order by id";
  166. cmd.CommandText = sql;
  167. using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
  168. if (reader.Read ()) {
  169. Assert.AreEqual (8, reader.GetBytes (0, 0, null, 0, 0),
  170. "#1 getbytes should return length of column");
  171. } else
  172. Assert.Fail ("#2 No test data");
  173. // for null value, length in bytes should return 0
  174. if (reader.Read ())
  175. Assert.AreEqual (0, reader.GetBytes (0, 0, null, 0, 0),
  176. "#3 on null value, it should return -1");
  177. else
  178. Assert.Fail ("#4 No test data");
  179. }
  180. } finally {
  181. ConnectionManager.Singleton.CloseConnection ();
  182. }
  183. }
  184. [Test]
  185. public void GetBytesTest ()
  186. {
  187. IDbConnection conn = ConnectionManager.Singleton.Connection;
  188. try {
  189. ConnectionManager.Singleton.OpenConnection ();
  190. IDbCommand cmd = conn.CreateCommand ();
  191. string sql = "SELECT type_blob FROM binary_family where id = 1";
  192. cmd.CommandText = sql;
  193. using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
  194. if (reader.Read ()) {
  195. // Get By Parts for the column blob
  196. long totalsize = reader.GetBytes (0, 0, null, 0, 0);
  197. int buffsize = 5;
  198. int start = 0;
  199. int offset = 0;
  200. long ret = 0;
  201. long count = 0;
  202. byte [] val = new byte [totalsize];
  203. do {
  204. ret = reader.GetBytes (0, offset, val, offset, buffsize);
  205. offset += (int) ret;
  206. count += ret;
  207. } while (count < totalsize);
  208. Assert.AreEqual (8, count,
  209. "#1 The assembled value length does not match");
  210. } else
  211. Assert.Fail ("#2 no test data");
  212. }
  213. } finally {
  214. ConnectionManager.Singleton.CloseConnection ();
  215. }
  216. }
  217. }
  218. }