-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Image upload/download tests DynamicsValue/fake-xrm-easy#157
- Loading branch information
1 parent
b195f5e
commit 6cd45ff
Showing
14 changed files
with
692 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
3 changes: 1 addition & 2 deletions
3
.../FileStorage/Db/FileUploadSessionTests.cs → ...torage/Db/Files/FileUploadSessionTests.cs
This file contains 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 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 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 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
71 changes: 71 additions & 0 deletions
71
tests/FakeXrmEasy.Core.Tests/FileStorage/Db/Images/DeleteFileTests.cs
This file contains 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,71 @@ | ||
using System; | ||
using DataverseEntities; | ||
using FakeXrmEasy.Core.Db; | ||
using FakeXrmEasy.Core.FileStorage.Db; | ||
using FakeXrmEasy.Core.FileStorage.Db.Exceptions; | ||
using FakeXrmEasy.Core.FileStorage.Download; | ||
using Microsoft.Xrm.Sdk; | ||
using Xunit; | ||
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment; | ||
|
||
namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Images | ||
{ | ||
public class DeleteFileTests | ||
{ | ||
private const string IMAGE_ATTRIBUTE_NAME = "dv_image"; | ||
|
||
private readonly InMemoryDb _db; | ||
private readonly InMemoryFileDb _fileDb; | ||
private readonly Entity _entity; | ||
private readonly FileAttachment _file; | ||
|
||
public DeleteFileTests() | ||
{ | ||
_db = new InMemoryDb(); | ||
_fileDb = new InMemoryFileDb(_db); | ||
|
||
_entity = new Entity(dv_test.EntityLogicalName) | ||
{ | ||
Id = Guid.NewGuid(), | ||
}; | ||
|
||
_file = new FileAttachment() | ||
{ | ||
Id = Guid.NewGuid().ToString(), | ||
MimeType = "image/png", | ||
FileName = "MyImage.png", | ||
Target = _entity.ToEntityReference(), | ||
AttributeName = IMAGE_ATTRIBUTE_NAME, | ||
Content = new byte[] { 1, 2, 3, 4 } | ||
}; | ||
|
||
_entity[IMAGE_ATTRIBUTE_NAME] = _file.Id; | ||
} | ||
|
||
[Fact] | ||
public void Should_be_empty_when_no_files_are_added() | ||
{ | ||
Assert.Empty(_fileDb.GetAllFiles()); | ||
} | ||
|
||
[Fact] | ||
public void Should_throw_exception_when_a_file_doesnt_exists() | ||
{ | ||
Assert.Throws<CouldNotDeleteFileException>(() => _fileDb.DeleteFile("invalid id")); | ||
} | ||
|
||
[Fact] | ||
public void Should_delete_an_existing_file() | ||
{ | ||
_db.AddEntityRecord(_entity); | ||
_fileDb.AddFile(_file); | ||
|
||
_fileDb.DeleteFile(_file.Id); | ||
|
||
Assert.Empty(_fileDb.GetAllFiles()); | ||
|
||
var entityAfter = _db.GetTable(_entity.LogicalName).GetById(_entity.Id); | ||
Assert.Null(entityAfter[_file.AttributeName]); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
tests/FakeXrmEasy.Core.Tests/FileStorage/Db/Images/FileUploadSessionTests.cs
This file contains 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,95 @@ | ||
using System; | ||
using FakeXrmEasy.Core.FileStorage.Db; | ||
using FakeXrmEasy.Core.FileStorage.Db.Exceptions; | ||
using FakeXrmEasy.Core.FileStorage.Upload; | ||
using Xunit; | ||
|
||
namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Images | ||
{ | ||
public class FileUploadSessionTests | ||
{ | ||
private readonly FileUploadSession _session; | ||
|
||
public FileUploadSessionTests() | ||
{ | ||
_session = new FileUploadSession() { FileUploadSessionId = new Guid().ToString() }; | ||
} | ||
|
||
[Fact] | ||
public void Should_be_empty_when_created() | ||
{ | ||
Assert.Empty(_session.GetAllBlocks()); | ||
} | ||
|
||
[Fact] | ||
public void Should_add_new_block_to_existing_session() | ||
{ | ||
var blockId = Guid.NewGuid().ToString(); | ||
_session.AddFileBlock(new UploadBlockProperties() | ||
{ | ||
BlockId = blockId, | ||
BlockContents = new byte[] { 1, 2, 3, 4 }, | ||
FileContinuationToken = _session.FileUploadSessionId | ||
}); | ||
|
||
var fileBlock = _session.GetFileBlock(blockId); | ||
Assert.NotNull(fileBlock); | ||
Assert.Equal(new byte[] {1 , 2, 3, 4}, fileBlock.Content); | ||
} | ||
|
||
[Fact] | ||
public void Should_throw_exception_if_a_block_already_exists() | ||
{ | ||
var blockId = Guid.NewGuid().ToString(); | ||
var uploadBlockProperties = new UploadBlockProperties() | ||
{ | ||
BlockId = blockId, | ||
BlockContents = new byte[] { 1, 2, 3, 4 }, | ||
FileContinuationToken = _session.FileUploadSessionId | ||
}; | ||
|
||
//First attempt ok | ||
_session.AddFileBlock(uploadBlockProperties); | ||
|
||
//Second fails | ||
Assert.Throws<DuplicateFileBlockException>(() => _session.AddFileBlock(uploadBlockProperties)); | ||
} | ||
|
||
[Fact] | ||
public void Should_convert_file_upload_session_to_file_attachment() | ||
{ | ||
//Add two blocks | ||
var blockId1 = Guid.NewGuid().ToString(); | ||
var uploadBlockProperties = new UploadBlockProperties() | ||
{ | ||
BlockId = blockId1, | ||
BlockContents = new byte[] { 1, 2, 3, 4 }, | ||
FileContinuationToken = _session.FileUploadSessionId | ||
}; | ||
_session.AddFileBlock(uploadBlockProperties); | ||
|
||
var blockId2 = Guid.NewGuid().ToString(); | ||
uploadBlockProperties = new UploadBlockProperties() | ||
{ | ||
BlockId = blockId2, | ||
BlockContents = new byte[] { 5, 6, 7, 8 }, | ||
FileContinuationToken = _session.FileUploadSessionId | ||
}; | ||
_session.AddFileBlock(uploadBlockProperties); | ||
|
||
var commitProperties = new CommitFileUploadSessionProperties() | ||
{ | ||
FileUploadSessionId = _session.FileUploadSessionId, | ||
FileName = "MyImage.png", | ||
MimeType = "image/png", | ||
BlockIdsListSequence = new[] { blockId2, blockId1 } //2 comes first | ||
}; | ||
|
||
var fileAttachment = _session.ToFileAttachment(commitProperties); | ||
Assert.NotNull(fileAttachment); | ||
Assert.Equal(new byte[] { 5, 6, 7, 8, 1, 2, 3, 4 }, fileAttachment.Content); | ||
Assert.Equal(commitProperties.FileName, fileAttachment.FileName); | ||
Assert.Equal(commitProperties.MimeType, fileAttachment.MimeType); | ||
} | ||
} | ||
} |
Oops, something went wrong.