Skip to content

Commit

Permalink
Remove the concept of AsAdministrator().
Browse files Browse the repository at this point in the history
  • Loading branch information
GillesTourreau committed Aug 20, 2024
1 parent 7efef4e commit 8f6ff02
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 221 deletions.
3 changes: 0 additions & 3 deletions src/UnitTests.Databases.SqlServer/SqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public SqlServer(string connectionString)

var connectionStringMaster = new SqlConnectionStringBuilder(connectionString);
connectionStringMaster.InitialCatalog = "master";
connectionStringMaster.IntegratedSecurity = true;
connectionStringMaster.Remove("User ID");
connectionStringMaster.Remove("Password");

this.Master = new SqlServerDatabase(this, connectionStringMaster.ToString());
}
Expand Down
10 changes: 0 additions & 10 deletions src/UnitTests.Databases.SqlServer/SqlServerDatabaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ Type t when Array.Exists(AuthorizedNonStringTypes, at => at == t) => builder.Add
return database.ExecuteNonQuery(statement);
}

public static SqlServerDatabase AsAdministrator(this SqlServerDatabase database)
{
var databaseConnectionString = new SqlConnectionStringBuilder(database.ConnectionString);

var masterConnectionString = new SqlConnectionStringBuilder(database.Server.Master.ConnectionString);
masterConnectionString.InitialCatalog = databaseConnectionString.InitialCatalog;

return new SqlServerDatabase(database.Server, masterConnectionString.ToString());
}

private sealed class SqlInsertStatementBuilder
{
private readonly string tableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ IF SUSER_ID ('{connectionStringBuilder.UserID}') IS NULL
database = server.GetDatabase(connectionStringBuilder.InitialCatalog);
}

ClearAllData(database.AsAdministrator());
ClearAllData(database);

return database;
}
Expand All @@ -58,10 +58,7 @@ public SqlServerDatabase Initialize<TContext>(TContext context)
{
server.DeleteDatabase(connectionStringBuilder.InitialCatalog);

// Change the connection with administrator rights.
context.Database.SetConnectionString(database.AsAdministrator().ConnectionString);
context.Database.EnsureCreated();
context.Database.SetConnectionString(connectionString);

if (!string.IsNullOrWhiteSpace(connectionStringBuilder.UserID))
{
Expand All @@ -73,7 +70,7 @@ IF SUSER_ID ('{connectionStringBuilder.UserID}') IS NULL
this.isDeployed = true;
}

ClearAllData(database.AsAdministrator());
ClearAllData(database);

return database;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,14 @@ public class SqlServerDatabaseExtensionsTest
{
private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseExtensionsTest)}; Integrated Security=True";

[Fact]
public void AsAdministrator()
{
var server = new SqlServer(ConnectionString);

var database = server.GetDatabase("TheDatabase");

database.AsAdministrator().ConnectionString.Should().Be("Data Source=(localDB)\\posinfo-unit-tests;Initial Catalog=TheDatabase;Integrated Security=True");
}

[Fact]
public void InsertInto_EnableIdentity()
{
var server = new SqlServer(ConnectionString);

var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest");

database.AsAdministrator().ExecuteNonQuery(@"
database.ExecuteNonQuery(@"
CREATE TABLE TableTest
(
Id INT NOT NULL IDENTITY(1, 1),
Expand Down Expand Up @@ -68,7 +58,7 @@ public void InsertInto_DisableIdentity()

var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest");

database.AsAdministrator().ExecuteNonQuery(@"
database.ExecuteNonQuery(@"
CREATE TABLE TableTest
(
Id INT NOT NULL IDENTITY(1, 1),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//-----------------------------------------------------------------------
// <copyright file="SqlServerDatabaseInitializerDacPacWithoutLogin.cs" company="P.O.S Informatique">
// <copyright file="SqlServerDatabaseInitializerDacPac.cs" company="P.O.S Informatique">
// Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace PosInformatique.UnitTests.Databases.SqlServer.Tests
{
[Collection("PosInformatique.UnitTests.Databases.SqlServer.Tests")]
public class SqlServerDatabaseInitializerDacPacWithoutLogin : IClassFixture<SqlServerDatabaseInitializer>
public class SqlServerDatabaseInitializerDacPac : IClassFixture<SqlServerDatabaseInitializer>
{
private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDacPacWithoutLogin)}; Integrated Security=True";
private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDacPac)}; Integrated Security=True";

private readonly SqlServerDatabase database;

public SqlServerDatabaseInitializerDacPacWithoutLogin(SqlServerDatabaseInitializer initializer)
public SqlServerDatabaseInitializerDacPac(SqlServerDatabaseInitializer initializer)
{
this.database = initializer.Initialize("UnitTests.Databases.SqlServer.Tests.DacPac.dacpac", ConnectionString);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
// <copyright file="SqlServerDatabaseInitializerDbContextWithoutLogin.cs" company="P.O.S Informatique">
// <copyright file="SqlServerDatabaseInitializerDbContext.cs" company="P.O.S Informatique">
// Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
Expand All @@ -9,13 +9,13 @@ namespace PosInformatique.UnitTests.Databases.SqlServer.Tests
using Microsoft.EntityFrameworkCore;

[Collection("PosInformatique.UnitTests.Databases.SqlServer.Tests")]
public class SqlServerDatabaseInitializerDbContextWithoutLogin : IClassFixture<SqlServerDatabaseInitializer>
public class SqlServerDatabaseInitializerDbContext : IClassFixture<SqlServerDatabaseInitializer>
{
private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDbContextWithoutLogin)}; Integrated Security=True";
private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDbContext)}; Integrated Security=True";

private readonly SqlServerDatabase database;

public SqlServerDatabaseInitializerDbContextWithoutLogin(SqlServerDatabaseInitializer initializer)
public SqlServerDatabaseInitializerDbContext(SqlServerDatabaseInitializer initializer)
{
var optionsBuilder = new DbContextOptionsBuilder<DbContextTest>()
.UseSqlServer(ConnectionString);
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion tests/UnitTests.Databases.SqlServer.Tests/SqlServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PosInformatique.UnitTests.Databases.SqlServer.Tests
public class SqlServerTest
{
[Theory]
[InlineData("Data Source=TheServer; Initial Catalog=TheDB; User ID=TheID; Password=ThePassword", "Data Source=TheServer;Initial Catalog=master;Integrated Security=True")]
[InlineData("Data Source=TheServer; Initial Catalog=TheDB; User ID=TheID; Password=ThePassword", "Data Source=TheServer;Initial Catalog=master;User ID=TheID;Password=ThePassword")]
[InlineData("Data Source=TheServer; Initial Catalog=TheDB; Integrated Security=True", "Data Source=TheServer;Initial Catalog=master;Integrated Security=True")]
public void Constructor(string connectionString, string expectedMasterConnectionString)
{
Expand Down

0 comments on commit 8f6ff02

Please sign in to comment.