PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Npgsql/Test/DataReaderTests.cs

https://github.com/iainlane/mono
C# | 537 lines | 297 code | 200 blank | 40 comment | 4 complexity | b9bec14b079da51a6141a141a9a2ec74 MD5 | raw file
  1. // created on 27/12/2002 at 17:05
  2. //
  3. // Author:
  4. // Francisco Figueiredo Jr. <fxjrlists@yahoo.com>
  5. //
  6. // Copyright (C) 2002 The Npgsql Development Team
  7. // npgsql-general@gborg.postgresql.org
  8. // http://gborg.postgresql.org/project/npgsql/projdisplay.php
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Lesser General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2.1 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this library; if not, write to the Free Software
  22. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. using System;
  24. using System.Data;
  25. using System.Web.UI.WebControls;
  26. using Npgsql;
  27. using NUnit.Framework;
  28. using NUnit.Core;
  29. namespace NpgsqlTests
  30. {
  31. [TestFixture]
  32. public class DataReaderTests
  33. {
  34. NpgsqlConnection _conn;
  35. [SetUp]
  36. protected void SetUp()
  37. {
  38. //NpgsqlEventLog.Level = LogLevel.None;
  39. //NpgsqlEventLog.Level = LogLevel.Debug;
  40. //NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
  41. _conn = new NpgsqlConnection (TestConfiguration.NpgsqlConnectionString);
  42. }
  43. [TearDown]
  44. protected void TearDown()
  45. {
  46. if (_conn != null &&_conn.State != ConnectionState.Closed)
  47. _conn.Close();
  48. }
  49. [Test]
  50. public void GetBoolean()
  51. {
  52. _conn.Open();
  53. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 4;", _conn);
  54. NpgsqlDataReader dr = command.ExecuteReader();
  55. dr.Read();
  56. Boolean result = dr.GetBoolean(4);
  57. Assert.AreEqual(true, result);
  58. }
  59. [Test]
  60. public void GetChars()
  61. {
  62. _conn.Open();
  63. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn);
  64. NpgsqlDataReader dr = command.ExecuteReader();
  65. dr.Read();
  66. Char[] result = new Char[6];
  67. Int64 a = dr.GetChars(1, 0, result, 0, 6);
  68. Assert.AreEqual("Random", new String(result));
  69. /*ConsoleWriter cw = new ConsoleWriter(Console.Out);
  70. cw.WriteLine(result);*/
  71. }
  72. [Test]
  73. public void GetInt32()
  74. {
  75. _conn.Open();
  76. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 2;", _conn);
  77. NpgsqlDataReader dr = command.ExecuteReader();
  78. dr.Read();
  79. Int32 result = dr.GetInt32(2);
  80. //ConsoleWriter cw = new ConsoleWriter(Console.Out);
  81. //cw.WriteLine(result.GetType().Name);
  82. Assert.AreEqual(4, result);
  83. }
  84. [Test]
  85. public void GetInt16()
  86. {
  87. _conn.Open();
  88. NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 1;", _conn);
  89. NpgsqlDataReader dr = command.ExecuteReader();
  90. dr.Read();
  91. Int16 result = dr.GetInt16(1);
  92. Assert.AreEqual(2, result);
  93. }
  94. [Test]
  95. public void GetDecimal()
  96. {
  97. _conn.Open();
  98. NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 3;", _conn);
  99. NpgsqlDataReader dr = command.ExecuteReader();
  100. dr.Read();
  101. Decimal result = dr.GetDecimal(3);
  102. Assert.AreEqual(4.2300000M, result);
  103. }
  104. [Test]
  105. public void GetDouble()
  106. {
  107. _conn.Open();
  108. NpgsqlCommand command = new NpgsqlCommand("select * from tabled where field_serial = 2;", _conn);
  109. NpgsqlDataReader dr = command.ExecuteReader();
  110. dr.Read();
  111. //Double result = Double.Parse(dr.GetInt32(2).ToString());
  112. Double result = dr.GetDouble(2);
  113. Assert.AreEqual(.123456789012345D, result);
  114. }
  115. [Test]
  116. public void GetFloat()
  117. {
  118. _conn.Open();
  119. NpgsqlCommand command = new NpgsqlCommand("select * from tabled where field_serial = 1;", _conn);
  120. NpgsqlDataReader dr = command.ExecuteReader();
  121. dr.Read();
  122. //Single result = Single.Parse(dr.GetInt32(2).ToString());
  123. Single result = dr.GetFloat(1);
  124. Assert.AreEqual(.123456F, result);
  125. }
  126. [Test]
  127. public void GetString()
  128. {
  129. _conn.Open();
  130. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn);
  131. NpgsqlDataReader dr = command.ExecuteReader();
  132. dr.Read();
  133. String result = dr.GetString(1);
  134. Assert.AreEqual("Random text", result);
  135. }
  136. [Test]
  137. public void GetStringWithParameter()
  138. {
  139. _conn.Open();
  140. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_text = :value;", _conn);
  141. String test = "Random text";
  142. NpgsqlParameter param = new NpgsqlParameter();
  143. param.ParameterName = "value";
  144. param.DbType = DbType.String;
  145. //param.NpgsqlDbType = NpgsqlDbType.Text;
  146. param.Size = test.Length;
  147. param.Value = test;
  148. command.Parameters.Add(param);
  149. NpgsqlDataReader dr = command.ExecuteReader();
  150. dr.Read();
  151. String result = dr.GetString(1);
  152. Assert.AreEqual(test, result);
  153. }
  154. [Test]
  155. public void GetStringWithQuoteWithParameter()
  156. {
  157. _conn.Open();
  158. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_text = :value;", _conn);
  159. String test = "Text with ' single quote";
  160. NpgsqlParameter param = new NpgsqlParameter();
  161. param.ParameterName = "value";
  162. param.DbType = DbType.String;
  163. //param.NpgsqlDbType = NpgsqlDbType.Text;
  164. param.Size = test.Length;
  165. param.Value = test;
  166. command.Parameters.Add(param);
  167. NpgsqlDataReader dr = command.ExecuteReader();
  168. dr.Read();
  169. String result = dr.GetString(1);
  170. Assert.AreEqual(test, result);
  171. }
  172. [Test]
  173. public void GetValueByName()
  174. {
  175. _conn.Open();
  176. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn);
  177. NpgsqlDataReader dr = command.ExecuteReader();
  178. dr.Read();
  179. String result = (String) dr["field_text"];
  180. Assert.AreEqual("Random text", result);
  181. }
  182. [Test]
  183. [ExpectedException(typeof(InvalidOperationException))]
  184. public void GetValueFromEmptyResultset()
  185. {
  186. _conn.Open();
  187. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_text = :value;", _conn);
  188. String test = "Text single quote";
  189. NpgsqlParameter param = new NpgsqlParameter();
  190. param.ParameterName = "value";
  191. param.DbType = DbType.String;
  192. //param.NpgsqlDbType = NpgsqlDbType.Text;
  193. param.Size = test.Length;
  194. param.Value = test;
  195. command.Parameters.Add(param);
  196. NpgsqlDataReader dr = command.ExecuteReader();
  197. dr.Read();
  198. // This line should throw the invalid operation exception as the datareader will
  199. // have an empty resultset.
  200. Console.WriteLine(dr.IsDBNull(1));
  201. }
  202. [Test]
  203. public void TestOverlappedParameterNames()
  204. {
  205. _conn.Open();
  206. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = :a or field_serial = :aa", _conn);
  207. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32, 4, "a"));
  208. command.Parameters.Add(new NpgsqlParameter("aa", DbType.Int32, 4, "aa"));
  209. command.Parameters[0].Value = 2;
  210. command.Parameters[1].Value = 3;
  211. NpgsqlDataReader dr = command.ExecuteReader();
  212. }
  213. [Test]
  214. [ExpectedException(typeof(IndexOutOfRangeException))]
  215. public void TestNonExistentParameterName()
  216. {
  217. _conn.Open();
  218. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = :a or field_serial = :aa", _conn);
  219. command.Parameters.Add(new NpgsqlParameter(":b", DbType.Int32, 4, "b"));
  220. command.Parameters.Add(new NpgsqlParameter(":aa", DbType.Int32, 4, "aa"));
  221. command.Parameters[0].Value = 2;
  222. command.Parameters[1].Value = 3;
  223. NpgsqlDataReader dr = command.ExecuteReader();
  224. }
  225. [Test]
  226. public void UseDataAdapter()
  227. {
  228. _conn.Open();
  229. NpgsqlCommand command = new NpgsqlCommand("select * from tablea", _conn);
  230. NpgsqlDataAdapter da = new NpgsqlDataAdapter();
  231. da.SelectCommand = command;
  232. DataSet ds = new DataSet();
  233. da.Fill(ds);
  234. //ds.WriteXml("TestUseDataAdapter.xml");
  235. }
  236. [Test]
  237. public void UseDataAdapterNpgsqlConnectionConstructor()
  238. {
  239. _conn.Open();
  240. NpgsqlCommand command = new NpgsqlCommand("select * from tablea", _conn);
  241. command.Connection = _conn;
  242. NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
  243. DataSet ds = new DataSet();
  244. da.Fill(ds);
  245. //ds.WriteXml("TestUseDataAdapterNpgsqlConnectionConstructor.xml");
  246. }
  247. [Test]
  248. public void UseDataAdapterStringNpgsqlConnectionConstructor()
  249. {
  250. _conn.Open();
  251. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tablea", _conn);
  252. DataSet ds = new DataSet();
  253. da.Fill(ds);
  254. //ds.WriteXml("TestUseDataAdapterStringNpgsqlConnectionConstructor.xml");
  255. }
  256. [Test]
  257. public void UseDataAdapterStringStringConstructor()
  258. {
  259. _conn.Open();
  260. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tablea", TestConfiguration.NpgsqlConnectionString);
  261. DataSet ds = new DataSet();
  262. da.Fill(ds);
  263. ds.WriteXml("TestUseDataAdapterStringStringConstructor.xml");
  264. }
  265. [Test]
  266. public void UseDataAdapterStringStringConstructor2()
  267. {
  268. _conn.Open();
  269. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb", TestConfiguration.NpgsqlConnectionString);
  270. DataSet ds = new DataSet();
  271. da.Fill(ds);
  272. ds.WriteXml("TestUseDataAdapterStringStringConstructor2.xml");
  273. }
  274. [Test]
  275. public void DataGridWebControlSupport()
  276. {
  277. _conn.Open();
  278. NpgsqlCommand command = new NpgsqlCommand("select * from tablea;", _conn);
  279. NpgsqlDataReader dr = command.ExecuteReader();
  280. DataGrid dg = new DataGrid();
  281. dg.DataSource = dr;
  282. dg.DataBind();
  283. }
  284. [Test]
  285. [ExpectedException(typeof(InvalidOperationException))]
  286. public void ReadPastDataReaderEnd()
  287. {
  288. _conn.Open();
  289. NpgsqlCommand command = new NpgsqlCommand("select * from tablea;", _conn);
  290. NpgsqlDataReader dr = command.ExecuteReader();
  291. while (dr.Read());
  292. Object o = dr[0];
  293. }
  294. [Test]
  295. public void IsDBNull()
  296. {
  297. _conn.Open();
  298. NpgsqlCommand command = new NpgsqlCommand("select field_text from tablea;", _conn);
  299. NpgsqlDataReader dr = command.ExecuteReader();
  300. dr.Read();
  301. Assert.AreEqual(false, dr.IsDBNull(0));
  302. dr.Read();
  303. Assert.AreEqual(true, dr.IsDBNull(0));
  304. }
  305. [Test]
  306. public void IsDBNullFromScalar()
  307. {
  308. _conn.Open();
  309. NpgsqlCommand command = new NpgsqlCommand("select max(field_serial) from tablea;", _conn);
  310. NpgsqlDataReader dr = command.ExecuteReader();
  311. dr.Read();
  312. Assert.AreEqual(false, dr.IsDBNull(0));
  313. }
  314. [Test]
  315. public void TypesNames()
  316. {
  317. _conn.Open();
  318. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where 1 = 2;", _conn);
  319. NpgsqlDataReader dr = command.ExecuteReader();
  320. dr.Read();
  321. Assert.AreEqual("int4", dr.GetDataTypeName(0));
  322. Assert.AreEqual("text", dr.GetDataTypeName(1));
  323. Assert.AreEqual("int4", dr.GetDataTypeName(2));
  324. Assert.AreEqual("int8", dr.GetDataTypeName(3));
  325. Assert.AreEqual("bool", dr.GetDataTypeName(4));
  326. dr.Close();
  327. command.CommandText = "select * from tableb where 1 = 2";
  328. dr = command.ExecuteReader();
  329. dr.Read();
  330. Assert.AreEqual("int4", dr.GetDataTypeName(0));
  331. Assert.AreEqual("int2", dr.GetDataTypeName(1));
  332. Assert.AreEqual("timestamp", dr.GetDataTypeName(2));
  333. Assert.AreEqual("numeric", dr.GetDataTypeName(3));
  334. }
  335. }
  336. }