@@ -9,7 +9,7 @@ namespace DotNetCore.CAP.SqlServer.Test | |||||
private const string ConnectionStringTemplateVariable = "Cap_SqlServer_ConnectionStringTemplate"; | private const string ConnectionStringTemplateVariable = "Cap_SqlServer_ConnectionStringTemplate"; | ||||
private const string MasterDatabaseName = "master"; | private const string MasterDatabaseName = "master"; | ||||
private const string DefaultDatabaseName = @"DotNetCore.CAP.EntityFrameworkCore.Test"; | |||||
private const string DefaultDatabaseName = @"DotNetCore.CAP.SqlServer.Test"; | |||||
private const string DefaultConnectionStringTemplate = | private const string DefaultConnectionStringTemplate = | ||||
@"Server=192.168.2.206;Initial Catalog={0};User Id=sa;Password=123123;MultipleActiveResultSets=True"; | @"Server=192.168.2.206;Initial Catalog={0};User Id=sa;Password=123123;MultipleActiveResultSets=True"; | ||||
@@ -1,4 +1,5 @@ | |||||
using System.Data; | using System.Data; | ||||
using System.Data.SqlClient; | |||||
using System.Threading; | using System.Threading; | ||||
using Dapper; | using Dapper; | ||||
using Microsoft.EntityFrameworkCore; | using Microsoft.EntityFrameworkCore; | ||||
@@ -29,12 +30,25 @@ namespace DotNetCore.CAP.SqlServer.Test | |||||
{ | { | ||||
var storage = GetService<SqlServerStorage>(); | var storage = GetService<SqlServerStorage>(); | ||||
var token = new CancellationTokenSource().Token; | var token = new CancellationTokenSource().Token; | ||||
CreateDatabase(); | |||||
storage.InitializeAsync(token).Wait(); | storage.InitializeAsync(token).Wait(); | ||||
_sqlObjectInstalled = true; | _sqlObjectInstalled = true; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private void CreateDatabase() | |||||
{ | |||||
var masterConn = ConnectionUtil.GetMasterConnectionString(); | |||||
var databaseName = ConnectionUtil.GetDatabaseName(); | |||||
using (var connection = ConnectionUtil.CreateConnection(masterConn)) | |||||
{ | |||||
connection.Execute($@" | |||||
IF NOT EXISTS (SELECT * FROM sysdatabases WHERE name = N'{databaseName}') | |||||
CREATE DATABASE [{databaseName}];"); | |||||
} | |||||
} | |||||
private void DeleteAllData() | private void DeleteAllData() | ||||
{ | { | ||||
using (CreateScope()) | using (CreateScope()) | ||||
@@ -0,0 +1,134 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using Dapper; | |||||
using DotNetCore.CAP.Infrastructure; | |||||
using DotNetCore.CAP.Models; | |||||
using Xunit; | |||||
namespace DotNetCore.CAP.SqlServer.Test | |||||
{ | |||||
public class SqlServerStorageConnectionTest : DatabaseTestHost | |||||
{ | |||||
private SqlServerStorageConnection _storage; | |||||
public SqlServerStorageConnectionTest() | |||||
{ | |||||
var options = GetService<SqlServerOptions>(); | |||||
_storage = new SqlServerStorageConnection(options); | |||||
} | |||||
[Fact] | |||||
public async void GetPublishedMessageAsync_Test() | |||||
{ | |||||
var sql = "INSERT INTO [Cap].[Published]([Name],[Content],[Retries],[Added],[ExpiresAt],[StatusName]) OUTPUT INSERTED.Id VALUES(@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);"; | |||||
var publishMessage = new CapPublishedMessage | |||||
{ | |||||
Name = "SqlServerStorageConnectionTest", | |||||
Content = "", | |||||
StatusName = StatusName.Scheduled | |||||
}; | |||||
var insertedId = default(int); | |||||
using (var connection = ConnectionUtil.CreateConnection()) | |||||
{ | |||||
insertedId = connection.QueryFirst<int>(sql, publishMessage); | |||||
} | |||||
var message = await _storage.GetPublishedMessageAsync(insertedId); | |||||
Assert.NotNull(message); | |||||
Assert.Equal("SqlServerStorageConnectionTest", message.Name); | |||||
Assert.Equal(StatusName.Scheduled, message.StatusName); | |||||
} | |||||
[Fact] | |||||
public async void FetchNextMessageAsync_Test() | |||||
{ | |||||
var sql = "INSERT INTO [Cap].[Queue]([MessageId],[MessageType]) VALUES(@MessageId,@MessageType);"; | |||||
var queue = new CapQueue | |||||
{ | |||||
MessageId = 3333, | |||||
MessageType = MessageType.Publish | |||||
}; | |||||
using (var connection = ConnectionUtil.CreateConnection()) | |||||
{ | |||||
connection.Execute(sql, queue); | |||||
} | |||||
var fetchedMessage = await _storage.FetchNextMessageAsync(); | |||||
fetchedMessage.Dispose(); | |||||
Assert.NotNull(fetchedMessage); | |||||
Assert.Equal(MessageType.Publish, fetchedMessage.MessageType); | |||||
Assert.Equal(3333, fetchedMessage.MessageId); | |||||
} | |||||
[Fact] | |||||
public async void StoreReceivedMessageAsync_Test() | |||||
{ | |||||
var receivedMessage = new CapReceivedMessage | |||||
{ | |||||
Name = "SqlServerStorageConnectionTest", | |||||
Content = "", | |||||
Group = "mygroup", | |||||
StatusName = StatusName.Scheduled | |||||
}; | |||||
Exception exception = null; | |||||
try | |||||
{ | |||||
await _storage.StoreReceivedMessageAsync(receivedMessage); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
exception = ex; | |||||
} | |||||
Assert.Null(exception); | |||||
} | |||||
[Fact] | |||||
public async void GetReceivedMessageAsync_Test() | |||||
{ | |||||
var sql = $@" | |||||
INSERT INTO [Cap].[Received]([Name],[Group],[Content],[Retries],[Added],[ExpiresAt],[StatusName]) OUTPUT INSERTED.Id | |||||
VALUES(@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);"; | |||||
var receivedMessage = new CapReceivedMessage | |||||
{ | |||||
Name = "SqlServerStorageConnectionTest", | |||||
Content = "", | |||||
Group = "mygroup", | |||||
StatusName = StatusName.Scheduled | |||||
}; | |||||
var insertedId = default(int); | |||||
using (var connection = ConnectionUtil.CreateConnection()) | |||||
{ | |||||
insertedId = connection.QueryFirst<int>(sql, receivedMessage); | |||||
} | |||||
var message = await _storage.GetReceivedMessageAsync(insertedId); | |||||
Assert.NotNull(message); | |||||
Assert.Equal(StatusName.Scheduled, message.StatusName); | |||||
Assert.Equal("SqlServerStorageConnectionTest", message.Name); | |||||
Assert.Equal("mygroup", message.Group); | |||||
} | |||||
[Fact] | |||||
public async void GetNextReceviedMessageToBeEnqueuedAsync_Test() | |||||
{ | |||||
var receivedMessage = new CapReceivedMessage | |||||
{ | |||||
Name = "SqlServerStorageConnectionTest", | |||||
Content = "", | |||||
Group = "mygroup", | |||||
StatusName = StatusName.Scheduled | |||||
}; | |||||
await _storage.StoreReceivedMessageAsync(receivedMessage); | |||||
var message = await _storage.GetNextReceviedMessageToBeEnqueuedAsync(); | |||||
Assert.NotNull(message); | |||||
Assert.Equal(StatusName.Scheduled, message.StatusName); | |||||
Assert.Equal("SqlServerStorageConnectionTest", message.Name); | |||||
Assert.Equal("mygroup", message.Group); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,70 @@ | |||||
using Xunit; | |||||
using Dapper; | |||||
namespace DotNetCore.CAP.SqlServer.Test | |||||
{ | |||||
public class SqlServerStorageTest : DatabaseTestHost | |||||
{ | |||||
[Fact] | |||||
public void Database_IsExists() | |||||
{ | |||||
var master = ConnectionUtil.GetMasterConnectionString(); | |||||
using (var connection = ConnectionUtil.CreateConnection(master)) | |||||
{ | |||||
var databaseName = ConnectionUtil.GetDatabaseName(); | |||||
var sql = $@" | |||||
IF EXISTS (SELECT * FROM sysdatabases WHERE name = N'{databaseName}') | |||||
SELECT 'True' | |||||
ELSE | |||||
SELECT 'False'"; | |||||
var result = connection.QueryFirst<bool>(sql); | |||||
Assert.Equal(true, result); | |||||
} | |||||
} | |||||
[Fact] | |||||
public void DatabaseTable_Published_IsExists() | |||||
{ | |||||
using (var connection = ConnectionUtil.CreateConnection()) | |||||
{ | |||||
var sql = @" | |||||
IF OBJECT_ID(N'[CAP].[Published]',N'U') IS NOT NULL | |||||
SELECT 'True' | |||||
ELSE | |||||
SELECT 'False'"; | |||||
var result = connection.QueryFirst<bool>(sql); | |||||
Assert.Equal(true, result); | |||||
} | |||||
} | |||||
[Fact] | |||||
public void DatabaseTable_Queue_IsExists() | |||||
{ | |||||
using (var connection = ConnectionUtil.CreateConnection()) | |||||
{ | |||||
var sql = @" | |||||
IF OBJECT_ID(N'[CAP].[Queue]',N'U') IS NOT NULL | |||||
SELECT 'True' | |||||
ELSE | |||||
SELECT 'False'"; | |||||
var result = connection.QueryFirst<bool>(sql); | |||||
Assert.Equal(true, result); | |||||
} | |||||
} | |||||
[Fact] | |||||
public void DatabaseTable_Received_IsExists() | |||||
{ | |||||
using (var connection = ConnectionUtil.CreateConnection()) | |||||
{ | |||||
var sql = @" | |||||
IF OBJECT_ID(N'[CAP].[Received]',N'U') IS NOT NULL | |||||
SELECT 'True' | |||||
ELSE | |||||
SELECT 'False'"; | |||||
var result = connection.QueryFirst<bool>(sql); | |||||
Assert.Equal(true, result); | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -7,6 +7,7 @@ namespace DotNetCore.CAP.SqlServer.Test | |||||
public abstract class TestHost : IDisposable | public abstract class TestHost : IDisposable | ||||
{ | { | ||||
protected IServiceCollection _services; | protected IServiceCollection _services; | ||||
protected string _connectionString; | |||||
private IServiceProvider _provider; | private IServiceProvider _provider; | ||||
private IServiceProvider _scopedProvider; | private IServiceProvider _scopedProvider; | ||||
@@ -27,10 +28,10 @@ namespace DotNetCore.CAP.SqlServer.Test | |||||
services.AddOptions(); | services.AddOptions(); | ||||
services.AddLogging(); | services.AddLogging(); | ||||
var connectionString = ConnectionUtil.GetConnectionString(); | |||||
services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString }); | |||||
_connectionString = ConnectionUtil.GetConnectionString(); | |||||
services.AddSingleton(new SqlServerOptions { ConnectionString = _connectionString }); | |||||
services.AddSingleton<SqlServerStorage>(); | services.AddSingleton<SqlServerStorage>(); | ||||
services.AddDbContext<TestDbContext>(options => options.UseSqlServer(connectionString)); | |||||
services.AddDbContext<TestDbContext>(options => options.UseSqlServer(_connectionString)); | |||||
_services = services; | _services = services; | ||||
} | } | ||||