Browse Source

update UnitTest.

master
yangxiaodong 7 years ago
parent
commit
93d2e5c372
9 changed files with 487 additions and 39 deletions
  1. +7
    -7
      CAP.sln
  2. +11
    -0
      test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs
  3. +47
    -0
      test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs
  4. +62
    -0
      test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs
  5. +48
    -0
      test/DotNetCore.CAP.SqlServer.Test/DotNetCore.CAP.SqlServer.Test.csproj
  6. +168
    -0
      test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs
  7. +18
    -0
      test/DotNetCore.CAP.SqlServer.Test/Properties/AssemblyInfo.cs
  8. +97
    -0
      test/DotNetCore.CAP.SqlServer.Test/TestHost.cs
  9. +29
    -32
      test/DotNetCore.CAP.Test/CAP.BuilderTest.cs

+ 7
- 7
CAP.sln View File

@@ -53,12 +53,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{10C0818D
build\version.props = build\version.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.EntityFrameworkCore.Test", "test\DotNetCore.CAP.EntityFrameworkCore.Test\DotNetCore.CAP.EntityFrameworkCore.Test.csproj", "{69370370-9873-4D6A-965D-D1E16694047D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.Test", "test\DotNetCore.CAP.Test\DotNetCore.CAP.Test.csproj", "{F608B509-A99B-4AC7-8227-42051DD4A578}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.SqlServer", "src\DotNetCore.CAP.SqlServer\DotNetCore.CAP.SqlServer.csproj", "{3B577468-6792-4EF1-9237-15180B176A24}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.SqlServer.Test", "test\DotNetCore.CAP.SqlServer.Test\DotNetCore.CAP.SqlServer.Test.csproj", "{DA00FA38-C4B9-4F55-8756-D480FBC1084F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -81,10 +81,6 @@ Global
{9961B80E-0718-4280-B2A0-271B003DE26B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9961B80E-0718-4280-B2A0-271B003DE26B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9961B80E-0718-4280-B2A0-271B003DE26B}.Release|Any CPU.Build.0 = Release|Any CPU
{69370370-9873-4D6A-965D-D1E16694047D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69370370-9873-4D6A-965D-D1E16694047D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69370370-9873-4D6A-965D-D1E16694047D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69370370-9873-4D6A-965D-D1E16694047D}.Release|Any CPU.Build.0 = Release|Any CPU
{F608B509-A99B-4AC7-8227-42051DD4A578}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F608B509-A99B-4AC7-8227-42051DD4A578}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F608B509-A99B-4AC7-8227-42051DD4A578}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -92,6 +88,10 @@ Global
{3B577468-6792-4EF1-9237-15180B176A24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B577468-6792-4EF1-9237-15180B176A24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B577468-6792-4EF1-9237-15180B176A24}.Release|Any CPU.Build.0 = Release|Any CPU
{DA00FA38-C4B9-4F55-8756-D480FBC1084F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA00FA38-C4B9-4F55-8756-D480FBC1084F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA00FA38-C4B9-4F55-8756-D480FBC1084F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA00FA38-C4B9-4F55-8756-D480FBC1084F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -102,8 +102,8 @@ Global
{2F095ED9-5BC9-4512-9013-A47685FB2508} = {3A6B6931-A123-477A-9469-8B468B5385AF}
{C42CDE33-0878-4BA0-96F2-4CB7C8FDEAAD} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{9961B80E-0718-4280-B2A0-271B003DE26B} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{69370370-9873-4D6A-965D-D1E16694047D} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0}
{F608B509-A99B-4AC7-8227-42051DD4A578} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0}
{3B577468-6792-4EF1-9237-15180B176A24} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{DA00FA38-C4B9-4F55-8756-D480FBC1084F} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0}
EndGlobalSection
EndGlobal

+ 11
- 0
test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public class CapMessageStoreTest
{
}
}

+ 47
- 0
test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs View File

@@ -0,0 +1,47 @@
using System;
using System.Data.SqlClient;

namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public static class ConnectionUtil
{
private const string DatabaseVariable = "Cap_SqlServer_DatabaseName";
private const string ConnectionStringTemplateVariable = "Cap_SqlServer_ConnectionStringTemplate";

private const string MasterDatabaseName = "master";
private const string DefaultDatabaseName = @"DotNetCore.CAP.EntityFrameworkCore.Test";

private const string DefaultConnectionStringTemplate =
@"Server=192.168.2.206;Initial Catalog={0};User Id=sa;Password=123123;MultipleActiveResultSets=True";

public static string GetDatabaseName()
{
return Environment.GetEnvironmentVariable(DatabaseVariable) ?? DefaultDatabaseName;
}

public static string GetMasterConnectionString()
{
return string.Format(GetConnectionStringTemplate(), MasterDatabaseName);
}

public static string GetConnectionString()
{
return string.Format(GetConnectionStringTemplate(), GetDatabaseName());
}

private static string GetConnectionStringTemplate()
{
return
Environment.GetEnvironmentVariable(ConnectionStringTemplateVariable) ??
DefaultConnectionStringTemplate;
}

public static SqlConnection CreateConnection(string connectionString = null)
{
connectionString = connectionString ?? GetConnectionString();
var connection = new SqlConnection(connectionString);
connection.Open();
return connection;
}
}
}

+ 62
- 0
test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs View File

@@ -0,0 +1,62 @@
using System.Data;
using System.Threading.Tasks;
using Dapper;
using Microsoft.EntityFrameworkCore;

namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
//public abstract class DatabaseTestHost : TestHost
//{
// private static bool _sqlObjectInstalled;

// protected override void PostBuildServices()
// {
// base.PostBuildServices();
// InitializeDatabase();
// }

// public override void Dispose()
// {
// DeleteAllData();
// base.Dispose();
// }

// private void InitializeDatabase()
// {
// if (!_sqlObjectInstalled)
// {
// using (CreateScope())
// {
// var context = GetService<TestDbContext>();
// context.Database.EnsureDeleted();
// context.Database.Migrate();
// _sqlObjectInstalled = true;
// }
// }
// }

// private void DeleteAllData()
// {
// using (CreateScope())
// {
// var context = GetService<TestDbContext>();

// var commands = new[]
// {
// "DISABLE TRIGGER ALL ON ?",
// "ALTER TABLE ? NOCHECK CONSTRAINT ALL",
// "DELETE FROM ?",
// "ALTER TABLE ? CHECK CONSTRAINT ALL",
// "ENABLE TRIGGER ALL ON ?"
// };
// foreach (var command in commands)
// {
// context.Database.GetDbConnection().Execute(
// "sp_MSforeachtable",
// new {command1 = command},
// commandType: CommandType.StoredProcedure);
// }
// }
// }
//}
}

+ 48
- 0
test/DotNetCore.CAP.SqlServer.Test/DotNetCore.CAP.SqlServer.Test.csproj View File

@@ -0,0 +1,48 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AssemblyName>DotNetCore.CAP.SqlServer.Test</AssemblyName>
<PackageId>DotNetCore.CAP.SqlServer.Test</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50;portable-net451+win8</PackageTargetFallback>
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Shared\*.cs" Exclude="bin\**;obj\**;**\*.xproj;packages\**" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DotNetCore.CAP.SqlServer\DotNetCore.CAP.SqlServer.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP\DotNetCore.CAP.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
<PackageReference Include="Moq" Version="4.7.63" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
<PackageReference Include="System.Data.SqlClient" Version="4.3.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>

+ 168
- 0
test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs View File

@@ -0,0 +1,168 @@
namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
//public class EFMessageStoreTest : DatabaseTestHost
//{
// [Fact]
// public void CanCreateSentMessageUsingEF()
// {
// using (var db = CreateContext())
// {
// var guid = Guid.NewGuid().ToString();
// var message = new CapPublishedMessage
// {
// Id = guid,
// Content = "this is message body",
// StatusName = StatusName.Enqueued
// };
// db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

