|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Calinga.NET.Infrastructure; |
| 7 | +using FluentAssertions; |
| 8 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | + |
| 10 | +namespace Calinga.NET.Tests |
| 11 | +{ |
| 12 | + [TestClass] |
| 13 | + public class FileSystemTests |
| 14 | + { |
| 15 | + private string _tempDir; |
| 16 | + private FileSystem _sut; |
| 17 | + |
| 18 | + [TestInitialize] |
| 19 | + public void Init() |
| 20 | + { |
| 21 | + _tempDir = Path.Combine(Path.GetTempPath(), $"FileSystemTest_{Guid.NewGuid()}"); |
| 22 | + Directory.CreateDirectory(_tempDir); |
| 23 | + _sut = new FileSystem(); |
| 24 | + } |
| 25 | + |
| 26 | + [TestCleanup] |
| 27 | + public void Cleanup() |
| 28 | + { |
| 29 | + if (Directory.Exists(_tempDir)) |
| 30 | + Directory.Delete(_tempDir, true); |
| 31 | + } |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public async Task ReadAllTextAsync_ShouldAllowConcurrentReads() |
| 35 | + { |
| 36 | + // Arrange |
| 37 | + var filePath = Path.Combine(_tempDir, "test.json"); |
| 38 | + var content = "{\"key\": \"value\"}"; |
| 39 | + await File.WriteAllTextAsync(filePath, content); |
| 40 | + |
| 41 | + // Act - Multiple concurrent reads should not throw |
| 42 | + var tasks = Enumerable.Range(0, 10) |
| 43 | + .Select(_ => Task.Run(() => _sut.ReadAllTextAsync(filePath))) |
| 44 | + .ToList(); |
| 45 | + |
| 46 | + Func<Task> act = async () => await Task.WhenAll(tasks); |
| 47 | + |
| 48 | + // Assert |
| 49 | + await act.Should().NotThrowAsync("concurrent reads should be allowed"); |
| 50 | + |
| 51 | + // Verify all reads returned correct content |
| 52 | + var results = await Task.WhenAll(tasks); |
| 53 | + foreach (var result in results) |
| 54 | + { |
| 55 | + result.Should().Be(content); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + [TestMethod] |
| 60 | + public async Task WriteAllTextAsync_ShouldCreateFileWithContent() |
| 61 | + { |
| 62 | + // Arrange |
| 63 | + var filePath = Path.Combine(_tempDir, "write_test.json"); |
| 64 | + var content = "{\"test\": \"data\"}"; |
| 65 | + |
| 66 | + // Act |
| 67 | + await _sut.WriteAllTextAsync(filePath, content); |
| 68 | + |
| 69 | + // Assert |
| 70 | + File.Exists(filePath).Should().BeTrue(); |
| 71 | + var readContent = await File.ReadAllTextAsync(filePath); |
| 72 | + readContent.Should().Be(content); |
| 73 | + } |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public async Task ReadAllTextAsync_ShouldReadFileContent() |
| 77 | + { |
| 78 | + // Arrange |
| 79 | + var filePath = Path.Combine(_tempDir, "read_test.json"); |
| 80 | + var content = "{\"hello\": \"world\"}"; |
| 81 | + await File.WriteAllTextAsync(filePath, content); |
| 82 | + |
| 83 | + // Act |
| 84 | + var result = await _sut.ReadAllTextAsync(filePath); |
| 85 | + |
| 86 | + // Assert |
| 87 | + result.Should().Be(content); |
| 88 | + } |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public void FileExists_ShouldReturnTrue_WhenFileExists() |
| 92 | + { |
| 93 | + // Arrange |
| 94 | + var filePath = Path.Combine(_tempDir, "exists_test.json"); |
| 95 | + File.WriteAllText(filePath, "test"); |
| 96 | + |
| 97 | + // Act |
| 98 | + var result = _sut.FileExists(filePath); |
| 99 | + |
| 100 | + // Assert |
| 101 | + result.Should().BeTrue(); |
| 102 | + } |
| 103 | + |
| 104 | + [TestMethod] |
| 105 | + public void FileExists_ShouldReturnFalse_WhenFileDoesNotExist() |
| 106 | + { |
| 107 | + // Arrange |
| 108 | + var filePath = Path.Combine(_tempDir, "nonexistent.json"); |
| 109 | + |
| 110 | + // Act |
| 111 | + var result = _sut.FileExists(filePath); |
| 112 | + |
| 113 | + // Assert |
| 114 | + result.Should().BeFalse(); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments