Skip to content

Commit 869efcb

Browse files
Fix unit tests.
1 parent 53cfb4b commit 869efcb

8 files changed

Lines changed: 81 additions & 69 deletions

File tree

tests/Testing.Databases.SqlServer.Dac.Tests/SqlServerDacExtensionsTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public void DeployDacPackage_WithSpecificDataFileName()
4545
// Create existing database to be sure the database is recreated when deploying the database with a DACPAC
4646
CreateDatabase("SqlServerDacExtensionsTest_DeployDacPackage_WithSpecificDataFileName");
4747

48-
using var temporaryFolder = TemporaryFolder.Create();
48+
using var otherDataPath = OtherDatabasePath.Create();
4949

5050
var server = new SqlServer(ConnectionString);
5151

5252
var settings = new SqlServerDacDeploymentSettings()
5353
{
54-
DataFileName = Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName.mdf"),
54+
DataFileName = Path.Combine(otherDataPath.Path, "TheSpecificDataFileName.mdf"),
5555
};
5656

5757
var database = server.DeployDacPackage("PosInformatique.Testing.Databases.SqlServer.Tests.DacPac.dacpac", "SqlServerDacExtensionsTest_DeployDacPackage_WithSpecificDataFileName", settings);
@@ -65,19 +65,19 @@ public void DeployDacPackage_WithSpecificDataFileName()
6565
database.InsertInto("MyTable", new { Id = 2, Name = "Name 2" });
6666

