From c9aad0c46686be8a1cdc897f97ae3b4cffe810c9 Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Sat, 21 Jun 2025 15:25:11 +0700 Subject: [PATCH] LocalStorageServiceTsts finished --- .../Services/LocalStorageServiceTests.cs | 63 ++++++++++++++++++- Govor.API/Services/LocalStorageService.cs | 9 ++- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/Govor.API.Tests/UnitTests/Services/LocalStorageServiceTests.cs b/Govor.API.Tests/UnitTests/Services/LocalStorageServiceTests.cs index 6ec0e00..42bff42 100644 --- a/Govor.API.Tests/UnitTests/Services/LocalStorageServiceTests.cs +++ b/Govor.API.Tests/UnitTests/Services/LocalStorageServiceTests.cs @@ -53,7 +53,7 @@ public class LocalStorageServiceTests // _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}"); + Assert.That(File.Exists(expectedFilePath), Is.True); } [Test] @@ -75,4 +75,65 @@ public class LocalStorageServiceTests // Act & Assert Assert.ThrowsAsync(async () => await _service.SaveAsync(default, name)); } + + [Test] + public async Task Given_ValidUrl_When_LoadAsync_Then_Returns_FileStream() + { + // Arrange + var date = DateTime.UtcNow.ToString("yyyy/MM"); + var corePath = Path.Combine(_tempDirectory, "uploads", date); + Directory.CreateDirectory(corePath); + + var fileName = _fixture.Create() + ".txt"; + var fileContent = _fixture.Create(); + var filePath = Path.Combine(corePath, fileName); + await File.WriteAllTextAsync(filePath, fileContent); + + // Act + var url = Path.Combine(date, fileName); + await using var stream = await _service.LoadAsync(url); + + // Assert + Assert.That(stream, Is.Not.Null); + Assert.That(stream.Length, Is.GreaterThan(0)); + + // Дополнительная проверка: содержимое файла + using var reader = new StreamReader(stream); + var content = await reader.ReadToEndAsync(); + Assert.That(content, Is.EqualTo(fileContent)); + } + + [Test] + public void Given_EmptyUrl_When_LoadAsync_Should_Throw_FileNotFoundException() + { + // Act & Assert + Assert.ThrowsAsync(async () => await _service.LoadAsync(_fixture.Create()+".txt")); + } + + [Test] + public async Task Given_ValidUrl_When_DeleteAsync_Then_FileIsDeleted() + { + // Arrange + var date = DateTime.UtcNow.ToString("yyyy/MM"); + var corePath = Path.Combine(_tempDirectory, "uploads", date); + Directory.CreateDirectory(corePath); + + var fileName = _fixture.Create() + ".txt"; + var fileContent = _fixture.Create(); + var filePath = Path.Combine(corePath, fileName); + await File.WriteAllTextAsync(filePath, fileContent); + + // Act + var url = Path.Combine(date, fileName); + await _service.RemoveAsync(url); + + Assert.That(File.Exists(filePath), Is.False); + } + + [Test] + public async Task Given_InvalidUrl_When_DeleteAsync_TShould_Not_Throw() + { + // Act & Assert + Assert.DoesNotThrowAsync(async () => await _service.RemoveAsync(_fixture.Create()+".txt")); + } } \ No newline at end of file diff --git a/Govor.API/Services/LocalStorageService.cs b/Govor.API/Services/LocalStorageService.cs index 61bac10..cc6439c 100644 --- a/Govor.API/Services/LocalStorageService.cs +++ b/Govor.API/Services/LocalStorageService.cs @@ -38,18 +38,17 @@ public class LocalStorageService : IStorageService public async Task LoadAsync(string url) { - var filePath = Path.Combine(_storagePath, Path.GetFileName(url)); + var filePath = Path.Combine(_storagePath, url); // url уже включает yyyy/MM if (!File.Exists(filePath)) throw new FileNotFoundException("File not found.", filePath); - - await using var file = new FileStream(filePath, FileMode.Open, FileAccess.Read); - return file; + + return new FileStream(filePath, FileMode.Open, FileAccess.Read); } public async Task RemoveAsync(string url) { - var path = Path.Combine(_storagePath, Path.GetFileName(url)); + var path = Path.Combine(_storagePath, url); if (File.Exists(path)) {