ConnectionTests.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // project created on 30/11/2002 at 22:00
  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 Npgsql;
  25. using System.Data;
  26. using NUnit.Framework;
  27. namespace NpgsqlTests
  28. {
  29. [TestFixture]
  30. public class ConnectionTests
  31. {
  32. private NpgsqlConnection _conn = null;
  33. private String _connString = "Server=localhost;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests";
  34. [SetUp]
  35. protected void SetUp()
  36. {
  37. //NpgsqlEventLog.Level = LogLevel.None;
  38. NpgsqlEventLog.Level = LogLevel.Debug;
  39. NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
  40. _conn = new NpgsqlConnection(_connString);
  41. }
  42. [TearDown]
  43. protected void TearDown()
  44. {
  45. _conn.Close();
  46. }
  47. [Test]
  48. public void Open()
  49. {
  50. try{
  51. _conn.Open();
  52. //Assertion.AssertEquals("ConnectionOpen", ConnectionState.Open, _conn.State);
  53. } catch (Exception e)
  54. {
  55. Console.WriteLine(e.ToString());
  56. }
  57. }
  58. [Test]
  59. public void ChangeDatabase()
  60. {
  61. _conn.Open();
  62. _conn.ChangeDatabase("template1");
  63. NpgsqlCommand command = new NpgsqlCommand("select current_database()", _conn);
  64. String result = (String)command.ExecuteScalar();
  65. Assertion.AssertEquals("template1", result);
  66. }
  67. [Test]
  68. [ExpectedException(typeof(InvalidOperationException))]
  69. public void NestedTransaction()
  70. {
  71. _conn.Open();
  72. NpgsqlTransaction t = null;
  73. try
  74. {
  75. t = _conn.BeginTransaction();
  76. t = _conn.BeginTransaction();
  77. }
  78. catch(Exception e)
  79. {
  80. // Catch exception so we call rollback the transaction initiated.
  81. // This way, the connection pool doesn't get a connection with a transaction
  82. // started.
  83. t.Rollback();
  84. throw e;
  85. }
  86. }
  87. [Test]
  88. public void SequencialTransaction()
  89. {
  90. _conn.Open();
  91. NpgsqlTransaction t = _conn.BeginTransaction();
  92. t.Rollback();
  93. t = _conn.BeginTransaction();
  94. t.Rollback();
  95. }
  96. }
  97. }