Skip to content

Commit 0a61d0b

Browse files
committed
Increase coverage
1 parent 847e38a commit 0a61d0b

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

PSql.Engine/SqlConnection.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public SqlConnection(
9999
/// </summary>
100100
protected bool HasErrors { get; private set; }
101101

102+
/// <summary>
103+
/// Gets whether the connection is open.
104+
/// </summary>
105+
public bool IsOpen => Connection.State is ConnectionState.Open;
106+
102107
/// <summary>
103108
/// Throws an exception if one or more error messages have been received
104109
/// over the connection since the most recent invocation of
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright Subatomix Research Inc.
2+
// SPDX-License-Identifier: MIT
3+
4+
namespace PSql.Tests.Integration;
5+
6+
[TestFixture]
7+
public class DisconnectSqlCommandIntegrationTests
8+
{
9+
[Test]
10+
public void Invoke_Default()
11+
{
12+
Execute(
13+
"""
14+
Disconnect-Sql
15+
"""
16+
)
17+
.ShouldBeEmpty();
18+
}
19+
20+
[Test]
21+
public void Invoke_Pipeline()
22+
{
23+
Execute(
24+
"""
25+
$Connections = (Connect-Sql), $null, (Connect-Sql)
26+
$Connections | Disconnect-Sql
27+
$Connections
28+
"""
29+
)
30+
.ShouldBe([false, null, false]);
31+
}
32+
33+
[Test]
34+
public void Invoke_Connection()
35+
{
36+
Execute(
37+
"""
38+
$Connections = $null, (Connect-Sql), (Connect-Sql)
39+
Disconnect-Sql -Connection $Connections
40+
$Connections
41+
"""
42+
)
43+
.ShouldBe(
44+
[null, false, false]
45+
);
46+
}
47+
48+
private static bool?[] Execute(string script)
49+
{
50+
var (output, exception) = ScriptExecutor.Execute(script);
51+
52+
exception.ShouldBeNull();
53+
54+
return output
55+
.Select(o => (SqlConnection?) o?.BaseObject)
56+
.Select(c => c?.InnerConnection.IsOpen)
57+
.ToArray();
58+
}
59+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright Subatomix Research Inc.
2+
// SPDX-License-Identifier: MIT
3+
4+
namespace PSql.Tests.Integration;
5+
6+
[TestFixture]
7+
public class ConnectSqlCommandIntegrationTests
8+
{
9+
[Test]
10+
public void Invoke_Default()
11+
{
12+
Execute(
13+
"Connect-Sql"
14+
)
15+
.ShouldBe(
16+
"Data Source=.;Integrated Security=true;" +
17+
"Encrypt=false;Trust Server Certificate=true;Pooling=false"
18+
);
19+
}
20+
21+
[Test]
22+
public void Invoke_Pipeline()
23+
{
24+
Execute(
25+
"New-SqlContext -ReadOnlyIntent | Connect-Sql"
26+
)
27+
.ShouldBe(
28+
"Data Source=.;Integrated Security=true;" +
29+
"Encrypt=false;Trust Server Certificate=true;" +
30+
"Application Intent=ReadOnly;Pooling=false"
31+
);
32+
}
33+
34+
[Test]
35+
public void Invoke_Context()
36+
{
37+
Execute(
38+
"Connect-Sql -Context (New-SqlContext -ReadOnlyIntent)"
39+
)
40+
.ShouldBe(
41+
"Data Source=.;Integrated Security=true;" +
42+
"Encrypt=false;Trust Server Certificate=true;" +
43+
"Application Intent=ReadOnly;Pooling=false"
44+
);
45+
}
46+
47+
[Test]
48+
public void Invoke_DatabaseName()
49+
{
50+
Execute(
51+
"New-SqlContext -ReadOnlyIntent | Connect-Sql -DatabaseName master"
52+
)
53+
.ShouldBe(
54+
"Data Source=.;Initial Catalog=master;Integrated Security=true;" +
55+
"Encrypt=false;Trust Server Certificate=true;" +
56+
"Application Intent=ReadOnly;Pooling=false"
57+
);
58+
}
59+
60+
private static string Execute(string script)
61+
{
62+
var (output, exception) = ScriptExecutor.Execute(script);
63+
64+
exception.ShouldBeNull();
65+
66+
output.ShouldHaveSingleItem().ShouldBeOfType<PSObject>()
67+
.BaseObject.ShouldBeOfType<SqlConnection>().AssignTo(out var connection);
68+
69+
try
70+
{
71+
return connection.InnerConnection.ConnectionString;
72+
}
73+
finally
74+
{
75+
connection.Dispose();
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)