-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from SCCapstone/main
Redeploy for testing purposes
- Loading branch information
Showing
123 changed files
with
6,294 additions
and
2,099 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
namespace FU.API.Tests; | ||
|
||
using FU.API.Data; | ||
using FU.API.Exceptions; | ||
using FU.API.Models; | ||
using FU.API.Services; | ||
using FU.API.Tests.Helpers; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class PostServiceTests | ||
public class PostServiceTests : IDisposable | ||
{ | ||
private readonly DbContextOptions<AppDbContext> _contextOptions; | ||
private readonly AppDbContext _dbContext; | ||
private readonly PostService _postService; | ||
|
||
// adapted from https://github.com/dotnet/EntityFramework.Docs/blob/main/samples/core/Testing/TestingWithoutTheDatabase/InMemoryBloggingControllerTest.cs | ||
public PostServiceTests() | ||
{ | ||
// Setup database | ||
_contextOptions = new DbContextOptionsBuilder<AppDbContext>() | ||
.UseInMemoryDatabase("PostServiceTestDb") | ||
.Options; | ||
|
||
using var context = new AppDbContext(_contextOptions); | ||
|
||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
|
||
context.SaveChanges(); | ||
_dbContext = new AppDbContext(_contextOptions); | ||
_dbContext.Database.EnsureDeleted(); | ||
_dbContext.Database.EnsureCreated(); | ||
_dbContext.SaveChanges(); | ||
|
||
// Setup PostServce | ||
_dbContext = new(_contextOptions); | ||
var chatService = new ChatService(_dbContext); | ||
_postService = new PostService(_dbContext, chatService); | ||
} | ||
|
||
[Theory] | ||
|
@@ -32,12 +38,10 @@ public PostServiceTests() | |
public async void GetPostUsers_WithValidPostId_CheckUserJoined(int postId, int checkUserId, bool expectedJoined) | ||
{ | ||
// Arrange | ||
var context = CreateContext(); | ||
|
||
var testUsers = MockDataHelper.CreateTestUsers(); | ||
var testChat = MockDataHelper.CreateTestChat(testUsers); | ||
context.Set<Chat>().Add(testChat); | ||
context.Set<ApplicationUser>().AddRange(testUsers); | ||
_dbContext.Set<Chat>().Add(testChat); | ||
_dbContext.Set<ApplicationUser>().AddRange(testUsers); | ||
|
||
var post = new Post() | ||
{ | ||
|
@@ -50,33 +54,24 @@ public async void GetPostUsers_WithValidPostId_CheckUserJoined(int postId, int c | |
Chat = testChat, | ||
}; | ||
|
||
context.Set<Post>().Add(post); | ||
context.SaveChanges(); | ||
|
||
|
||
var chatService = new ChatService(context); | ||
var postService = new PostService(context, chatService); | ||
_dbContext.Set<Post>().Add(post); | ||
_dbContext.SaveChanges(); | ||
|
||
// Act | ||
var postUsers = await postService.GetPostUsers(postId); | ||
var postUsers = await _postService.GetPostUsers(postId); | ||
|
||
// Assert | ||
var joined = postUsers.Any(u => u.UserId == checkUserId); | ||
Assert.Equal(expectedJoined, joined); | ||
Assert.Equal(6, postUsers.Count()); | ||
} | ||
|
||
AppDbContext CreateContext() => new(_contextOptions); | ||
|
||
[Fact] | ||
public async void CreatePost_WithValidParams_ReturnsCreated() | ||
{ | ||
// Arrange | ||
using var context = CreateContext(); | ||
var gameService = new GameService(context); | ||
var chatService = new ChatService(context); | ||
var postService = new PostService(context, chatService); | ||
ApplicationUser user = await TestsHelper.CreateUserAsync(context); | ||
var gameService = new GameService(_dbContext); | ||
ApplicationUser user = await TestsHelper.CreateUserAsync(_dbContext); | ||
|
||
// Act | ||
Game game = await gameService.CreateGame("Game Title"); | ||
|
@@ -88,7 +83,7 @@ public async void CreatePost_WithValidParams_ReturnsCreated() | |
Creator = user, | ||
CreatorId = user.UserId, | ||
}; | ||
var createdPost = await postService.CreatePost(post); | ||
var createdPost = await _postService.CreatePost(post); | ||
|
||
// Assert | ||
Assert.Equal(post.Title, createdPost.Title); | ||
|
@@ -99,12 +94,27 @@ public async void CreatePost_WithValidParams_ReturnsCreated() | |
public async void UpdatePost_WithValidParams_ReturnsUpdated() | ||
{ | ||
// Arrange | ||
using var context = CreateContext(); | ||
var gameService = new GameService(context); | ||
var chatService = new ChatService(context); | ||
var postService = new PostService(context, chatService); | ||
ApplicationUser user = await TestsHelper.CreateUserAsync(context); | ||
var createdPost = await TestsHelper.CreateTestPostAsync(_dbContext); | ||
|
||
// Act | ||
createdPost.Description = "Description Text 2"; | ||
var updatedPost = await _postService.UpdatePost(createdPost); | ||
|
||
// Assert | ||
Assert.Equal(createdPost.Description, updatedPost.Description); | ||
} | ||
|
||
[Theory] | ||
[InlineData("2022-01-01T00:00:00", "2022-01-01T00:00:00")] // Date in the past | ||
[InlineData("2025-01-01T00:00:01", "2025-01-01T00:00:00")] // End time before start time | ||
[InlineData("9999-01-01T00:00:00", "9999-01-01T00:00:01")] // Date far in the future | ||
public async void CreatePost_InvalidDate(DateTime startTime, DateTime endTime) | ||
{ | ||
// Arrange | ||
var gameService = new GameService(_dbContext); | ||
ApplicationUser user = await TestsHelper.CreateUserAsync(_dbContext); | ||
|
||
// Act | ||
Game game = await gameService.CreateGame("Game Title"); | ||
Post post = new() | ||
{ | ||
|
@@ -113,14 +123,76 @@ public async void UpdatePost_WithValidParams_ReturnsUpdated() | |
GameId = game.Id, | ||
Creator = user, | ||
CreatorId = user.UserId, | ||
StartTime = startTime, | ||
EndTime = endTime, | ||
}; | ||
var createdPost = await postService.CreatePost(post); | ||
|
||
// Calling create post should throw a PostException | ||
await Assert.ThrowsAsync<PostException>(async () => await _postService.CreatePost(post)); | ||
} | ||
|
||
[Fact] | ||
public async void GetPostUsers_WithMultipleUsers_ReturnsCorrectUserCount() | ||
{ | ||
// Arrange | ||
// Note: CreateTestPostAsync creates one user as part of creating a post | ||
Post post = await TestsHelper.CreateTestPostAsync(_dbContext); | ||
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "pass2", Email = "[email protected]" }); | ||
await _postService.JoinPost(post.Id, user2); | ||
|
||
// Act | ||
createdPost.Description = "Description Text 2"; | ||
var updatedPost = await postService.UpdatePost(createdPost); | ||
var posts = await _postService.GetPostUsers(post.Id); | ||
|
||
// Assert | ||
Assert.Equal(createdPost.Description, updatedPost.Description); | ||
Assert.Equal(2, posts.Count()); | ||
} | ||
|
||
[Fact] | ||
public async void DeletePost_WithValidRequest_DeletesPost() | ||
{ | ||
// Arrange | ||
Post post = await TestsHelper.CreateTestPostAsync(_dbContext); | ||
|
||
// Act | ||
await _postService.DeletePost(post.Id); | ||
|
||
// Assert | ||
var posts = await _postService.GetPostUsers(post.Id); | ||
Assert.Empty(posts); | ||
} | ||
|
||
[Fact] | ||
public async void JoinPost_WhenAlreadyMember_ThrowsConflictException() | ||
{ | ||
// Arrange | ||
Post post = await TestsHelper.CreateTestPostAsync(_dbContext); | ||
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "pass2", Email = "[email protected]" }); | ||
await _postService.JoinPost(post.Id, user2); | ||
|
||
// Act & Assert | ||
await Assert.ThrowsAnyAsync<ExceptionWithResponse>(async () => | ||
await _postService.JoinPost(post.Id, user2)); | ||
|
||
} | ||
|
||
[Fact] | ||
public async void LeavePost_WhenMember_LeavesPost() | ||
{ | ||
// Arrange | ||
Post post = await TestsHelper.CreateTestPostAsync(_dbContext); | ||
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "pass2", Email = "[email protected]" }); | ||
await _postService.JoinPost(post.Id, user2); | ||
|
||
// Act | ||
await _postService.LeavePost(post.Id, user2); | ||
|
||
// Assert | ||
var posts = await _postService.GetPostUsers(post.Id); | ||
Assert.DoesNotContain(user2, posts); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_dbContext.Dispose(); | ||
} | ||
} |
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
Oops, something went wrong.