DataAdapterTests.cs 4.3 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. private NpgsqlConnection _conn = null;
  36. private String _connString = "Server=localhost;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests";
  37. [SetUp]
  38. protected void SetUp()
  39. {
  40. //NpgsqlEventLog.Level = LogLevel.None;
  41. NpgsqlEventLog.Level = LogLevel.Debug;
  42. NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
  43. _conn = new NpgsqlConnection(_connString);
  44. }
  45. [TearDown]
  46. protected void TearDown()
  47. {
  48. if (_conn.State != ConnectionState.Closed)
  49. _conn.Close();
  50. }
  51. [Test]
  52. public void InsertWithDataSet()
  53. {
  54. _conn.Open();
  55. DataSet ds = new DataSet();
  56. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb", _conn);
  57. da.InsertCommand = new NpgsqlCommand("insert into tableb(field_int2, field_timestamp, field_numeric) values (:a, :b, :c)", _conn);
  58. da.InsertCommand.Parameters.Add(new NpgsqlParameter("a", DbType.Int16));
  59. da.InsertCommand.Parameters.Add(new NpgsqlParameter("b", DbType.DateTime));
  60. da.InsertCommand.Parameters.Add(new NpgsqlParameter("c", DbType.Decimal));
  61. da.InsertCommand.Parameters[0].Direction = ParameterDirection.Input;
  62. da.InsertCommand.Parameters[1].Direction = ParameterDirection.Input;
  63. da.InsertCommand.Parameters[2].Direction = ParameterDirection.Input;
  64. da.InsertCommand.Parameters[0].SourceColumn = "field_int2";
  65. da.InsertCommand.Parameters[1].SourceColumn = "field_timestamp";
  66. da.InsertCommand.Parameters[2].SourceColumn = "field_numeric";
  67. da.Fill(ds);
  68. DataTable dt = ds.Tables[0];
  69. DataRow dr = dt.NewRow();
  70. dr["field_int2"] = 4;
  71. dr["field_timestamp"] = new DateTime(2003, 03, 03, 14, 0, 0);
  72. dr["field_numeric"] = 7.3M;
  73. dt.Rows.Add(dr);
  74. DataSet ds2 = ds.GetChanges();
  75. da.Update(ds2);
  76. ds.Merge(ds2);
  77. ds.AcceptChanges();
  78. NpgsqlDataReader dr2 = new NpgsqlCommand("select * from tableb where field_serial > 4", _conn).ExecuteReader();
  79. dr2.Read();
  80. Assertion.AssertEquals(4, dr2[1]);
  81. Assertion.AssertEquals(7.3M, dr2[3]);
  82. new NpgsqlCommand("delete from tableb where field_serial > 4", _conn).ExecuteNonQuery();
  83. }
  84. [Test]
  85. public void FillWithEmptyResultset()
  86. {
  87. _conn.Open();
  88. DataSet ds = new DataSet();
  89. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb where field_serial = -1", _conn);
  90. da.Fill(ds);
  91. Assertion.AssertEquals(1, ds.Tables.Count);
  92. Assertion.AssertEquals(4, ds.Tables[0].Columns.Count);
  93. Assertion.AssertEquals("field_serial", ds.Tables[0].Columns[0].ColumnName);
  94. Assertion.AssertEquals("field_int2", ds.Tables[0].Columns[1].ColumnName);
  95. Assertion.AssertEquals("field_timestamp", ds.Tables[0].Columns[2].ColumnName);
  96. Assertion.AssertEquals("field_numeric", ds.Tables[0].Columns[3].ColumnName);
  97. }
  98. [Test]
  99. public void FillWithDuplicateColumnName()
  100. {
  101. _conn.Open();
  102. DataSet ds = new DataSet();
  103. NpgsqlDataAdapter da = new NpgsqlDataAdapter("select field_serial, field_serial from tableb", _conn);
  104. da.Fill(ds);
  105. }
  106. }
  107. }