Skip to content

Commit

Permalink
Merge pull request #1364 from motzky/unit_test_speed
Browse files Browse the repository at this point in the history
Unit test speed
  • Loading branch information
bassmaster187 authored Oct 2, 2024
2 parents 045e854 + 7e75210 commit 1ccaa02
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions TeslaLogger/KVS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ internal static int Get(string key, out double value)
// defaults to int.MinValue, check return code for SUCCESS
internal static int Get(string key, out int value)
{
if(Tools.IsUnitTest())
{
value = int.MinValue;
return NOT_FOUND;
}

try
{
using (MySqlConnection con = new MySqlConnection(DBHelper.DBConnectionstring))
Expand Down Expand Up @@ -362,6 +368,12 @@ internal static int Get(string key, out int value)
// defaults to {} (empty JSON), check return code for SUCCESS
internal static int Get(string key, out string value)
{
if (Tools.IsUnitTest())
{
value = "{}";
return NOT_FOUND;
}

try
{
using (MySqlConnection con = new MySqlConnection(DBHelper.DBConnectionstring))
Expand Down
23 changes: 23 additions & 0 deletions TeslaLogger/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,29 @@ public static bool IsDocker()
return false;
}

public static bool IsUnitTest()
{
try
{
foreach(var ass in AppDomain.CurrentDomain.GetAssemblies())
{
if(ass.FullName.StartsWith("NUnit.framework", StringComparison.CurrentCultureIgnoreCase) ||
ass.FullName.StartsWith("Microsoft.VisualStudio.QualityTools.UnitTestFramework", StringComparison.InvariantCultureIgnoreCase) ||
ass.FullName.StartsWith("Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter", StringComparison.InvariantCultureIgnoreCase)||
ass.FullName.StartsWith("Microsoft.TestPlatform", StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
}
catch (Exception ex)
{
ex.ToExceptionless().FirstCarUserID().Submit();
Logfile.ExceptionWriter(ex, "IsUnitTest");
}
return false;
}

public static bool IsShareData()
{
try
Expand Down
7 changes: 7 additions & 0 deletions UnitTestsTeslalogger/UnitTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,5 +1195,12 @@ public void ParsePackCurrent()
Assert.AreEqual(current, 121.9d);

}

[TestMethod]
public void IsInUnitTest()
{
var inTest = Tools.IsUnitTest();
Assert.IsTrue(inTest, "Should detect that we are in unit test");
}
}
}

0 comments on commit 1ccaa02

Please sign in to comment.