2005-01-19 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Npgsql / Test / DataAdapterTests.cs
1 // created on 3/5/2003 at 14:29
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
23
24
25 using System;
26 using System.Data;
27 using System.Web.UI.WebControls;
28 using Npgsql;
29
30 using NpgsqlTypes;
31
32 using NUnit.Framework;
33 using NUnit.Core;
34
35 namespace NpgsqlTests
36 {
37         
38         [TestFixture]
39         public class DataAdapterTests
40         {
41                 
42                 private NpgsqlConnection        _conn = null;
43                 private String                                          _connString = "Server=localhost;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests;maxpoolsize=2;";
44                 
45                 [SetUp]
46                 protected void SetUp()
47                 {
48                         //NpgsqlEventLog.Level = LogLevel.None;
49                         //NpgsqlEventLog.Level = LogLevel.Debug;
50                         //NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
51                         _conn = new NpgsqlConnection(_connString);
52                 }
53                 
54                 [TearDown]
55                 protected void TearDown()
56                 {
57                         if (_conn.State != ConnectionState.Closed)
58                                 _conn.Close();
59                 }
60                 
61                 [Test]
62                 public void InsertWithDataSet()
63                 {
64                         
65                         _conn.Open();
66                         
67                         DataSet ds = new DataSet();
68
69                         NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb", _conn);
70         
71                         da.InsertCommand = new NpgsqlCommand("insert into tableb(field_int2, field_timestamp, field_numeric) values (:a, :b, :c)", _conn);
72                         
73                         da.InsertCommand.Parameters.Add(new NpgsqlParameter("a", DbType.Int16));
74         
75                         da.InsertCommand.Parameters.Add(new NpgsqlParameter("b", DbType.DateTime));
76                         
77                         da.InsertCommand.Parameters.Add(new NpgsqlParameter("c", DbType.Decimal));
78         
79                         da.InsertCommand.Parameters[0].Direction = ParameterDirection.Input;
80                         da.InsertCommand.Parameters[1].Direction = ParameterDirection.Input;
81                         da.InsertCommand.Parameters[2].Direction = ParameterDirection.Input;
82         
83                         da.InsertCommand.Parameters[0].SourceColumn = "field_int2";
84                         da.InsertCommand.Parameters[1].SourceColumn = "field_timestamp";
85                         da.InsertCommand.Parameters[2].SourceColumn = "field_numeric";
86         
87         
88                         da.Fill(ds);
89         
90                         
91                         DataTable dt = ds.Tables[0];
92                         
93                         DataRow dr = dt.NewRow();
94                         dr["field_int2"] = 4;
95                         dr["field_timestamp"] = new DateTime(2003, 03, 03, 14, 0, 0);
96                         dr["field_numeric"] = 7.3M;
97                         
98                         dt.Rows.Add(dr);
99         
100                         
101                         DataSet ds2 = ds.GetChanges();
102                         
103                         da.Update(ds2);
104                         
105                         ds.Merge(ds2);
106                         ds.AcceptChanges();
107                         
108                         
109                         NpgsqlDataReader dr2 = new NpgsqlCommand("select * from tableb where field_serial > 4", _conn).ExecuteReader();
110                         dr2.Read();
111                         
112                         
113                         Assert.AreEqual(4, dr2[1]);
114                         Assert.AreEqual(7.3000000M, dr2[3]);
115                         
116                         new NpgsqlCommand("delete from tableb where field_serial > 4", _conn).ExecuteNonQuery();
117                         
118                         
119                                                 
120                 }
121                 
122                 [Test]
123                 public void FillWithEmptyResultset()
124                 {
125                   
126                   _conn.Open();
127                         
128                         DataSet ds = new DataSet();
129
130                         NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb where field_serial = -1", _conn);
131                   
132                   
133                   da.Fill(ds);
134                   
135                   Assert.AreEqual(1, ds.Tables.Count);
136                   Assert.AreEqual(4, ds.Tables[0].Columns.Count);
137                   Assert.AreEqual("field_serial", ds.Tables[0].Columns[0].ColumnName);
138                   Assert.AreEqual("field_int2", ds.Tables[0].Columns[1].ColumnName);
139                   Assert.AreEqual("field_timestamp", ds.Tables[0].Columns[2].ColumnName);
140                   Assert.AreEqual("field_numeric", ds.Tables[0].Columns[3].ColumnName);
141                   
142                 }
143         
144         [Test]
145         public void FillWithDuplicateColumnName()
146         {
147             _conn.Open();
148             DataSet ds = new DataSet();
149
150                         NpgsqlDataAdapter da = new NpgsqlDataAdapter("select field_serial, field_serial from tableb", _conn);
151                   
152                     da.Fill(ds);
153             
154         }
155         }
156 }