mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 10:54:50 +08:00
Skip SqlServer related unit tests if SqlServer is not installed
--HG-- branch : dev extra : transplant_source : g%B6%FFf%9DD%B0UD%97%E8%2B%94%2B%3A%CF%A8B%17%A0
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Autofac.Features.Metadata;
|
using Autofac.Features.Metadata;
|
||||||
using NHibernate;
|
using NHibernate;
|
||||||
@@ -13,35 +14,22 @@ namespace Orchard.Tests.Data {
|
|||||||
|
|
||||||
public static void RunWithSqlServer(IEnumerable<RecordBlueprint> recordDescriptors, Action<ISessionFactory> action) {
|
public static void RunWithSqlServer(IEnumerable<RecordBlueprint> recordDescriptors, Action<ISessionFactory> action) {
|
||||||
var temporaryPath = Path.GetTempFileName();
|
var temporaryPath = Path.GetTempFileName();
|
||||||
if(File.Exists(temporaryPath))
|
if (File.Exists(temporaryPath))
|
||||||
File.Delete(temporaryPath);
|
File.Delete(temporaryPath);
|
||||||
Directory.CreateDirectory(temporaryPath);
|
Directory.CreateDirectory(temporaryPath);
|
||||||
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
|
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
|
||||||
var databaseName = Path.GetFileNameWithoutExtension(databasePath);
|
var databaseName = Path.GetFileNameWithoutExtension(databasePath);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// create database
|
// create database
|
||||||
using ( var connection = new SqlConnection(
|
if (!TryCreateSqlServerDatabase(databasePath, databaseName))
|
||||||
"Data Source=.\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=true;User Instance=True;") ) {
|
return;
|
||||||
connection.Open();
|
|
||||||
using ( var command = connection.CreateCommand() ) {
|
|
||||||
command.CommandText =
|
|
||||||
"CREATE DATABASE " + databaseName +
|
|
||||||
" ON PRIMARY (NAME=" + databaseName +
|
|
||||||
", FILENAME='" + databasePath.Replace("'", "''") + "')";
|
|
||||||
command.ExecuteNonQuery();
|
|
||||||
|
|
||||||
command.CommandText =
|
var meta = new Meta<CreateDataServicesProvider>((dataFolder, connectionString) =>
|
||||||
"EXEC sp_detach_db '" + databaseName + "', 'true'";
|
new SqlServerDataServicesProvider(dataFolder, connectionString),
|
||||||
command.ExecuteNonQuery();
|
new Dictionary<string, object> { { "ProviderName", "SqlServer" } });
|
||||||
}
|
|
||||||
}
|
var manager = (IDataServicesProviderFactory)new DataServicesProviderFactory(new[] { meta });
|
||||||
|
|
||||||
var manager = (IDataServicesProviderFactory)new DataServicesProviderFactory(new[] {
|
|
||||||
new Meta<CreateDataServicesProvider>(
|
|
||||||
(dataFolder, connectionString) => new SqlServerDataServicesProvider(dataFolder, connectionString),
|
|
||||||
new Dictionary<string, object> {{"ProviderName", "SqlServer"}})
|
|
||||||
});
|
|
||||||
var parameters = new SessionFactoryParameters {
|
var parameters = new SessionFactoryParameters {
|
||||||
Provider = "SqlServer",
|
Provider = "SqlServer",
|
||||||
DataFolder = temporaryPath,
|
DataFolder = temporaryPath,
|
||||||
@@ -55,7 +43,7 @@ namespace Orchard.Tests.Data {
|
|||||||
|
|
||||||
new SchemaExport(configuration).Execute(false, true, false);
|
new SchemaExport(configuration).Execute(false, true, false);
|
||||||
|
|
||||||
using ( var sessionFactory = configuration.BuildSessionFactory() ) {
|
using (var sessionFactory = configuration.BuildSessionFactory()) {
|
||||||
action(sessionFactory);
|
action(sessionFactory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,13 +51,46 @@ namespace Orchard.Tests.Data {
|
|||||||
try {
|
try {
|
||||||
Directory.Delete(temporaryPath, true);
|
Directory.Delete(temporaryPath, true);
|
||||||
}
|
}
|
||||||
catch (IOException) {}
|
catch (IOException) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryCreateSqlServerDatabase(string databasePath, string databaseName) {
|
||||||
|
var connection = TryOpenSqlServerConnection();
|
||||||
|
if (connection == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
using (connection) {
|
||||||
|
using (var command = connection.CreateCommand()) {
|
||||||
|
command.CommandText =
|
||||||
|
"CREATE DATABASE " + databaseName +
|
||||||
|
" ON PRIMARY (NAME=" + databaseName +
|
||||||
|
", FILENAME='" + databasePath.Replace("'", "''") + "')";
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
|
||||||
|
command.CommandText =
|
||||||
|
"EXEC sp_detach_db '" + databaseName + "', 'true'";
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SqlConnection TryOpenSqlServerConnection() {
|
||||||
|
try {
|
||||||
|
var connection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=true;User Instance=True;");
|
||||||
|
connection.Open();
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
catch (SqlException e) {
|
||||||
|
Trace.WriteLine(string.Format("Error opening connection to Sql Server ('{0}'). Skipping test.", e.Message));
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RunWithSqlCe(IEnumerable<RecordBlueprint> recordDescriptors, Action<ISessionFactory> action) {
|
public static void RunWithSqlCe(IEnumerable<RecordBlueprint> recordDescriptors, Action<ISessionFactory> action) {
|
||||||
var temporaryPath = Path.GetTempFileName();
|
var temporaryPath = Path.GetTempFileName();
|
||||||
if ( File.Exists(temporaryPath) )
|
if (File.Exists(temporaryPath))
|
||||||
File.Delete(temporaryPath);
|
File.Delete(temporaryPath);
|
||||||
Directory.CreateDirectory(temporaryPath);
|
Directory.CreateDirectory(temporaryPath);
|
||||||
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
|
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
|
||||||
@@ -94,7 +115,7 @@ namespace Orchard.Tests.Data {
|
|||||||
|
|
||||||
new SchemaExport(configuration).Execute(false, true, false);
|
new SchemaExport(configuration).Execute(false, true, false);
|
||||||
|
|
||||||
using ( var sessionFactory = configuration.BuildSessionFactory() ) {
|
using (var sessionFactory = configuration.BuildSessionFactory()) {
|
||||||
action(sessionFactory);
|
action(sessionFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +124,7 @@ namespace Orchard.Tests.Data {
|
|||||||
try {
|
try {
|
||||||
Directory.Delete(temporaryPath, true);
|
Directory.Delete(temporaryPath, true);
|
||||||
}
|
}
|
||||||
catch (IOException) {}
|
catch (IOException) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user