mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 03:25:23 +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.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using Autofac.Features.Metadata;
|
||||
using NHibernate;
|
||||
@@ -13,35 +14,22 @@ namespace Orchard.Tests.Data {
|
||||
|
||||
public static void RunWithSqlServer(IEnumerable<RecordBlueprint> recordDescriptors, Action<ISessionFactory> action) {
|
||||
var temporaryPath = Path.GetTempFileName();
|
||||
if(File.Exists(temporaryPath))
|
||||
if (File.Exists(temporaryPath))
|
||||
File.Delete(temporaryPath);
|
||||
Directory.CreateDirectory(temporaryPath);
|
||||
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
|
||||
var databaseName = Path.GetFileNameWithoutExtension(databasePath);
|
||||
try {
|
||||
|
||||
// create database
|
||||
using ( var connection = new SqlConnection(
|
||||
"Data Source=.\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=true;User Instance=True;") ) {
|
||||
connection.Open();
|
||||
using ( var command = connection.CreateCommand() ) {
|
||||
command.CommandText =
|
||||
"CREATE DATABASE " + databaseName +
|
||||
" ON PRIMARY (NAME=" + databaseName +
|
||||
", FILENAME='" + databasePath.Replace("'", "''") + "')";
|
||||
command.ExecuteNonQuery();
|
||||
if (!TryCreateSqlServerDatabase(databasePath, databaseName))
|
||||
return;
|
||||
|
||||
command.CommandText =
|
||||
"EXEC sp_detach_db '" + databaseName + "', 'true'";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
var meta = new Meta<CreateDataServicesProvider>((dataFolder, connectionString) =>
|
||||
new SqlServerDataServicesProvider(dataFolder, connectionString),
|
||||
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 {
|
||||
Provider = "SqlServer",
|
||||
DataFolder = temporaryPath,
|
||||
@@ -55,7 +43,7 @@ namespace Orchard.Tests.Data {
|
||||
|
||||
new SchemaExport(configuration).Execute(false, true, false);
|
||||
|
||||
using ( var sessionFactory = configuration.BuildSessionFactory() ) {
|
||||
using (var sessionFactory = configuration.BuildSessionFactory()) {
|
||||
action(sessionFactory);
|
||||
}
|
||||
}
|
||||
@@ -63,13 +51,46 @@ namespace Orchard.Tests.Data {
|
||||
try {
|
||||
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) {
|
||||
var temporaryPath = Path.GetTempFileName();
|
||||
if ( File.Exists(temporaryPath) )
|
||||
if (File.Exists(temporaryPath))
|
||||
File.Delete(temporaryPath);
|
||||
Directory.CreateDirectory(temporaryPath);
|
||||
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
|
||||
@@ -94,7 +115,7 @@ namespace Orchard.Tests.Data {
|
||||
|
||||
new SchemaExport(configuration).Execute(false, true, false);
|
||||
|
||||
using ( var sessionFactory = configuration.BuildSessionFactory() ) {
|
||||
using (var sessionFactory = configuration.BuildSessionFactory()) {
|
||||
action(sessionFactory);
|
||||
}
|
||||
|
||||
@@ -103,7 +124,7 @@ namespace Orchard.Tests.Data {
|
||||
try {
|
||||
Directory.Delete(temporaryPath, true);
|
||||
}
|
||||
catch (IOException) {}
|
||||
catch (IOException) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user