Skip to content

Commit

Permalink
Unit tests around command behavior across connections
Browse files Browse the repository at this point in the history
Relates to npgsql#395
  • Loading branch information
roji committed Oct 26, 2014
1 parent 6440284 commit 5a38024
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Npgsql/NpgsqlCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,26 @@ public Int64 LastInsertedOID
get { return _lastInsertedOid; }
}

/// <summary>
/// Returns whether this query will execute as a prepared (compiled) query.
/// </summary>
public bool IsPrepared
{
get
{
switch (_prepared)
{
case PrepareStatus.NotPrepared:
return false;
case PrepareStatus.NeedsPrepare:
case PrepareStatus.Prepared:
return true;
default:
throw new ArgumentOutOfRangeException();
}
}
}

#endregion Public properties

#region State management
Expand Down
68 changes: 68 additions & 0 deletions tests/CommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4175,5 +4175,73 @@ public void DisposePreparedAfterCommandClose()
}
}
}

[Test]
[IssueLink("https://github.com/npgsql/npgsql/issues/395")]
public void PreparedAcrossCloseOpen()
{
using (var c = new NpgsqlConnection(ConnectionString))
{
using (var cmd = c.CreateCommand())
{
c.Open();
cmd.CommandText = "SELECT 1";
cmd.Prepare();
Assert.That(cmd.IsPrepared, Is.True);
c.Close();
c.Open();
Assert.That(cmd.IsPrepared, Is.False);
cmd.Prepare();
Assert.That(cmd.ExecuteScalar(), Is.EqualTo(1));
}
}
}

[Test]
[IssueLink("https://github.com/npgsql/npgsql/issues/395")]
public void UseAcrossConnectionChange([Values(PrepareOrNot.Prepared, PrepareOrNot.NotPrepared)] PrepareOrNot prepare)
{
using (var c = new NpgsqlConnection(ConnectionString))
{
using (var cmd = c.CreateCommand())
{
c.Open();
cmd.CommandText = "SELECT 1";
if (prepare == PrepareOrNot.Prepared)
cmd.Prepare();
cmd.Connection = Conn;
Assert.That(cmd.IsPrepared, Is.False);
if (prepare == PrepareOrNot.Prepared)
cmd.Prepare();
Assert.That(cmd.ExecuteScalar(), Is.EqualTo(1));
}
}
}

[Test]
[IssueLink("https://github.com/npgsql/npgsql/issues/395")]
public void DefaultCommandTimeout()
{
if (Conn.Connector.DefaultCommandTimeout >= 100 && Conn.Connector.DefaultCommandTimeout < 105)
TestUtil.Inconclusive("Bad default command timeout");
using (var c1 = new NpgsqlConnection(ConnectionString + ";CommandTimeout=100"))
{
using (var cmd = c1.CreateCommand())
{
Assert.That(cmd.CommandTimeout, Is.EqualTo(100));
using (var c2 = new NpgsqlConnection(ConnectionString + ";CommandTimeout=101"))
{
cmd.Connection = c2;
Assert.That(cmd.CommandTimeout, Is.EqualTo(101));
}
cmd.CommandTimeout = 102;
using (var c2 = new NpgsqlConnection(ConnectionString + ";CommandTimeout=101"))
{
cmd.Connection = c2;
Assert.That(cmd.CommandTimeout, Is.EqualTo(102));
}
}
}
}
}
}

0 comments on commit 5a38024

Please sign in to comment.