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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Config
{
using System.Data.Common;
using System.Data.Entity.Core.Common;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Infrastructure.Pluralization;
using System.Data.Entity.Internal;
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.History;
using System.Data.Entity.Migrations.Sql;
using System.Data.Entity.Resources;
using System.Data.Entity.Spatial;
using System.Data.Entity.TestHelpers;
using Moq;
using Xunit;
public class DbConfigurationTests
{
public class SetConfiguration
{
[Fact]
public void DbConfiguration_cannot_be_set_to_null()
{
Assert.Equal(
"configuration",
Assert.Throws<ArgumentNullException>(() => DbConfiguration.SetConfiguration(null)).ParamName);
}
}
public class AddDependencyResolver
{
[Fact]
public void AddDependencyResolver_throws_if_given_a_null_resolver()
{
Assert.Equal(
"resolver",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().AddDependencyResolver(null)).ParamName);
}
[Fact]
public void AddDependencyResolver_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("AddDependencyResolver"),
Assert.Throws<InvalidOperationException>(
() => configuration.AddDependencyResolver(new Mock<IDbDependencyResolver>().Object)).Message);
}
[Fact]
public void AddDependencyResolver_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var resolver = new Mock<IDbDependencyResolver>().Object;
new DbConfiguration(mockInternalConfiguration.Object).AddDependencyResolver(resolver);
mockInternalConfiguration.Verify(m => m.AddDependencyResolver(resolver, false));
}
}
public class OnLockingConfiguration
{
[Fact]
public void OnLockingConfiguration_throws_when_attempting_to_add_or_remove_a_null_handler()
{
Assert.Equal(
"value",
Assert.Throws<ArgumentNullException>(() => DbConfiguration.OnLockingConfiguration += null).ParamName);
Assert.Equal(
"value",
Assert.Throws<ArgumentNullException>(() => DbConfiguration.OnLockingConfiguration -= null).ParamName);
}
}
public class AddDbProviderServices
{
[Fact]
public void AddDbProviderServices_throws_if_given_a_null_provider_or_bad_invariant_name()
{
Assert.Equal(
"provider",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().AddDbProviderServices("Karl", null)).ParamName);
Assert.Equal(
Strings.ArgumentIsNullOrWhitespace("providerInvariantName"),
Assert.Throws<ArgumentException>(
() => new DbConfiguration().AddDbProviderServices(null, new Mock<DbProviderServices>().Object)).Message);
Assert.Equal(
Strings.ArgumentIsNullOrWhitespace("providerInvariantName"),
Assert.Throws<ArgumentException>(
() => new DbConfiguration().AddDbProviderServices("", new Mock<DbProviderServices>().Object)).Message);
Assert.Equal(
Strings.ArgumentIsNullOrWhitespace("providerInvariantName"),
Assert.Throws<ArgumentException>(
() => new DbConfiguration().AddDbProviderServices(" ", new Mock<DbProviderServices>().Object)).Message);
}
[Fact]
public void AddDbProviderServices_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var providerServices = new Mock<DbProviderServices>().Object;
new DbConfiguration(mockInternalConfiguration.Object).AddDbProviderServices("900.FTW", providerServices);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(providerServices, "900.FTW"));
}
[Fact]
public void AddDbProviderServices_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("AddDbProviderServices"),
Assert.Throws<InvalidOperationException>(
() => configuration.AddDbProviderServices("Karl", new Mock<DbProviderServices>().Object)).Message);
}
}
public class AddDbProviderFactory
{
[Fact]
public void AddDbProviderFactory_throws_if_given_a_null_provider_or_bad_invariant_name()
{
Assert.Equal(
"providerFactory",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().AddDbProviderFactory("Karl", null)).ParamName);
Assert.Equal(
Strings.ArgumentIsNullOrWhitespace("providerInvariantName"),
Assert.Throws<ArgumentException>(
() => new DbConfiguration().AddDbProviderFactory(null, new Mock<DbProviderFactory>().Object)).Message);
Assert.Equal(
Strings.ArgumentIsNullOrWhitespace("providerInvariantName"),
Assert.Throws<ArgumentException>(
() => new DbConfiguration().AddDbProviderFactory("", new Mock<DbProviderFactory>().Object)).Message);
Assert.Equal(
Strings.ArgumentIsNullOrWhitespace("providerInvariantName"),
Assert.Throws<ArgumentException>(
() => new DbConfiguration().AddDbProviderFactory(" ", new Mock<DbProviderFactory>().Object)).Message);
}
[Fact]
public void AddDbProviderFactory_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var providerFactory = new Mock<DbProviderFactory>().Object;
new DbConfiguration(mockInternalConfiguration.Object).AddDbProviderFactory("920.FTW", providerFactory);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(providerFactory, "920.FTW"));
mockInternalConfiguration.Verify(m => m.AddDependencyResolver(new InvariantNameResolver(providerFactory, "920.FTW"), false));
}
[Fact]
public void AddDbProviderFactory_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("AddDbProviderFactory"),
Assert.Throws<InvalidOperationException>(
() => configuration.AddDbProviderFactory("Karl", new Mock<DbProviderFactory>().Object)).Message);
}
}
public class SetDefaultConnectionFactory : TestBase
{
[Fact]
public void Setting_DefaultConnectionFactory_throws_if_given_a_null_factory()
{
Assert.Equal(
"connectionFactory",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().SetDefaultConnectionFactory(null)).ParamName);
}
[Fact]
public void Setting_DefaultConnectionFactory_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("SetDefaultConnectionFactory"),
Assert.Throws<InvalidOperationException>(
() => configuration.SetDefaultConnectionFactory(new Mock<IDbConnectionFactory>().Object)).Message);
}
[Fact]
public void SetDefaultConnectionFactory_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var connectionFactory = new Mock<IDbConnectionFactory>().Object;
new DbConfiguration(mockInternalConfiguration.Object).SetDefaultConnectionFactory(connectionFactory);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(connectionFactory, null));
}
[Fact]
public void DefaultConnectionFactory_set_in_code_can_be_overriden_before_config_is_locked()
{
Assert.IsType<SqlConnectionFactory>(DbConfiguration.GetService<IDbConnectionFactory>());
Assert.IsType<DefaultUnitTestsConnectionFactory>(FunctionalTestsConfiguration.OriginalConnectionFactories[0]);
}
}
public class SetPluralizationService
{
[Fact]
public void Setting_PluralizationService_throws_if_given_a_null_service()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
"pluralizationService",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().SetPluralizationService(null)).ParamName);
}
[Fact]
public void Setting_PluralizationService_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("SetPluralizationService"),
Assert.Throws<InvalidOperationException>(
() => configuration.SetPluralizationService(new Mock<IPluralizationService>().Object)).Message);
}
[Fact]
public void SetPluralizationService_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var pluralizationService = new Mock<IPluralizationService>().Object;
new DbConfiguration(mockInternalConfiguration.Object).SetPluralizationService(pluralizationService);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(pluralizationService, null));
}
}
public class GetService
{
[Fact]
public void Delegates_to_internal_configuration()
{
Assert.NotNull(DbConfiguration.GetService<IDbCommandInterceptor>(null));
}
}
public class DependencyResolver
{
[Fact]
public void Default_IDbModelCacheKeyFactory_is_returned_by_default()
{
Assert.IsType<DefaultModelCacheKeyFactory>(DbConfiguration.GetService<IDbModelCacheKeyFactory>());
}
}
public class SetDatabaseInitializer
{
[Fact]
public void SetDatabaseInitializer_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("SetDatabaseInitializer"),
Assert.Throws<InvalidOperationException>(
() => configuration.SetDatabaseInitializer(new Mock<IDatabaseInitializer<DbContext>>().Object)).Message);
}
[Fact]
public void SetDatabaseInitializer_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var initializer = new Mock<IDatabaseInitializer<DbContext>>().Object;
new DbConfiguration(mockInternalConfiguration.Object).SetDatabaseInitializer(initializer);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(initializer, null));
}
[Fact]
public void SetDatabaseInitializer_creates_null_initializer_when_given_null_argument()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
new DbConfiguration(mockInternalConfiguration.Object).SetDatabaseInitializer<DbContext>(null);
mockInternalConfiguration.Verify(
m => m.RegisterSingleton<IDatabaseInitializer<DbContext>>(It.IsAny<NullDatabaseInitializer<DbContext>>(), null));
}
}
public class AddMigrationSqlGenerator
{
[Fact]
public void AddMigrationSqlGenerator_throws_if_given_a_null_generator_or_bad_invariant_name()
{
Assert.Equal(
"sqlGenerator",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().AddMigrationSqlGenerator("Karl", null)).ParamName);
Assert.Equal(
Strings.ArgumentIsNullOrWhitespace("providerInvariantName"),
Assert.Throws<ArgumentException>(
() => new DbConfiguration().AddMigrationSqlGenerator(null, () => new Mock<MigrationSqlGenerator>().Object)).Message);
Assert.Equal(
Strings.ArgumentIsNullOrWhitespace("providerInvariantName"),
Assert.Throws<ArgumentException>(
() => new DbConfiguration().AddMigrationSqlGenerator("", () => new Mock<MigrationSqlGenerator>().Object)).Message);
Assert.Equal(
Strings.ArgumentIsNullOrWhitespace("providerInvariantName"),
Assert.Throws<ArgumentException>(
() => new DbConfiguration().AddMigrationSqlGenerator(" ", () => new Mock<MigrationSqlGenerator>().Object)).Message);
}
[Fact]
public void AddMigrationSqlGenerator_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("AddMigrationSqlGenerator"),
Assert.Throws<InvalidOperationException>(
() => configuration.AddMigrationSqlGenerator("Karl", () => new Mock<MigrationSqlGenerator>().Object)).Message);
}
[Fact]
public void AddMigrationSqlGenerator_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var generator = new Func<MigrationSqlGenerator>(() => new Mock<MigrationSqlGenerator>().Object);
new DbConfiguration(mockInternalConfiguration.Object).AddMigrationSqlGenerator("Karl", generator);
mockInternalConfiguration.Verify(
m => m.AddDependencyResolver(It.IsAny<TransientDependencyResolver<MigrationSqlGenerator>>(), false));
}
}
public class SetManifestTokenService
{
[Fact]
public void SetManifestTokenService_throws_if_given_a_null_service()
{
Assert.Equal(
"service",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().SetManifestTokenService(null)).ParamName);
}
[Fact]
public void SetManifestTokenService_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("SetManifestTokenService"),
Assert.Throws<InvalidOperationException>(
() => configuration.SetManifestTokenService(new Mock<IManifestTokenService>().Object)).Message);
}
[Fact]
public void SetManifestTokenService_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var service = new Mock<IManifestTokenService>().Object;
new DbConfiguration(mockInternalConfiguration.Object).SetManifestTokenService(service);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(service, null));
}
}
public class SetProviderFactoryService
{
[Fact]
public void SetProviderFactoryService_throws_if_given_a_null_service()
{
Assert.Equal(
"providerFactoryService",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().SetProviderFactoryService(null)).ParamName);
}
[Fact]
public void SetProviderFactoryService_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("SetProviderFactoryService"),
Assert.Throws<InvalidOperationException>(
() => configuration.SetProviderFactoryService(new Mock<IDbProviderFactoryService>().Object)).Message);
}
[Fact]
public void SetProviderFactoryService_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var service = new Mock<IDbProviderFactoryService>().Object;
new DbConfiguration(mockInternalConfiguration.Object).SetProviderFactoryService(service);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(service, null));
}
}
public class SetModelCacheKeyFactory
{
[Fact]
public void SetModelCacheKeyFactory_throws_if_given_a_null_factory()
{
Assert.Equal(
"keyFactory",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().SetModelCacheKeyFactory(null)).ParamName);
}
[Fact]
public void SetModelCacheKeyFactory_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("SetModelCacheKeyFactory"),
Assert.Throws<InvalidOperationException>(
() => configuration.SetModelCacheKeyFactory(new Mock<IDbModelCacheKeyFactory>().Object)).Message);
}
[Fact]
public void SetModelCacheKeyFactory_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var factory = new Mock<IDbModelCacheKeyFactory>().Object;
new DbConfiguration(mockInternalConfiguration.Object).SetModelCacheKeyFactory(factory);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(factory, null));
}
}
public class SetHistoryContextFactory
{
[Fact]
public void Throws_if_given_a_null_factory()
{
Assert.Equal(
"historyContextFactory",
Assert.Throws<ArgumentNullException>(
() => new DbConfiguration().SetHistoryContextFactory<DbMigrationsConfiguration>(null)).ParamName);
}
[Fact]
public void Throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("SetHistoryContextFactory"),
Assert.Throws<InvalidOperationException>(
() => configuration.SetHistoryContextFactory<DbMigrationsConfiguration>(
new Mock<IHistoryContextFactory>().Object)).Message);
}
[Fact]
public void Delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var factory = new Mock<IHistoryContextFactory>().Object;
new DbConfiguration(mockInternalConfiguration.Object)
.SetHistoryContextFactory<DbMigrationsConfiguration>(factory);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(factory, typeof(DbMigrationsConfiguration)));
}
}
public class SetSpatialProvider
{
[Fact]
public void SetSpatialProvider_throws_if_given_a_null_factory()
{
Assert.Equal(
"spatialProvider",
Assert.Throws<ArgumentNullException>(() => new DbConfiguration().SetSpatialProvider(null)).ParamName);
}
[Fact]
public void SetSpatialProvider_throws_if_the_configuation_is_locked()
{
var configuration = CreatedLockedConfiguration();
Assert.Equal(
Strings.ConfigurationLocked("SetSpatialProvider"),
Assert.Throws<InvalidOperationException>(
() => configuration.SetSpatialProvider(new Mock<DbSpatialServices>().Object)).Message);
}
[Fact]
public void SetSpatialProvider_delegates_to_internal_configuration()
{
var mockInternalConfiguration = new Mock<InternalConfiguration>();
var provider = new Mock<DbSpatialServices>().Object;
new DbConfiguration(mockInternalConfiguration.Object).SetSpatialProvider(provider);
mockInternalConfiguration.Verify(m => m.RegisterSingleton(provider, null));
}
}
private static DbConfiguration CreatedLockedConfiguration()
{
var configuration = new DbConfiguration();
configuration.InternalConfiguration.Lock();
return configuration;
}
}
}
|