DataReaderTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // created on 27/12/2002 at 17:05
  2. //
  3. // Author:
  4. // Francisco Figueiredo Jr. <[email protected]>
  5. //
  6. // Copyright (C) 2002 The Npgsql Development Team
  7. // [email protected]
  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. private NpgsqlConnection _conn = null;
  35. private String _connString = "Server=localhost;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests";
  36. [SetUp]
  37. protected void SetUp()
  38. {
  39. //NpgsqlEventLog.Level = LogLevel.None;
  40. NpgsqlEventLog.Level = LogLevel.Debug;
  41. NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
  42. _conn = new NpgsqlConnection(_connString);
  43. }
  44. [TearDown]
  45. protected void TearDown()
  46. {
  47. if (_conn.State != ConnectionState.Closed)
  48. _conn.Close();
  49. }
  50. [Test]
  51. public void GetBoolean()
  52. {
  53. _conn.Open();
  54. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 4;", _conn);
  55. NpgsqlDataReader dr = command.ExecuteReader();
  56. dr.Read();
  57. Boolean result = dr.GetBoolean(4);
  58. Assertion.AssertEquals(true, result);
  59. }
  60. [Test]
  61. public void GetChars()
  62. {
  63. _conn.Open();
  64. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn);
  65. NpgsqlDataReader dr = command.ExecuteReader();
  66. dr.Read();
  67. Char[] result = new Char[6];
  68. Int64 a = dr.GetChars(1, 0, result, 0, 6);
  69. Assertion.AssertEquals("Random", new String(result));
  70. /*ConsoleWriter cw = new ConsoleWriter(Console.Out);
  71. cw.WriteLine(result);*/
  72. }
  73. [Test]
  74. public void GetInt32()
  75. {
  76. _conn.Open();
  77. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 2;", _conn);
  78. NpgsqlDataReader dr = command.ExecuteReader();
  79. dr.Read();
  80. Int32 result = dr.GetInt32(2);
  81. //ConsoleWriter cw = new ConsoleWriter(Console.Out);
  82. //cw.WriteLine(result.GetType().Name);
  83. Assertion.AssertEquals(4, result);
  84. }
  85. [Test]
  86. public void GetInt16()
  87. {
  88. _conn.Open();
  89. NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 1;", _conn);
  90. NpgsqlDataReader dr = command.ExecuteReader();
  91. dr.Read();
  92. Int16 result = dr.GetInt16(1);
  93. Assertion.AssertEquals(2, result);
  94. }
  95. [Test]
  96. public void GetDecimal()
  97. {
  98. _conn.Open();
  99. NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 3;", _conn);
  100. NpgsqlDataReader dr = command.ExecuteReader();
  101. dr.Read();
  102. Decimal result = dr.GetDecimal(3);
  103. Assertion.AssertEquals(4.23M, result);
  104. }
  105. [Test]
  106. public void GetDouble()
  107. {
  108. _conn.Open();
  109. NpgsqlCommand command = new NpgsqlCommand("select * from tabled where field_serial = 2;", _conn);
  110. NpgsqlDataReader dr = command.ExecuteReader();
  111. dr.Read();
  112. //Double result = Double.Parse(dr.GetInt32(2).ToString());
  113. Double result = dr.GetDouble(2);
  114. Assertion.AssertEquals(.123456789012345D, result);
  115. }
  116. [Test]
  117. public void GetFloat()
  118. {
  119. _conn.Open();
  120. NpgsqlCommand command = new NpgsqlCommand("select * from tabled where field_serial = 1;", _conn);
  121. NpgsqlDataReader dr = command.ExecuteReader();
  122. dr.Read();
  123. //Single result = Single.Parse(dr.GetInt32(2).ToString());
  124. Single result = dr.GetFloat(1);
  125. Assertion.AssertEquals(.123456F, result);
  126. }
  127. [Test]
  128. public void GetString()
  129. {
  130. _conn.Open();
  131. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn);
  132. NpgsqlDataReader dr = command.ExecuteReader();
  133. dr.Read();
  134. String result = dr.GetString(1);
  135. Assertion.AssertEquals("Random text", result);
  136. }
  137. [Test]
  138. public void GetStringWithParameter()
  139. {
  140. _conn.Open();
  141. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_text = :value;", _conn);
  142. String test = "Random text";
  143. NpgsqlParameter param = new NpgsqlParameter();
  144. param.ParameterName = "value";
  145. param.DbType = DbType.String;
  146. //param.NpgsqlDbType = NpgsqlDbType.Text;
  147. param.Size = test.Length;
  148. param.Value = test;
  149. command.Parameters.Add(param);
  150. NpgsqlDataReader dr = command.ExecuteReader();
  151. dr.Read();
  152. String result = dr.GetString(1);
  153. Assertion.AssertEquals(test, result);
  154. }
  155. [Test]
  156. public void GetStringWithQuoteWithParameter()
  157. {
  158. _conn.Open();
  159. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_text = :value;", _conn);
  160. String test = "Text with ' single quote";
  161. NpgsqlParameter param = new NpgsqlParameter();
  162. param.ParameterName = "value";
  163. param.DbType = DbType.String;
  164. //param.NpgsqlDbType = NpgsqlDbType.Text;
  165. param.Size = test.Length;
  166. param.Value = test;
  167. command.Parameters.Add(param);
  168. NpgsqlDataReader dr = command.ExecuteReader();
  169. dr.Read();
  170. String result = dr.GetString(1);
  171. Assertion.AssertEquals(test, result);
  172. }
  173. [Test]
  174. public void GetValueByName()
  175. {
  176. _conn.Open();
  177. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn);
  178. NpgsqlDataReader dr = command.ExecuteReader();
  179. dr.Read();
  180. String result = (String) dr["field_text"];
  181. Assertion.AssertEquals("Random text", result);
  182. }
  183. [Test]
  184. [ExpectedException(typeof(InvalidOperationException))]
  185. public void GetValueFromEmptyResultset()
  186. {
  187. _conn.Open();
  188. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_text = :value;", _conn);
  189. String test = "Text single quote";
  190. NpgsqlParameter param = new NpgsqlParameter();
  191. param.ParameterName = "value";
  192. param.DbType = DbType.String;
  193. //param.NpgsqlDbType = NpgsqlDbType.Text;
  194. param.Size = test.Length;
  195. param.Value = test;
  196. command.Parameters.Add(param);
  197. NpgsqlDataReader dr = command.ExecuteReader();
  198. dr.Read();
  199. // This line should throw the invalid operation exception as the datareader will
  200. // have an empty resultset.
  201. Console.WriteLine(dr.IsDBNull(1));
  202. }
  203. [Test]
  204. public void TestOverlappedParameterNames()
  205. {
  206. _conn.Open();
  207. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = :a or field_serial = :aa", _conn);
  208. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32, 4, "a"));
  209. command.Parameters.Add(new NpgsqlParameter("aa", DbType.Int32, 4, "aa"));
  210. command.Parameters[0].Value = 2;
  211. command.Parameters[1].Value = 3;
  212. NpgsqlDataReader dr = command.ExecuteReader();
  213. }
  214. [Test]
  215. [ExpectedException(typeof(NpgsqlException))]
  216. public void TestNonExistentParameterName()
  217. {
  218. _conn.Open();
  219. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = :a or field_serial = :aa", _conn);
  220. command.Parameters.Add(new NpgsqlParameter(":b", DbType.Int32, 4, "b"));
  221. command.Parameters.Add(new NpgsqlParameter(":aa", DbType.Int32, 4, "aa"));
  222. command.Parameters[0].Value = 2;
  223. command.Parameters[1].Value = 3;
  224. NpgsqlDataReader dr = command.ExecuteReader();
  225. }
  226. [Test]
  227. public void UseDataAdapter()
  228. {
  229. _conn.Open();
  230. NpgsqlCommand command = new NpgsqlCommand("select * from tablea", _conn);
  231. NpgsqlDataAdapter da = new NpgsqlDataAdapter();
  232. da.SelectCommand = command;
  233. DataSet ds = new DataSet();
  234. da.Fill(ds);
  235. //ds.WriteXml("TestUseDataAdapter.xml");
  236. }
  237. [Test]
  238. public void UseDataAdapterNpgsqlConnectionConstructor()
  239. {
  240. _conn.Open();
  241. NpgsqlCommand command = new NpgsqlCommand("select * from tablea", _conn);
  242. command.Connection = _conn;
  243. NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
  244. DataSet ds = new DataSet();
  245. da.Fill(ds);
  246. //ds.WriteXml("TestUseDataAdapterNpgsqlConnectionConstructor.xml");
  247. }
  248. [Test]
  249. public void UseDataAdapterStringNpgsqlConnectionConstructor()
  250. {
  251. _conn.Open();
  252. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tablea", _conn);
  253. DataSet ds = new DataSet();
  254. da.Fill(ds);
  255. //ds.WriteXml("TestUseDataAdapterStringNpgsqlConnectionConstructor.xml");
  256. }
  257. [Test]
  258. public void UseDataAdapterStringStringConstructor()
  259. {
  260. _conn.Open();
  261. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tablea", _connString);
  262. DataSet ds = new DataSet();
  263. da.Fill(ds);
  264. ds.WriteXml("TestUseDataAdapterStringStringConstructor.xml");
  265. }
  266. [Test]
  267. public void UseDataAdapterStringStringConstructor2()
  268. {
  269. _conn.Open();
  270. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb", _connString);
  271. DataSet ds = new DataSet();
  272. da.Fill(ds);
  273. ds.WriteXml("TestUseDataAdapterStringStringConstructor2.xml");
  274. }
  275. [Test]
  276. public void DataGridWebControlSupport()
  277. {
  278. _conn.Open();
  279. NpgsqlCommand command = new NpgsqlCommand("select * from tablea;", _conn);
  280. NpgsqlDataReader dr = command.ExecuteReader();
  281. DataGrid dg = new DataGrid();
  282. dg.DataSource = dr;
  283. dg.DataBind();
  284. }
  285. }
  286. }