Commit 65b26d6e authored by Marc Gravell's avatar Marc Gravell

Merge pull request #413 from StackExchange/tests/mysql

MySQL Test Suite for Contrib
parents 8c43e939 e5798034
...@@ -5,7 +5,11 @@ ...@@ -5,7 +5,11 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapper.Contrib.Extensions; using Dapper.Contrib.Extensions;
#if XUNIT2
using FactAttribute = Dapper.Tests.Contrib.SkippableFactAttribute;
#else
using Xunit; using Xunit;
#endif
namespace Dapper.Tests.Contrib namespace Dapper.Tests.Contrib
{ {
......
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
using System.Data.SqlServerCe; using System.Data.SqlServerCe;
using System.Transactions; using System.Transactions;
#endif #endif
#if XUNIT2
using FactAttribute = Dapper.Tests.Contrib.SkippableFactAttribute;
#endif
namespace Dapper.Tests.Contrib namespace Dapper.Tests.Contrib
{ {
......
...@@ -2,13 +2,15 @@ ...@@ -2,13 +2,15 @@
using System.Data; using System.Data;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.IO; using System.IO;
using Xunit;
using Xunit.Sdk;
#if COREFX #if COREFX
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using IDbConnection = System.Data.Common.DbConnection; using IDbConnection = System.Data.Common.DbConnection;
#else #else
using System.Data.SQLite; using System.Data.SQLite;
using System.Data.SqlServerCe; using System.Data.SqlServerCe;
using MySql.Data.MySqlClient;
using SqliteConnection = System.Data.SQLite.SQLiteConnection; using SqliteConnection = System.Data.SQLite.SQLiteConnection;
#endif #endif
...@@ -18,6 +20,11 @@ namespace Dapper.Tests.Contrib ...@@ -18,6 +20,11 @@ namespace Dapper.Tests.Contrib
// the entire set of tests without declarations per method // 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 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 public class SqlServerTestSuite : TestSuite
{ {
const string DbName = "tempdb"; const string DbName = "tempdb";
...@@ -52,6 +59,61 @@ static SqlServerTestSuite() ...@@ -52,6 +59,61 @@ static SqlServerTestSuite()
} }
} }
#if !COREFX
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()
{
if (_skip) throw new SkipTestException("Skipping MySQL Tests - no server.");
return new MySqlConnection(ConnectionString);
}
private static readonly bool _skip;
static MySqlServerTestSuite()
{
try
{
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;
}
}
}
#endif
#if !COREFX && !DNX451 #if !COREFX && !DNX451
// This doesn't work on DNX right now due to: // This doesn't work on DNX right now due to:
// In Visual Studio: Interop loads (works from console, though) // In Visual Studio: Interop loads (works from console, though)
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0", "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0",
"description": "Dapper Contrib Test Suite", "description": "Dapper Contrib Test Suite",
"title": "Dapper.Contrib.Tests", "title": "Dapper.Contrib.Tests",
"version": "1.0.0-*", "version": "1.0.0",
"copyright": "2015 Stack Exchange, Inc.", "copyright": "2015 Stack Exchange, Inc.",
"dependencies": { "dependencies": {
"Dapper": { "Dapper": {
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
"compile": [ "compile": [
"**/*.cs", "**/*.cs",
"../Dapper.Tests/Assert.cs", "../Dapper.Tests/Assert.cs",
"../Dapper.Tests/XunitSkippable.cs",
"../Dapper/TypeExtensions.cs" "../Dapper/TypeExtensions.cs"
], ],
"commands": { "commands": {
...@@ -43,13 +44,14 @@ ...@@ -43,13 +44,14 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.SqlServer.Compact": "4.0.8876.1", "Microsoft.SqlServer.Compact": "4.0.8876.1",
"MySql.Data": "6.9.8",
"System.Data.SQLite.Core": "1.0.98.1", "System.Data.SQLite.Core": "1.0.98.1",
"xunit": "1.9.2" "xunit": "1.9.2"
} }
}, },
"net45": { "net45": {
"compilationOptions": { "compilationOptions": {
"define": [ "ASYNC" ] "define": [ "ASYNC", "XUNIT2" ]
}, },
"frameworkAssemblies": { "frameworkAssemblies": {
"System.Configuration": "4.0.0.0", "System.Configuration": "4.0.0.0",
...@@ -61,13 +63,14 @@ ...@@ -61,13 +63,14 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.SqlServer.Compact": "4.0.8876.1", "Microsoft.SqlServer.Compact": "4.0.8876.1",
"MySql.Data": "6.9.8",
"System.Data.SQLite.Core": "1.0.98.1", "System.Data.SQLite.Core": "1.0.98.1",
"xunit": "2.1.0" "xunit": "2.1.0"
} }
}, },
"dotnet5.4": { "dotnet5.4": {
"compilationOptions": { "compilationOptions": {
"define": [ "ASYNC", "COREFX" ] "define": [ "ASYNC", "COREFX", "XUNIT2" ]
}, },
"dependencies": { "dependencies": {
"Microsoft.CSharp": "4.0.1-*", "Microsoft.CSharp": "4.0.1-*",
...@@ -83,7 +86,7 @@ ...@@ -83,7 +86,7 @@
}, },
"dnx451": { "dnx451": {
"compilationOptions": { "compilationOptions": {
"define": [ "ASYNC" ] "define": [ "ASYNC", "XUNIT2" ]
}, },
"frameworkAssemblies": { "frameworkAssemblies": {
"System.Configuration": "4.0.0.0", "System.Configuration": "4.0.0.0",
...@@ -92,6 +95,7 @@ ...@@ -92,6 +95,7 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.SqlServer.Compact": "4.0.8876.1", "Microsoft.SqlServer.Compact": "4.0.8876.1",
"MySql.Data": "6.9.8",
"System.Data.SQLite.Core": "1.0.98.1", "System.Data.SQLite.Core": "1.0.98.1",
"xunit": "2.1.0", "xunit": "2.1.0",
"xunit.runner.dnx": "2.1.0-*" "xunit.runner.dnx": "2.1.0-*"
...@@ -99,7 +103,7 @@ ...@@ -99,7 +103,7 @@
}, },
"dnxcore50": { "dnxcore50": {
"compilationOptions": { "compilationOptions": {
"define": [ "COREFX", "ASYNC" ] "define": [ "COREFX", "ASYNC", "XUNIT2" ]
}, },
"dependencies": { "dependencies": {
"Microsoft.Data.Sqlite": "1.0.0-rc1-final", "Microsoft.Data.Sqlite": "1.0.0-rc1-final",
......
...@@ -68,14 +68,15 @@ namespace Dapper.Tests ...@@ -68,14 +68,15 @@ namespace Dapper.Tests
{ {
public partial class TestSuite : IDisposable public partial class TestSuite : IDisposable
{ {
public const string LocalConnectionString = "Data Source=.;Initial Catalog=tempdb;Integrated Security=True",
AppveyorConnectionStrng = @"Server=(local)\SQL2014;Database=tempdb;User ID=sa;Password=Password12!",
OleDbConnectionString = "Provider=SQLOLEDB;Data Source=.;Initial Catalog=tempdb;Integrated Security=SSPI";
public static string ConnectionString => public static string ConnectionString =>
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPVEYOR")) !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPVEYOR"))
? AppveyorConnectionStrng ? @"Server=(local)\SQL2014;Database=tempdb;User ID=sa;Password=Password12!"
: LocalConnectionString; : "Data Source=.;Initial Catalog=tempdb;Integrated Security=True";
public static string OleDbConnectionString =>
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPVEYOR"))
? @"Provider=SQLOLEDB;Data Source=(local)\SQL2014;Initial Catalog=tempdb;User Id=sa;Password=Password12!"
: "Provider=SQLOLEDB;Data Source=.;Initial Catalog=tempdb;Integrated Security=SSPI";
public static SqlConnection GetOpenConnection(bool mars = false) public static SqlConnection GetOpenConnection(bool mars = false)
{ {
...@@ -718,10 +719,10 @@ public void ExecuteReader() ...@@ -718,10 +719,10 @@ public void ExecuteReader()
[Fact] [Fact]
public void MultiRSSqlCE() public void MultiRSSqlCE()
{ {
if (File.Exists("Test.sdf")) if (File.Exists("Test.DB.sdf"))
File.Delete("Test.sdf"); File.Delete("Test.DB.sdf");
var cnnStr = "Data Source = Test.sdf;"; var cnnStr = "Data Source = Test.DB.sdf;";
var engine = new SqlCeEngine(cnnStr); var engine = new SqlCeEngine(cnnStr);
engine.CreateDatabase(); engine.CreateDatabase();
......
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