Commit d28f2cf4 authored by Nick Craver's avatar Nick Craver

Skip MySQL tests if the server isn't running.

This is the "approved" xUnit pattern, via:
https://github.com/xunit/samples.xunit/tree/master/DynamicSkipExample
It's also the only way I can make the damn skipping work...but at least
it's it's one-time code to make any server set relatively easily
skippable.
parent 6ed2f3ba
......@@ -5,7 +5,11 @@
using System.Threading.Tasks;
using Dapper.Contrib.Extensions;
#if XUNIT2
using FactAttribute = Dapper.Tests.Contrib.SkippableFactAttribute;
#else
using Xunit;
#endif
namespace Dapper.Tests.Contrib
{
......
......@@ -13,6 +13,9 @@
using System.Data.SqlServerCe;
using System.Transactions;
#endif
#if XUNIT2
using FactAttribute = Dapper.Tests.Contrib.SkippableFactAttribute;
#endif
namespace Dapper.Tests.Contrib
{
......
......@@ -2,6 +2,8 @@
using System.Data;
using System.Data.SqlClient;
using System.IO;
using Xunit;
using Xunit.Sdk;
#if COREFX
using Microsoft.Data.Sqlite;
using IDbConnection = System.Data.Common.DbConnection;
......@@ -18,6 +20,11 @@ namespace Dapper.Tests.Contrib
// the entire set of tests without declarations per method
// If we want to support a new provider, they need only be added here - not in multiple places
#if XUNIT2
[XunitTestCaseDiscoverer("Dapper.Tests.SkippableFactDiscoverer", "Dapper.Tests.Contrib")]
public class SkippableFactAttribute : FactAttribute { }
#endif
public class SqlServerTestSuite : TestSuite
{
const string DbName = "tempdb";
......@@ -56,35 +63,52 @@ static SqlServerTestSuite()
public class MySqlServerTestSuite : TestSuite
{
const string DbName = "DapperContribTests";
public static string ConnectionString =>
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPVEYOR"))
? @"Server=localhost;Uid=root;Pwd=Password12!;"
: $"Server=localhost;Uid=root;Pwd=Password12!;";
public override IDbConnection GetConnection() => new MySqlConnection(ConnectionString);
public override IDbConnection GetConnection()
{
if (_skip) throw new SkipTestException("Skipping MySQL Tests - no server.");
return new MySqlConnection(ConnectionString);
}
private static readonly bool _skip;
static MySqlServerTestSuite()
{
using (var connection = new MySqlConnection(ConnectionString))
try
{
// ReSharper disable once AccessToDisposedClosure
Action<string> dropTable = name => connection.Execute($@"DROP TABLE IF EXISTS `{name}`;");
connection.Open();
connection.Execute($@"DROP DATABASE IF EXISTS {DbName}; CREATE DATABASE {DbName}; USE {DbName};");
dropTable("Stuff");
connection.Execute(@"CREATE TABLE Stuff (TheId int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null, Created DateTime null);");
dropTable("People");
connection.Execute(@"CREATE TABLE People (Id int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null);");
dropTable("Users");
connection.Execute(@"CREATE TABLE Users (Id int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null, Age int not null);");
dropTable("Automobiles");
connection.Execute(@"CREATE TABLE Automobiles (Id int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null);");
dropTable("Results");
connection.Execute(@"CREATE TABLE Results (Id int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null, `Order` int not null);");
dropTable("ObjectX");
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null);");
dropTable("ObjectY");
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null);");
using (var connection = new MySqlConnection(ConnectionString))
{
// ReSharper disable once AccessToDisposedClosure
Action<string> dropTable = name => connection.Execute($@"DROP TABLE IF EXISTS `{name}`;");
connection.Open();
connection.Execute($@"DROP DATABASE IF EXISTS {DbName}; CREATE DATABASE {DbName}; USE {DbName};");
dropTable("Stuff");
connection.Execute(@"CREATE TABLE Stuff (TheId int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null, Created DateTime null);");
dropTable("People");
connection.Execute(@"CREATE TABLE People (Id int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null);");
dropTable("Users");
connection.Execute(@"CREATE TABLE Users (Id int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null, Age int not null);");
dropTable("Automobiles");
connection.Execute(@"CREATE TABLE Automobiles (Id int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null);");
dropTable("Results");
connection.Execute(@"CREATE TABLE Results (Id int not null AUTO_INCREMENT PRIMARY KEY, Name nvarchar(100) not null, `Order` int not null);");
dropTable("ObjectX");
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null);");
dropTable("ObjectY");
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null);");
}
}
catch (MySqlException e)
{
if (e.Message.Contains("Unable to connect"))
_skip = true;
else
throw;
}
}
}
......
......@@ -5,7 +5,7 @@
"licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0",
"description": "Dapper Contrib Test Suite",
"title": "Dapper.Contrib.Tests",
"version": "1.0.0-*",
"version": "1.0.0",
"copyright": "2015 Stack Exchange, Inc.",
"dependencies": {
"Dapper": {
......@@ -24,6 +24,7 @@
"compile": [
"**/*.cs",
"../Dapper.Tests/Assert.cs",
"../Dapper.Tests/XunitSkippable.cs",
"../Dapper/TypeExtensions.cs"
],
"commands": {
......@@ -50,7 +51,7 @@
},
"net45": {
"compilationOptions": {
"define": [ "ASYNC" ]
"define": [ "ASYNC", "XUNIT2" ]
},
"frameworkAssemblies": {
"System.Configuration": "4.0.0.0",
......@@ -69,7 +70,7 @@
},
"dotnet5.4": {
"compilationOptions": {
"define": [ "ASYNC", "COREFX" ]
"define": [ "ASYNC", "COREFX", "XUNIT2" ]
},
"dependencies": {
"Microsoft.CSharp": "4.0.1-*",
......@@ -85,7 +86,7 @@
},
"dnx451": {
"compilationOptions": {
"define": [ "ASYNC" ]
"define": [ "ASYNC", "XUNIT2" ]
},
"frameworkAssemblies": {
"System.Configuration": "4.0.0.0",
......@@ -102,7 +103,7 @@
},
"dnxcore50": {
"compilationOptions": {
"define": [ "COREFX", "ASYNC" ]
"define": [ "COREFX", "ASYNC", "XUNIT2" ]
},
"dependencies": {
"Microsoft.Data.Sqlite": "1.0.0-rc1-final",
......
using System;
#if XUNIT2 // Not in .Net 4.0 (xUnit v1)...
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit.Abstractions;
using Xunit.Sdk;
#endif
namespace Dapper.Tests
{
public class SkipTestException : Exception
{
public SkipTestException(string reason) : base(reason) { }
}
#if XUNIT2
// Most of the below is a direct copy & port from the wonderful examples by Brad Wilson at
// https://github.com/xunit/samples.xunit/tree/master/DynamicSkipExample
public class SkippableFactDiscoverer : IXunitTestCaseDiscoverer
{
readonly IMessageSink _diagnosticMessageSink;
public SkippableFactDiscoverer(IMessageSink diagnosticMessageSink)
{
_diagnosticMessageSink = diagnosticMessageSink;
}
public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
yield return new SkippableFactTestCase(_diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod);
}
}
public class SkippableFactTestCase : XunitTestCase
{
[Obsolete("Called by the de-serializer; should only be called by deriving classes for de-serialization purposes")]
public SkippableFactTestCase() { }
public SkippableFactTestCase(IMessageSink diagnosticMessageSink, TestMethodDisplay defaultMethodDisplay, ITestMethod testMethod, object[] testMethodArguments = null)
: base(diagnosticMessageSink, defaultMethodDisplay, testMethod, testMethodArguments)
{ }
public override async Task<RunSummary> RunAsync(
IMessageSink diagnosticMessageSink,
IMessageBus messageBus,
object[] constructorArguments,
ExceptionAggregator aggregator,
CancellationTokenSource cancellationTokenSource)
{
var skipMessageBus = new SkippableFactMessageBus(messageBus);
var result = await base.RunAsync(
diagnosticMessageSink,
skipMessageBus,
constructorArguments,
aggregator,
cancellationTokenSource);
if (skipMessageBus.DynamicallySkippedTestCount > 0)
{
result.Failed -= skipMessageBus.DynamicallySkippedTestCount;
result.Skipped += skipMessageBus.DynamicallySkippedTestCount;
}
return result;
}
}
public class SkippableFactMessageBus : IMessageBus
{
readonly IMessageBus _innerBus;
public SkippableFactMessageBus(IMessageBus innerBus)
{
_innerBus = innerBus;
}
public int DynamicallySkippedTestCount { get; private set; }
public void Dispose() { }
public bool QueueMessage(IMessageSinkMessage message)
{
var testFailed = message as ITestFailed;
if (testFailed != null)
{
var exceptionType = testFailed.ExceptionTypes.FirstOrDefault();
if (exceptionType == typeof(SkipTestException).FullName)
{
DynamicallySkippedTestCount++;
return _innerBus.QueueMessage(new TestSkipped(testFailed.Test, testFailed.Messages.FirstOrDefault()));
}
}
return _innerBus.QueueMessage(message);
}
}
#endif
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment