Standardize sql db names for tests

This commit is contained in:
Hao Kung 2014-09-09 15:26:29 -07:00
parent 00261c1984
commit 19d6805c7c
7 changed files with 76 additions and 50 deletions

View File

@ -12,7 +12,6 @@ namespace Microsoft.AspNet.Identity.SqlServer
{ {
public IdentityDbContext() { } public IdentityDbContext() { }
public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) { } public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
public IdentityDbContext(IServiceProvider serviceProvider, string nameOrConnectionString) : base(serviceProvider, nameOrConnectionString) { }
public IdentityDbContext(DbContextOptions options) : base(options) { } public IdentityDbContext(DbContextOptions options) : base(options) { }
public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { }
} }
@ -23,7 +22,6 @@ namespace Microsoft.AspNet.Identity.SqlServer
{ {
public IdentityDbContext() { } public IdentityDbContext() { }
public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) { } public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
public IdentityDbContext(IServiceProvider serviceProvider, string nameOrConnectionString) : base(serviceProvider, nameOrConnectionString) { }
public IdentityDbContext(DbContextOptions options) : base(options) { } public IdentityDbContext(DbContextOptions options) : base(options) { }
public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { }
} }
@ -40,25 +38,11 @@ namespace Microsoft.AspNet.Identity.SqlServer
public DbSet<TRole> Roles { get; set; } public DbSet<TRole> Roles { get; set; }
public DbSet<IdentityRoleClaim<TKey>> RoleClaims { get; set; } public DbSet<IdentityRoleClaim<TKey>> RoleClaims { get; set; }
private readonly string _nameOrConnectionString;
public IdentityDbContext() { } public IdentityDbContext() { }
public IdentityDbContext(IServiceProvider serviceProvider, string nameOrConnectionString) : base(serviceProvider)
{
_nameOrConnectionString = nameOrConnectionString;
}
public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) { } public IdentityDbContext(IServiceProvider serviceProvider) : base(serviceProvider) { }
public IdentityDbContext(DbContextOptions options) : base(options) { } public IdentityDbContext(DbContextOptions options) : base(options) { }
public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { } public IdentityDbContext(IServiceProvider serviceProvider, DbContextOptions options) : base(serviceProvider, options) { }
protected override void OnConfiguring(DbContextOptions builder)
{
if (!string.IsNullOrEmpty(_nameOrConnectionString))
{
builder.UseSqlServer(_nameOrConnectionString);
}
}
protected override void OnModelCreating(ModelBuilder builder) protected override void OnModelCreating(ModelBuilder builder)
{ {
builder.Entity<TUser>(b => builder.Entity<TUser>(b =>

View File

@ -1,10 +1,11 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Identity.Test;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.OptionsModel;
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -12,9 +13,10 @@ using Xunit;
namespace Microsoft.AspNet.Identity.SqlServer.Test namespace Microsoft.AspNet.Identity.SqlServer.Test
{ {
[TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")]
public class CustomPocoTest public class CustomPocoTest
{ {
private const string ConnectionString = @"Server=(localdb)\v11.0;Database=CustomPocoTest;Trusted_Connection=True;"; private readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=CustomUserContextTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;";
public class User<TKey> where TKey : IEquatable<TKey> public class User<TKey> where TKey : IEquatable<TKey>
{ {
@ -23,33 +25,27 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test
} }
public class CustomDbContext<TUser> : DbContext where TUser : class public class CustomDbContext<TUser> : DbContext where TUser : class
//where TUser : User<TKey> where TKey : IEquatable<TKey>
{ {
public DbSet<TUser> Users { get; set; } public DbSet<TUser> Users { get; set; }
public CustomDbContext(IServiceProvider services) : base(services) { } public CustomDbContext(IServiceProvider services) :
base(services, services.GetService<IOptionsAccessor<DbContextOptions>>().Options) { }
protected override void OnConfiguring(DbContextOptions builder)
{
builder.UseSqlServer(ConnectionString);
}
//protected override void OnModelCreating(ModelBuilder builder)
//{
// builder.Entity<TUser>()
// .Key(u => u.Id)
// .Properties(ps => ps.Property(u => u.UserName));
//}
} }
//public static CustomDbContext<User<TKey>, TKey> CreateContext<TKey>(bool delete = false) where TKey : IEquatable<TKey>
public static CustomDbContext<TUser> CreateContext<TUser>(bool delete = false) where TUser : class public CustomDbContext<TUser> GetContext<TUser>() where TUser : class
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.Add(OptionsServices.GetDefaultServices());
services.AddEntityFramework().AddSqlServer(); services.AddEntityFramework().AddSqlServer();
services.SetupOptions<DbContextOptions>(options => options.UseSqlServer(ConnectionString));
var serviceProvider = services.BuildServiceProvider(); var serviceProvider = services.BuildServiceProvider();
return new CustomDbContext<TUser>(serviceProvider);
}
var db = new CustomDbContext<TUser>(serviceProvider); public CustomDbContext<TUser> CreateContext<TUser>(bool delete = false) where TUser : class
{
var db = GetContext<TUser>();
if (delete) if (delete)
{ {
db.Database.EnsureDeleted(); db.Database.EnsureDeleted();
@ -58,6 +54,26 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test
return db; return db;
} }
[TestPriority(-1000)]
[Fact]
public void DropDatabaseStart()
{
DropDb();
}
[TestPriority(10000)]
[Fact]
public void DropDatabaseDone()
{
DropDb();
}
public void DropDb()
{
var db = GetContext<User<string>>();
db.Database.EnsureDeleted();
}
[Fact] [Fact]
public async Task CanUpdateNameGuid() public async Task CanUpdateNameGuid()
{ {

View File

@ -6,32 +6,44 @@ using Microsoft.AspNet.Identity.Test;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.OptionsModel;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Identity.SqlServer.Test namespace Microsoft.AspNet.Identity.SqlServer.Test
{ {
[TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")]
public class DefaultPocoTest public class DefaultPocoTest
{ {
private const string ConnectionString = @"Server=(localdb)\v11.0;Database=DefaultPocoTest;Trusted_Connection=True;"; private readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=DefaultSchemaTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;";
public static IdentityDbContext CreateContext(bool delete = false) public IdentityDbContext CreateContext(bool ensureCreated = false)
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.Add(OptionsServices.GetDefaultServices());
services.AddEntityFramework().AddSqlServer(); services.AddEntityFramework().AddSqlServer();
services.SetupOptions<DbContextOptions>(options => options.UseSqlServer(ConnectionString));
var serviceProvider = services.BuildServiceProvider(); var serviceProvider = services.BuildServiceProvider();
var db = new IdentityDbContext(serviceProvider,
var db = new IdentityDbContext(serviceProvider, ConnectionString); serviceProvider.GetService<IOptionsAccessor<DbContextOptions>>().Options);
if (delete) if (ensureCreated)
{ {
db.Database.EnsureDeleted(); db.Database.EnsureCreated();
} }
db.Database.EnsureCreated();
return db; return db;
} }
public static void EnsureDatabase() public void DropDb()
{ {
CreateContext(); var db = CreateContext();
db.Database.EnsureDeleted();
}
[TestPriority(-1000)]
[Fact]
public void DropDatabaseStart()
{
DropDb();
} }
[Fact] [Fact]
@ -62,5 +74,12 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test
IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password));
IdentityResultAssert.IsSuccess(await userManager.DeleteAsync(user)); IdentityResultAssert.IsSuccess(await userManager.DeleteAsync(user));
} }
[TestPriority(10000)]
[Fact]
public void DropDatabaseDone()
{
DropDb();
}
} }
} }

View File

@ -30,11 +30,13 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test
[TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")]
public class UserStoreGuidTest : SqlStoreTestBase<GuidUser, GuidRole, Guid> public class UserStoreGuidTest : SqlStoreTestBase<GuidUser, GuidRole, Guid>
{ {
private readonly string _connectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreGuidTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;";
public override string ConnectionString public override string ConnectionString
{ {
get get
{ {
return @"Server=(localdb)\v11.0;Database=SqlUserStoreGuidTest;Trusted_Connection=True;"; return _connectionString;
} }
} }

View File

@ -30,11 +30,13 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test
[TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")]
public class UserStoreIntTest : SqlStoreTestBase<IntUser, IntRole, int> public class UserStoreIntTest : SqlStoreTestBase<IntUser, IntRole, int>
{ {
private readonly string _connectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreIntTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;";
public override string ConnectionString public override string ConnectionString
{ {
get get
{ {
return @"Server=(localdb)\v11.0;Database=SqlUserStoreIntTest;Trusted_Connection=True;"; return _connectionString;
} }
} }
} }

View File

@ -28,11 +28,13 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test
[TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")]
public class UserStoreStringKeyTest : SqlStoreTestBase<StringUser, StringRole, string> public class UserStoreStringKeyTest : SqlStoreTestBase<StringUser, StringRole, string>
{ {
private readonly string _connectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreStringTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;";
public override string ConnectionString public override string ConnectionString
{ {
get get
{ {
return @"Server=(localdb)\v11.0;Database=SqlUserStoreStringTest;Trusted_Connection=True;"; return _connectionString;
} }
} }
} }

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test
[TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")] [TestCaseOrderer("Microsoft.AspNet.Identity.Test.PriorityOrderer", "Microsoft.AspNet.Identity.SqlServer.Test")]
public class UserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole> public class UserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole>
{ {
private static readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreTest;Trusted_Connection=True;"; private readonly string ConnectionString = @"Server=(localdb)\v11.0;Database=SqlUserStoreTest" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ";Trusted_Connection=True;";
public class ApplicationUser : IdentityUser { } public class ApplicationUser : IdentityUser { }
@ -135,9 +135,10 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddEntityFramework().AddSqlServer(); services.AddEntityFramework().AddSqlServer();
var dbOptions = new DbContextOptions();
dbOptions.UseSqlServer(ConnectionString);
var serviceProvider = services.BuildServiceProvider(); var serviceProvider = services.BuildServiceProvider();
var db = new IdentityDbContext(serviceProvider, dbOptions);
var db = new IdentityDbContext(serviceProvider, ConnectionString);
if (delete) if (delete)
{ {
db.Database.EnsureDeleted(); db.Database.EnsureDeleted();