6767
// Check the location of the database
68-
File.Exists(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName.mdf")).Should().BeTrue();
69-
File.Exists(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName_log.ldf")).Should().BeTrue();
68+
File.Exists(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName.mdf")).Should().BeTrue();
69+
File.Exists(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName_log.ldf")).Should().BeTrue();
7070

7171
var result = database.ExecuteQuery("SELECT * FROM [sys].[database_files] ORDER BY [physical_name]");
7272

7373
result.Rows.Should().HaveCount(2);
7474

7575
result.Rows[0]["name"].Should().Be("SqlServerDacExtensionsTest_DeployDacPackage_WithSpecificDataFileName");
76-
result.Rows[0]["physical_name"].Should().Be(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName.mdf"));
76+
result.Rows[0]["physical_name"].Should().Be(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName.mdf"));
7777
result.Rows[0]["type_desc"].Should().Be("ROWS");
7878

7979
result.Rows[1]["name"].Should().Be("TheSpecificDataFileName_log");
80-
result.Rows[1]["physical_name"].Should().Be(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName_log.ldf"));
80+
result.Rows[1]["physical_name"].Should().Be(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName_log.ldf"));
8181
result.Rows[1]["type_desc"].Should().Be("LOG");
8282

8383
// Delete the database (for deleting the temporary folder).

tests/Testing.Databases.SqlServer.Dac.Tests/SqlServerDatabaseInitializerTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ public void Initialize_WithSpecificDataFileName()
129129
// Create existing database to be sure the database is recreated when deploying the database with a DACPAC
130130
CreateDatabase("SqlServerDatabaseInitializerTest_Initialize_WithSpecificDataFileName");
131131

132-
using var temporaryFolder = TemporaryFolder.Create();
132+
using var otherDataPath = OtherDatabasePath.Create();
133133

134134
var server = new SqlServer(ConnectionString);
135135

136136
var settings = new SqlServerDacDeploymentSettings()
137137
{
138-
DataFileName = Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName.mdf"),
138+
DataFileName = Path.Combine(otherDataPath.Path, "TheSpecificDataFileName.mdf"),
139139
};
140140

141141
var database = server.DeployDacPackage("PosInformatique.Testing.Databases.SqlServer.Tests.DacPac.dacpac", "SqlServerDatabaseInitializerTest_Initialize_WithSpecificDataFileName", settings);
@@ -149,19 +149,19 @@ public void Initialize_WithSpecificDataFileName()
149149
database.InsertInto("MyTable", new { Id = 2, Name = "Name 2" });
150150

151151
// Check the location of the database
152-
File.Exists(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName.mdf")).Should().BeTrue();
153-
File.Exists(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName_log.ldf")).Should().BeTrue();
152+
File.Exists(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName.mdf")).Should().BeTrue();
153+
File.Exists(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName_log.ldf")).Should().BeTrue();
154154

155155
var result = database.ExecuteQuery("SELECT * FROM [sys].[database_files] ORDER BY [physical_name]");
156156

157157
result.Rows.Should().HaveCount(2);
158158

159159
result.Rows[0]["name"].Should().Be("SqlServerDatabaseInitializerTest_Initialize_WithSpecificDataFileName");
160-
result.Rows[0]["physical_name"].Should().Be(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName.mdf"));
160+
result.Rows[0]["physical_name"].Should().Be(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName.mdf"));
161161
result.Rows[0]["type_desc"].Should().Be("ROWS");
162162

163163
result.Rows[1]["name"].Should().Be("TheSpecificDataFileName_log");
164-
result.Rows[1]["physical_name"].Should().Be(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName_log.ldf"));
164+
result.Rows[1]["physical_name"].Should().Be(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName_log.ldf"));
165165
result.Rows[1]["type_desc"].Should().Be("LOG");
166166

167167
// Delete the database (for deleting the temporary folder).

tests/Testing.Databases.SqlServer.Dac.Tests/Testing.Databases.SqlServer.Dac.Tests.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
<TargetFramework>net8.0</TargetFramework>
55
</PropertyGroup>
66

7-
<ItemGroup>
8-
<Compile Include="..\Testing.Databases.SqlServer.Tests\TemporaryFolder.cs" Link="TemporaryFolder.cs" />
9-
</ItemGroup>
10-
117
<ItemGroup>
128
<Content Include="..\Testing.Databases.SqlServer.Tests.DacPac\bin\$(Configuration)\netstandard2.1\PosInformatique.Testing.Databases.SqlServer.Tests.DacPac.dacpac" Link="PosInformatique.Testing.Databases.SqlServer.Tests.DacPac.dacpac">
139
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="OtherDatabasePath.cs" company="P.O.S Informatique">
3+
// Copyright (c) P.O.S Informatique. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace PosInformatique.Testing.Databases.SqlServer.Tests
8+
{
9+
public sealed class OtherDatabasePath : IDisposable
10+
{
11+
private readonly bool deleteOnDispose;
12+
13+
private OtherDatabasePath(string path, bool deleteOnDispose)
14+
{
15+
this.Path = path;
16+
this.deleteOnDispose = deleteOnDispose;
17+
}
18+
19+
public string Path { get; }
20+
21+
public static OtherDatabasePath Create()
22+
{
23+
var otherDataPath = Environment.GetEnvironmentVariable("SQL_SERVER_UNIT_TESTS_OTHER_DATA_PATH");
24+
25+
var deleteOnDispose = false;
26+
27+
if (otherDataPath is null)
28+
{
29+
otherDataPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "PosInformatique.Testing.Databases.SqlServer.Tests", Guid.NewGuid().ToString());
30+
31+
Directory.CreateDirectory(otherDataPath);
32+
33+
deleteOnDispose = true;
34+
}
35+
36+
return new OtherDatabasePath(otherDataPath, deleteOnDispose);
37+
}
38+
39+
public void Dispose()
40+
{
41+
if (this.deleteOnDispose)
42+
{
43+
try
44+
{
45+
Directory.Delete(this.Path, true);
46+
}
47+
catch (IOException)
48+
{
49+
// Ignore the errors.
50+
}
51+
}
52+
}
53+
}
54+
}

tests/Testing.Databases.SqlServer.Shared.Tests/Testing.Databases.SqlServer.Shared.Tests.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)ConnectionStrings.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)OtherDatabasePath.cs" />
1314
</ItemGroup>
1415
</Project>

tests/Testing.Databases.SqlServer.Tests/Comparer/SqlObjectPropertyDifferenceTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public void ToString_NotNull()
1313
{
1414
var difference = new SqlObjectPropertyDifference("The name", 12, 34);
1515

16-
difference.ToString().Should().Be("* The name:\r\n Source: 12\r\n Target: 34\r\n");
16+
difference.ToString().Should().Be($"* The name:{Environment.NewLine} Source: 12{Environment.NewLine} Target: 34{Environment.NewLine}");
1717
}
1818

1919
[Fact]
2020
public void ToString_Null()
2121
{
2222
var difference = new SqlObjectPropertyDifference("The name", null, null);
2323

24-
difference.ToString().Should().Be("* The name:\r\n Source: <No value>\r\n Target: <No value>\r\n");
24+
difference.ToString().Should().Be($"* The name:{Environment.NewLine} Source: <No value>{Environment.NewLine} Target: <No value>{Environment.NewLine}");
2525
}
2626
}
2727
}

tests/Testing.Databases.SqlServer.Tests/SqlServerTest.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public async Task CreateAndDelete(bool withCreationSettings)
5353
[Fact]
5454
public async Task CreateAndDelete_WithSpecificDataFileName()
5555
{
56-
using var temporaryFolder = TemporaryFolder.Create();
56+
using var otherDataPath = OtherDatabasePath.Create();
5757

5858
var server = new SqlServer(ConnectionString);
5959

6060
var settings = new SqlDatabaseCreationSettings()
6161
{
62-
DataFileName = Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName.mdf"),
62+
DataFileName = Path.Combine(otherDataPath.Path, "TheSpecificDataFileName.mdf"),
6363
};
6464

6565
var database = server.CreateEmptyDatabase("CreateAndDeleteDB_WithSpecificDataFileName", settings);
@@ -70,19 +70,19 @@ public async Task CreateAndDelete_WithSpecificDataFileName()
7070
table.Rows.Should().HaveCount(1);
7171

7272
// Check the location of the database
73-
File.Exists(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName.mdf")).Should().BeTrue();
74-
File.Exists(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName_log.ldf")).Should().BeTrue();
73+
File.Exists(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName.mdf")).Should().BeTrue();
74+
File.Exists(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName_log.ldf")).Should().BeTrue();
7575

7676
var result = database.ExecuteQuery("SELECT * FROM [sys].[database_files] ORDER BY [physical_name]");
7777

7878
result.Rows.Should().HaveCount(2);
7979

8080
result.Rows[0]["name"].Should().Be("CreateAndDeleteDB_WithSpecificDataFileName");
81-
result.Rows[0]["physical_name"].Should().Be(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName.mdf"));
81+
result.Rows[0]["physical_name"].Should().Be(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName.mdf"));
8282
result.Rows[0]["type_desc"].Should().Be("ROWS");
8383

8484
result.Rows[1]["name"].Should().Be("TheSpecificDataFileName_log");
85-
result.Rows[1]["physical_name"].Should().Be(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileName_log.ldf"));
85+
result.Rows[1]["physical_name"].Should().Be(Path.Combine(otherDataPath.Path, "TheSpecificDataFileName_log.ldf"));
8686
result.Rows[1]["type_desc"].Should().Be("LOG");
8787

8888
// Delete the database
@@ -114,13 +114,13 @@ public async Task CreateAndDeleteAsync()
114114
[Fact]
115115
public async Task CreateAndDeleteAsync_WithSpecificDataFileName()
116116
{
117-
using var temporaryFolder = TemporaryFolder.Create();
117+
using var otherDataPath = OtherDatabasePath.Create();
118118

119119
var server = new SqlServer(ConnectionString);
120120

121121
var settings = new SqlDatabaseCreationSettings()
122122
{
123-
DataFileName = Path.Combine(temporaryFolder.Path, "TheSpecificDataFileNameAsync.mdf"),
123+
DataFileName = Path.Combine(otherDataPath.Path, "TheSpecificDataFileNameAsync.mdf"),
124124
};
125125

126126
var database = await server.CreateEmptyDatabaseAsync("CreateAndDeleteDB_WithSpecificDataFileNameAsync", settings);
@@ -131,19 +131,19 @@ public async Task CreateAndDeleteAsync_WithSpecificDataFileName()
131131
table.Rows.Should().HaveCount(1);
132132

133133
// Check the location of the database
134-
File.Exists(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileNameAsync.mdf")).Should().BeTrue();
135-
File.Exists(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileNameAsync_log.ldf")).Should().BeTrue();
134+
File.Exists(Path.Combine(otherDataPath.Path, "TheSpecificDataFileNameAsync.mdf")).Should().BeTrue();
135+
File.Exists(Path.Combine(otherDataPath.Path, "TheSpecificDataFileNameAsync_log.ldf")).Should().BeTrue();
136136

137137
var result = database.ExecuteQuery("SELECT * FROM [sys].[database_files] ORDER BY [physical_name]");
138138

139139
result.Rows.Should().HaveCount(2);
140140

141141
result.Rows[0]["name"].Should().Be("CreateAndDeleteDB_WithSpecificDataFileNameAsync");
142-
result.Rows[0]["physical_name"].Should().Be(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileNameAsync.mdf"));
142+
result.Rows[0]["physical_name"].Should().Be(Path.Combine(otherDataPath.Path, "TheSpecificDataFileNameAsync.mdf"));
143143
result.Rows[0]["type_desc"].Should().Be("ROWS");
144144

145145
result.Rows[1]["name"].Should().Be("TheSpecificDataFileNameAsync_log");
146-
result.Rows[1]["physical_name"].Should().Be(Path.Combine(temporaryFolder.Path, "TheSpecificDataFileNameAsync_log.ldf"));
146+
result.Rows[1]["physical_name"].Should().Be(Path.Combine(otherDataPath.Path, "TheSpecificDataFileNameAsync_log.ldf"));
147147
result.Rows[1]["type_desc"].Should().Be("LOG");
148148

149149
// Delete the database

tests/Testing.Databases.SqlServer.Tests/TemporaryFolder.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)