Skip to content

Commit 5a7cee0

Browse files
committed
Code factorization
1 parent 27f809d commit 5a7cee0

13 files changed

Lines changed: 86 additions & 67 deletions

res/Database.sqlite

-12 KB
Binary file not shown.

test/AssertExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Belin.Sql;
2+
3+
using System.Data.SQLite;
4+
5+
/// <summary>
6+
/// Provides extensions members for test assertions.
7+
/// </summary>
8+
public static class AssertExtensions {
9+
// TODO (.NET 10) extension(Assert _) {
10+
11+
/// <summary>
12+
/// Creates a new in-memory SQLite database, initialized with a default dataset.
13+
/// </summary>
14+
/// <param name="_">The instance of the assertion functionality.</param>
15+
/// <returns>The connection to the newly created in-memory database.</returns>
16+
public static SQLiteConnection CreateInMemoryDatabase(this Assert _) {
17+
var connection = new SQLiteConnection("DataSource=:memory:");
18+
connection.Open();
19+
20+
using var command = connection.CreateCommand();
21+
command.CommandText = File.ReadAllText(Path.Join(AppContext.BaseDirectory, "../res/Schema.sql"));
22+
command.ExecuteNonQuery();
23+
24+
return connection;
25+
}
26+
}

test/Cmdlets/AfterEach.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$connection.Close()

test/Cmdlets/BeforeAll.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Import-Module "$PSScriptRoot/../../Sql.psd1"
2+
Import-Module "$PSScriptRoot/../../bin/System.Data.SQLite.dll"
3+
. "$PSScriptRoot/../Fixtures/Character.ps1"

test/Cmdlets/BeforeEach.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using namespace System.Diagnostics.CodeAnalysis
2+
3+
[SuppressMessage("PSUseDeclaredVarsMoreThanAssignments", "")]
4+
$connection = [System.Data.SQLite.SQLiteConnection]::new("DataSource=:memory:")
5+
$connection.Open()
6+
7+
$command = $connection.CreateCommand()
8+
$command.CommandText = Get-Content "res/Schema.sql" -Raw
9+
$command.ExecuteNonQuery()
10+
$command.Dispose()

