Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
/ ServerCommon Public archive

Commit

Permalink
Add pending migration test (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed May 8, 2019
1 parent d6de585 commit aeaeb66
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<Compile Include="Entities\PackageSignatureTypeTests.cs" />
<Compile Include="Entities\EndCertificateUseTests.cs" />
<Compile Include="Entities\SymbolServerRequestTests.cs" />
<Compile Include="PendingMigrationsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServiceBusMessageSerializerTests.cs" />
<Compile Include="TestData.Designer.cs">
Expand Down
96 changes: 96 additions & 0 deletions tests/NuGet.Services.Validation.Tests/PendingMigrationsTests.cs
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);
}
}
}

0 comments on commit aeaeb66

Please sign in to comment.