2010-03-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Npgsql / Test / DataReaderTests.cs
blob0fc40e35046cbfd95f71556d9d76736614d1a39c
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
24 using System;
25 using System.Data;
26 using System.Web.UI.WebControls;
27 using Npgsql;
29 using NUnit.Framework;
30 using NUnit.Core;
32 namespace NpgsqlTests
35 [TestFixture]
36 public class DataReaderTests
39 NpgsqlConnection _conn;
41 [SetUp]
42 protected void SetUp()
44 //NpgsqlEventLog.Level = LogLevel.None;
45 //NpgsqlEventLog.Level = LogLevel.Debug;
46 //NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
47 _conn = new NpgsqlConnection (TestConfiguration.NpgsqlConnectionString);
50 [TearDown]
51 protected void TearDown()
53 if (_conn != null &&_conn.State != ConnectionState.Closed)
54 _conn.Close();
57 [Test]
58 public void GetBoolean()
60 _conn.Open();
62 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 4;", _conn);
64 NpgsqlDataReader dr = command.ExecuteReader();
66 dr.Read();
67 Boolean result = dr.GetBoolean(4);
68 Assert.AreEqual(true, result);
73 [Test]
74 public void GetChars()
76 _conn.Open();
77 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn);
79 NpgsqlDataReader dr = command.ExecuteReader();
81 dr.Read();
82 Char[] result = new Char[6];
85 Int64 a = dr.GetChars(1, 0, result, 0, 6);
87 Assert.AreEqual("Random", new String(result));
88 /*ConsoleWriter cw = new ConsoleWriter(Console.Out);
90 cw.WriteLine(result);*/
95 [Test]
96 public void GetInt32()
98 _conn.Open();
99 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 2;", _conn);
101 NpgsqlDataReader dr = command.ExecuteReader();
103 dr.Read();
106 Int32 result = dr.GetInt32(2);
108 //ConsoleWriter cw = new ConsoleWriter(Console.Out);
110 //cw.WriteLine(result.GetType().Name);
111 Assert.AreEqual(4, result);
116 [Test]
117 public void GetInt16()
119 _conn.Open();
120 NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 1;", _conn);
122 NpgsqlDataReader dr = command.ExecuteReader();
124 dr.Read();
126 Int16 result = dr.GetInt16(1);
128 Assert.AreEqual(2, result);
133 [Test]
134 public void GetDecimal()
136 _conn.Open();
137 NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 3;", _conn);
139 NpgsqlDataReader dr = command.ExecuteReader();
141 dr.Read();
143 Decimal result = dr.GetDecimal(3);
146 Assert.AreEqual(4.2300000M, result);
153 [Test]
154 public void GetDouble()
156 _conn.Open();
157 NpgsqlCommand command = new NpgsqlCommand("select * from tabled where field_serial = 2;", _conn);
159 NpgsqlDataReader dr = command.ExecuteReader();
161 dr.Read();
163 //Double result = Double.Parse(dr.GetInt32(2).ToString());
164 Double result = dr.GetDouble(2);
166 Assert.AreEqual(.123456789012345D, result);
171 [Test]
172 public void GetFloat()
174 _conn.Open();
175 NpgsqlCommand command = new NpgsqlCommand("select * from tabled where field_serial = 1;", _conn);
177 NpgsqlDataReader dr = command.ExecuteReader();
179 dr.Read();
181 //Single result = Single.Parse(dr.GetInt32(2).ToString());
182 Single result = dr.GetFloat(1);
184 Assert.AreEqual(.123456F, result);
189 [Test]
190 public void GetString()
192 _conn.Open();
193 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn);
195 NpgsqlDataReader dr = command.ExecuteReader();
197 dr.Read();
199 String result = dr.GetString(1);
201 Assert.AreEqual("Random text", result);
206 [Test]
207 public void GetStringWithParameter()
209 _conn.Open();
210 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_text = :value;", _conn);
212 String test = "Random text";
213 NpgsqlParameter param = new NpgsqlParameter();
214 param.ParameterName = "value";
215 param.DbType = DbType.String;
216 //param.NpgsqlDbType = NpgsqlDbType.Text;
217 param.Size = test.Length;
218 param.Value = test;
219 command.Parameters.Add(param);
221 NpgsqlDataReader dr = command.ExecuteReader();
223 dr.Read();
225 String result = dr.GetString(1);
227 Assert.AreEqual(test, result);
231 [Test]
232 public void GetStringWithQuoteWithParameter()
234 _conn.Open();
235 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_text = :value;", _conn);
237 String test = "Text with ' single quote";
238 NpgsqlParameter param = new NpgsqlParameter();
239 param.ParameterName = "value";
240 param.DbType = DbType.String;
241 //param.NpgsqlDbType = NpgsqlDbType.Text;
242 param.Size = test.Length;
243 param.Value = test;
244 command.Parameters.Add(param);
246 NpgsqlDataReader dr = command.ExecuteReader();
248 dr.Read();
250 String result = dr.GetString(1);
252 Assert.AreEqual(test, result);
257 [Test]
258 public void GetValueByName()
260 _conn.Open();
261 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn);
263 NpgsqlDataReader dr = command.ExecuteReader();
265 dr.Read();
267 String result = (String) dr["field_text"];
269 Assert.AreEqual("Random text", result);
273 [Test]
274 [ExpectedException(typeof(InvalidOperationException))]
275 public void GetValueFromEmptyResultset()
277 _conn.Open();
278 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_text = :value;", _conn);
280 String test = "Text single quote";
281 NpgsqlParameter param = new NpgsqlParameter();
282 param.ParameterName = "value";
283 param.DbType = DbType.String;
284 //param.NpgsqlDbType = NpgsqlDbType.Text;
285 param.Size = test.Length;
286 param.Value = test;
287 command.Parameters.Add(param);
289 NpgsqlDataReader dr = command.ExecuteReader();
291 dr.Read();
294 // This line should throw the invalid operation exception as the datareader will
295 // have an empty resultset.
296 Console.WriteLine(dr.IsDBNull(1));
302 [Test]
303 public void TestOverlappedParameterNames()
305 _conn.Open();
307 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = :a or field_serial = :aa", _conn);
308 command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32, 4, "a"));
309 command.Parameters.Add(new NpgsqlParameter("aa", DbType.Int32, 4, "aa"));
311 command.Parameters[0].Value = 2;
312 command.Parameters[1].Value = 3;
314 NpgsqlDataReader dr = command.ExecuteReader();
318 [Test]
319 [ExpectedException(typeof(IndexOutOfRangeException))]
320 public void TestNonExistentParameterName()
322 _conn.Open();
324 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_serial = :a or field_serial = :aa", _conn);
325 command.Parameters.Add(new NpgsqlParameter(":b", DbType.Int32, 4, "b"));
326 command.Parameters.Add(new NpgsqlParameter(":aa", DbType.Int32, 4, "aa"));
328 command.Parameters[0].Value = 2;
329 command.Parameters[1].Value = 3;
331 NpgsqlDataReader dr = command.ExecuteReader();
339 [Test]
340 public void UseDataAdapter()
343 _conn.Open();
345 NpgsqlCommand command = new NpgsqlCommand("select * from tablea", _conn);
347 NpgsqlDataAdapter da = new NpgsqlDataAdapter();
349 da.SelectCommand = command;
351 DataSet ds = new DataSet();
353 da.Fill(ds);
355 //ds.WriteXml("TestUseDataAdapter.xml");
360 [Test]
361 public void UseDataAdapterNpgsqlConnectionConstructor()
364 _conn.Open();
366 NpgsqlCommand command = new NpgsqlCommand("select * from tablea", _conn);
368 command.Connection = _conn;
370 NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
372 DataSet ds = new DataSet();
374 da.Fill(ds);
376 //ds.WriteXml("TestUseDataAdapterNpgsqlConnectionConstructor.xml");
381 [Test]
382 public void UseDataAdapterStringNpgsqlConnectionConstructor()
385 _conn.Open();
388 NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tablea", _conn);
390 DataSet ds = new DataSet();
392 da.Fill(ds);
394 //ds.WriteXml("TestUseDataAdapterStringNpgsqlConnectionConstructor.xml");
400 [Test]
401 public void UseDataAdapterStringStringConstructor()
404 _conn.Open();
407 NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tablea", TestConfiguration.NpgsqlConnectionString);
409 DataSet ds = new DataSet();
411 da.Fill(ds);
413 ds.WriteXml("TestUseDataAdapterStringStringConstructor.xml");
418 [Test]
419 public void UseDataAdapterStringStringConstructor2()
422 _conn.Open();
425 NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb", TestConfiguration.NpgsqlConnectionString);
427 DataSet ds = new DataSet();
429 da.Fill(ds);
431 ds.WriteXml("TestUseDataAdapterStringStringConstructor2.xml");
436 [Test]
437 public void DataGridWebControlSupport()
440 _conn.Open();
442 NpgsqlCommand command = new NpgsqlCommand("select * from tablea;", _conn);
444 NpgsqlDataReader dr = command.ExecuteReader();
446 DataGrid dg = new DataGrid();
448 dg.DataSource = dr;
449 dg.DataBind();
455 [Test]
456 [ExpectedException(typeof(InvalidOperationException))]
457 public void ReadPastDataReaderEnd()
459 _conn.Open();
460 NpgsqlCommand command = new NpgsqlCommand("select * from tablea;", _conn);
462 NpgsqlDataReader dr = command.ExecuteReader();
464 while (dr.Read());
466 Object o = dr[0];
470 [Test]
471 public void IsDBNull()
473 _conn.Open();
474 NpgsqlCommand command = new NpgsqlCommand("select field_text from tablea;", _conn);
476 NpgsqlDataReader dr = command.ExecuteReader();
478 dr.Read();
479 Assert.AreEqual(false, dr.IsDBNull(0));
480 dr.Read();
481 Assert.AreEqual(true, dr.IsDBNull(0));
486 [Test]
487 public void IsDBNullFromScalar()
489 _conn.Open();
490 NpgsqlCommand command = new NpgsqlCommand("select max(field_serial) from tablea;", _conn);
492 NpgsqlDataReader dr = command.ExecuteReader();
494 dr.Read();
495 Assert.AreEqual(false, dr.IsDBNull(0));
501 [Test]
502 public void TypesNames()
504 _conn.Open();
505 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where 1 = 2;", _conn);
507 NpgsqlDataReader dr = command.ExecuteReader();
509 dr.Read();
511 Assert.AreEqual("int4", dr.GetDataTypeName(0));
512 Assert.AreEqual("text", dr.GetDataTypeName(1));
513 Assert.AreEqual("int4", dr.GetDataTypeName(2));
514 Assert.AreEqual("int8", dr.GetDataTypeName(3));
515 Assert.AreEqual("bool", dr.GetDataTypeName(4));
517 dr.Close();
519 command.CommandText = "select * from tableb where 1 = 2";
521 dr = command.ExecuteReader();
523 dr.Read();
525 Assert.AreEqual("int4", dr.GetDataTypeName(0));
526 Assert.AreEqual("int2", dr.GetDataTypeName(1));
527 Assert.AreEqual("timestamp", dr.GetDataTypeName(2));
528 Assert.AreEqual("numeric", dr.GetDataTypeName(3));