mirror of
https://github.com/Ikatono/ComiServ.git
synced 2025-10-28 20:45:35 -05:00
fixed and reorganized tests
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ComiServ.Entities;
|
||||
using ComiServ.Services;
|
||||
|
||||
namespace ComiServUnitTest;
|
||||
|
||||
[TestClass]
|
||||
public class AuthenticationServiceTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void FailAuth()
|
||||
{
|
||||
IAuthenticationService auth = new AuthenticationService();
|
||||
Assert.IsFalse(auth.Tested);
|
||||
auth.FailAuth();
|
||||
Assert.IsTrue(auth.Tested);
|
||||
Assert.IsNull(auth.User);
|
||||
}
|
||||
[TestMethod]
|
||||
public void AuthenticateUser()
|
||||
{
|
||||
IAuthenticationService auth = new AuthenticationService();
|
||||
User user = new()
|
||||
{
|
||||
Username = "NewUser",
|
||||
UserTypeId = UserTypeEnum.User,
|
||||
};
|
||||
Assert.IsFalse(auth.Tested);
|
||||
auth.Authenticate(user);
|
||||
Assert.IsTrue(auth.Tested);
|
||||
Assert.IsNotNull(auth.User);
|
||||
Assert.AreSame(user, auth.User);
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,9 @@ public class ComicControllerTests
|
||||
ComicPage comicPage = new(PAGE_FILEPATH, PAGE_MIME, [1, 2, 3, 4, 5]);
|
||||
MockComicAnalyzer analyzer = new();
|
||||
analyzer.ComicPages.Add((Path.Join(config.LibraryRoot, comic.Filepath), PAGE_NUMBER), comicPage);
|
||||
IPictureConverter converter = new MockPictureConverter();
|
||||
//returned from all MockPictureConverter functions
|
||||
byte[] mockPictureData = [1, 2, 3, 4, 5];
|
||||
MockPictureConverter converter = new MockPictureConverter(mockPictureData);
|
||||
AuthenticationService auth = new();
|
||||
auth.Authenticate(user);
|
||||
var controller = new ComicController(
|
||||
@@ -78,10 +80,17 @@ public class ComicControllerTests
|
||||
var contents = ((FileContentResult)result).FileContents;
|
||||
Assert.IsTrue(comicPage.Data.SequenceEqual(contents));
|
||||
//invalid handle (too short)
|
||||
var result2 = await controller.GetComicFile(string.Join("", Enumerable.Repeat("A", ComicsContext.HANDLE_LENGTH - 1)));
|
||||
var result2 = await controller.GetComicPage(comic.Handle.Substring(comic.Handle.Length-1),
|
||||
PAGE_NUMBER, null, null, null);
|
||||
Assert.IsInstanceOfType<BadRequestObjectResult>(result2);
|
||||
//valid handle but doesn't exist
|
||||
var result3 = await controller.GetComicFile(string.Join("", Enumerable.Repeat("B", ComicsContext.HANDLE_LENGTH)));
|
||||
var result3 = await controller.GetComicPage(string.Join("", Enumerable.Repeat("B", ComicsContext.HANDLE_LENGTH)),
|
||||
PAGE_NUMBER, null, null, null);
|
||||
Assert.IsInstanceOfType<NotFoundObjectResult>(result3);
|
||||
//valid handle and convert
|
||||
var result4 = await controller.GetComicPage(comic.Handle, PAGE_NUMBER, 500, 500, PictureFormats.Webp);
|
||||
Assert.AreEqual(1, converter.ResizeIfBiggerCount);
|
||||
Assert.IsInstanceOfType<FileContentResult>(result4);
|
||||
Assert.IsTrue(mockPictureData.SequenceEqual(((FileContentResult)result4).FileContents));
|
||||
}
|
||||
}
|
||||
|
||||
87
ComiServUnitTest/EntityTests/PaginatedTests.cs
Normal file
87
ComiServUnitTest/EntityTests/PaginatedTests.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using ComiServ.Models;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||
using System.CodeDom;
|
||||
using System.Security.Policy;
|
||||
|
||||
namespace ComiServUnitTest.EntityTests;
|
||||
|
||||
[TestClass]
|
||||
public class PaginatedTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void PageUnderUnderflow()
|
||||
{
|
||||
const int pageNum = 1;
|
||||
const int pageSize = 20;
|
||||
const int dataSize = 30;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsTrue(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(10, page.Count);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.AreEqual(20, page.Items.First());
|
||||
Assert.AreEqual(data.Last(), page.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void PageOverflow()
|
||||
{
|
||||
const int pageNum = 2;
|
||||
const int pageSize = 30;
|
||||
const int dataSize = 150;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsFalse(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(pageSize, page.Count);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.AreEqual(pageSize * pageNum, page.Items.First());
|
||||
Assert.AreEqual(pageSize * (pageNum + 1) - 1, page.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void PageExact()
|
||||
{
|
||||
const int pageNum = 1;
|
||||
const int pageSize = 30;
|
||||
const int dataSize = 60;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsTrue(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(pageSize, page.Count);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.AreEqual(pageSize * pageNum, page.Items.First());
|
||||
Assert.AreEqual(data.Last(), page.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void PageEmpty()
|
||||
{
|
||||
const int pageNum = 0;
|
||||
const int pageSize = 10;
|
||||
const int dataSize = 0;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsTrue(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.IsFalse(page.Items.Any());
|
||||
}
|
||||
[TestMethod]
|
||||
public void PageDoesntExist()
|
||||
{
|
||||
const int pageNum = 5;
|
||||
const int pageSize = 20;
|
||||
const int dataSize = 50;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsTrue(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.IsFalse(page.Items.Any());
|
||||
}
|
||||
}
|
||||
67
ComiServUnitTest/EntityTests/TruncatedTests.cs
Normal file
67
ComiServUnitTest/EntityTests/TruncatedTests.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ComiServ.Models;
|
||||
|
||||
namespace ComiServUnitTest.EntityTests;
|
||||
|
||||
[TestClass]
|
||||
public class TruncatedTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void TruncationUnderflow()
|
||||
{
|
||||
const int truncMax = 20;
|
||||
const int dataSize = 10;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var trunc = new Truncated<int>(truncMax, data);
|
||||
Assert.IsTrue(trunc.Complete);
|
||||
Assert.AreEqual(truncMax, trunc.Max);
|
||||
Assert.AreEqual(dataSize, trunc.Count);
|
||||
Assert.AreEqual(trunc.Items.Count, trunc.Count);
|
||||
Assert.AreEqual(data.First(), trunc.Items.First());
|
||||
Assert.AreEqual(data.Last(), trunc.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void TruncationOverflow()
|
||||
{
|
||||
const int truncMax = 20;
|
||||
const int dataSize = 30;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var trunc = new Truncated<int>(truncMax, data);
|
||||
Assert.IsFalse(trunc.Complete);
|
||||
Assert.AreEqual(truncMax, trunc.Max);
|
||||
Assert.AreEqual(truncMax, trunc.Count);
|
||||
Assert.AreEqual(trunc.Items.Count, trunc.Count);
|
||||
Assert.AreEqual(data.First(), trunc.Items.First());
|
||||
Assert.AreEqual(truncMax - 1, trunc.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void TruncationExact()
|
||||
{
|
||||
const int truncMax = 5;
|
||||
const int dataSize = 5;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var trunc = new Truncated<int>(truncMax, data);
|
||||
Assert.IsTrue(trunc.Complete);
|
||||
Assert.AreEqual(truncMax, trunc.Max);
|
||||
Assert.AreEqual(truncMax, trunc.Count);
|
||||
Assert.AreEqual(trunc.Items.Count, trunc.Count);
|
||||
Assert.AreEqual(data.First(), trunc.Items.First());
|
||||
Assert.AreEqual(data.Last(), trunc.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void TruncationEmpty()
|
||||
{
|
||||
const int truncMax = 5;
|
||||
const int dataSize = 0;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var trunc = new Truncated<int>(truncMax, data);
|
||||
Assert.IsTrue(trunc.Complete);
|
||||
Assert.AreEqual(truncMax, trunc.Max);
|
||||
Assert.AreEqual(0, trunc.Count);
|
||||
Assert.AreEqual(trunc.Items.Count, trunc.Count);
|
||||
}
|
||||
}
|
||||
@@ -10,24 +10,37 @@ namespace ComiServUnitTest.Mocks
|
||||
{
|
||||
internal class MockPictureConverter : IPictureConverter
|
||||
{
|
||||
public MockPictureConverter()
|
||||
public int MakeThumbnailCount = 0;
|
||||
public int ResizeCount = 0;
|
||||
public int ResizeIfBiggerCount = 0;
|
||||
private byte[] StreamObject { get; }
|
||||
public MockPictureConverter(byte[] streamObject)
|
||||
{
|
||||
|
||||
StreamObject = streamObject;
|
||||
}
|
||||
|
||||
public Task<Stream> MakeThumbnail(Stream image)
|
||||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
public async Task<Stream> MakeThumbnail(Stream image)
|
||||
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
MakeThumbnailCount++;
|
||||
return new MemoryStream(StreamObject);
|
||||
}
|
||||
|
||||
public Task<Stream> Resize(Stream image, Size newSize, PictureFormats? newFormat = null)
|
||||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
public async Task<Stream> Resize(Stream image, Size newSize, PictureFormats? newFormat = null)
|
||||
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
ResizeCount++;
|
||||
return new MemoryStream(StreamObject);
|
||||
}
|
||||
|
||||
public Task<Stream> ResizeIfBigger(Stream image, Size maxSize, PictureFormats? newFormat = null)
|
||||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
public async Task<Stream> ResizeIfBigger(Stream image, Size maxSize, PictureFormats? newFormat = null)
|
||||
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
ResizeIfBiggerCount++;
|
||||
return new MemoryStream(StreamObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
using ComiServ.Models;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||
using System.CodeDom;
|
||||
using System.Security.Policy;
|
||||
|
||||
namespace ComiServUnitTest;
|
||||
|
||||
[TestClass]
|
||||
public class PaginatedTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void PageUnderUnderflow()
|
||||
{
|
||||
const int pageNum = 1;
|
||||
const int pageSize = 20;
|
||||
const int dataSize = 30;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsTrue(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(10, page.Count);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.AreEqual(20, page.Items.First());
|
||||
Assert.AreEqual(data.Last(), page.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void PageOverflow()
|
||||
{
|
||||
const int pageNum = 2;
|
||||
const int pageSize = 30;
|
||||
const int dataSize = 150;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsFalse(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(pageSize, page.Count);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.AreEqual(pageSize*pageNum, page.Items.First());
|
||||
Assert.AreEqual(pageSize*(pageNum+1)-1, page.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void PageExact()
|
||||
{
|
||||
const int pageNum = 1;
|
||||
const int pageSize = 30;
|
||||
const int dataSize = 60;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsTrue(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(pageSize, page.Count);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.AreEqual(pageSize * pageNum, page.Items.First());
|
||||
Assert.AreEqual(data.Last(), page.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void PageEmpty()
|
||||
{
|
||||
const int pageNum = 0;
|
||||
const int pageSize = 10;
|
||||
const int dataSize = 0;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsTrue(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.IsFalse(page.Items.Any());
|
||||
}
|
||||
[TestMethod]
|
||||
public void PageDoesntExist()
|
||||
{
|
||||
const int pageNum = 5;
|
||||
const int pageSize = 20;
|
||||
const int dataSize = 50;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var page = new Paginated<int>(pageSize, pageNum, data);
|
||||
Assert.IsTrue(page.Last);
|
||||
Assert.AreEqual(pageSize, page.Max);
|
||||
Assert.AreEqual(pageNum, page.Page);
|
||||
Assert.AreEqual(page.Items.Count, page.Count);
|
||||
Assert.IsFalse(page.Items.Any());
|
||||
}
|
||||
}
|
||||
38
ComiServUnitTest/ServiceTests/AuthenticationServiceTests.cs
Normal file
38
ComiServUnitTest/ServiceTests/AuthenticationServiceTests.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ComiServ.Entities;
|
||||
using ComiServ.Services;
|
||||
|
||||
namespace ComiServUnitTest.ServiceTests;
|
||||
|
||||
[TestClass]
|
||||
public class AuthenticationServiceTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void FailAuth()
|
||||
{
|
||||
IAuthenticationService auth = new AuthenticationService();
|
||||
Assert.IsFalse(auth.Tested);
|
||||
auth.FailAuth();
|
||||
Assert.IsTrue(auth.Tested);
|
||||
Assert.IsNull(auth.User);
|
||||
}
|
||||
[TestMethod]
|
||||
public void AuthenticateUser()
|
||||
{
|
||||
IAuthenticationService auth = new AuthenticationService();
|
||||
User user = new()
|
||||
{
|
||||
Username = "NewUser",
|
||||
UserTypeId = UserTypeEnum.User,
|
||||
};
|
||||
Assert.IsFalse(auth.Tested);
|
||||
auth.Authenticate(user);
|
||||
Assert.IsTrue(auth.Tested);
|
||||
Assert.IsNotNull(auth.User);
|
||||
Assert.AreSame(user, auth.User);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ComiServ.Models;
|
||||
|
||||
namespace ComiServUnitTest;
|
||||
|
||||
[TestClass]
|
||||
public class TruncatedTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void TruncationUnderflow()
|
||||
{
|
||||
const int truncMax = 20;
|
||||
const int dataSize = 10;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var trunc = new Truncated<int>(truncMax, data);
|
||||
Assert.IsTrue(trunc.Complete);
|
||||
Assert.AreEqual(truncMax, trunc.Max);
|
||||
Assert.AreEqual(dataSize, trunc.Count);
|
||||
Assert.AreEqual(trunc.Items.Count, trunc.Count);
|
||||
Assert.AreEqual(data.First(), trunc.Items.First());
|
||||
Assert.AreEqual(data.Last(), trunc.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void TruncationOverflow()
|
||||
{
|
||||
const int truncMax = 20;
|
||||
const int dataSize = 30;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var trunc = new Truncated<int>(truncMax, data);
|
||||
Assert.IsFalse(trunc.Complete);
|
||||
Assert.AreEqual(truncMax, trunc.Max);
|
||||
Assert.AreEqual(truncMax, trunc.Count);
|
||||
Assert.AreEqual(trunc.Items.Count, trunc.Count);
|
||||
Assert.AreEqual(data.First(), trunc.Items.First());
|
||||
Assert.AreEqual(truncMax - 1, trunc.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void TruncationExact()
|
||||
{
|
||||
const int truncMax = 5;
|
||||
const int dataSize = 5;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var trunc = new Truncated<int>(truncMax, data);
|
||||
Assert.IsTrue(trunc.Complete);
|
||||
Assert.AreEqual(truncMax, trunc.Max);
|
||||
Assert.AreEqual(truncMax, trunc.Count);
|
||||
Assert.AreEqual(trunc.Items.Count, trunc.Count);
|
||||
Assert.AreEqual(data.First(), trunc.Items.First());
|
||||
Assert.AreEqual(data.Last(), trunc.Items.Last());
|
||||
}
|
||||
[TestMethod]
|
||||
public void TruncationEmpty()
|
||||
{
|
||||
const int truncMax = 5;
|
||||
const int dataSize = 0;
|
||||
var data = Enumerable.Range(0, dataSize).ToArray();
|
||||
var trunc = new Truncated<int>(truncMax, data);
|
||||
Assert.IsTrue(trunc.Complete);
|
||||
Assert.AreEqual(truncMax, trunc.Max);
|
||||
Assert.AreEqual(0, trunc.Count);
|
||||
Assert.AreEqual(trunc.Items.Count, trunc.Count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user