2007-07-31 Nagappan A <anagappan@novell.com>
[mono.git] / mcs / class / System.Data / Test / ProviderTests / System.Data.Odbc / OdbcDataAdapterTest.cs
1 // OdbcDataAdapterTest.cs - NUnit Test Cases for testing the
2 //                          OdbcDataAdapter class
3 // Author:
4 //      Sureshkumar T (TSureshkumar@novell.com)
5 //
6 // Copyright (c) 2004 Novell Inc., and the individuals listed
7 // on the ChangeLog entries.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Data;
31 using System.Data.Odbc;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Data.Odbc
36 {
37
38         [TestFixture]
39         [Category ("odbc")]
40         public class OdbcDataAdapterTest
41         {
42           
43                 [Test]
44                 public void FillTest () 
45                 {
46                         IDbConnection conn = ConnectionManager.Singleton.Connection;
47                         try {
48                                 ConnectionManager.Singleton.OpenConnection ();
49                                 // For this Test, you must create sample table
50                                 // called person-age, with a non-zero number of rows
51                                 // and non-zero number of columns
52                                 // run the test initialization script mono_test_mysql.sql
53                                 string tableName = "employee";
54                                 string sql= "select * from " + tableName; 
55                                 OdbcDataAdapter da = new OdbcDataAdapter (sql, (OdbcConnection) conn);
56                                 DataSet ds = new DataSet (tableName);
57                                 da.Fill (ds, tableName);
58                                 Assert.AreEqual (true, 
59                                                  ds.Tables.Count > 0,
60                                                  "#1 Table count must not be zero");
61                                 Assert.AreEqual (true, 
62                                                  ds.Tables [0].Rows.Count > 0,
63                                                  "#2 Row count must not be zero");
64                                 foreach (DataColumn dc in ds.Tables [0].Columns)
65                                         Assert.AreEqual (true,
66                                                          dc.ColumnName.Length > 0,
67                                                          "#3 DataSet column names must noot be of size 0");
68                                 foreach (DataRow dr in ds.Tables [0].Rows) {
69                                         foreach (DataColumn dc in ds.Tables [0].Columns) 
70                                                 Assert.AreEqual (true,
71                                                                  dc.ColumnName.Length > 0,
72                                                                  "#4 column values must not be of size 0");
73                                 }
74                         } finally {
75                                 ConnectionManager.Singleton.CloseConnection ();
76                         }
77                 }
78                 [Test]
79                 public void InsertUtf8Test ()
80                 {
81                         IDbConnection conn = ConnectionManager.Singleton.Connection;
82                         try {
83                                 ConnectionManager.Singleton.OpenConnection ();
84                                 DoExecuteNonQuery ((OdbcConnection) conn,
85                                                    "CREATE TABLE odbc_ins_utf8_test(ival int not null, sval varchar(20))");
86                                 Assert.AreEqual (DoExecuteNonQuery ((OdbcConnection) conn,
87                                                                     "INSERT INTO odbc_ins_utf8_test(ival, sval) VALUES (1, 'English')"),
88                                                  1);
89                                 Assert.AreEqual (DoExecuteNonQuery ((OdbcConnection) conn,
90                                                                     "INSERT INTO odbc_ins_utf8_test(ival, sval) VALUES (2, 'Français')"),
91                                                  1);
92                                 Assert.AreEqual (DoExecuteNonQuery ((OdbcConnection) conn,
93                                                                     "INSERT INTO odbc_ins_utf8_test(ival, sval) VALUES (3, 'Español')"),
94                                                  1);
95                                 Assert.AreEqual (DoExecuteScalar   ((OdbcConnection) conn,
96                                                                     "SELECT COUNT(*) FROM odbc_ins_utf8_test WHERE sval " +
97                                                                     "IN('English', 'Français', 'Español')"),
98                                                  3);
99                         } finally {
100                                 DoExecuteNonQuery ((OdbcConnection) conn, "DROP TABLE odbc_ins_utf8_test");
101                                 ConnectionManager.Singleton.CloseConnection ();
102                         }
103                 }
104                 private int DoExecuteNonQuery (OdbcConnection conn, string sql) {
105                         OdbcCommand cmd = new OdbcCommand (sql, conn);
106                         return cmd.ExecuteNonQuery ();
107                 }
108
109                 private int DoExecuteScalar (OdbcConnection conn, string sql) {
110                         OdbcCommand cmd = new OdbcCommand (sql, conn);
111                         object value = cmd.ExecuteScalar ();
112                         return (int) Convert.ChangeType (value, typeof (int));
113                 }
114         }
115 }