Skip to content

Commit

Permalink
Merge pull request #168 from MrDave1999/test/sqlite-integration-tests
Browse files Browse the repository at this point in the history
test: Add integration tests for repositories using SQLite
  • Loading branch information
MrDave1999 authored Oct 14, 2024
2 parents c1accb0 + e5231c1 commit f200310
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Import database
- name: Check sqlite3 version
run: sqlite3 --version
- name: Import databases
run: |
docker exec -i mariadb mariadb -uroot -p123456789 -h127.0.0.1 gamemode < ./scripts/mariadb/gamemode.sql
sqlite3 gamemode.db < ./scripts/sqlite/gamemode.sql
- name: Create .env files
run: |
cp ./tests/Persistence.Tests/.env.test.example ./tests/Persistence.Tests/.env.test
Expand Down
21 changes: 21 additions & 0 deletions scripts/sqlite/gamemode.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "players" (
"id" INTEGER,
"name" TEXT NOT NULL UNIQUE COLLATE NOCASE,
"password" TEXT NOT NULL,
"total_kills" INTEGER NOT NULL,
"total_deaths" INTEGER NOT NULL,
"max_killing_spree" INTEGER NOT NULL,
"brought_flags" INTEGER NOT NULL,
"captured_flags" INTEGER NOT NULL,
"dropped_flags" INTEGER NOT NULL,
"returned_flags" INTEGER NOT NULL,
"head_shots" INTEGER NOT NULL,
"role_id" INTEGER NOT NULL,
"skin_id" INTEGER NOT NULL,
"rank_id" INTEGER NOT NULL,
"created_at" TEXT NOT NULL,
"last_connection" TEXT NOT NULL,
PRIMARY KEY("id")
);
COMMIT;
2 changes: 1 addition & 1 deletion src/Persistence/Persistence.SQLite/SQLiteSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Persistence.SQLite;

internal class SQLiteSettings
public class SQLiteSettings
{
public string DataSource { get; set; }
public string ConnectionString { get; set; }
Expand Down
25 changes: 25 additions & 0 deletions src/Persistence/Persistence.SQLite/sql/seed_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- name: InitializeSeedData
DELETE FROM players;
INSERT INTO players
(
name,
password,
total_kills,
total_deaths,
max_killing_spree,
brought_flags,
captured_flags,
dropped_flags,
returned_flags,
head_shots,
role_id,
skin_id,
rank_id,
created_at,
last_connection
)
VALUES
('Admin_Player','$2a$10$60QnEiafBCLfVBMfQkExVeolyBxVHWcSQKTvkxVJj9FUozRpRP/GW',0,0,0,0,0,0,0,0,3,146,0,'2023-10-12 12:19:24','2023-10-13 12:19:24'),
('Moderator_Player','$2a$10$60QnEiafBCLfVBMfQkExVeolyBxVHWcSQKTvkxVJj9FUozRpRP/GW',0,0,0,0,0,0,0,0,2,146,0,'2023-10-12 12:19:24','2023-10-13 12:19:24'),
('VIP_Player','$2a$10$60QnEiafBCLfVBMfQkExVeolyBxVHWcSQKTvkxVJj9FUozRpRP/GW',0,0,0,0,0,0,0,0,1,146,0,'2023-10-12 12:19:24','2023-10-13 12:19:24'),
('Basic_Player','$2a$10$60QnEiafBCLfVBMfQkExVeolyBxVHWcSQKTvkxVJj9FUozRpRP/GW',0,0,0,0,0,0,0,0,0,146,0,'2023-10-12 12:19:24','2023-10-13 12:19:24');
2 changes: 1 addition & 1 deletion tests/Persistence.Tests/.env.test.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ MariaDB__Database=gamemode
MariaDB__UserName=root
MariaDB__Password=123456789

SQLite__DataSource=gamemode.db
SQLite__DataSource=/home/runner/work/Capture-The-Flag/Capture-The-Flag/gamemode.db
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Persistence.Tests.Common;
namespace Persistence.Tests.Common.DatabaseProviders;

public class InMemoryRepositoryManager : IRepositoryManager
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Persistence.Tests.Common;
namespace Persistence.Tests.Common.DatabaseProviders;

public class MariaDbRepositoryManager : IRepositoryManager
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Persistence.Tests.Common.DatabaseProviders;

public class SqliteRepositoryManager : IRepositoryManager
{
private readonly ServiceProvider _serviceProvider;
public IPlayerRepository PlayerRepository { get; }
public SqliteRepositoryManager()
{
var services = new ServiceCollection();
IConfiguration configuration = EnvConfigurationBuilder.Instance;
services.AddSingleton<IPasswordHasher, FakePasswordHasher>();
services.AddPersistenceSQLiteServices(configuration);
_serviceProvider = services.BuildServiceProvider();
PlayerRepository = _serviceProvider.GetRequiredService<IPlayerRepository>();
}

public void Dispose()
{
_serviceProvider.Dispose();
GC.SuppressFinalize(this);
}

public void InitializeSeedData()
{
var settings = _serviceProvider.GetRequiredService<SQLiteSettings>();
var sqlCollection = _serviceProvider.GetRequiredService<ISqlCollection>();
using var connection = new SqliteConnection(settings.ConnectionString);
connection.Open();
SqliteCommand command = connection.CreateCommand();
command.CommandText = sqlCollection["InitializeSeedData"];
command.ExecuteNonQuery();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class RepositoryManagerTestCases : IEnumerable<IRepositoryManager>
public IEnumerator<IRepositoryManager> GetEnumerator()
{
yield return new InMemoryRepositoryManager();
yield return new SqliteRepositoryManager();
yield return new MariaDbRepositoryManager();
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Persistence.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Configuration;
global using MySqlConnector;
global using Microsoft.Data.Sqlite;
global using YeSql.Net;
global using CTF.Application.Common.Services;
global using CTF.Application.Players.Accounts;
global using CTF.Application.Players.Ranks;
global using Persistence.Tests.Common;
global using Persistence.Tests.Common.DatabaseProviders;
global using Persistence.InMemory;
global using Persistence.SQLite;
global using Persistence.MariaDB;

0 comments on commit f200310

Please sign in to comment.