Skip to content
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

Introduced xUnit testing #923

Draft
wants to merge 13 commits into
base: develop
Choose a base branch
from
6 changes: 6 additions & 0 deletions Gordon360.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestsGordon360", "UnitTestsGordon360\UnitTestsGordon360.csproj", "{DB19BBAF-B279-4884-B215-2FB9E1C07F6B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{47DF568B-4E41-4398-BD88-B6BAB507334A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47DF568B-4E41-4398-BD88-B6BAB507334A}.Release|Any CPU.Build.0 = Release|Any CPU
{47DF568B-4E41-4398-BD88-B6BAB507334A}.Release|Any CPU.Deploy.0 = Release|Any CPU
{DB19BBAF-B279-4884-B215-2FB9E1C07F6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB19BBAF-B279-4884-B215-2FB9E1C07F6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB19BBAF-B279-4884-B215-2FB9E1C07F6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB19BBAF-B279-4884-B215-2FB9E1C07F6B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
24 changes: 18 additions & 6 deletions Gordon360/Documentation/Gordon360.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions UnitTestsGordon360/AuthenticationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Gordon360;
using Gordon360.Static.Names;
using Gordon360.Authorization;

// Tests function names are made of three parts:
// 1. The name of the method being tested
// 2. The scenario under which it's being tested
// 3. The expected behavior when the scenario is invoked
//
// Body of test should have three parts:
// 1. Arrange your objects, create and set them up
// 2. Act on an object
// 3. Assert that something is as expected

namespace UnitTestsGordon360
{
public class AuthenticationTests
{
[Theory]
// [InlineData(Resource.NEWS)] // Resource.NEWS
[InlineData(Resource.SLIDER)] // Resource.SLIDER
public void CanReadPublic_X_Y(string resource)
{
// Arrange
var result = true;

// Act
//var result = StateYourBusiness.CanReadPublic(resource);

// Here is the body of CanReadPublic() but with some changes just to see if this
// test would run at all. (I was learning how namespaces are implemented in C#)
switch (resource)
{
case Resource.SLIDER:
result = true;
break;
case Resource.NEWS:
result = false;
break;
default:
result = false;
break;
}

// Assert
Assert.True(result); // Should be true for NEWS
}
}
}
216 changes: 216 additions & 0 deletions UnitTestsGordon360/ImageUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
using Gordon360.Exceptions;
// using UnitTestsGordon360.Images;
using Microsoft.AspNetCore.Http;
// using Moq;
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using Xunit;
using Gordon360.Models.CCT;

namespace Gordon360.Utilities.Tests
{
public class ImageUtilsTests
{
// May need to use the following commented-out code later
// private const string TestImagePath = "c:\\Users\\arabella.ji\\Source\\Repos\\gordon-cs\\gordon-360-api\\UnitTestsGordon360\\activityImage.png";
private const string TestImagePath = "\\\\cftrain1\\photos\\testimage.png";
//private const string TestImageData = "base64-encoded-image-data";
private const string TestImageData = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABlBMVEVyhbP///+V8u2BAAAAAWJLR0QB/wIt3gAAAAlwSFlzAAALEgAACxIB0t1+/AAAAAd0SU1FB+cGFxQSHPLBPCwAAAAKSURBVAjXY2AAAAACAAHiIbwzAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDIzLTA1LTI1VDE4OjQyOjAwKzAwOjAw1iuafQAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNi0wOC0xOFQxNjoxNTo0MSswMDowMLyx2xsAAAAASUVORK5CYII=";
private const string ValidImagePath = "\\\\cftrain1\\photos\\21607000171196.jpg";
private const string ValidImageData = "base64-encoded-image-data";
private const string NonExistingImagePath = "path/to/non/existing/image.jpg";
private const string DefaultImageURL = "https://example.com/default-image.jpg";
// private const string TestImageData = UnitTestsGordon360.

[Fact]
public void DeleteImage_ShouldDeleteImageFromPath()
{
// Arrange
File.Create(TestImagePath).Dispose(); // Create a test image file

// Act
ImageUtils.DeleteImage(TestImagePath);

// Assert
Assert.False(File.Exists(TestImagePath));
}

[Fact]
public void RetrieveImageFromPath_WithValidImagePath_ShouldReturnImageData()
{
// Arrange
ImageUtils.UploadImage(TestImagePath, TestImageData, ImageFormat.Png);

// Act & Assert
Assert.Throws<Exception>(() => ImageUtils.DeleteImage(ValidImagePath));
}

[Fact]
public void RetrieveImageFromPath_ExistingImagePath_ReturnsBase64Data()
{
// Arrange
string imagePath = "path/to/image.jpg";
File.Create(imagePath).Dispose();

// Act
string imageData = ImageUtils.RetrieveImageFromPath(TestImagePath);

// Assert
Assert.Equal(imageData, TestImageData);
}

[Fact]
public void RetrieveImageFromPath_WithNullImagePath_ShouldReturnEmptyString()
{
// Act
string imageData = ImageUtils.RetrieveImageFromPath(null);

// Assert
Assert.Equal(string.Empty, imageData);
}

[Fact]
public void UploadImage_WithValidImageData_ShouldSaveImage()
{
// Arrange
string imagePath = "path/to/save/image.jpg";
string imageData = "base64-encoded-image-data";

// Act
ImageUtils.UploadImage(TestImagePath, TestImageData, ImageFormat.Jpeg);

// Assert
Assert.True(File.Exists(TestImagePath));
}

[Fact]
public async Task UploadImageAsync_WithValidImageFile_ShouldSaveImage()
{
// Arrange
using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
writer.Write(TestImageData);
writer.Flush();
stream.Position = 0;
var formFile = new FormFile(stream, 0, stream.Length, "image", "image.jpg");

// Act
ImageUtils.UploadImageAsync(TestImagePath, formFile);

// Assert
await Task.Delay(500); // Wait for the file to be saved asynchronously
Assert.True(File.Exists(TestImagePath));
}

/*
[Fact]
public async Task DownloadImageFromURL_WithValidURL_ShouldReturnImageData()
{
// Arrange
string testImageURL = "https://example.com/image.jpg";
var httpClientMock = new Mock<HttpClientWrapper>();
httpClientMock.Setup(m => m.GetAsync(testImageURL))
.ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent(TestImageData)
});

// Act
string imageData = await ImageUtils.DownloadImageFromURL(testImageURL, httpClientMock.Object);

// Assert
Assert.Equal(TestImageData, imageData);
}
*/

[Fact]
public async Task GetImageFromPathOrDefaultFromURL_WithExistingImagePath_ShouldRetrieveImage()
{
// Arrange
File.Create(TestImagePath).Dispose(); // Create a test image file

// Act
string imageData = await ImageUtils.GetImageFromPathOrDefaultFromURL(TestImagePath, DefaultImageURL);

// Assert
Assert.Equal(TestImageData, imageData);
}

/*
[Fact]
public async Task GetImageFromPathOrDefaultFromURL_WithNonExistingImagePath_ShouldDownloadDefaultImage()
{
// Arrange
var httpClientMock = new Mock<HttpClientWrapper>();
httpClientMock.Setup(m => m.GetAsync(DefaultImageURL))
.ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent(TestImageData)
});

// Act
string imageData = await ImageUtils.GetImageFromPathOrDefaultFromURL(NonExistingImagePath, DefaultImageURL, httpClientMock.Object);

// Assert
Assert.Equal(TestImageData, imageData);
}
*/

[Fact]
public void GetImageFormat_WithValidImageFile_ShouldReturnFormatExtensionAndFormat()
{
// Arrange
using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
writer.Write(TestImageData);
writer.Flush();
stream.Position = 0;
var formFile = new FormFile(stream, 0, stream.Length, "image", "image.jpg");

// Act
var (formatExtension, format) = ImageUtils.GetImageFormat(formFile);

// Assert
Assert.Equal("jpg", formatExtension);
Assert.Equal(ImageFormat.Jpeg, format);
}

[Fact]
public void GetImageFormat_WithInvalidImageFile_ShouldThrowBadInputException()
{
// Arrange
using var stream = new MemoryStream();
var formFile = new FormFile(stream, 0, stream.Length, "image", "image.txt");

// Act & Assert
Assert.Throws<BadInputException>(() => ImageUtils.GetImageFormat(formFile));
}

[Fact]
public void GetImageFormat_WithValidBase64Image_ShouldReturnFormatExtensionAndFormat()
{
// Arrange
string base64Image = $"data:image/jpeg;base64,{TestImageData}";

// Act
var (formatExtension, format, imageData) = ImageUtils.GetImageFormat(base64Image);

// Assert
Assert.Equal("jpeg", formatExtension);
Assert.Equal(ImageFormat.Jpeg, format);
Assert.Equal(TestImageData, imageData);
}

[Fact]
public void GetImageFormat_WithInvalidBase64Image_ShouldThrowBadInputException()
{
// Arrange
string base64Image = "invalid-base64-image";

// Act & Assert
Assert.Throws<BadInputException>(() => ImageUtils.GetImageFormat(base64Image));
}
}
}
Binary file added UnitTestsGordon360/Images/testImageOnePixel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions UnitTestsGordon360/Images/testImageOnePixelBase64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UnitTestsGordon360.Images
{
// public string base64Data = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABlBMVEVyhbP///+V8u2BAAAAAWJLR0QB/wIt3gAAAAlwSFlzAAALEgAACxIB0t1+/AAAAAd0SU1FB+cGFxQSHPLBPCwAAAAKSURBVAjXY2AAAAACAAHiIbwzAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDIzLTA1LTI1VDE4OjQyOjAwKzAwOjAw1iuafQAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNi0wOC0xOFQxNjoxNTo0MSswMDowMLyx2xsAAAAASUVORK5CYII=";
public class testImageOnePixelBase64
{
public string base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABlBMVEVyhbP///+V8u2BAAAAAWJLR0QB/wIt3gAAAAlwSFlzAAALEgAACxIB0t1+/AAAAAd0SU1FB+cGFxQSHPLBPCwAAAAKSURBVAjXY2AAAAACAAHiIbwzAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDIzLTA1LTI1VDE4OjQyOjAwKzAwOjAw1iuafQAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNi0wOC0xOFQxNjoxNTo0MSswMDowMLyx2xsAAAAASUVORK5CYII=";
}
}
31 changes: 31 additions & 0 deletions UnitTestsGordon360/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Gordon360;

namespace UnitTestsGordon360
{
public class UnitTest1
{
[Fact]
public void Test1_ThisIsNotAGoodName()
{
Assert.Equal(1, 1);
}

[Theory]
[InlineData(1)]
public void IsOdd_OddInteger_ReturnsTrue(int value)
{
Assert.True(IsOdd(value));
}

[Theory,InlineData(2)]
public void IsOdd_EvenInteger_ReturnsFalse(int value)
{
Assert.False(IsOdd(value));
}

bool IsOdd(int value)
{
return value % 2 == 1;
}
}
}
Loading