1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity
{
using System.Data.Common;
using System.Data.Entity.Infrastructure;
using System.Globalization;
using System.IO;
using FunctionalTests.ProductivityApi.TemplateModels.CsAdvancedPatterns;
using FunctionalTests.ProductivityApi.TemplateModels.CsMonsterModel;
using ProductivityApiTests;
using SimpleModel;
using Xunit;
/// <summary>
/// Base class for Productivity API tests that sets up a simple model and some data.
/// Call ClassInitializeBase from the ClassInitialize method of your test class to ensure
/// that the test data is configured.
/// </summary>
public class FunctionalTestBase : TestBase
{
#region Test database setup
protected int AdvancedModelOfficeCount
{
get { return 4; }
}
protected int AdvancedModelBuildingCount
{
get { return 2; }
}
protected int AdvancedModelWhiteboardCount
{
get { return 3; }
}
/// <summary>
/// Ensures the database for the context is created and seeded. This is typically used
/// when a test is going to use a transaction to ensure that the DDL happens outside of
/// the transaction.
/// </summary>
/// <param name="createContext"> A func to create the context. </param>
protected static void EnsureDatabaseInitialized(Func<DbContext> createContext)
{
using (var context = createContext())
{
context.Database.Initialize(force: false);
}
}
/// <summary>
/// Drops the database that would be used for the context. Usually used to avoid errors during initialization.
/// </summary>
/// <param name="createContext"> A func to create the context. </param>
protected static void DropDatabase(Func<DbContext> createContext)
{
using (var context = createContext())
{
context.Database.Delete();
}
}
/// <summary>
/// Drops and then initializes the database that will be used for the context.
/// </summary>
/// <param name="createContext"> A func to create the context. </param>
protected static void ResetDatabase(Func<DbContext> createContext)
{
DropDatabase(createContext);
EnsureDatabaseInitialized(createContext);
}
/// <summary>
/// Initializes the metadata files and creates databases for existing CSDL/EDMX files.
/// </summary>
protected static void InitializeModelFirstDatabases()
{
const string prefix = "FunctionalTests.ProductivityApi.TemplateModels.Schemas.";
ResourceUtilities.CopyEmbeddedResourcesToCurrentDir(
typeof(TemplateTests).Assembly,
prefix,
/*overwrite*/ true,
"AdvancedPatterns.edmx",
"MonsterModel.csdl",
"MonsterModel.msl",
"MonsterModel.ssdl");
// Extract the csdl, msl, and ssdl from the edmx so that they can be referenced in the connection string.
ModelHelpers.WriteMetadataFiles(File.ReadAllText(@".\AdvancedPatterns.edmx"), @".\AdvancedPatterns");
using (var context = new AdvancedPatternsModelFirstContext())
{
context.Database.Initialize(force: false);
}
using (var context = new MonsterModel())
{
Database.SetInitializer(new DropCreateDatabaseAlways<MonsterModel>());
context.Database.Initialize(force: false);
}
}
private static bool _metadataForSimpleModelCreated;
/// <summary>
/// Creates the metadata files (CSDL/SSDL/MSL) for the SimpleModelContext.
/// </summary>
protected static void CreateMetadataFilesForSimpleModel()
{
if (!_metadataForSimpleModelCreated)
{
var builder = SimpleModelContext.CreateBuilder();
ModelHelpers.WriteMetadataFiles(builder, @".\SimpleModel");
using (var connection = SimpleConnection<SimpleModelContext>())
{
new SimpleModelContext(connection, builder.Build(connection).Compile()).Database.Initialize(false);
}
_metadataForSimpleModelCreated = true;
}
}
private static string _simpleModelEntityConnectionString;
/// <summary>
/// An entity connection string for the SimpleModelContext.
/// </summary>
protected static string SimpleModelEntityConnectionString
{
get
{
const string baseConnectionString =
@"metadata=.\SimpleModel.csdl|.\SimpleModel.ssdl|.\SimpleModel.msl;
provider=System.Data.SqlClient;provider connection string='{0}'";
return _simpleModelEntityConnectionString ??
(_simpleModelEntityConnectionString =
String.Format(
CultureInfo.InvariantCulture, baseConnectionString,
SimpleConnectionString<SimpleModelContext>()));
}
}
#endregion
#region Construction helpers
protected static TContext CreateContext<TContext>(
DbContextConstructorArgumentType arguments,
string providerName = "System.Data.SqlClient")
where TContext : DbContext
{
DbConnection connection = null;
if (arguments == DbContextConstructorArgumentType.Connection
||
arguments == DbContextConstructorArgumentType.ConnectionAndDbCompiledModel)
{
if (providerName == "System.Data.SqlClient")
{
connection = SimpleConnection<TContext>();
}
else if (providerName == "System.Data.SqlServerCe.4.0")
{
connection = SimpleCeConnection<TContext>();
}
else
{
throw new ArgumentException("Invalid provider specified, " + providerName);
}
}
string connectionString = null;
if (arguments == DbContextConstructorArgumentType.ConnectionString
||
arguments == DbContextConstructorArgumentType.ConnectionStringAndDbCompiledModel)
{
if (providerName == "System.Data.SqlClient")
{
connectionString = SimpleConnectionString<TContext>();
}
else if (providerName == "System.Data.SqlServerCe.4.0")
{
connectionString = SimpleCeConnectionString<TContext>();
}
else
{
throw new ArgumentException("Invalid provider specified, " + providerName);
}
}
var providerInfo
= (providerName == "System.Data.SqlServerCe.4.0")
? ProviderRegistry.SqlCe4_ProviderInfo
: ProviderRegistry.Sql2008_ProviderInfo;
TContext context = null;
switch (arguments)
{
case DbContextConstructorArgumentType.Parameterless:
context = (TContext)Activator.CreateInstance(typeof(TContext));
break;
case DbContextConstructorArgumentType.DbCompiledModel:
context =
(TContext)
Activator.CreateInstance(
typeof(TContext),
SimpleModelContext.CreateBuilder().Build(providerInfo).Compile());
break;
case DbContextConstructorArgumentType.Connection:
context = (TContext)Activator.CreateInstance(typeof(TContext), connection, false);
break;
case DbContextConstructorArgumentType.ConnectionString:
context = (TContext)Activator.CreateInstance(typeof(TContext), connectionString);
break;
case DbContextConstructorArgumentType.ConnectionAndDbCompiledModel:
context =
(TContext)
Activator.CreateInstance(
typeof(TContext), connection,
SimpleModelContext.CreateBuilder().Build(connection).Compile(), false);
break;
case DbContextConstructorArgumentType.ConnectionStringAndDbCompiledModel:
context =
(TContext)
Activator.CreateInstance(
typeof(TContext), connectionString,
SimpleModelContext.CreateBuilder().Build(providerInfo).Compile());
break;
default:
throw new ArgumentException("Invalid DbContext constructor arguments " + arguments);
}
return context;
}
protected void VerifySetsAreInitialized<TContext>(
DbCompiledModelContents contents,
DbProviderInfo providerInfo = null)
where TContext : SimpleModelContextWithNoData
{
providerInfo = providerInfo ?? ProviderRegistry.Sql2008_ProviderInfo;
// Arrange
// DbCompiledModel creation as appropriate for the various model content options
var builder = new DbModelBuilder();
switch (contents)
{
case DbCompiledModelContents.IsEmpty:
// Do nothing as builder has already been initialized
break;
case DbCompiledModelContents.IsSubset:
// Product is not defined here
builder.Entity<Category>();
break;
case DbCompiledModelContents.IsSuperset:
builder.Entity<Category>();
builder.Entity<Product>();
builder.Entity<Login>();
break;
case DbCompiledModelContents.Match:
builder.Entity<Category>();
builder.Entity<Product>();
break;
case DbCompiledModelContents.DontMatch:
builder.Entity<FeaturedProduct>();
builder.Entity<Login>();
break;
default:
throw new ArgumentException("Invalid DbCompiledModelContents Arguments passed in, " + contents);
}
var model = builder.Build(providerInfo).Compile();
// Act
using (var context = (TContext)Activator.CreateInstance(typeof(TContext), model))
{
// Verification as appropriate for the various model content options
switch (contents)
{
case DbCompiledModelContents.IsEmpty:
Assert.NotNull(context.Categories);
Assert.NotNull(context.Products);
context.Assert<Category>().IsNotInModel();
context.Assert<Product>().IsNotInModel();
break;
case DbCompiledModelContents.IsSubset:
Assert.NotNull(context.Categories);
Assert.NotNull(context.Products);
context.Assert<Category>().IsInModel();
context.Assert<Product>().IsInModel(); // reachability
break;
case DbCompiledModelContents.IsSuperset:
Assert.NotNull(context.Categories);
Assert.NotNull(context.Products);
context.Assert<Category>().IsInModel();
context.Assert<Product>().IsInModel();
context.Assert<Login>().IsInModel();
break;
case DbCompiledModelContents.Match:
Assert.NotNull(context.Categories);
Assert.NotNull(context.Products);
context.Assert<Category>().IsInModel();
context.Assert<Product>().IsInModel();
context.Assert<Login>().IsNotInModel();
break;
case DbCompiledModelContents.DontMatch:
Assert.NotNull(context.Categories);
Assert.NotNull(context.Products);
context.Assert<Login>().IsInModel();
context.Assert<FeaturedProduct>().IsInModel();
break;
default:
throw new ArgumentException("Invalid DbCompiledModelContents Arguments passed in, " + contents);
}
}
}
#endregion
}
}
|