-
Notifications
You must be signed in to change notification settings - Fork 28
Add tests for azure blob stream #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e7a038a
add stream tests classes
8ffc6cc
fix azure blob stream lenght
714a842
fix tests
a7523bd
add write test for azure blob
afccd53
add seek test
40db074
Merge branch 'develop' into rybina/test_azure_stream
c094d83
Merge branch 'develop' into rybina/test_azure_stream
292df32
add properties variable
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
ManagedCode.Storage.Tests/Storages/Azure/AzureBlobStreamTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using ManagedCode.Storage.Azure; | ||
| using ManagedCode.Storage.Core.Models; | ||
| using ManagedCode.Storage.Tests.Common; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Testcontainers.Azurite; | ||
| using Xunit; | ||
|
|
||
| namespace ManagedCode.Storage.Tests.Storages.Azure; | ||
|
|
||
| public class AzureBlobStreamTests : StreamTests<AzuriteContainer> | ||
| { | ||
| protected override AzuriteContainer Build() | ||
| { | ||
| return new AzuriteBuilder() | ||
| .WithImage("mcr.microsoft.com/azure-storage/azurite:3.26.0") | ||
| .Build(); | ||
| } | ||
|
|
||
| protected override ServiceProvider ConfigureServices() | ||
| { | ||
| return AzureConfigurator.ConfigureServices(Container.GetConnectionString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadStreamWithStreamReader_WhenFileExists_ReturnData() | ||
| { | ||
| // Arrange | ||
| var directory = "test-directory"; | ||
| var fileSizeInBytes = 10; | ||
| await using var localFile = LocalFile.FromRandomNameWithExtension(".txt"); | ||
| FileHelper.GenerateLocalFileWithData(localFile, fileSizeInBytes); | ||
| var storage = (IAzureStorage) Storage; | ||
|
|
||
| UploadOptions options = new() { FileName = localFile.Name, Directory = directory }; | ||
| await using var localFileStream = localFile.FileInfo.OpenRead(); | ||
| var result = await storage.UploadAsync(localFileStream, options); | ||
|
|
||
| await using var blobStream = storage.GetBlobStream(result.Value.FullName); | ||
|
|
||
| // Act | ||
| using var streamReader = new StreamReader(blobStream); | ||
| var content = await streamReader.ReadToEndAsync(); | ||
|
|
||
| // Assert | ||
| await using var fileStream = localFile.FileInfo.OpenRead(); | ||
| using var fileReader = new StreamReader(fileStream); | ||
| var fileContent = await fileReader.ReadToEndAsync(); | ||
| content.Should().NotBeNullOrEmpty(); | ||
| fileContent.Should().NotBeNullOrEmpty(); | ||
| content.Should().Be(fileContent); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadStream_WhenFileExists_ReturnData() | ||
| { | ||
| // Arrange | ||
| var directory = "test-directory"; | ||
| var fileSizeInBytes = 10; | ||
| await using var localFile = LocalFile.FromRandomNameWithExtension(".txt"); | ||
| FileHelper.GenerateLocalFileWithData(localFile, fileSizeInBytes); | ||
| var storage = (IAzureStorage) Storage; | ||
|
|
||
| UploadOptions options = new() { FileName = localFile.Name, Directory = directory }; | ||
| await using var fileStream = localFile.FileInfo.OpenRead(); | ||
| var result = await storage.UploadAsync(fileStream, options); | ||
|
|
||
| await using var blobStream = storage.GetBlobStream(result.Value.FullName); | ||
|
|
||
| var chunkSize = (int) blobStream.Length / 2; | ||
| var chunk1 = new byte[chunkSize]; | ||
| var chunk2 = new byte[chunkSize]; | ||
|
|
||
| // Act | ||
| var bytesReadForChunk1 = await blobStream.ReadAsync(chunk1, 0, chunkSize); | ||
| var bytesReadForChunk2 = await blobStream.ReadAsync(chunk2, 0, chunkSize); | ||
|
|
||
| // Assert | ||
| bytesReadForChunk1.Should().Be(chunkSize); | ||
| bytesReadForChunk2.Should().Be(chunkSize); | ||
| chunk1.Should().NotBeNullOrEmpty().And.HaveCount(chunkSize); | ||
| chunk2.Should().NotBeNullOrEmpty().And.HaveCount(chunkSize); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadStream_WhenFileDoesNotExists_ReturnNoData() | ||
| { | ||
| // Arrange | ||
| var directory = "test-directory"; | ||
| var storage = (IAzureStorage) Storage; | ||
| await storage.CreateContainerAsync(); | ||
| var fullFileName = $"{directory}/{Guid.NewGuid()}.txt"; | ||
|
|
||
| await using var blobStream = storage.GetBlobStream(fullFileName); | ||
| var chunk = new byte[4]; | ||
|
|
||
| // Act | ||
| var bytesRead = await blobStream.ReadAsync(chunk, 0, 4); | ||
|
|
||
| // Assert | ||
| bytesRead.Should().Be(0); | ||
| chunk.Should().NotBeNullOrEmpty(); | ||
| chunk.Should().AllBeEquivalentTo(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WriteStreamWithStreamWriter_SaveData() | ||
| { | ||
| // Arrange | ||
| var directory = "test-directory"; | ||
| await using var localFile = LocalFile.FromRandomNameWithExtension(".txt"); | ||
| var fileSizeInBytes = 10; | ||
| FileHelper.GenerateLocalFileWithData(localFile, fileSizeInBytes); | ||
| var fullFileName = $"{directory}/{localFile.FileInfo.FullName}"; | ||
|
|
||
| var storage = (IAzureStorage) Storage; | ||
|
|
||
| await storage.CreateContainerAsync(); | ||
|
|
||
| // Act | ||
| await using (var blobStream = storage.GetBlobStream(fullFileName)) | ||
| { | ||
| await using (var localFileStream = localFile.FileStream) | ||
| { | ||
| await localFileStream.CopyToAsync(blobStream); | ||
| } | ||
| } | ||
|
|
||
| // Assert | ||
| var fileResult = await storage.DownloadAsync(fullFileName); | ||
| fileResult.IsSuccess.Should().BeTrue(); | ||
| fileResult.Value.Should().NotBeNull(); | ||
| await using var fileStream = fileResult.Value.FileStream; | ||
| using var streamReader = new StreamReader(fileStream); | ||
| var fileContent = await streamReader.ReadLineAsync(); | ||
| fileContent.Should().NotBeNullOrEmpty(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Seek_WhenFileExists_ReturnData() | ||
| { | ||
| // Arrange | ||
| var directory = "test-directory"; | ||
| var fileSizeInBytes = 10; | ||
| await using var localFile = LocalFile.FromRandomNameWithExtension(".txt"); | ||
| FileHelper.GenerateLocalFileWithData(localFile, fileSizeInBytes); | ||
| var storage = (IAzureStorage) Storage; | ||
|
|
||
| UploadOptions options = new() { FileName = localFile.Name, Directory = directory }; | ||
| await using var localFileStream = localFile.FileInfo.OpenRead(); | ||
| var result = await storage.UploadAsync(localFileStream, options); | ||
|
|
||
| await using var blobStream = storage.GetBlobStream(result.Value.FullName); | ||
|
|
||
| // Act | ||
| var seekInPosition = fileSizeInBytes / 2; | ||
| blobStream.Seek(seekInPosition, SeekOrigin.Current); | ||
| var buffer = new byte[seekInPosition]; | ||
| var bytesRead = await blobStream.ReadAsync(buffer); | ||
|
|
||
| // Assert | ||
| bytesRead.Should().Be(seekInPosition); | ||
| await using var fileStream = localFile.FileInfo.OpenRead(); | ||
| using var fileReader = new StreamReader(fileStream); | ||
| var fileContent = await fileReader.ReadToEndAsync(); | ||
| var content = Encoding.UTF8.GetString(buffer); | ||
| content.Should().NotBeNullOrEmpty(); | ||
| var trimmedFileContent = fileContent.Remove(0, seekInPosition); | ||
| content.Should().Be(trimmedFileContent); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using System.Threading.Tasks; | ||
| using DotNet.Testcontainers.Containers; | ||
| using ManagedCode.Storage.Tests.Common; | ||
| using Xunit; | ||
|
|
||
| namespace ManagedCode.Storage.Tests.Storages; | ||
|
|
||
| public abstract class StreamTests<T> : BaseContainer<T> | ||
| where T : IContainer | ||
| { | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.