2010-06-21 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / Npgsql / Test / ConnectionTests.cs
1 // project created on 30/11/2002 at 22:00
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 using System;
25 using Npgsql;
26 using System.Data;
27
28 using NUnit.Framework;
29
30 namespace NpgsqlTests
31 {
32         
33         
34                 
35         [TestFixture]
36         public class ConnectionTests
37         {
38                 NpgsqlConnection _conn;
39                 
40                 [SetUp]
41                 protected void SetUp()
42                 {
43                         //NpgsqlEventLog.Level = LogLevel.None;
44                         //NpgsqlEventLog.Level = LogLevel.Debug;
45                         //NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
46                         _conn = new NpgsqlConnection (TestConfiguration.NpgsqlConnectionString);
47                 }
48                 
49                 [TearDown]
50                 protected void TearDown()
51                 {
52                         if (_conn != null)
53                                 _conn.Close();
54                 }
55                 
56                 
57                 [Test]
58                 public void Open()
59                 {
60                         try{
61                                 _conn.Open();
62                                 //Assert.AreEqual("ConnectionOpen", ConnectionState.Open, _conn.State);
63                         } catch (Exception e)
64                         {
65                                 Console.WriteLine(e.ToString());
66                         }
67                         
68                         
69                 }
70                 
71                 [Test]
72                 public void ChangeDatabase()
73                 {
74                         _conn.Open();
75                         
76                         _conn.ChangeDatabase("template1");
77                         
78                         NpgsqlCommand command = new NpgsqlCommand("select current_database()", _conn);
79                         
80                         String result = (String)command.ExecuteScalar();
81                         
82                         Assert.AreEqual("template1", result);
83                                 
84                 }
85                 
86                 [Test]
87                 [ExpectedException(typeof(InvalidOperationException))]
88                 public void NestedTransaction()
89                 {
90                         _conn.Open();
91                         
92             NpgsqlTransaction t = null;
93             try
94             {
95                             t = _conn.BeginTransaction();
96                         
97                             t = _conn.BeginTransaction();
98             }
99             catch(Exception e)
100             {
101                 // Catch exception so we call rollback the transaction initiated.
102                 // This way, the connection pool doesn't get a connection with a transaction
103                 // started.
104                 t.Rollback();
105                 throw e;
106             }            
107                         
108                 }
109                 
110                 [Test]
111                 public void SequencialTransaction()
112                 {
113                         _conn.Open();
114                         
115                         NpgsqlTransaction t = _conn.BeginTransaction();
116                         
117                         t.Rollback();
118                         
119                         t = _conn.BeginTransaction();
120                         
121                         t.Rollback();
122                         
123                         
124                 }
125                 
126         }
127 }