Skip to content

Commit db22bca

Browse files
committed
Use mock IDbContextTransaction instead of concrete transaction object (#6494)
Fix https://github.com/NuGet/Engineering/issues/1745
1 parent 39a1f87 commit db22bca

10 files changed

Lines changed: 70 additions & 27 deletions

src/NuGetGallery.Core/Entities/DatabaseWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public Task<int> ExecuteSqlCommandAsync(string sql, params object[] parameters)
2323
return _database.ExecuteSqlCommandAsync(sql, parameters);
2424
}
2525

26-
public DbContextTransaction BeginTransaction()
26+
public IDbContextTransaction BeginTransaction()
2727
{
28-
return _database.BeginTransaction();
28+
return new DbContextTransactionWrapper(_database.BeginTransaction());
2929
}
3030

3131
/// <summary>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Data.Entity;
6+
7+
namespace NuGetGallery
8+
{
9+
public class DbContextTransactionWrapper : IDbContextTransaction
10+
{
11+
private readonly DbContextTransaction _transaction;
12+
13+
public DbContextTransactionWrapper(DbContextTransaction transaction)
14+
{
15+
_transaction = transaction ?? throw new ArgumentNullException(nameof(transaction));
16+
}
17+
18+
public void Commit()
19+
{
20+
_transaction.Commit();
21+
}
22+
23+
public void Dispose()
24+
{
25+
_transaction.Dispose();
26+
}
27+
}
28+
}

