This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses NuGet/NuGetGallery#4934 Similar to NuGet/NuGetGallery#7143
- Loading branch information
1 parent
d6de585
commit aeaeb66
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
tests/NuGet.Services.Validation.Tests/PendingMigrationsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Data.Entity.Infrastructure; | ||
using System.Data.Entity.Migrations; | ||
using System.Data.Entity.Migrations.Design; | ||
using System.Data.SqlClient; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace NuGet.Services.Validation.Tests | ||
{ | ||
public class PendingMigrationsFacts : IAsyncLifetime | ||
{ | ||
private string _dbName; | ||
private readonly ITestOutputHelper _output; | ||
|
||
public PendingMigrationsFacts(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
public Task InitializeAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
if (_dbName == null) | ||
{ | ||
return; | ||
} | ||
|
||
const string connectionString = @"Data Source=(localdb)\mssqllocaldb; Initial Catalog=master; Integrated Security=True; MultipleActiveResultSets=True"; | ||
using (var sqlConnection = new SqlConnection(connectionString)) | ||
{ | ||
await sqlConnection.OpenAsync(); | ||
using (var sqlCommand = sqlConnection.CreateCommand()) | ||
{ | ||
sqlCommand.CommandText = $"ALTER DATABASE {_dbName} SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE {_dbName};"; | ||
await sqlCommand.ExecuteNonQueryAsync(); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void NoPendingMigrations() | ||
{ | ||
var currentTimestamp = DateTimeOffset.UtcNow.ToString("yyyyMMddHHmmssFFFFFFF"); | ||
_dbName = $"PendingMigrationsTest{currentTimestamp}Validation"; | ||
var connectionString = $@"Data Source=(localdb)\mssqllocaldb; Initial Catalog={_dbName}; Integrated Security=True; MultipleActiveResultSets=True"; | ||
|
||
var migrationsConfiguration = new ValidationMigrationsConfiguration | ||
{ | ||
TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient"), | ||
}; | ||
|
||
var dbMigrator = new DbMigrator(migrationsConfiguration); | ||
var migrations = dbMigrator.GetLocalMigrations(); | ||
dbMigrator.Update(migrations.Last()); | ||
|
||
var migrationScaffolder = new MigrationScaffolder(dbMigrator.Configuration); | ||
|
||
var migrationName = $"TestMigration{currentTimestamp}"; | ||
var result = migrationScaffolder.Scaffold(migrationName); | ||
|
||
_output.WriteLine("Migration content:"); | ||
_output.WriteLine(new string('-', 60)); | ||
_output.WriteLine(result.UserCode); | ||
_output.WriteLine(new string('-', 60)); | ||
|
||
Assert.Equal( | ||
$@"namespace {dbMigrator.Configuration.MigrationsNamespace} | ||
{{ | ||
using System; | ||
using System.Data.Entity.Migrations; | ||
public partial class {migrationName} : DbMigration | ||
{{ | ||
public override void Up() | ||
{{ | ||
}} | ||
public override void Down() | ||
{{ | ||
}} | ||
}} | ||
}} | ||
", result.UserCode); | ||
} | ||
} | ||
} |