@@ -8,11 +8,11 @@ namespace DotNetCore.CAP.PostgreSql.Test | |||||
private const string DatabaseVariable = "Cap_PostgreSql_DatabaseName"; | private const string DatabaseVariable = "Cap_PostgreSql_DatabaseName"; | ||||
private const string ConnectionStringTemplateVariable = "Cap_PostgreSql_ConnectionStringTemplate"; | private const string ConnectionStringTemplateVariable = "Cap_PostgreSql_ConnectionStringTemplate"; | ||||
private const string MasterDatabaseName = "master"; | |||||
private const string MasterDatabaseName = "postgres"; | |||||
private const string DefaultDatabaseName = @"DotNetCore.CAP.PostgreSql.Test"; | private const string DefaultDatabaseName = @"DotNetCore.CAP.PostgreSql.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=localhost;Database={0};UserId=postgres;Password=123123;"; | |||||
public static string GetDatabaseName() | public static string GetDatabaseName() | ||||
{ | { | ||||
@@ -48,31 +48,21 @@ namespace DotNetCore.CAP.PostgreSql.Test | |||||
using (var connection = ConnectionUtil.CreateConnection(masterConn)) | using (var connection = ConnectionUtil.CreateConnection(masterConn)) | ||||
{ | { | ||||
connection.Execute($@" | connection.Execute($@" | ||||
IF NOT EXISTS (SELECT * FROM sysdatabases WHERE name = N'{databaseName}') | |||||
CREATE DATABASE [{databaseName}];"); | |||||
DROP DATABASE IF EXISTS ""{databaseName}""; | |||||
CREATE DATABASE ""{databaseName}"";"); | |||||
} | } | ||||
} | } | ||||
private void DeleteAllData() | private void DeleteAllData() | ||||
{ | { | ||||
var conn = ConnectionUtil.GetConnectionString(); | var conn = ConnectionUtil.GetConnectionString(); | ||||
using (var connection = new SqlConnection(conn)) | |||||
{ | |||||
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) | |||||
{ | |||||
connection.Execute( | |||||
"sp_MSforeachtable", | |||||
new { command1 = command }, | |||||
commandType: CommandType.StoredProcedure); | |||||
} | |||||
using (var connection = ConnectionUtil.CreateConnection(conn)) | |||||
{ | |||||
connection.Execute($@" | |||||
TRUNCATE TABLE ""cap"".""published""; | |||||
TRUNCATE TABLE ""cap"".""received""; | |||||
TRUNCATE TABLE ""cap"".""queue"";"); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -21,7 +21,7 @@ namespace DotNetCore.CAP.PostgreSql.Test | |||||
[Fact] | [Fact] | ||||
public async Task GetPublishedMessageAsync_Test() | public async Task GetPublishedMessageAsync_Test() | ||||
{ | { | ||||
var sql = @"INSERT INTO ""Cap"".""Published""(""Name"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"") VALUES(@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName);SELECT @@IDENTITY;"; | |||||
var sql = @"INSERT INTO ""cap"".""published""(""Name"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"") VALUES(@Name,@Content,@Retries,@Added,@ExpiresAt,@StatusName) RETURNING ""Id"";"; | |||||
var publishMessage = new CapPublishedMessage | var publishMessage = new CapPublishedMessage | ||||
{ | { | ||||
Name = "PostgreSqlStorageConnectionTest", | Name = "PostgreSqlStorageConnectionTest", | ||||
@@ -42,7 +42,7 @@ namespace DotNetCore.CAP.PostgreSql.Test | |||||
[Fact] | [Fact] | ||||
public async Task FetchNextMessageAsync_Test() | public async Task FetchNextMessageAsync_Test() | ||||
{ | { | ||||
var sql = @"INSERT INTO ""Cap"".""Queue""(""MessageId"",""MessageType"") VALUES(@MessageId,@MessageType);"; | |||||
var sql = @"INSERT INTO ""cap"".""queue""(""MessageId"",""MessageType"") VALUES(@MessageId,@MessageType);"; | |||||
var queue = new CapQueue | var queue = new CapQueue | ||||
{ | { | ||||
MessageId = 3333, | MessageId = 3333, | ||||
@@ -87,8 +87,8 @@ namespace DotNetCore.CAP.PostgreSql.Test | |||||
{ | { | ||||
var sql = $@" | var sql = $@" | ||||
INSERT INTO ""Cap"".""Received""(""Name"",""Group"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"") OUTPUT INSERTED.Id | |||||
VALUES(@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName);"; | |||||
INSERT INTO ""cap"".""received""(""Name"",""Group"",""Content"",""Retries"",""Added"",""ExpiresAt"",""StatusName"") | |||||
VALUES(@Name,@Group,@Content,@Retries,@Added,@ExpiresAt,@StatusName) RETURNING ""Id"";"; | |||||
var receivedMessage = new CapReceivedMessage | var receivedMessage = new CapReceivedMessage | ||||
{ | { | ||||
Name = "PostgreSqlStorageConnectionTest", | Name = "PostgreSqlStorageConnectionTest", | ||||
@@ -3,67 +3,44 @@ using Dapper; | |||||
namespace DotNetCore.CAP.PostgreSql.Test | namespace DotNetCore.CAP.PostgreSql.Test | ||||
{ | { | ||||
//[Collection("postgresql")] | |||||
[Collection("postgresql")] | |||||
public class SqlServerStorageTest : DatabaseTestHost | public class SqlServerStorageTest : DatabaseTestHost | ||||
{ | { | ||||
private readonly string _dbName; | private readonly string _dbName; | ||||
private readonly string _masterDbConnectionString; | private readonly string _masterDbConnectionString; | ||||
private readonly string _dbConnectionString; | |||||
public SqlServerStorageTest() | public SqlServerStorageTest() | ||||
{ | { | ||||
_dbName = ConnectionUtil.GetDatabaseName(); | _dbName = ConnectionUtil.GetDatabaseName(); | ||||
_masterDbConnectionString = ConnectionUtil.GetMasterConnectionString(); | _masterDbConnectionString = ConnectionUtil.GetMasterConnectionString(); | ||||
_dbConnectionString = ConnectionUtil.GetConnectionString(); | |||||
} | } | ||||
//[Fact] | |||||
[Fact] | |||||
public void Database_IsExists() | public void Database_IsExists() | ||||
{ | { | ||||
using (var connection = ConnectionUtil.CreateConnection(_masterDbConnectionString)) | using (var connection = ConnectionUtil.CreateConnection(_masterDbConnectionString)) | ||||
{ | { | ||||
var databaseName = ConnectionUtil.GetDatabaseName(); | var databaseName = ConnectionUtil.GetDatabaseName(); | ||||
var sql = $@"SELECT SCHEMA_NAME FROM SCHEMATA WHERE SCHEMA_NAME = '{databaseName}'"; | |||||
var sql = $@"select * from pg_database where datname = '{databaseName}'"; | |||||
var result = connection.QueryFirstOrDefault<string>(sql); | var result = connection.QueryFirstOrDefault<string>(sql); | ||||
Assert.NotNull(result); | Assert.NotNull(result); | ||||
Assert.True(databaseName.Equals(result, System.StringComparison.CurrentCultureIgnoreCase)); | Assert.True(databaseName.Equals(result, System.StringComparison.CurrentCultureIgnoreCase)); | ||||
} | } | ||||
} | } | ||||
//[Fact] | |||||
public void DatabaseTable_Published_IsExists() | |||||
[Theory] | |||||
[InlineData("cap.published")] | |||||
[InlineData("cap.queue")] | |||||
[InlineData("cap.received")] | |||||
public void DatabaseTable_IsExists(string tableName) | |||||
{ | { | ||||
var tableName = "cap.published"; | |||||
using (var connection = ConnectionUtil.CreateConnection(_masterDbConnectionString)) | |||||
{ | |||||
var sql = $"SELECT TABLE_NAME FROM `TABLES` WHERE TABLE_SCHEMA='{_dbName}' AND TABLE_NAME = '{tableName}'"; | |||||
var result = connection.QueryFirstOrDefault<string>(sql); | |||||
Assert.NotNull(result); | |||||
Assert.Equal(tableName, result); | |||||
} | |||||
} | |||||
//[Fact] | |||||
public void DatabaseTable_Queue_IsExists() | |||||
{ | |||||
var tableName = "cap.queue"; | |||||
using (var connection = ConnectionUtil.CreateConnection(_masterDbConnectionString)) | |||||
using (var connection = ConnectionUtil.CreateConnection(_dbConnectionString)) | |||||
{ | { | ||||
var sql = $"SELECT TABLE_NAME FROM `TABLES` WHERE TABLE_SCHEMA='{_dbName}' AND TABLE_NAME = '{tableName}'"; | |||||
var result = connection.QueryFirstOrDefault<string>(sql); | |||||
Assert.NotNull(result); | |||||
Assert.Equal(tableName, result); | |||||
} | |||||
} | |||||
//[Fact] | |||||
public void DatabaseTable_Received_IsExists() | |||||
{ | |||||
var tableName = "cap.received"; | |||||
using (var connection = ConnectionUtil.CreateConnection(_masterDbConnectionString)) | |||||
{ | |||||
var sql = $"SELECT TABLE_NAME FROM `TABLES` WHERE TABLE_SCHEMA='{_dbName}' AND TABLE_NAME = '{tableName}'"; | |||||
var result = connection.QueryFirstOrDefault<string>(sql); | |||||
Assert.NotNull(result); | |||||
Assert.Equal(tableName, result); | |||||
var sql = $"SELECT to_regclass('{tableName}') is not null;"; | |||||
var result = connection.QueryFirstOrDefault<bool>(sql); | |||||
Assert.True(result); | |||||
} | } | ||||
} | } | ||||
} | } | ||||