1 // created on 27/12/2002 at 17:05
4 // Francisco Figueiredo Jr. <fxjrlists@yahoo.com>
6 // Copyright (C) 2002 The Npgsql Development Team
7 // npgsql-general@gborg.postgresql.org
8 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
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.
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.
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
26 using System
.Web
.UI
.WebControls
;
29 using NUnit
.Framework
;
36 public class DataReaderTests
39 NpgsqlConnection _conn
;
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
);
51 protected void TearDown()
53 if (_conn
!= null &&_conn
.State
!= ConnectionState
.Closed
)
58 public void GetBoolean()
62 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea where field_serial = 4;", _conn
);
64 NpgsqlDataReader dr
= command
.ExecuteReader();
67 Boolean result
= dr
.GetBoolean(4);
68 Assert
.AreEqual(true, result
);
74 public void GetChars()
77 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn
);
79 NpgsqlDataReader dr
= command
.ExecuteReader();
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);*/
96 public void GetInt32()
99 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea where field_serial = 2;", _conn
);
101 NpgsqlDataReader dr
= command
.ExecuteReader();
106 Int32 result
= dr
.GetInt32(2);
108 //ConsoleWriter cw = new ConsoleWriter(Console.Out);
110 //cw.WriteLine(result.GetType().Name);
111 Assert
.AreEqual(4, result
);
117 public void GetInt16()
120 NpgsqlCommand command
= new NpgsqlCommand("select * from tableb where field_serial = 1;", _conn
);
122 NpgsqlDataReader dr
= command
.ExecuteReader();
126 Int16 result
= dr
.GetInt16(1);
128 Assert
.AreEqual(2, result
);
134 public void GetDecimal()
137 NpgsqlCommand command
= new NpgsqlCommand("select * from tableb where field_serial = 3;", _conn
);
139 NpgsqlDataReader dr
= command
.ExecuteReader();
143 Decimal result
= dr
.GetDecimal(3);
146 Assert
.AreEqual(4.2300000M
, result
);
154 public void GetDouble()
157 NpgsqlCommand command
= new NpgsqlCommand("select * from tabled where field_serial = 2;", _conn
);
159 NpgsqlDataReader dr
= command
.ExecuteReader();
163 //Double result = Double.Parse(dr.GetInt32(2).ToString());
164 Double result
= dr
.GetDouble(2);
166 Assert
.AreEqual(.123456789012345D
, result
);
172 public void GetFloat()
175 NpgsqlCommand command
= new NpgsqlCommand("select * from tabled where field_serial = 1;", _conn
);
177 NpgsqlDataReader dr
= command
.ExecuteReader();
181 //Single result = Single.Parse(dr.GetInt32(2).ToString());
182 Single result
= dr
.GetFloat(1);
184 Assert
.AreEqual(.123456F
, result
);
190 public void GetString()
193 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn
);
195 NpgsqlDataReader dr
= command
.ExecuteReader();
199 String result
= dr
.GetString(1);
201 Assert
.AreEqual("Random text", result
);
207 public void GetStringWithParameter()
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
;
219 command
.Parameters
.Add(param
);
221 NpgsqlDataReader dr
= command
.ExecuteReader();
225 String result
= dr
.GetString(1);
227 Assert
.AreEqual(test
, result
);
232 public void GetStringWithQuoteWithParameter()
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
;
244 command
.Parameters
.Add(param
);
246 NpgsqlDataReader dr
= command
.ExecuteReader();
250 String result
= dr
.GetString(1);
252 Assert
.AreEqual(test
, result
);
258 public void GetValueByName()
261 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea where field_serial = 1;", _conn
);
263 NpgsqlDataReader dr
= command
.ExecuteReader();
267 String result
= (String
) dr
["field_text"];
269 Assert
.AreEqual("Random text", result
);
274 [ExpectedException(typeof(InvalidOperationException
))]
275 public void GetValueFromEmptyResultset()
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
;
287 command
.Parameters
.Add(param
);
289 NpgsqlDataReader dr
= command
.ExecuteReader();
294 // This line should throw the invalid operation exception as the datareader will
295 // have an empty resultset.
296 Console
.WriteLine(dr
.IsDBNull(1));
303 public void TestOverlappedParameterNames()
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();
319 [ExpectedException(typeof(IndexOutOfRangeException
))]
320 public void TestNonExistentParameterName()
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();
340 public void UseDataAdapter()
345 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea", _conn
);
347 NpgsqlDataAdapter da
= new NpgsqlDataAdapter();
349 da
.SelectCommand
= command
;
351 DataSet ds
= new DataSet();
355 //ds.WriteXml("TestUseDataAdapter.xml");
361 public void UseDataAdapterNpgsqlConnectionConstructor()
366 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea", _conn
);
368 command
.Connection
= _conn
;
370 NpgsqlDataAdapter da
= new NpgsqlDataAdapter(command
);
372 DataSet ds
= new DataSet();
376 //ds.WriteXml("TestUseDataAdapterNpgsqlConnectionConstructor.xml");
382 public void UseDataAdapterStringNpgsqlConnectionConstructor()
388 NpgsqlDataAdapter da
= new NpgsqlDataAdapter("select * from tablea", _conn
);
390 DataSet ds
= new DataSet();
394 //ds.WriteXml("TestUseDataAdapterStringNpgsqlConnectionConstructor.xml");
401 public void UseDataAdapterStringStringConstructor()
407 NpgsqlDataAdapter da
= new NpgsqlDataAdapter("select * from tablea", TestConfiguration
.NpgsqlConnectionString
);
409 DataSet ds
= new DataSet();
413 ds
.WriteXml("TestUseDataAdapterStringStringConstructor.xml");
419 public void UseDataAdapterStringStringConstructor2()
425 NpgsqlDataAdapter da
= new NpgsqlDataAdapter("select * from tableb", TestConfiguration
.NpgsqlConnectionString
);
427 DataSet ds
= new DataSet();
431 ds
.WriteXml("TestUseDataAdapterStringStringConstructor2.xml");
437 public void DataGridWebControlSupport()
442 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea;", _conn
);
444 NpgsqlDataReader dr
= command
.ExecuteReader();
446 DataGrid dg
= new DataGrid();
456 [ExpectedException(typeof(InvalidOperationException
))]
457 public void ReadPastDataReaderEnd()
460 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea;", _conn
);
462 NpgsqlDataReader dr
= command
.ExecuteReader();
471 public void IsDBNull()
474 NpgsqlCommand command
= new NpgsqlCommand("select field_text from tablea;", _conn
);
476 NpgsqlDataReader dr
= command
.ExecuteReader();
479 Assert
.AreEqual(false, dr
.IsDBNull(0));
481 Assert
.AreEqual(true, dr
.IsDBNull(0));
487 public void IsDBNullFromScalar()
490 NpgsqlCommand command
= new NpgsqlCommand("select max(field_serial) from tablea;", _conn
);
492 NpgsqlDataReader dr
= command
.ExecuteReader();
495 Assert
.AreEqual(false, dr
.IsDBNull(0));
502 public void TypesNames()
505 NpgsqlCommand command
= new NpgsqlCommand("select * from tablea where 1 = 2;", _conn
);
507 NpgsqlDataReader dr
= command
.ExecuteReader();
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));
519 command
.CommandText
= "select * from tableb where 1 = 2";
521 dr
= command
.ExecuteReader();
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));