add tests
This commit is contained in:
47
ChatBot.Tests/Services/DatabaseInitializationServiceTests.cs
Normal file
47
ChatBot.Tests/Services/DatabaseInitializationServiceTests.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using ChatBot.Data;
|
||||
using ChatBot.Services;
|
||||
using ChatBot.Tests.TestUtilities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
|
||||
namespace ChatBot.Tests.Services;
|
||||
|
||||
public class DatabaseInitializationServiceTests : UnitTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void DatabaseInitializationService_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var serviceProviderMock = new Mock<IServiceProvider>();
|
||||
var loggerMock = new Mock<ILogger<DatabaseInitializationService>>();
|
||||
|
||||
// Act
|
||||
var service = new DatabaseInitializationService(
|
||||
serviceProviderMock.Object,
|
||||
loggerMock.Object
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(service);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DatabaseInitializationService_StopAsync_ShouldComplete()
|
||||
{
|
||||
// Arrange
|
||||
var serviceProviderMock = new Mock<IServiceProvider>();
|
||||
var loggerMock = new Mock<ILogger<DatabaseInitializationService>>();
|
||||
var service = new DatabaseInitializationService(
|
||||
serviceProviderMock.Object,
|
||||
loggerMock.Object
|
||||
);
|
||||
|
||||
// Act & Assert
|
||||
await service.StopAsync(CancellationToken.None);
|
||||
|
||||
// If we reach here, the method completed successfully
|
||||
Assert.True(true);
|
||||
}
|
||||
}
|
||||
289
ChatBot.Tests/Services/OllamaClientAdapterTests.cs
Normal file
289
ChatBot.Tests/Services/OllamaClientAdapterTests.cs
Normal file
@@ -0,0 +1,289 @@
|
||||
using ChatBot.Services;
|
||||
using ChatBot.Services.Interfaces;
|
||||
using OllamaSharp.Models;
|
||||
using OllamaSharp.Models.Chat;
|
||||
|
||||
namespace ChatBot.Tests.Services;
|
||||
|
||||
public class OllamaClientAdapterTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WithValidUrl_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:11434";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithNullUrl_ShouldThrowArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
string? url = null;
|
||||
|
||||
// Act & Assert
|
||||
var exception = Assert.Throws<ArgumentException>(() => new OllamaClientAdapter(url!));
|
||||
Assert.Equal("URL cannot be empty (Parameter 'url')", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithEmptyUrl_ShouldThrowArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var url = "";
|
||||
|
||||
// Act & Assert
|
||||
var exception = Assert.Throws<ArgumentException>(() => new OllamaClientAdapter(url));
|
||||
Assert.Equal("URL cannot be empty (Parameter 'url')", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithWhitespaceUrl_ShouldThrowArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var url = " ";
|
||||
|
||||
// Act & Assert
|
||||
var exception = Assert.Throws<ArgumentException>(() => new OllamaClientAdapter(url));
|
||||
Assert.Equal("URL cannot be empty (Parameter 'url')", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithHttpsUrl_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "https://ollama.example.com";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithCustomPort_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:8080";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectedModel_GetAndSet_ShouldWork()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:11434";
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
var modelName = "llama2";
|
||||
|
||||
// Act
|
||||
adapter.SelectedModel = modelName;
|
||||
var result = adapter.SelectedModel;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(modelName, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectedModel_SetMultipleTimes_ShouldUpdateValue()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:11434";
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Act
|
||||
adapter.SelectedModel = "llama2";
|
||||
adapter.SelectedModel = "codellama";
|
||||
var result = adapter.SelectedModel;
|
||||
|
||||
// Assert
|
||||
Assert.Equal("codellama", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ChatAsync_ShouldReturnAsyncEnumerable()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:11434";
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
var request = new ChatRequest
|
||||
{
|
||||
Model = "llama2",
|
||||
Messages = new List<Message>
|
||||
{
|
||||
new() { Role = "user", Content = "Hello" },
|
||||
},
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = adapter.ChatAsync(request);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.IsAssignableFrom<IAsyncEnumerable<ChatResponseStream?>>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ListLocalModelsAsync_ShouldReturnTask()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:11434";
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Act
|
||||
var result = adapter.ListLocalModelsAsync();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.IsAssignableFrom<Task<IEnumerable<Model>>>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithSpecialCharactersInUrl_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://user:pass@localhost:11434/path?query=value#fragment";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithIpAddress_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://192.168.1.100:11434";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithLocalhostVariations_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var urls = new[]
|
||||
{
|
||||
"http://localhost:11434",
|
||||
"http://127.0.0.1:11434",
|
||||
"http://0.0.0.0:11434",
|
||||
};
|
||||
|
||||
foreach (var url in urls)
|
||||
{
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithDifferentProtocols_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var urls = new[] { "http://localhost:11434", "https://localhost:11434" };
|
||||
|
||||
foreach (var url in urls)
|
||||
{
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithTrailingSlash_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:11434/";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithPath_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:11434/api/v1";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithUnicodeUrl_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://тест.рф:11434";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithVeryLongUrl_ShouldThrowException()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://" + new string('a', 1000) + ".example.com:11434";
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<UriFormatException>(() => new OllamaClientAdapter(url));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithUrlContainingSpaces_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:11434/path with spaces";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithUrlContainingSpecialCharacters_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var url = "http://localhost:11434/path!@#$%^&*()";
|
||||
|
||||
// Act
|
||||
var adapter = new OllamaClientAdapter(url);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(adapter);
|
||||
}
|
||||
}
|
||||
121
ChatBot.Tests/Services/Telegram/TelegramServicesTests.cs
Normal file
121
ChatBot.Tests/Services/Telegram/TelegramServicesTests.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using ChatBot.Services.Telegram.Services;
|
||||
using ChatBot.Tests.TestUtilities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace ChatBot.Tests.Services.Telegram;
|
||||
|
||||
public class TelegramServicesTests : UnitTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void TelegramBotService_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var loggerMock = new Mock<ILogger<TelegramBotService>>();
|
||||
var botClientMock = new Mock<ITelegramBotClient>();
|
||||
var serviceProviderMock = new Mock<IServiceProvider>();
|
||||
|
||||
// Act
|
||||
var service = new TelegramBotService(
|
||||
loggerMock.Object,
|
||||
botClientMock.Object,
|
||||
serviceProviderMock.Object
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(service);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TelegramMessageHandler_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var loggerMock = new Mock<ILogger<TelegramMessageHandler>>();
|
||||
var commandProcessorMock =
|
||||
new Mock<ChatBot.Services.Telegram.Interfaces.ITelegramCommandProcessor>();
|
||||
var messageSenderMock =
|
||||
new Mock<ChatBot.Services.Telegram.Interfaces.ITelegramMessageSender>();
|
||||
|
||||
// Act
|
||||
var handler = new TelegramMessageHandler(
|
||||
loggerMock.Object,
|
||||
commandProcessorMock.Object,
|
||||
messageSenderMock.Object
|
||||
);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(handler);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TelegramErrorHandler_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var loggerMock = new Mock<ILogger<TelegramErrorHandler>>();
|
||||
|
||||
// Act
|
||||
var handler = new TelegramErrorHandler(loggerMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(handler);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TelegramMessageSender_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var loggerMock = new Mock<ILogger<TelegramMessageSender>>();
|
||||
|
||||
// Act
|
||||
var sender = new TelegramMessageSender(loggerMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(sender);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BotInfoService_ShouldCreateInstance()
|
||||
{
|
||||
// Arrange
|
||||
var botClientMock = new Mock<ITelegramBotClient>();
|
||||
var loggerMock = new Mock<ILogger<BotInfoService>>();
|
||||
|
||||
// Act
|
||||
var service = new BotInfoService(botClientMock.Object, loggerMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(service);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BotInfoService_IsCacheValid_ShouldReturnFalseInitially()
|
||||
{
|
||||
// Arrange
|
||||
var botClientMock = new Mock<ITelegramBotClient>();
|
||||
var loggerMock = new Mock<ILogger<BotInfoService>>();
|
||||
var service = new BotInfoService(botClientMock.Object, loggerMock.Object);
|
||||
|
||||
// Act
|
||||
var isValid = service.IsCacheValid();
|
||||
|
||||
// Assert
|
||||
Assert.False(isValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BotInfoService_InvalidateCache_ShouldWork()
|
||||
{
|
||||
// Arrange
|
||||
var botClientMock = new Mock<ITelegramBotClient>();
|
||||
var loggerMock = new Mock<ILogger<BotInfoService>>();
|
||||
var service = new BotInfoService(botClientMock.Object, loggerMock.Object);
|
||||
|
||||
// Act
|
||||
service.InvalidateCache();
|
||||
|
||||
// Assert
|
||||
Assert.False(service.IsCacheValid());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user