Changed Sqlite to microsoft sqlite implementation

This commit is contained in:
2025-01-28 13:36:49 +02:00
parent d26c84480a
commit 20165af15a
13 changed files with 227 additions and 34 deletions

45
SethCoreTests/ApiTests.cs Normal file
View File

@@ -0,0 +1,45 @@
using DiscordBotCore;
using DiscordBotCore.API.Endpoints.PluginManagement;
using DiscordBotCore.Online;
using DiscordBotCore.Plugin;
using Moq;
namespace SethCoreTests;
public class PluginInstallEndpointTests
{
private readonly Mock<IPluginManager> _mockPluginManager;
private readonly PluginInstallEndpoint _endpoint;
public PluginInstallEndpointTests()
{
_mockPluginManager = new Mock<IPluginManager>();
_endpoint = new PluginInstallEndpoint(_mockPluginManager.Object);
}
[Fact]
public async Task HandleRequest_SuccessfulPluginInstallation_ReturnsOk()
{
var pluginName = "TestPlugin";
var pluginInfo = new OnlinePlugin(1, pluginName, "Description", "1.0", "Author", "http://link", 1);
_mockPluginManager.Setup(pm => pm.GetPluginDataByName(pluginName)).ReturnsAsync(pluginInfo);
_mockPluginManager.Setup(pm => pm.InstallPluginNoProgress(pluginInfo)).Returns(Task.CompletedTask);
var jsonRequest = $"{{\"pluginName\":\"{pluginName}\"}}";
var response = await _endpoint.HandleRequest(jsonRequest);
Assert.True(response.Success);
}
[Fact]
public async Task HandleRequest_PluginNotFound_ReturnsFail()
{
var pluginName = "NonExistentPlugin";
_mockPluginManager.Setup(pm => pm.GetPluginDataByName(pluginName)).ReturnsAsync((OnlinePlugin?)null);
var jsonRequest = $"{{\"pluginName\":\"{pluginName}\"}}";
var response = await _endpoint.HandleRequest(jsonRequest);
Assert.False(response.Success);
}
}

View File

@@ -0,0 +1,69 @@
using System.Net;
using DiscordBotCore.Interfaces.PluginManagement;
using DiscordBotCore.Online.Helpers;
using Moq;
using Moq.Protected;
namespace SethCoreTests;
public class PluginRepositoryTests
{
private readonly Mock<IPluginRepositoryConfiguration> _mockConfig;
private readonly Mock<HttpMessageHandler> _mockHttpMessageHandler;
private readonly PluginRepository _pluginRepository;
public PluginRepositoryTests()
{
_mockConfig = new Mock<IPluginRepositoryConfiguration>();
_mockConfig.SetupGet(c => c.BaseUrl).Returns("http://localhost/");
_mockConfig.SetupGet(c => c.PluginRepositoryLocation).Returns("api/plugins/");
_mockConfig.SetupGet(c => c.DependenciesRepositoryLocation).Returns("api/dependencies/");
_mockHttpMessageHandler = new Mock<HttpMessageHandler>();
var httpClient = new HttpClient(_mockHttpMessageHandler.Object)
{
BaseAddress = new System.Uri(_mockConfig.Object.BaseUrl)
};
_pluginRepository = new PluginRepository(_mockConfig.Object)
{
_httpClient = httpClient
};
}
[Fact]
public async Task GetAllPlugins_ReturnsListOfPlugins()
{
var pluginsJson = "[{\"PluginId\":1,\"PluginName\":\"TestPlugin\",\"PluginDescription\":\"Description\",\"LatestVersion\":\"1.0\",\"PluginAuthor\":\"Author\",\"PluginLink\":\"http://link\",\"OperatingSystem\":1}]";
_mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(pluginsJson)
});
var result = await _pluginRepository.GetAllPlugins();
Assert.Single(result);
Assert.Equal("TestPlugin", result[0].PluginName);
}
[Fact]
public async Task GetPluginById_ReturnsPlugin()
{
var pluginJson = "{\"PluginId\":1,\"PluginName\":\"TestPlugin\",\"PluginDescription\":\"Description\",\"LatestVersion\":\"1.0\",\"PluginAuthor\":\"Author\",\"PluginLink\":\"http://link\",\"OperatingSystem\":1}";
_mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(pluginJson)
});
var result = await _pluginRepository.GetPluginById(1);
Assert.NotNull(result);
Assert.Equal("TestPlugin", result.PluginName);
}
}

View File

@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>
<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DiscordBotCore\DiscordBotCore.csproj" />
</ItemGroup>
</Project>