error messages review
[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                 NpgsqlConnection _conn;
43                                 
44                 [SetUp]
45                 protected void SetUp()
46                 {
47                         //NpgsqlEventLog.Level = LogLevel.None;
48                         //NpgsqlEventLog.Level = LogLevel.Debug;
49                         //NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
50                         _conn = new NpgsqlConnection(TestConfiguration.NpgsqlConnectionString);
51                 }
52                 
53                 [TearDown]
54                 protected void TearDown()
55                 {
56                         if (_conn != null && _conn.State != ConnectionState.Closed)
57                                 _conn.Close();
58                 }
59                 
60                 [Test]
61                 public void InsertWithDataSet()
62                 {
63                         
64                         _conn.Open();
65                         
66                         DataSet ds = new DataSet();
67
68                         NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb", _conn);
69         
70                         da.InsertCommand = new NpgsqlCommand("insert into tableb(field_int2, field_timestamp, field_numeric) values (:a, :b, :c)", _conn);
71                         
72                         da.InsertCommand.Parameters.Add(new NpgsqlParameter("a", DbType.Int16));
73         
74                         da.InsertCommand.Parameters.Add(new NpgsqlParameter("b", DbType.DateTime));
75                         
76                         da.InsertCommand.Parameters.Add(new NpgsqlParameter("c", DbType.Decimal));
77         
78                         da.InsertCommand.Parameters[0].Direction = ParameterDirection.Input;
79                         da.InsertCommand.Parameters[1].Direction = ParameterDirection.Input;
80                         da.InsertCommand.Parameters[2].Direction = ParameterDirection.Input;
81         
82                         da.InsertCommand.Parameters[0].SourceColumn = "field_int2";
83                         da.InsertCommand.Parameters[1].SourceColumn = "field_timestamp";
84                         da.InsertCommand.Parameters[2].SourceColumn = "field_numeric";
85         
86         
87                         da.Fill(ds);
88         
89                         
90                         DataTable dt = ds.Tables[0];
91                         
92                         DataRow dr = dt.NewRow();
93                         dr["field_int2"] = 4;
94                         dr["field_timestamp"] = new DateTime(2003, 03, 03, 14, 0, 0);
95                         dr["field_numeric"] = 7.3M;
96                         
97                         dt.Rows.Add(dr);
98         
99                         
100                         DataSet ds2 = ds.GetChanges();
101                         
102                         da.Update(ds2);
103                         
104                         ds.Merge(ds2);
105                         ds.AcceptChanges();
106                         
107                         
108                         NpgsqlDataReader dr2 = new NpgsqlCommand("select * from tableb where field_serial > 4", _conn).ExecuteReader();
109                         dr2.Read();
110                         
111                         
112                         Assert.AreEqual(4, dr2[1]);
113                         Assert.AreEqual(7.3000000M, dr2[3]);
114                         
115                         new NpgsqlCommand("delete from tableb where field_serial > 4", _conn).ExecuteNonQuery();
116                         
117                         
118                                                 
119                 }
120                 
121                 [Test]
122                 public void FillWithEmptyResultset()
123                 {
124                   
125                   _conn.Open();
126                         
127                         DataSet ds = new DataSet();
128
129                         NpgsqlDataAdapter da = new NpgsqlDataAdapter("select * from tableb where field_serial = -1", _conn);
130                   
131                   
132                   da.Fill(ds);
133                   
134                   Assert.AreEqual(1, ds.Tables.Count);
135                   Assert.AreEqual(4, ds.Tables[0].Columns.Count);
136                   Assert.AreEqual("field_serial", ds.Tables[0].Columns[0].ColumnName);
137                   Assert.AreEqual("field_int2", ds.Tables[0].Columns[1].ColumnName);
138                   Assert.AreEqual("field_timestamp", ds.Tables[0].Columns[2].ColumnName);
139                   Assert.AreEqual("field_numeric", ds.Tables[0].Columns[3].ColumnName);
140                   
141                 }
142         
143         [Test]
144         public void FillWithDuplicateColumnName()
145         {
146             _conn.Open();
147             DataSet ds = new DataSet();
148
149                         NpgsqlDataAdapter da = new NpgsqlDataAdapter("select field_serial, field_serial from tableb", _conn);
150                   
151                     da.Fill(ds);
152             
153         }
154         }
155 }