// db.SaveChanges();

// Assert.True(db.CapSentMessages.Any(u => u.Id == guid));
// Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued));
// }
// }

// [Fact]
// public void CanUpdateSentMessageUsingEF()
// {
// using (var db = CreateContext())
// {
// var guid = Guid.NewGuid().ToString();
// var message = new CapPublishedMessage
// {
// Id = guid,
// Content = "this is message body",
// StatusName = StatusName.Enqueued
// };
// db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

// db.SaveChanges();

// var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
// Assert.NotNull(selectedMessage);

// selectedMessage.StatusName = StatusName.Succeeded;
// selectedMessage.Content = "Test";
// db.SaveChanges();

// selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded);
// Assert.NotNull(selectedMessage);
// Assert.True(selectedMessage.Content == "Test");
// }
// }

// [Fact]
// public void CanRemoveSentMessageUsingEF()
// {
// using (var db = CreateContext())
// {
// var guid = Guid.NewGuid().ToString();
// var message = new CapPublishedMessage
// {
// Id = guid,
// Content = "this is message body",
// StatusName = StatusName.Enqueued
// };
// db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

// db.SaveChanges();

// var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
// Assert.NotNull(selectedMessage);

// db.CapSentMessages.Remove(selectedMessage);
// db.SaveChanges();
// selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
// Assert.Null(selectedMessage);
// }
// }

// [Fact]
// public void CanCreateReceivedMessageUsingEF()
// {
// using (var db = CreateContext())
// {
// var guid = Guid.NewGuid().ToString();
// var message = new CapReceivedMessage
// {
// Id = guid,
// Content = "this is message body",
// StatusName = StatusName.Enqueued
// };
// db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

// db.SaveChanges();

// Assert.True(db.CapReceivedMessages.Any(u => u.Id == guid));
// Assert.NotNull(db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued));
// }
// }

// [Fact]
// public void CanUpdateReceivedMessageUsingEF()
// {
// using (var db = CreateContext())
// {
// var guid = Guid.NewGuid().ToString();
// var message = new CapReceivedMessage
// {
// Id = guid,
// Content = "this is message body",
// StatusName = StatusName.Enqueued
// };
// db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

// db.SaveChanges();

// var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
// Assert.NotNull(selectedMessage);

// selectedMessage.StatusName = StatusName.Succeeded;
// selectedMessage.Content = "Test";
// db.SaveChanges();

// selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded);
// Assert.NotNull(selectedMessage);
// Assert.True(selectedMessage.Content == "Test");
// }
// }

// [Fact]
// public void CanRemoveReceivedMessageUsingEF()
// {
// using (var db = CreateContext())
// {
// var guid = Guid.NewGuid().ToString();
// var message = new CapReceivedMessage
// {
// Id = guid,
// Content = "this is message body",
// StatusName = StatusName.Enqueued
// };
// db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

// db.SaveChanges();

// var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
// Assert.NotNull(selectedMessage);

// db.CapReceivedMessages.Remove(selectedMessage);
// db.SaveChanges();
// selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
// Assert.Null(selectedMessage);
// }
// }

// public TestDbContext CreateContext(bool delete = false)
// {
// var db = Provider.GetRequiredService<TestDbContext>();
// if (delete)
// {
// db.Database.EnsureDeleted();
// }
// db.Database.EnsureCreated();
// return db;
// }
//}
}

+ 18
- 0
test/DotNetCore.CAP.SqlServer.Test/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,18 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DotNetCore.CAP.EntityFrameworkCore.Test")]
[assembly: AssemblyTrademark("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7442c942-1ddc-40e4-8f1b-654e721eaa45")]

+ 97
- 0
test/DotNetCore.CAP.SqlServer.Test/TestHost.cs View File

