Skip to content

Commit 48d0c82

Browse files
committed
Adding PostgreSql database helpers
1 parent b1f25fa commit 48d0c82

File tree

9 files changed

+455
-19
lines changed

9 files changed

+455
-19
lines changed

ReleaseNotes.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Release notes
22

3-
## 6.0.0-preview001
3+
## 6.0.0-priview001
44

5-
- Updated to Net6 preview
6-
- Added the Seed Database feature at request of users
5+
- Updated to Net6-rc.2 preview
6+
- Added PostgreSQL database helpers
7+
- Added the Seed Database feature back in at request of users
78

89
## 5.0.0
910

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
22
// Licensed under MIT license. See License.txt in the project root for license information.
33

4+
using Test.Helpers;
45
using TestSupport.Attributes;
56
using TestSupport.EfHelpers;
67
using Xunit.Abstractions;
@@ -16,22 +17,26 @@ public DeleteAllUnitTestDatabases(ITestOutputHelper output)
1617
_output = output;
1718
}
1819

19-
//Run this method to wipe ALL the test databases using your appsetting.json connection string
20+
//Run this method to wipe ALL the SQL Server test databases using your appsetting.json connection string
2021
//You need to run it in debug mode - that stops it being run when you "run all" unit tests
2122
[RunnableInDebugOnly] //#A
22-
public void DeleteAllTestDatabasesOk() //#B
23+
public void DeleteAllSqlServerTestDatabasesOk() //#B
2324
{
2425
var numDeleted = DatabaseTidyHelper //#C
2526
.DeleteAllUnitTestDatabases();//#C
2627
_output.WriteLine( //#D
27-
"This deleted {0} databases.", numDeleted); //#D
28+
"This deleted {0} SQL Server databases.", numDeleted); //#D
2829
}
2930

30-
/****************************************************************
31-
#A The [RunnableInDebugOnly] makes sure the unit command is not run by accident when the main unit tests are run. I must manually run this method in debug mode
32-
#B This has the format of a unit test, that is a public method which returns void
33-
#C I call the DeleteAllUnitTestBranchDatabases method from my EcCore.TestSupport library. This returns the number of databases that it deleted
34-
#D I then write out how many database were deleted by this method
35-
* ****************************************************************/
31+
//Run this method to wipe ALL the PostgreSql test databases using your appsetting.json connection string
32+
//You need to run it in debug mode - that stops it being run when you "run all" unit tests
33+
[RunnableInDebugOnly] //#A
34+
public void DeleteAllPostgreSqlTestDatabasesOk()
35+
{
36+
var numDeleted = PostgreSqlHelpers
37+
.DeleteAllPostgreSqlUnitTestDatabases();
38+
_output.WriteLine(
39+
"This deleted {0} PostgreSql databases.", numDeleted);
40+
}
3641
}
3742
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
2+
// Licensed under MIT license. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using DataLayer.BookApp.EfCode;
8+
using Microsoft.EntityFrameworkCore;
9+
using Npgsql;
10+
using Test.Helpers;
11+
using TestSupport.Attributes;
12+
using TestSupport.EfHelpers;
13+
using Xunit;
14+
using Xunit.Abstractions;
15+
using Xunit.Extensions.AssertExtensions;
16+
17+
namespace Test.UnitTests.TestDataLayer
18+
{
19+
public class TestPostgreSqlHelpers
20+
{
21+
private readonly ITestOutputHelper _output;
22+
23+
public TestPostgreSqlHelpers(ITestOutputHelper output)
24+
{
25+
_output = output;
26+
}
27+
28+
[Fact]
29+
public void TestPostgreSqlUniqueClassOk()
30+
{
31+
//SETUP
32+
//ATTEMPT
33+
var options = this.CreatePostgreSqlUniqueDatabaseOptions<BookContext>();
34+
using (var context = new BookContext(options))
35+
{
36+
//VERIFY
37+
var builder = new NpgsqlConnectionStringBuilder(context.Database.GetDbConnection().ConnectionString);
38+
builder.Database.ShouldEndWith(GetType().Name);
39+
}
40+
}
41+
42+
[Fact]
43+
public void TestPostgreSqUniqueMethodOk()
44+
{
45+
//SETUP
46+
//ATTEMPT
47+
var options = this.CreatePostgreSqlUniqueMethodOptions<BookContext>();
48+
using (var context = new BookContext(options))
49+
{
50+
51+
//VERIFY
52+
var builder = new NpgsqlConnectionStringBuilder(context.Database.GetDbConnection().ConnectionString);
53+
builder.Database
54+
.ShouldEndWith($"{GetType().Name}_{nameof(TestPostgreSqUniqueMethodOk)}" );
55+
}
56+
}
57+
58+
[Fact]
59+
public void TestEnsureDeletedEnsureCreatedOk()
60+
{
61+
//SETUP
62+
var options = this.CreatePostgreSqlUniqueDatabaseOptions<BookContext>();
63+
using var context = new BookContext(options);
64+
65+
context.Database.EnsureCreated();
66+
67+
using (new TimeThings(_output, "Time to EnsureDeleted and EnsureCreated"))
68+
{
69+
context.Database.EnsureDeleted();
70+
context.Database.EnsureCreated();
71+
}
72+
73+
//ATTEMPT
74+
context.SeedDatabaseFourBooks();
75+
76+
//VERIFY
77+
context.Books.Count().ShouldEqual(4);
78+
}
79+
80+
[Fact]
81+
public async Task TestEnsureCreatedAndEmptyPostgreSqlOk()
82+
{
83+
//SETUP
84+
var options = this.CreatePostgreSqlUniqueDatabaseOptions<BookContext>();
85+
using (var context = new BookContext(options))
86+
{
87+
context.Database.EnsureCreated();
88+
context.SeedDatabaseFourBooks();
89+
}
90+
using (var context = new BookContext(options))
91+
{
92+
//ATTEMPT
93+
using (new TimeThings(_output, "Time to empty database"))
94+
{
95+
await context.EnsureCreatedAndEmptyPostgreSqlAsync();
96+
}
97+
98+
//VERIFY
99+
context.Books.Count().ShouldEqual(0);
100+
}
101+
}
102+
103+
[RunnableInDebugOnly]
104+
public void TestCreatePostgreSqlUniqueClassOptionsWithLogToOk()
105+
{
106+
//SETUP
107+
var logs = new List<string>();
108+
var options = this.CreatePostgreSqlUniqueClassOptionsWithLogTo<BookContext>(log => logs.Add(log));
109+
using (var context = new BookContext(options))
110+
{
111+
//ATTEMPT
112+
context.Database.EnsureDeleted();
113+
context.Database.EnsureCreated();
114+
115+
//VERIFY
116+
foreach (var log in logs)
117+
{
118+
_output.WriteLine(log);
119+
}
120+
}
121+
}
122+
123+
[Fact]
124+
public void TestAddExtraBuilderOptions()
125+
{
126+
//SETUP
127+
var options1 = this.CreatePostgreSqlUniqueDatabaseOptions<BookContext>();
128+
using (var context = new BookContext(options1))
129+
{
130+
context.Database.EnsureCreated();
131+
context.SeedDatabaseDummyBooks(100);
132+
133+
var book = context.Books.First();
134+
context.Entry(book).State.ShouldEqual(EntityState.Unchanged);
135+
}
136+
//ATTEMPT
137+
var options2 = this.CreatePostgreSqlUniqueDatabaseOptions<BookContext>(
138+
builder => builder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));
139+
using (var context = new BookContext(options2))
140+
{
141+
//VERIFY
142+
var book = context.Books.First();
143+
context.Entry(book).State.ShouldEqual(EntityState.Detached);
144+
}
145+
}
146+
}
147+
}

