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
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Internal
{
using System.Collections.Generic;
using System.Data.Entity.Config;
using System.Data.Entity.Migrations.Model;
using System.Data.Entity.Migrations.Sql;
using System.Data.Entity.Resources;
using System.Data.Entity.SqlServer;
using System.Data.Entity.Utilities;
using Xunit;
public class ProviderConfigTests
{
public class TryGetMigrationSqlGeneratorFactory : AppConfigTestBase
{
[Fact]
public void TryGetMigrationSqlGeneratorFactory_returns_factory_for_null_if_invariant_name_is_not_in_config()
{
Assert.Null(CreateAppConfig().Providers.TryGetMigrationSqlGeneratorFactory("System.Data.SqlClient")());
}
[Fact]
public void TryGetMigrationSqlGeneratorFactory_returns_factory_for_null_if_no_migrations_SQL_generator_is_registered()
{
Assert.Null(
CreateAppConfig(
"Learning.To.Fly", typeof(ProviderServicesFactoryTests.FakeProviderWithPublicProperty).AssemblyQualifiedName)
.Providers
.TryGetMigrationSqlGeneratorFactory("Learning.To.Fly")());
}
public class MySqlGenerator : MigrationSqlGenerator
{
public override IEnumerable<MigrationStatement> Generate(
IEnumerable<MigrationOperation> migrationOperations,
string providerManifestToken)
{
throw new NotImplementedException();
}
}
[Fact]
public void TryGetMigrationSqlGeneratorFactory_returns_generator_if_registered_in_config()
{
Assert.IsType<MySqlGenerator>(
CreateAppConfig(
"The.Hamster",
typeof(ProviderServicesFactoryTests.FakeProviderWithPublicProperty).AssemblyQualifiedName,
typeof(MySqlGenerator).AssemblyQualifiedName)
.Providers
.TryGetMigrationSqlGeneratorFactory("The.Hamster")());
}
[Fact]
public void TryGetMigrationSqlGeneratorFactory_throws_if_type_cannot_be_loaded()
{
Assert.Equal(
Strings.SqlGeneratorTypeMissing("Ben.Collins", "The.Stig"),
Assert.Throws<InvalidOperationException>(
() => CreateAppConfig(
"The.Stig",
typeof(ProviderServicesFactoryTests.FakeProviderWithPublicProperty).AssemblyQualifiedName,
"Ben.Collins")
.Providers
.TryGetMigrationSqlGeneratorFactory("The.Stig")).Message);
}
[Fact]
public void TryGetMigrationSqlGeneratorFactory_throws_if_type_cannot_be_used()
{
Assert.Equal(
Strings.CreateInstance_BadSqlGeneratorType(typeof(object).ToString(), typeof(MigrationSqlGenerator).ToString()),
Assert.Throws<InvalidOperationException>(
() => CreateAppConfig(
"Jezza",
typeof(ProviderServicesFactoryTests.FakeProviderWithPublicProperty).AssemblyQualifiedName,
typeof(object).AssemblyQualifiedName)
.Providers
.TryGetMigrationSqlGeneratorFactory("Jezza")()).Message);
}
}
public class TryGetDbProviderServices : AppConfigTestBase
{
[Fact]
public void TryGetDbProviderServices_returns_null_if_invariant_name_is_not_in_config()
{
Assert.Null(CreateAppConfig().Providers.TryGetDbProviderServices("System.Data.SqlClient"));
}
[Fact]
public void TryGetDbProviderServices_returns_provider_if_invariant_name_is_in_config()
{
Assert.Same(
ProviderServicesFactoryTests.FakeProviderWithPublicProperty.Instance,
CreateAppConfig(
"Learning.To.Fly", typeof(ProviderServicesFactoryTests.FakeProviderWithPublicProperty).AssemblyQualifiedName)
.Providers
.TryGetDbProviderServices("Learning.To.Fly"));
}
}
public class TryGetSpatialProvider : AppConfigTestBase
{
[Fact]
public void TryGetSpatialProvider_returns_null_if_spatialProviderType_is_not_in_config()
{
Assert.Null(CreateAppConfig().Providers.TryGetSpatialProvider());
}
[Fact]
public void TryGetSpatialProvider_returns_provider_instance()
{
Assert.Same(
SqlSpatialServices.Instance,
CreateAppConfigWithSpatial(typeof(SqlSpatialServices).AssemblyQualifiedName)
.Providers
.TryGetSpatialProvider());
}
[Fact]
public void TryLoadFromConfig_throws_if_type_cannot_be_loaded()
{
var providerConfig = CreateAppConfigWithSpatial("I.Is.Not.A.Type").Providers;
Assert.Equal(
Strings.DbSpatialServicesTypeNotFound("I.Is.Not.A.Type"),
Assert.Throws<InvalidOperationException>(() => providerConfig.TryGetSpatialProvider()).Message);
}
[Fact]
public void TryLoadFromConfig_throws_if_type_does_not_have_Instance_member()
{
var providerConfig = CreateAppConfigWithSpatial(typeof(Random).AssemblyQualifiedName).Providers;
Assert.Equal(
Strings.DbSpatialServices_InstanceMissing(typeof(Random).AssemblyQualifiedName),
Assert.Throws<InvalidOperationException>(() => providerConfig.TryGetSpatialProvider()).Message);
}
[Fact]
public void TryLoadFromConfig_throws_if_Instance_member_returns_wrong_thing()
{
var providerConfig = CreateAppConfigWithSpatial(typeof(SqlProviderServices).AssemblyQualifiedName).Providers;
Assert.Equal(
Strings.DbSpatialServices_NotDbSpatialServices(typeof(SqlProviderServices).AssemblyQualifiedName),
Assert.Throws<InvalidOperationException>(() => providerConfig.TryGetSpatialProvider()).Message);
}
}
}
}
|