mirror of
https://github.com/BDDSM/YY.EventLogReaderAssistant.git
synced 2024-11-26 18:11:45 +02:00
Добавил тесты для служебного класса работы со SQLite базой. Рефакторинг.
This commit is contained in:
parent
944670dc11
commit
36a0daec08
Binary file not shown.
@ -8,6 +8,8 @@ namespace YY.EventLogAssistant.Services.Tests
|
||||
{
|
||||
public class DateTimeExtensionsTests
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
[Fact]
|
||||
public void ToNullIfTooEarlyForDb_IsNull_Test()
|
||||
{
|
||||
@ -45,5 +47,7 @@ namespace YY.EventLogAssistant.Services.Tests
|
||||
|
||||
Assert.Equal(correctDate, resultDate);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ namespace YY.EventLogAssistant.Services.Tests
|
||||
{
|
||||
public class IntExtensionsTests
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
[Fact]
|
||||
public void ToDateTimeFormat_Test()
|
||||
{
|
||||
@ -26,5 +28,7 @@ namespace YY.EventLogAssistant.Services.Tests
|
||||
|
||||
Assert.Equal(checkDate, resultDate);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
using YY.EventLogAssistant.Services;
|
||||
|
||||
namespace YY.EventLogAssistant.Services.Tests
|
||||
{
|
||||
public class SQLiteExtensionsTests
|
||||
{
|
||||
#region Private Member Variables
|
||||
|
||||
private string sampleDataDirectory;
|
||||
private string sampleDatabaseFile;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public SQLiteExtensionsTests()
|
||||
{
|
||||
string currentDirectory = Directory.GetCurrentDirectory();
|
||||
sampleDataDirectory = Path.Combine(currentDirectory, "SampleData");
|
||||
sampleDatabaseFile = Path.Combine(sampleDataDirectory, "SQLiteFormatEventLog", "1Cv8.lgd");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
[Fact]
|
||||
public void GetStringOrDefault_Test()
|
||||
{
|
||||
string connectionString = SQLiteExtensions.GetConnectionString(sampleDatabaseFile);
|
||||
|
||||
string queryText = string.Format(
|
||||
"Select\n" +
|
||||
" \"Hello, world!\" AS DataPresentation,\n" +
|
||||
" null AS DataPresentationEmpty\n" +
|
||||
"From\n" +
|
||||
" EventLog el\n" +
|
||||
"Where RowID > {0}\n" +
|
||||
"Order By rowID\n" +
|
||||
"Limit {1}\n", 0, 1);
|
||||
|
||||
string DataPresentation = null;
|
||||
string DataPresentationEmpty = null;
|
||||
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
using (SQLiteCommand command = new SQLiteCommand(queryText, connection))
|
||||
{
|
||||
SQLiteDataReader reader = command.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
DataPresentation = SQLiteExtensions.GetStringOrDefault(reader, 0);
|
||||
DataPresentationEmpty = SQLiteExtensions.GetStringOrDefault(reader, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Equal("Hello, world!", DataPresentation);
|
||||
Assert.Equal(String.Empty, DataPresentationEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInt64OrDefault_Test()
|
||||
{
|
||||
string connectionString = SQLiteExtensions.GetConnectionString(sampleDatabaseFile);
|
||||
|
||||
string queryText = String.Format(
|
||||
"Select\n" +
|
||||
" null AS ConnectId,\n" +
|
||||
" 777 AS Session\n" +
|
||||
"From\n" +
|
||||
" EventLog el\n" +
|
||||
"Where RowID > {0}\n" +
|
||||
"Order By rowID\n" +
|
||||
"Limit {1}\n", 0, 1);
|
||||
|
||||
long connectionId = 0;
|
||||
long sessionId = 0;
|
||||
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
using (SQLiteCommand command = new SQLiteCommand(queryText, connection))
|
||||
{
|
||||
SQLiteDataReader reader = command.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
connectionId = SQLiteExtensions.GetInt64OrDefault(reader, 0);
|
||||
sessionId = SQLiteExtensions.GetInt64OrDefault(reader, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Equal(0, connectionId);
|
||||
Assert.Equal(777, sessionId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRowAsString_Test()
|
||||
{
|
||||
string connectionString = SQLiteExtensions.GetConnectionString(sampleDatabaseFile);
|
||||
|
||||
string queryText = String.Format(
|
||||
"Select\n" +
|
||||
" el.RowId AS RowId,\n" +
|
||||
" el.Date AS Date,\n" +
|
||||
" el.ConnectId AS ConnectId,\n" +
|
||||
" el.Session AS Session\n" +
|
||||
"From\n" +
|
||||
" EventLog el\n" +
|
||||
"Where RowID > {0}\n" +
|
||||
"Order By rowID\n" +
|
||||
"Limit {1}\n", 0, 1);
|
||||
|
||||
string rowAsString = String.Empty;
|
||||
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
using (SQLiteCommand command = new SQLiteCommand(queryText, connection))
|
||||
{
|
||||
SQLiteDataReader reader = command.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
rowAsString = reader.GetRowAsString();
|
||||
}
|
||||
}
|
||||
}
|
||||
int countLines = rowAsString.Split('\n').Where(str => str != string.Empty).Count();
|
||||
|
||||
Assert.NotEqual(String.Empty, rowAsString);
|
||||
Assert.Equal(4, countLines);
|
||||
Assert.Contains("RowId", rowAsString);
|
||||
Assert.Contains("Date", rowAsString);
|
||||
Assert.Contains("ConnectId", rowAsString);
|
||||
Assert.Contains("Session", rowAsString);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetConnectionString_Test()
|
||||
{
|
||||
string connectionString = SQLiteExtensions.GetConnectionString(sampleDatabaseFile);
|
||||
|
||||
string queryText = String.Format(
|
||||
"Select\n" +
|
||||
" COUNT(el.RowId) CNT\n" +
|
||||
"From\n" +
|
||||
" EventLog el\n");
|
||||
|
||||
long rowsCount = 0;
|
||||
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
using (SQLiteCommand command = new SQLiteCommand(queryText, connection))
|
||||
{
|
||||
SQLiteDataReader reader = command.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
object rowsCountObject = reader.GetValue(0);
|
||||
if (rowsCountObject is long)
|
||||
{
|
||||
rowsCount = Convert.ToInt64(rowsCountObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assert.NotEqual(0, rowsCount);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -7,6 +7,8 @@ namespace YY.EventLogAssistant.Services.Tests
|
||||
{
|
||||
public class StringExtensionsTests
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
[Fact]
|
||||
public void From16To10_Test()
|
||||
{
|
||||
@ -91,5 +93,7 @@ namespace YY.EventLogAssistant.Services.Tests
|
||||
|
||||
Assert.Equal(checkValue, resultValue);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -22,4 +22,10 @@
|
||||
<ProjectReference Include="..\YY.EventLogAssistant\YY.EventLogAssistant.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="SampleData\SQLiteFormatEventLog\1Cv8.lgd">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -7,6 +7,8 @@ namespace YY.EventLogAssistant.Services
|
||||
{
|
||||
internal static class DateTimeExtensions
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
public static DateTime? ToNullIfTooEarlyForDb(this DateTime date)
|
||||
{
|
||||
return (date >= (DateTime)SqlDateTime.MinValue) ? date : (DateTime?)null;
|
||||
@ -28,5 +30,7 @@ namespace YY.EventLogAssistant.Services
|
||||
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
|
||||
).TotalMilliseconds;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ namespace YY.EventLogAssistant.Services
|
||||
{
|
||||
internal static class IntExtensions
|
||||
{
|
||||
#region #region Public Methods
|
||||
|
||||
public static DateTime ToDateTimeFormat(this long s)
|
||||
{
|
||||
return DateTime.MinValue.AddSeconds((double)s / 10000);
|
||||
@ -18,5 +20,7 @@ namespace YY.EventLogAssistant.Services
|
||||
else
|
||||
return DateTime.MinValue.AddSeconds((double)s / 10000);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,8 @@ namespace YY.EventLogAssistant.Services
|
||||
{
|
||||
internal static class SQLiteExtensions
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
public static string GetStringOrDefault(this SQLiteDataReader reader, int valueIndex)
|
||||
{
|
||||
try
|
||||
@ -51,5 +53,7 @@ namespace YY.EventLogAssistant.Services
|
||||
{
|
||||
return String.Format("Data Source={0};Version=3;Read Only=True;", dbFile);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -8,14 +8,20 @@ namespace YY.EventLogAssistant.Services
|
||||
{
|
||||
internal class StreamLineReader : IDisposable
|
||||
{
|
||||
const int BufferLength = 1024;
|
||||
#region Private Member Variables
|
||||
|
||||
Stream _Base;
|
||||
int _Read = 0, _Index = 0;
|
||||
byte[] _Bff = new byte[BufferLength];
|
||||
private const int BufferLength = 1024;
|
||||
|
||||
long _CurrentPosition = 0;
|
||||
long _CurrentLine = 0;
|
||||
private Stream _Base;
|
||||
private int _Read = 0, _Index = 0;
|
||||
private byte[] _Bff = new byte[BufferLength];
|
||||
|
||||
private long _CurrentPosition = 0;
|
||||
private long _CurrentLine = 0;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public long CurrentPosition { get { return _CurrentPosition; } }
|
||||
|
||||
@ -27,34 +33,6 @@ namespace YY.EventLogAssistant.Services
|
||||
|
||||
public long GetCount(long goToLine) { return IGetCount(goToLine, false); }
|
||||
|
||||
long IGetCount(long goToLine, bool stopWhenLine)
|
||||
{
|
||||
_Base.Seek(0, SeekOrigin.Begin);
|
||||
_CurrentPosition = 0;
|
||||
_CurrentLine = 0;
|
||||
_Index = 0;
|
||||
_Read = 0;
|
||||
|
||||
long savePosition = _Base.Length;
|
||||
|
||||
do
|
||||
{
|
||||
if (_CurrentLine == goToLine)
|
||||
{
|
||||
savePosition = _CurrentPosition;
|
||||
if (stopWhenLine) return _CurrentLine;
|
||||
}
|
||||
}
|
||||
while (ReadLine() != null);
|
||||
|
||||
long count = _CurrentLine;
|
||||
|
||||
_CurrentLine = goToLine;
|
||||
_Base.Seek(savePosition, SeekOrigin.Begin);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public string ReadLine()
|
||||
{
|
||||
bool found = false;
|
||||
@ -74,7 +52,7 @@ namespace YY.EventLogAssistant.Services
|
||||
}
|
||||
}
|
||||
|
||||
for (int max = _Index + _Read; _Index < max; )
|
||||
for (int max = _Index + _Read; _Index < max;)
|
||||
{
|
||||
char ch = (char)_Bff[_Index];
|
||||
_Read--; _Index++;
|
||||
@ -103,5 +81,39 @@ namespace YY.EventLogAssistant.Services
|
||||
_Base = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private long IGetCount(long goToLine, bool stopWhenLine)
|
||||
{
|
||||
_Base.Seek(0, SeekOrigin.Begin);
|
||||
_CurrentPosition = 0;
|
||||
_CurrentLine = 0;
|
||||
_Index = 0;
|
||||
_Read = 0;
|
||||
|
||||
long savePosition = _Base.Length;
|
||||
|
||||
do
|
||||
{
|
||||
if (_CurrentLine == goToLine)
|
||||
{
|
||||
savePosition = _CurrentPosition;
|
||||
if (stopWhenLine) return _CurrentLine;
|
||||
}
|
||||
}
|
||||
while (ReadLine() != null);
|
||||
|
||||
long count = _CurrentLine;
|
||||
|
||||
_CurrentLine = goToLine;
|
||||
_Base.Seek(savePosition, SeekOrigin.Begin);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,16 @@ namespace YY.EventLogAssistant.Services
|
||||
{
|
||||
internal static class StreamReaderExtensions
|
||||
{
|
||||
#region Private Member Variables
|
||||
|
||||
readonly static FieldInfo charPosField = typeof(StreamReader).GetField("_charPos", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
|
||||
readonly static FieldInfo byteLenField = typeof(StreamReader).GetField("_byteLen", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
|
||||
readonly static FieldInfo charBufferField = typeof(StreamReader).GetField("_charBuffer", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public static long GetPosition(this StreamReader reader)
|
||||
{
|
||||
int byteLen = (int)byteLenField.GetValue(reader);
|
||||
@ -41,6 +47,7 @@ namespace YY.EventLogAssistant.Services
|
||||
stream.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user