mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
rework messages
+ addition LocalStorageService + tests
This commit is contained in:
@@ -39,6 +39,10 @@ public class UsersRepositoryTests
|
||||
var random = new Random();
|
||||
var users = _fixture.CreateMany<User>(random.Next(2, 10)).ToList();
|
||||
|
||||
_options = new DbContextOptionsBuilder<GovorDbContext>()
|
||||
.UseInMemoryDatabase(databaseName: "DbGovor_users_getall")
|
||||
.Options;
|
||||
|
||||
await using var context = new GovorDbContext(_options);
|
||||
var userRepository = new UsersRepository(context, _userValidator);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ using Govor.API.Services.Authentication;
|
||||
using Govor.Core.Infrastructure.Extensions;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories.Users;
|
||||
using Govor.Core.Services;
|
||||
using Govor.API.Services.Authentication.Interfaces;
|
||||
using Moq;
|
||||
|
||||
namespace Govor.API.Tests.UnitTests.Services;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
using AutoFixture;
|
||||
using Govor.API.Services;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Moq;
|
||||
|
||||
namespace Govor.API.Tests.UnitTests.Services;
|
||||
|
||||
[TestFixture]
|
||||
public class LocalStorageServiceTests
|
||||
{
|
||||
private Fixture _fixture;
|
||||
private IStorageService _service;
|
||||
private Mock<IWebHostEnvironment> _webHostEnvironmentMock;
|
||||
private string _tempDirectory;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
_webHostEnvironmentMock = new Mock<IWebHostEnvironment>();
|
||||
|
||||
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
||||
Directory.CreateDirectory(_tempDirectory);
|
||||
|
||||
_webHostEnvironmentMock.Setup(w => w.ContentRootPath).Returns(_tempDirectory);
|
||||
|
||||
_service = new LocalStorageService(_webHostEnvironmentMock.Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
if (Directory.Exists(_tempDirectory))
|
||||
{
|
||||
Directory.Delete(_tempDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidDataAndFileName_When_SaveAsync_Then_FileIsSaved()
|
||||
{
|
||||
// Arrange
|
||||
var data = _fixture.CreateMany<byte>(1000).ToArray();
|
||||
var fileName = _fixture.Create<string>() + ".txt";
|
||||
var saveDate = DateTime.UtcNow;
|
||||
|
||||
// Act
|
||||
var url = await _service.SaveAsync(data, fileName);
|
||||
|
||||
// Assert
|
||||
Assert.That(url, Is.Not.Null);
|
||||
|
||||
// _tempDirectory/uploads/yyyy/MM/url
|
||||
var datePath = saveDate.ToString("yyyy/MM");
|
||||
var expectedFilePath = Path.Combine(_tempDirectory, "uploads", url);
|
||||
Assert.That(File.Exists(expectedFilePath), Is.True, $"Файл не найден по пути: {expectedFilePath}");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_EmptyName_When_SaveAsync_Should_Throw_ArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var data = _fixture.CreateMany<byte>(1000).ToArray();
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<ArgumentException>(async () => await _service.SaveAsync(data, string.Empty));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_EmptyData_When_SaveAsync_Should_Throw_ArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var name = _fixture.Create<string>();
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<ArgumentException>(async () => await _service.SaveAsync(default, name));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user