Test/UnitTests/TestSupport/TestAppSettings.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.Data.SqlClient;
55
using Microsoft.Extensions.Configuration;
6+
using Npgsql;
67
using TestSupport.Helpers;
78
using Xunit;
89
using Xunit.Extensions.AssertExtensions;
@@ -108,6 +109,53 @@ public void GetTestConnectionStringWithExtraMethodNameOk()
108109
newDatabaseName.ShouldEqual($"{orgDbName}_{typeof(TestAppSettings).Name}_ExtraMethodName");
109110
}
110111

112+
[Fact]
113+
public void GetUniquePostgreSqlConnectionStringOk()
114+
{
115+
//SETUP
116+
var config = AppSettings.GetConfiguration();
117+
var orgDbName = new NpgsqlConnectionStringBuilder(config.GetConnectionString(AppSettings.PostgreSqlConnectionString)).Database;
118+
119+
//ATTEMPT
120+
var con = this.GetUniquePostgreSqlConnectionString();
121+
122+
//VERIFY
123+
var newDatabaseName = new NpgsqlConnectionStringBuilder(con).Database;
124+
newDatabaseName.ShouldEqual($"{orgDbName}_{GetType().Name}");
125+
}
126+
127+
[Fact]
128+
public void GetUniquePostgreSqlConnectionStringDifferentSeperatorOk()
129+
{
130+
//SETUP
131+
var config = AppSettings.GetConfiguration();
132+
var orgDbName = new NpgsqlConnectionStringBuilder(config.GetConnectionString(AppSettings.PostgreSqlConnectionString)).Database;
133+
134+
//ATTEMPT
135+
var con = this.GetUniquePostgreSqlConnectionString(null, '.');
136+
137+
//VERIFY
138+
var newDatabaseName = new NpgsqlConnectionStringBuilder(con).Database;
139+
newDatabaseName.ShouldEqual($"{orgDbName}.{GetType().Name}");
140+
}
141+
142+
[Fact]
143+
public void GetUniquePostgreSqlConnectionStringWithExtraMethodNameOk()
144+
{
145+
//SETUP
146+
var config = AppSettings.GetConfiguration();
147+
var orgDbName = new NpgsqlConnectionStringBuilder(config.GetConnectionString(AppSettings.PostgreSqlConnectionString)).Database;
148+
149+
//ATTEMPT
150+
var con = this.GetUniquePostgreSqlConnectionString("ExtraMethodName");
151+
152+
//VERIFY
153+
var newDatabaseName = new NpgsqlConnectionStringBuilder(con).Database;
154+
newDatabaseName.ShouldEqual($"{orgDbName}_{typeof(TestAppSettings).Name}_ExtraMethodName");
155+
}
156+
157+
158+
111159
[Fact]
112160
public void GetMyIntOk()
113161
{

Test/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"ConnectionStrings": {
33
"UnitTestConnection": "Server=(localdb)\\mssqllocaldb;Database=EfCore.TestSupport-Test;Trusted_Connection=True;MultipleActiveResultSets=true",
4+
"PostgreSqlConnection": "host=localhost;Database=TestSupport-Test;Username=postgres;Password=LetMeIn",
45
"BookOrderConnection": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=EfCore.TestSupport-Test_ComparerBooksAndOrders;Integrated Security=True;MultipleActiveResultSets=True"
56
},
67
"MyInt": 1,

0 commit comments

Comments
 (0)