src/NuGetGallery.Core/Entities/IDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace NuGetGallery
88
{
99
public interface IDatabase
1010
{
11-
DbContextTransaction BeginTransaction();
11+
IDbContextTransaction BeginTransaction();
1212

1313
Task<int> ExecuteSqlCommandAsync(string sql, params object[] parameters);
1414

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace NuGetGallery
7+
{
8+
public interface IDbContextTransaction : IDisposable
9+
{
10+
void Commit();
11+
}
12+
}

src/NuGetGallery.Core/NuGetGallery.Core.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@
118118
<Compile Include="Entities\CuratedFeed.cs" />
119119
<Compile Include="Entities\CuratedPackage.cs" />
120120
<Compile Include="Entities\DatabaseWrapper.cs" />
121+
<Compile Include="Entities\DbContextTransactionWrapper.cs" />
121122
<Compile Include="Entities\IDatabase.cs" />
123+
<Compile Include="Entities\IDbContextTransaction.cs" />
122124
<Compile Include="Entities\Membership.cs" />
123125
<Compile Include="Entities\Organization.cs" />
124126
<Compile Include="Entities\AccountDelete.cs" />

tests/NuGetGallery.Facts/Services/DeleteAccountServiceFacts.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Data.Entity;
76
using System.Linq;
87
using System.Threading.Tasks;
8+
using Moq;
99
using NuGetGallery.Areas.Admin;
1010
using NuGetGallery.Areas.Admin.Models;
1111
using NuGetGallery.Auditing;
1212
using NuGetGallery.Authentication;
1313
using NuGetGallery.Security;
1414
using Xunit;
15-
using Moq;
1615

1716
namespace NuGetGallery.Services
1817
{
@@ -466,8 +465,9 @@ public User User
466465
private Mock<IEntitiesContext> SetupEntitiesContext()
467466
{
468467
var mockContext = new Mock<IEntitiesContext>();
469-
var dbContext = new Mock<DbContext>();
470-
mockContext.Setup(m => m.GetDatabase()).Returns(new DatabaseWrapper(dbContext.Object.Database));
468+
var database = new Mock<IDatabase>();
469+
database.Setup(x => x.BeginTransaction()).Returns(() => new Mock<IDbContextTransaction>().Object);
470+
mockContext.Setup(m => m.GetDatabase()).Returns(database.Object);
471471
return mockContext;
472472
}
473473

tests/NuGetGallery.Facts/Services/PackageDeleteServiceFacts.cs

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

44
using System;
55
using System.Collections.Generic;
6-
using System.Data.Entity;
76
using System.Data.SqlClient;
87
using System.IO;
98
using System.Linq;
@@ -41,9 +40,10 @@ private static IPackageDeleteService CreateService(
4140
packageRegistrationRepository = packageRegistrationRepository ?? new Mock<IEntityRepository<PackageRegistration>>();
4241
packageDeletesRepository = packageDeletesRepository ?? new Mock<IEntityRepository<PackageDelete>>();
4342

44-
var dbContext = new Mock<DbContext>();
4543
entitiesContext = entitiesContext ?? new Mock<IEntitiesContext>();
46-
entitiesContext.Setup(m => m.GetDatabase()).Returns(new DatabaseWrapper(dbContext.Object.Database));
44+
var database = new Mock<IDatabase>();
45+
database.Setup(x => x.BeginTransaction()).Returns(() => new Mock<IDbContextTransaction>().Object);
46+
entitiesContext.Setup(m => m.GetDatabase()).Returns(database.Object);
4747

4848
packageService = packageService ?? new Mock<IPackageService>();
4949
indexingService = indexingService ?? new Mock<IIndexingService>();

tests/NuGetGallery.Facts/Services/PackageOwnershipManagementServiceFacts.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using Moq;
5-
using Xunit;
64
using System;
5+
using System.Collections.Generic;
76
using System.Linq;
8-
using System.Data.Entity;
97
using System.Threading.Tasks;
10-
using System.Collections.Generic;
8+
using Moq;
119
using NuGetGallery.Auditing;
1210
using NuGetGallery.Framework;
1311
using NuGetGallery.TestUtils;
12+
using Xunit;
1413

1514
namespace NuGetGallery
1615
{
@@ -24,9 +23,10 @@ private static PackageOwnershipManagementService CreateService(
2423
IAuditingService auditingService = null,
2524
bool useDefaultSetup = true)
2625
{
27-
var dbContext = new Mock<DbContext>();
2826
entitiesContext = entitiesContext ?? new Mock<IEntitiesContext>();
29-
entitiesContext.Setup(m => m.GetDatabase()).Returns(new DatabaseWrapper(dbContext.Object.Database));
27+
var database = new Mock<IDatabase>();
28+
database.Setup(x => x.BeginTransaction()).Returns(() => new Mock<IDbContextTransaction>().Object);
29+
entitiesContext.Setup(m => m.GetDatabase()).Returns(database.Object);
3030
packageService = packageService ?? new Mock<IPackageService>();
3131
reservedNamespaceService = reservedNamespaceService ?? new Mock<IReservedNamespaceService>();
3232
packageOwnerRequestService = packageOwnerRequestService ?? new Mock<IPackageOwnerRequestService>();

tests/NuGetGallery.Facts/Services/ReflowPackageServiceFacts.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
6-
using System.Data.Entity;
75
using System.IO;
86
using System.IO.Compression;
9-
using System.Linq;
107
using System.Threading.Tasks;
118
using Moq;
129
using NuGet.Packaging;
1310
using NuGetGallery.Framework;
1411
using NuGetGallery.Packaging;
15-
using Xunit;
16-
using NuGetGallery.TestUtils;
1712
using NuGetGallery.Security;
13+
using NuGetGallery.TestUtils;
14+
using Xunit;
1815

1916
namespace NuGetGallery
2017
{
@@ -27,9 +24,10 @@ private static ReflowPackageService CreateService(
2724
Mock<ITelemetryService> telemetryService = null,
2825
Action<Mock<ReflowPackageService>> setup = null)
2926
{
30-
var dbContext = new Mock<DbContext>();
3127
entitiesContext = entitiesContext ?? new Mock<IEntitiesContext>();
32-
entitiesContext.Setup(m => m.GetDatabase()).Returns(new DatabaseWrapper(dbContext.Object.Database));
28+
var database = new Mock<IDatabase>();
29+
database.Setup(x => x.BeginTransaction()).Returns(() => new Mock<IDbContextTransaction>().Object);
30+
entitiesContext.Setup(m => m.GetDatabase()).Returns(database.Object);
3331

3432
packageService = packageService ?? new Mock<PackageService>();
3533
packageFileService = packageFileService ?? new Mock<IPackageFileService>();

tests/NuGetGallery.Facts/TestUtils/TestServiceUtility.cs

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

44
using System;
55
using System.Collections.Generic;
6-
using System.Data.Entity;
76
using System.IO;
87
using System.Linq;
98
using System.Linq.Expressions;
@@ -290,9 +289,13 @@ private Mock<IEntityRepository<ReservedNamespace>> SetupReservedNamespaceReposit
290289
private Mock<IEntitiesContext> SetupEntitiesContext()
291290
{
292291
var mockContext = new Mock<IEntitiesContext>();
293-
var dbContext = new Mock<DbContext>();
294-
mockContext.Setup(m => m.GetDatabase())
295-
.Returns(new DatabaseWrapper(dbContext.Object.Database));
292+
var database = new Mock<IDatabase>();
293+
database
294+
.Setup(x => x.BeginTransaction())
295+
.Returns(() => new Mock<IDbContextTransaction>().Object);
296+
mockContext
297+
.Setup(m => m.GetDatabase())
298+
.Returns(database.Object);
296299

297300
return mockContext;
298301
}

0 commit comments

Comments
 (0)