test/Cmdlets/Get-First.Tests.ps1

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
1-
using namespace System.Diagnostics.CodeAnalysis
2-
31
<#
42
.SYNOPSIS
53
Tests the features of the `Get-First` cmdlet.
64
#>
75
Describe "Get-First" {
8-
BeforeAll {
9-
Import-Module "$PSScriptRoot/../../Sql.psd1"
10-
Import-Module "$PSScriptRoot/../../bin/System.Data.SQLite.dll"
11-
. "$PSScriptRoot/../Fixtures/Character.ps1"
12-
}
13-
14-
BeforeEach {
15-
[SuppressMessage("PSUseDeclaredVarsMoreThanAssignments", "")]
16-
$connection = [System.Data.SQLite.SQLiteConnection]::new("DataSource=$PSScriptRoot/../../res/Database.sqlite")
17-
}
18-
19-
AfterEach {
20-
$connection.Close()
21-
}
6+
BeforeAll { . "$PSScriptRoot/BeforeAll.ps1" }
7+
BeforeEach { . "$PSScriptRoot/BeforeEach.ps1" }
8+
AfterEach { . "$PSScriptRoot/AfterEach.ps1" }
229

2310
It "should return the first record produced by the SQL query" {
2411
$sql = "SELECT * FROM Characters WHERE FullName = @FullName"

test/Cmdlets/Get-Mapper.Tests.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#>
55
Describe "Get-Mapper" {
66
BeforeAll {
7-
Import-Module "$PSScriptRoot/../../Sql.psd1"
8-
. "$PSScriptRoot/../Fixtures/Character.ps1"
7+
. "$PSScriptRoot/BeforeAll.ps1"
98
}
109

1110
Describe "CreateInstance()" {

test/Cmdlets/Get-Scalar.Tests.ps1

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
using namespace System.Diagnostics.CodeAnalysis
2-
31
<#
42
.SYNOPSIS
53
Tests the features of the `Get-Scalar` cmdlet.
64
#>
75
Describe "Get-Scalar" {
8-
BeforeAll {
9-
Import-Module "$PSScriptRoot/../../Sql.psd1"
10-
Import-Module "$PSScriptRoot/../../bin/System.Data.SQLite.dll"
11-
}
12-
13-
BeforeEach {
14-
[SuppressMessage("PSUseDeclaredVarsMoreThanAssignments", "")]
15-
$connection = [System.Data.SQLite.SQLiteConnection]::new("DataSource=$PSScriptRoot/../../res/Database.sqlite")
16-
}
17-
18-
AfterEach {
19-
$connection.Close()
20-
}
6+
BeforeAll { . "$PSScriptRoot/BeforeAll.ps1" }
7+
BeforeEach { . "$PSScriptRoot/BeforeEach.ps1" }
8+
AfterEach { . "$PSScriptRoot/AfterEach.ps1" }
219

2210
It "should return the single value produced by the SQL query" {
2311
$sql = "SELECT COUNT(*) FROM Characters WHERE Gender = @Gender"

test/Cmdlets/Get-Single.Tests.ps1

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
1-
using namespace System.Diagnostics.CodeAnalysis
2-
31
<#
42
.SYNOPSIS
53
Tests the features of the `Get-Single` cmdlet.
64
#>
75
Describe "Get-Single" {
8-
BeforeAll {
9-
Import-Module "$PSScriptRoot/../../Sql.psd1"
10-
Import-Module "$PSScriptRoot/../../bin/System.Data.SQLite.dll"
11-
. "$PSScriptRoot/../Fixtures/Character.ps1"
12-
}
13-
14-
BeforeEach {
15-
[SuppressMessage("PSUseDeclaredVarsMoreThanAssignments", "")]
16-
$connection = [System.Data.SQLite.SQLiteConnection]::new("DataSource=$PSScriptRoot/../../res/Database.sqlite")
17-
}
18-
19-
AfterEach {
20-
$connection.Close()
21-
}
6+
BeforeAll { . "$PSScriptRoot/BeforeAll.ps1" }
7+
BeforeEach { . "$PSScriptRoot/BeforeEach.ps1" }
8+
AfterEach { . "$PSScriptRoot/AfterEach.ps1" }
229

2310
It "should return the single record produced by the SQL query" {
2411
$sql = "SELECT * FROM Characters WHERE FullName = @FullName"

test/Cmdlets/Invoke-Query.Tests.ps1

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
1-
using namespace System.Diagnostics.CodeAnalysis
2-
31
<#
42
.SYNOPSIS
53
Tests the features of the `Invoke-Query` cmdlet.
64
#>
75
Describe "Invoke-Query" {
8-
BeforeAll {
9-
Import-Module "$PSScriptRoot/../../Sql.psd1"
10-
Import-Module "$PSScriptRoot/../../bin/System.Data.SQLite.dll"
11-
. "$PSScriptRoot/../Fixtures/Character.ps1"
12-
}
13-
14-
BeforeEach {
15-
[SuppressMessage("PSUseDeclaredVarsMoreThanAssignments", "")]
16-
$connection = [System.Data.SQLite.SQLiteConnection]::new("DataSource=$PSScriptRoot/../../res/Database.sqlite")
17-
}
18-
19-
AfterEach {
20-
$connection.Close()
21-
}
6+
BeforeAll { . "$PSScriptRoot/BeforeAll.ps1" }
7+
BeforeEach { . "$PSScriptRoot/BeforeEach.ps1" }
8+
AfterEach { . "$PSScriptRoot/AfterEach.ps1" }
229

2310
It "should return the records produced by the SQL query" {
2411
$sql = "SELECT * FROM Characters WHERE Gender = @Gender ORDER BY FullName"

0 commit comments

Comments
 (0)