-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
112,639 additions
and
4 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
DbTools/DbTools.Simple.Nuget.Tests/DbTools.Simple.Nuget.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
<LangVersion>8</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DbTools.Simple" Version="1.0.6" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
135 changes: 135 additions & 0 deletions
135
DbTools/DbTools.Simple.Nuget.Tests/Managers/TestCommonDbManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using DbTools.Core; | ||
using DbTools.Core.Managers; | ||
using DbTools.Simple.Factories; | ||
using DbTools.Simple.Utils; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
|
||
namespace DbTools.Simple.Nuget.Tests.Managers | ||
{ | ||
public class TestCommonDbManager | ||
{ | ||
[Theory] | ||
[InlineData(DbEngine.MySql, false, "root", "123", true)] | ||
[InlineData(DbEngine.MySql, false, "root", "123", false)] | ||
private void TestCreateLongDataWithNupkgLib(DbEngine dbEngine, bool useIntegratedSecurity, string userName, string password, bool isAsync) | ||
{ | ||
IDbManager dbManager = CreateTestDbManager(dbEngine); | ||
string connectionString = BuildConnectionString(dbEngine, useIntegratedSecurity, userName, password); | ||
dbManager.CreateDatabase(connectionString, true); | ||
string createTablesCmd = File.ReadAllText(Path.GetFullPath(CreateStructureForLongDataTestScriptFile)); | ||
string insertDataCmd = File.ReadAllText(Path.GetFullPath(InsertDataForLongDataTestScriptFile)); | ||
ExecuteScriptAndCheck(dbManager, connectionString, createTablesCmd, isAsync); | ||
ExecuteScriptAndCheck(dbManager, connectionString, insertDataCmd, isAsync); | ||
} | ||
|
||
private void ExecuteScriptAndCheck(IDbManager dbManager, string connectionString, string cmd, bool isAsync) | ||
{ | ||
bool result = false; | ||
if (isAsync) | ||
{ | ||
Task<bool> executionTask = dbManager.ExecuteNonQueryAsync(connectionString, cmd); | ||
executionTask.Wait(); | ||
result = executionTask.Result; | ||
} | ||
else result = dbManager.ExecuteNonQuery(connectionString, cmd); | ||
Assert.True(result); | ||
} | ||
|
||
private IDbManager CreateTestDbManager(DbEngine dbEngine) | ||
{ | ||
return DbManagerFactory.Create(dbEngine, _loggerFactory); | ||
} | ||
|
||
private string BuildConnectionString(DbEngine dbEngine, bool useIntegratedSecurity, string userName, string password) | ||
{ | ||
Tuple<string, string> hostAndDatabase = _hostAndDatabaseOptions[dbEngine]; | ||
IDictionary<string, string> options = new Dictionary<string, string>(); | ||
options.Add(DbParametersKeys.HostKey, hostAndDatabase.Item1); | ||
options.Add(DbParametersKeys.DatabaseKey, hostAndDatabase.Item2); | ||
options.Add(DbParametersKeys.UseIntegratedSecurityKey, useIntegratedSecurity.ToString()); | ||
options.Add(DbParametersKeys.LoginKey, userName); | ||
options.Add(DbParametersKeys.PasswordKey, password); | ||
return ConnectionStringBuilder.Build(dbEngine, options); | ||
} | ||
|
||
/*private void CheckDatabaseExists(IDbManager dbManager, DbEngine dbEngine, string connectionString, bool expected) | ||
{ | ||
string cmd = null; | ||
string sysConnectionString = null; | ||
string dbName = _hostAndDatabaseOptions[dbEngine].Item2; | ||
if (dbEngine == DbEngine.SqlServer) | ||
{ | ||
sysConnectionString = ConnectionStringHelper.GetSqlServerMasterConnectionString(connectionString); | ||
cmd = string.Format(SelectDatabaseTemplate, "name", "master.dbo.sysdatabases", $"N'{dbName}'"); | ||
} | ||
if (dbEngine == DbEngine.MySql) | ||
{ | ||
sysConnectionString = ConnectionStringHelper.GetMySqlSysDbConnectionString(connectionString); | ||
cmd = string.Format(SelectDatabaseTemplate, "SCHEMA_NAME", "INFORMATION_SCHEMA.SCHEMATA", $"'{dbName.ToLower()}'"); | ||
} | ||
if (dbEngine == DbEngine.PostgresSql) | ||
{ | ||
sysConnectionString = ConnectionStringHelper.GetPostgresSqlSysDbConnectionString(connectionString); | ||
cmd = string.Format(SelectDatabaseTemplate, "datname", "pg_database", $"'{dbName.ToLower()}'"); | ||
} | ||
if (dbEngine == DbEngine.SqLite) | ||
{ | ||
Assert.Equal(expected, File.Exists(dbName)); | ||
return; | ||
} | ||
if (cmd != null) | ||
{ | ||
string result = string.Empty; | ||
Tuple<IDataReader, IDbConnection> reader = dbManager.ExecuteDbReader(sysConnectionString, cmd); | ||
while (reader.Item1.Read()) | ||
result = reader.Item1.GetString(0); | ||
reader.Item1.Close(); | ||
reader.Item1.Dispose(); | ||
reader.Item2.Close(); | ||
if (expected) | ||
Assert.Equal(dbName.ToLower(), result.ToLower()); | ||
else Assert.Equal(string.Empty, result); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException("Unable to check Db existence"); | ||
} | ||
}*/ | ||
|
||
private const string TestMySqlHost = "localhost"; | ||
private const string TestSqlServerHost = @"(localdb)\mssqllocaldb"; | ||
private const string TestPostgresSqlHost = "localhost"; | ||
|
||
private const string TestSqlServerDatabase = "SQLServerTestDb_{0}"; | ||
private const string TestSqLiteDatabase = "SqLiteTestDb_{0}.sqlite"; | ||
private const string TestMySqlDatabase = "MySqlTestDb_{0}"; | ||
private const string TestPostgresSqlDatabase = "PostgresTestDb_{0}"; | ||
|
||
//private const string SelectDatabaseTemplate = "SELECT {0} FROM {1} WHERE {0}={2};"; | ||
//private const string SelectCitiesQuery = "SELECT Id, Name, RegionId FROM City"; | ||
//private const string CreateStructureScriptFile = @"..\..\..\TestScripts\CreateDb.sql"; | ||
//private const string InsertDataScriptFile = @"..\..\..\TestScripts\InsertData.sql"; | ||
private const string CreateStructureForLongDataTestScriptFile = @"..\..\..\TestScripts\CreateDbLong.sql"; | ||
private const string InsertDataForLongDataTestScriptFile = @"..\..\..\TestScripts\InsertDataLong.sql"; | ||
|
||
private readonly ILoggerFactory _loggerFactory = new LoggerFactory(); | ||
|
||
private readonly IDictionary<DbEngine, Tuple<string, string>> _hostAndDatabaseOptions = new Dictionary<DbEngine, Tuple<string, string>>() | ||
{ | ||
{DbEngine.SqlServer, new Tuple<string, string>(TestSqlServerHost, string.Format(TestSqlServerDatabase, Guid.NewGuid().ToString().Replace("-","")))}, | ||
{DbEngine.SqLite, new Tuple<string, string>(string.Empty, string.Format(TestSqLiteDatabase, Guid.NewGuid().ToString().Replace("-","")))}, | ||
{DbEngine.MySql, new Tuple<string, string>(TestMySqlHost, string.Format(TestMySqlDatabase, Guid.NewGuid().ToString().Replace("-","")))}, | ||
{DbEngine.PostgresSql, new Tuple<string, string>(TestPostgresSqlHost, string.Format(TestPostgresSqlDatabase, Guid.NewGuid().ToString().Replace("-","")))} | ||
}; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
DbTools/DbTools.Simple.Nuget.Tests/TestScripts/CreateDbLong.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
SET GLOBAL max_allowed_packet=64000000; | ||
CREATE TABLE `stock` ( | ||
`Id` int(11) NOT NULL AUTO_INCREMENT, | ||
`Symbol` varchar(255) CHARACTER SET utf8mb4 NOT NULL, | ||
`Description` longtext CHARACTER SET utf8mb4 NOT NULL, | ||
`Exchange` longtext CHARACTER SET utf8mb4 NOT NULL, | ||
`FirstUpdate` datetime(6) DEFAULT NULL, | ||
`LastUpdate` datetime(6) DEFAULT NULL, | ||
`Status` int(11) DEFAULT NULL, | ||
`PreviousDayClose` double DEFAULT NULL, | ||
`PreviousDay` datetime(6) DEFAULT NULL, | ||
`SplitFactor` double DEFAULT NULL, | ||
PRIMARY KEY (`Id`), | ||
UNIQUE KEY `IX_Stock_Symbol` (`Symbol`) | ||
) ENGINE=InnoDB AUTO_INCREMENT=10222 DEFAULT CHARSET=latin1; | ||
|
||
CREATE TABLE `stockquote` ( | ||
`Id` int(11) NOT NULL AUTO_INCREMENT, | ||
`StockId` int(11) NOT NULL, | ||
`Timestamp` datetime(6) NOT NULL, | ||
`Bid` double NOT NULL, | ||
`BidSize` int(11) NOT NULL, | ||
`Ask` double NOT NULL, | ||
`AskSize` int(11) NOT NULL, | ||
`Volume` bigint(20) NOT NULL, | ||
`SplitFactor` double NOT NULL, | ||
PRIMARY KEY (`Id`), | ||
UNIQUE KEY `IX_StockQuote_Timestamp_StockId` (`Timestamp`,`StockId`), | ||
KEY `IX_StockQuote_StockId` (`StockId`), | ||
KEY `IX_StockQuote_Timestamp` (`Timestamp`), | ||
CONSTRAINT `FK_StockQuote_Stock_StockId` FOREIGN KEY (`StockId`) REFERENCES `stock` (`Id`) ON DELETE CASCADE | ||
) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Oops, something went wrong.