DataAdapterTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // created on 3/5/2003 at 14:29
  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 NpgsqlTypes;
  28. using NUnit.Framework;
  29. using NUnit.Core;
  30. namespace NpgsqlTests
  31. {
  32. [TestFixture]
  33. public class DataAdapterTests
  34. {
  35. NpgsqlConnection _conn;
  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(TestConfiguration.NpgsqlConnectionString);
  43. }
  44. [TearDown]
  45. protected void TearDown()
  46. {
  47. if (_conn != null && _conn.State != ConnectionState.Closed)
  48. _conn.Close();
  49. }
  50. [Test]
  51. public void InsertWithDataSet()
  52. {
  53. _conn.Open();
  54. DataSet ds = new DataSet();
  55. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb", _conn);
  56. da.InsertCommand = new NpgsqlCommand("insert into tableb(field_int2, field_timestamp, field_numeric) values (:a, :b, :c)", _conn);
  57. da.InsertCommand.Parameters.Add(new NpgsqlParameter("a", DbType.Int16));
  58. da.InsertCommand.Parameters.Add(new NpgsqlParameter("b", DbType.DateTime));
  59. da.InsertCommand.Parameters.Add(new NpgsqlParameter("c", DbType.Decimal));
  60. da.InsertCommand.Parameters[0].Direction = ParameterDirection.Input;
  61. da.InsertCommand.Parameters[1].Direction = ParameterDirection.Input;
  62. da.InsertCommand.Parameters[2].Direction = ParameterDirection.Input;
  63. da.InsertCommand.Parameters[0].SourceColumn = "field_int2";
  64. da.InsertCommand.Parameters[1].SourceColumn = "field_timestamp";
  65. da.InsertCommand.Parameters[2].SourceColumn = "field_numeric";
  66. da.Fill(ds);
  67. DataTable dt = ds.Tables[0];
  68. DataRow dr = dt.NewRow();
  69. dr["field_int2"] = 4;
  70. dr["field_timestamp"] = new DateTime(2003, 03, 03, 14, 0, 0);
  71. dr["field_numeric"] = 7.3M;
  72. dt.Rows.Add(dr);
  73. DataSet ds2 = ds.GetChanges();
  74. da.Update(ds2);
  75. ds.Merge(ds2);
  76. ds.AcceptChanges();
  77. NpgsqlDataReader dr2 = new NpgsqlCommand("select * from tableb where field_serial > 4", _conn).ExecuteReader();
  78. dr2.Read();
  79. Assert.AreEqual(4, dr2[1]);
  80. Assert.AreEqual(7.3000000M, dr2[3]);
  81. new NpgsqlCommand("delete from tableb where field_serial > 4", _conn).ExecuteNonQuery();
  82. }
  83. [Test]
  84. public void FillWithEmptyResultset()
  85. {
  86. _conn.Open();
  87. DataSet ds = new DataSet();
  88. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb where field_serial = -1", _conn);
  89. da.Fill(ds);
  90. Assert.AreEqual(1, ds.Tables.Count);
  91. Assert.AreEqual(4, ds.Tables[0].Columns.Count);
  92. Assert.AreEqual("field_serial", ds.Tables[0].Columns[0].ColumnName);
  93. Assert.AreEqual("field_int2", ds.Tables[0].Columns[1].ColumnName);
  94. Assert.AreEqual("field_timestamp", ds.Tables[0].Columns[2].ColumnName);
  95. Assert.AreEqual("field_numeric", ds.Tables[0].Columns[3].ColumnName);
  96. }
  97. [Test]
  98. public void FillWithDuplicateColumnName()
  99. {
  100. _conn.Open();
  101. DataSet ds = new DataSet();
  102. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select field_serial, field_serial from tableb", _conn);
  103. da.Fill(ds);
  104. }
  105. }
  106. }