Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tschumpr committed Sep 26, 2024
1 parent 94ecbe8 commit 4695db6
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ services:
ports:
- 9000:9000
- 9002:9002
dataextraction:
image: borehole-api
restart: unless-stopped
ports:
- 8000:8000
environment:
AWS_ENDPOINT: http://minio:9000
AWS_ACCESS_KEY_ID: REDSQUIRREL
AWS_SECRET_ACCESS_KEY: YELLOWMONKEY
AWS_S3_BUCKET: "cannonflea"
db:
image: postgis/postgis:15-3.4-alpine
restart: unless-stopped
Expand Down
4 changes: 2 additions & 2 deletions src/api/Controllers/BoreholeFileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public async Task<IActionResult> GetDataExtractionFileInfo([Required, Range(1, i
{
var result = await boreholeFileCloudService.GetDataExtractionImageInfo(fileUuid, index).ConfigureAwait(false);

return Ok(new
return Ok(new DataExtractionInfo
{
fileName = result.FileName,
width = result.Width,
Expand All @@ -129,7 +129,7 @@ public async Task<IActionResult> GetDataExtractionFileInfo([Required, Range(1, i
{
if (ex.Message.Contains("The specified key does not exist.", StringComparison.CurrentCulture))
{
return Ok(new
return Ok(new DataExtractionInfo
{
fileName = fileUuid,
width = 0,
Expand Down
11 changes: 11 additions & 0 deletions src/api/Models/DataExtractionInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;

namespace BDMS.Models;

public class DataExtractionInfo
{
public string fileName { get; set; }

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

Check failure on line 7 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

public int width { get; set; }

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

Check failure on line 8 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

public int height { get; set; }

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

Check failure on line 9 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

public int count { get; set; }

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

Check failure on line 10 in src/api/Models/DataExtractionInfo.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

}
12 changes: 12 additions & 0 deletions tests/BDMS.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,17 @@
<None Update="TestData\white space.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\labeling_attachment.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\labeling_attachment-1.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\labeling_attachment-2.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\labeling_attachment-3.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
100 changes: 100 additions & 0 deletions tests/Controllers/BoreholeFileControllerTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Amazon.S3;
using Amazon.S3.Model;
using Azure;
using BDMS.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
Expand All @@ -12,6 +15,7 @@

namespace BDMS.Controllers;

[DeploymentItem("TestData")]
[TestClass]
public class BoreholeFileControllerTest
{
Expand Down Expand Up @@ -328,6 +332,102 @@ public async Task UpdateWithBoreholeFileNotFound()
ActionResultAssert.IsNotFound(result);
}

[TestMethod]
public async Task GetDataExtractionInfo()
{
// Test setup
var minBoreholeId = context.Boreholes.Min(b => b.Id);
var labelingFile = GetFormFileByExistingFile("labeling_attachment.pdf");
var uploadResult = await controller.Upload(labelingFile, minBoreholeId);
ActionResultAssert.IsOk(uploadResult);
var file = (uploadResult as OkObjectResult)?.Value as BoreholeFile;
var fileUuid = file.File.NameUuid.Replace(".pdf","");

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (3)

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (2)

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (4)

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (5)

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)

Check failure on line 344 in tests/Controllers/BoreholeFileControllerTest.cs

View workflow job for this annotation

GitHub Actions / Run cypress tests (1)


var image1 = GetFormFileByExistingFile("labeling_attachment-1.png");
await boreholeFileCloudService.UploadObject(image1, $"dataextraction/{fileUuid}-1.png");
var image2 = GetFormFileByExistingFile("labeling_attachment-2.png");
await boreholeFileCloudService.UploadObject(image2, $"dataextraction/{fileUuid}-2.png");
var image3 = GetFormFileByExistingFile("labeling_attachment-3.png");
await boreholeFileCloudService.UploadObject(image3, $"dataextraction/{fileUuid}-3.png");

// Test
var result = await controller.GetDataExtractionFileInfo(file.FileId, 1);
ActionResultAssert.IsOk(result);
var dataExtractionInfo = (result as OkObjectResult)?.Value as DataExtractionInfo;
Assert.IsNotNull(dataExtractionInfo);
Assert.AreEqual($"{fileUuid}-1.png", dataExtractionInfo.fileName);
Assert.AreEqual(1786, dataExtractionInfo.width);
Assert.AreEqual(2526, dataExtractionInfo.height);
Assert.AreEqual(3, dataExtractionInfo.count);

// Reset data
await boreholeFileCloudService.DeleteObject($"dataextraction/{fileUuid}-1.png");
await boreholeFileCloudService.DeleteObject($"dataextraction/{fileUuid}-2.png");
await boreholeFileCloudService.DeleteObject($"dataextraction/{fileUuid}-3.png");
}

[TestMethod]
public async Task GetDataExtractionInfoFileNoPngFound()
{
// Test setup
var minBoreholeId = context.Boreholes.Min(b => b.Id);
var labelingFile = GetFormFileByExistingFile("labeling_attachment.pdf");
var uploadResult = await controller.Upload(labelingFile, minBoreholeId);
ActionResultAssert.IsOk(uploadResult);
var file = (uploadResult as OkObjectResult)?.Value as BoreholeFile;
var fileUuid = file.File.NameUuid.Replace(".pdf", "");

// Test
var result = await controller.GetDataExtractionFileInfo(file.FileId, 1);
ActionResultAssert.IsOk(result);
var dataExtractionInfo = (result as OkObjectResult)?.Value as DataExtractionInfo;
Assert.IsNotNull(dataExtractionInfo);
Assert.AreEqual(fileUuid, dataExtractionInfo.fileName);
Assert.AreEqual(0, dataExtractionInfo.width);
Assert.AreEqual(0, dataExtractionInfo.height);
Assert.AreEqual(0, dataExtractionInfo.count);
}

[TestMethod]
public async Task GetDataExtractionInfoFileNotFound()
{
var result = await controller.Update(new BoreholeFileUpdate(), 1, 1);
ActionResultAssert.IsNotFound(result);
}

[TestMethod]
public async Task GetDataExtractionImage()
{
// Test setup
var minBoreholeId = context.Boreholes.Min(b => b.Id);
var labelingFile = GetFormFileByExistingFile("labeling_attachment.pdf");
var uploadResult = await controller.Upload(labelingFile, minBoreholeId);
ActionResultAssert.IsOk(uploadResult);
var file = (uploadResult as OkObjectResult)?.Value as BoreholeFile;
var fileUuid = file.File.NameUuid.Replace(".pdf", "");

var image1 = GetFormFileByExistingFile("labeling_attachment-1.png");
await boreholeFileCloudService.UploadObject(image1, $"dataextraction/{fileUuid}-1.png");

// Test
var response = await controller.GetDataExtractionImage($"{fileUuid}-1.png");
Assert.IsNotNull(response);
var fileContentResult = (FileContentResult)response;
Assert.AreEqual("image/png", fileContentResult.ContentType);

byte[] originalBytes = new byte[image1.Length];
using (var ms = new MemoryStream())
{
image1.CopyTo(ms);
originalBytes = ms.ToArray();
}

CollectionAssert.AreEqual(originalBytes, fileContentResult.FileContents);

// Reset data
await boreholeFileCloudService.DeleteObject($"dataextraction/{fileUuid}-1.png");
}

private async Task AssertIsBadRequestResponse(Func<Task<IActionResult>> action)
{
var result = await action();
Expand Down
Binary file added tests/TestData/labeling_attachment-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/TestData/labeling_attachment-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/TestData/labeling_attachment-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/TestData/labeling_attachment.pdf
Binary file not shown.

0 comments on commit 4695db6

Please sign in to comment.