Skip to content

Commit

Permalink
Tidy unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkael committed Jan 5, 2025
1 parent f9e0b86 commit 36630e4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 29 deletions.
2 changes: 1 addition & 1 deletion DataProviderService/DataProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public DataProviderService ( BgsService bgsService = null, StarMapService edsmSe
this.bgsService = bgsService ?? new BgsService();
this.edsmService = edsmService ?? new StarMapService();
this.spanshService = spanshService ?? new SpanshService();
this.starSystemRepository = starSystemRepository ?? new StarSystemSqLiteRepository();
this.starSystemRepository = starSystemRepository ?? new StarSystemSqLiteRepository(unitTesting);
}

public List<string> GetTypeAheadSystems ( string systemName )
Expand Down
24 changes: 22 additions & 2 deletions DataProviderService/SqlLiteBaseRepository.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
using System.Data.SQLite;
using System.IO;
using Utilities;

namespace EddiDataProviderService
{
public class SqLiteBaseRepository
{
public static string DbFile
protected static bool unitTesting
{
get { return Constants.DATA_DIR + @"\EDDI.sqlite"; }
get => _unitTesting;
set
{
if ( _unitTesting != value )
{
ResetTestDatabase();
_unitTesting = value;
}
}
}
private static bool _unitTesting;

private static void ResetTestDatabase ()
{
var testDatabase = new FileInfo( Constants.DATA_DIR + @"\EDDI_TEST.sqlite" );
if ( testDatabase.Exists ) { testDatabase.Delete(); }
}

protected static string DbFile => unitTesting
? Constants.DATA_DIR + @"\EDDI_TEST.sqlite"
: Constants.DATA_DIR + @"\EDDI.sqlite";

public static SQLiteConnection SimpleDbConnection()
{
Expand Down
35 changes: 19 additions & 16 deletions DataProviderService/StarSystemSqLiteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ UPDATE starsystems
private const string WHERE_SYSTEMADDRESS = @"WHERE systemaddress = @systemaddress; PRAGMA optimize;";
private const string WHERE_NAME = @"WHERE name = @name; PRAGMA optimize;";

public StarSystemSqLiteRepository ( SQLiteConnection connection = null )
public StarSystemSqLiteRepository ( bool unitTesting = false )
{
CreateOrUpdateDatabase( connection );
SqLiteBaseRepository.unitTesting = unitTesting;
CreateOrUpdateDatabase();
}

public DatabaseStarSystem GetSqlStarSystem ( ulong systemAddress )
Expand Down Expand Up @@ -468,11 +469,11 @@ private void deleteStarSystems(ImmutableList<StarSystem> systems)
}
}

private void CreateOrUpdateDatabase( SQLiteConnection connection = null )
private void CreateOrUpdateDatabase()
{
lock ( nameof(SimpleDbConnection) ) // Lock before writing to the database
{
using ( var con = connection ?? SimpleDbConnection() )
using ( var con = SimpleDbConnection() )
{
try
{
Expand Down Expand Up @@ -554,7 +555,7 @@ private void CreateOrUpdateDatabase( SQLiteConnection connection = null )
}

/// <summary> Valid columnNames are "systemaddress" and "comment" </summary>
private static void AddColumnIfMissing(SQLiteConnection con, string columnName)
private void AddColumnIfMissing(SQLiteConnection con, string columnName )
{
// Parameters like `DISTINCT` cannot be set on columns by this method
string command = string.Empty;
Expand All @@ -567,42 +568,44 @@ private static void AddColumnIfMissing(SQLiteConnection con, string columnName)
command = @"ALTER TABLE starsystems ADD COLUMN comment TEXT;";
break;
}
if (!string.IsNullOrEmpty(command))

if ( !string.IsNullOrEmpty( command ) )
{
bool columnExists = false;
using (var cmd = new SQLiteCommand(TABLE_INFO_SQL, con))
using ( var cmd = new SQLiteCommand( TABLE_INFO_SQL, con ) )
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
using ( SQLiteDataReader rdr = cmd.ExecuteReader() )
{
while (rdr.Read())
while ( rdr.Read() )
{
if (columnName == rdr.GetString(1))
if ( columnName == rdr.GetString( 1 ) )
{
columnExists = true;
break;
}
}
}
}
if (!columnExists)

if ( !columnExists )
{
Logging.Debug("Updating starsystem repository with new column " + columnName);
Logging.Debug( "Updating starsystem repository with new column " + columnName );
try
{
using (var cmd = new SQLiteCommand(command, con))
using ( var cmd = new SQLiteCommand( command, con ) )
{
cmd.ExecuteNonQuery();
}
}
catch (SQLiteException ex)
catch ( SQLiteException ex )
{
handleSqlLiteException(con, ex);
handleSqlLiteException( con, ex );
}
}
}
}

private static void handleSqlLiteException(SQLiteConnection con, SQLiteException ex)
private void handleSqlLiteException( SQLiteConnection con, SQLiteException ex )
{
Logging.Warn("SQLite error: ", ex.ToString());

Expand Down
10 changes: 9 additions & 1 deletion Tests/EddnTests.cs

Large diffs are not rendered by default.

10 changes: 1 addition & 9 deletions Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data.SQLite;
using System.IO;

// Number of worker threads is automatic because `Workers` is set to 0.
Expand All @@ -33,9 +32,7 @@ public class TestBase
internal static readonly FakeEdsmRestClient fakeEdsmRestClient = new FakeEdsmRestClient();
internal static readonly StarMapService fakeEdsmService = new StarMapService(fakeEdsmRestClient);

private static readonly string testDatabaseDir = @"C:\Temp";
internal static SQLiteConnection fakeSqLiteConnection;
internal static StarSystemSqLiteRepository fakeStarSystemSqLiteRepository;
internal static StarSystemSqLiteRepository fakeStarSystemSqLiteRepository = new StarSystemSqLiteRepository( true );

internal void MakeSafe()
{
Expand All @@ -54,11 +51,6 @@ internal void MakeSafe()

internal DataProviderService ConfigureTestDataProvider ()
{
// Create a temporary database for testing
Directory.CreateDirectory( testDatabaseDir );
fakeSqLiteConnection = new SQLiteConnection( $@"Data Source={testDatabaseDir}\EDDI_TEST.sqlite" );
fakeStarSystemSqLiteRepository = new StarSystemSqLiteRepository( fakeSqLiteConnection );

return new DataProviderService( fakeBgsService, fakeEdsmService, fakeSpanshService, fakeStarSystemSqLiteRepository );
}

Expand Down

0 comments on commit 36630e4

Please sign in to comment.