@@ -0,0 +1,97 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public abstract class TestHost : IDisposable
{
protected IServiceCollection _services;
private IServiceProvider _provider;
private IServiceProvider _scopedProvider;

public TestHost()
{
CreateServiceCollection();
PreBuildServices();
BuildServices();
PostBuildServices();
}

protected IServiceProvider Provider => _scopedProvider ?? _provider;

private void CreateServiceCollection()
{
var services = new ServiceCollection();

services.AddOptions();
services.AddLogging();

var connectionString = ConnectionUtil.GetConnectionString();
//services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString });
//services.AddDbContext<TestDbContext>(options => options.UseSqlServer(connectionString));

_services = services;
}

protected virtual void PreBuildServices()
{
}

private void BuildServices()
{
_provider = _services.BuildServiceProvider();
}

protected virtual void PostBuildServices()
{
}

public IDisposable CreateScope()
{
var scope = CreateScope(_provider);
var loc = scope.ServiceProvider;
_scopedProvider = loc;
return new DelegateDisposable(() =>
{
if (_scopedProvider == loc)
{
_scopedProvider = null;
}
scope.Dispose();
});
}

public IServiceScope CreateScope(IServiceProvider provider)
{
var scope = provider.GetService<IServiceScopeFactory>().CreateScope();
return scope;
}

public T GetService<T>() => Provider.GetService<T>();

public T Ensure<T>(ref T service)
where T : class
=> service ?? (service = GetService<T>());

public virtual void Dispose()
{
(_provider as IDisposable)?.Dispose();
}

private class DelegateDisposable : IDisposable
{
private Action _dispose;

public DelegateDisposable(Action dispose)
{
_dispose = dispose;
}

public void Dispose()
{
_dispose();
}
}
}
}

+ 29
- 32
test/DotNetCore.CAP.Test/CAP.BuilderTest.cs View File

@@ -1,8 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using DotNetCore.CAP.Processor;
using DotNetCore.CAP.Models;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using System.Data;
@@ -11,42 +8,42 @@ namespace DotNetCore.CAP.Test
{
public class CapBuilderTest
{
//[Fact]
//public void CanOverrideMessageStore()
//{
// var services = new ServiceCollection();
// services.AddCap().AddMessageStore<MyMessageStore>();

// var thingy = services.BuildServiceProvider()
// .GetRequiredService<ICapMessageStore>() as MyMessageStore;

// Assert.NotNull(thingy);
////}
[Fact]
public void CanCreateInstanceAndGetService()
{
var services = new ServiceCollection();
services.AddSingleton<ICapPublisher, MyProducerService>();
var builder = new CapBuilder(services);
Assert.NotNull(builder);

//[Fact]
//public void CanOverrideJobs()
//{
// var services = new ServiceCollection();
// services.AddCap().AddJobs<MyJobTest>();
var count = builder.Services.Count;
Assert.Equal(1, count);
}

// var thingy = services.BuildServiceProvider()
// .GetRequiredService<IJob>() as MyJobTest;
[Fact]
public void CanAddCapService()
{
var services = new ServiceCollection();
services.AddCap(x => { });
var builder = services.BuildServiceProvider();

// Assert.NotNull(thingy);
//}
var markService = builder.GetService<CapMarkerService>();
Assert.NotNull(markService);
}

//[Fact]
//public void CanOverrideProducerService()
//{
// var services = new ServiceCollection();
// services.AddCap(x=> { });

// var thingy = services.BuildServiceProvider()
// .GetRequiredService<ICapPublisher>() as MyProducerService;
[Fact]
public void CanOverridePublishService()
{
var services = new ServiceCollection();
services.AddCap(x => { }).AddProducerService<MyProducerService>();

// Assert.NotNull(thingy);
//}
var thingy = services.BuildServiceProvider()
.GetRequiredService<ICapPublisher>() as MyProducerService;

Assert.NotNull(thingy);
}

private class MyProducerService : ICapPublisher
{
@@ -69,6 +66,6 @@ namespace DotNetCore.CAP.Test
{
throw new NotImplementedException();
}
}
}
}
}

Loading…
Cancel
Save