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

[RSN-19] - Create Controllers #79

Merged
merged 8 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 20 additions & 145 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/CommentServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,13 @@
using ReasnAPI.Models.DTOs;
using ReasnAPI.Models.Enums;
using ReasnAPI.Services;
using System.Runtime.Versioning;

namespace ReasnAPI.Tests.Services;

[TestClass]
public class CommentServiceTests
{
[TestMethod]
public void GetCommentById_CommentExist_CommentReturned()
{
var mockContext = new Mock<ReasnContext>();

var event1 = new Event
{
Id = 1,
Name = "Event",
Description = "Description",
};

var user = new User
{
Id = 1,
Username = "Username",
Email = "Email",
Password = "Password",
};

var comment = new Comment
{
Id = 1,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
};

var fakeComment = new FakeDbSet<Comment> { comment };
var fakeEvent = new FakeDbSet<Event> { event1 };
var fakeUser = new FakeDbSet<User> { user };

mockContext.Setup(c => c.Users).Returns(fakeUser);
mockContext.Setup(c => c.Events).Returns(fakeEvent);
mockContext.Setup(c => c.Comments).Returns(fakeComment);

var commentService = new CommentService(mockContext.Object);

var result = commentService.GetCommentById(1);

Assert.IsNotNull(result);
Assert.AreEqual("Content", result.Content);
Assert.AreEqual(1, result.EventId);
Assert.AreEqual(1, result.UserId);
}

[TestMethod]
public void GetCommentById_CommentDoesNotExist_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Comments).ReturnsDbSet([]);

var commentService = new CommentService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => commentService.GetCommentById(1));
}

[TestMethod]
public void GetAllComments_CommentsExist_CommentsReturned()
{
Expand All @@ -86,22 +30,23 @@ public void GetAllComments_CommentsExist_CommentsReturned()
Id = 1,
Name = "Event",
Description = "Description",
Slug = "Slug"
};

var comment1 = new Comment
{
Id = 1,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
Event = event1,
User = user
};

var comment2 = new Comment
{
Id = 2,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
Event = event1,
User = user
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
Expand Down Expand Up @@ -148,22 +93,25 @@ public void GetCommentsByFilter_CommentsExist_CommentsReturned()
Id = 1,
Name = "Event",
Description = "Description",
Slug = "Slug"
};

var comment1 = new Comment
{
Id = 1,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
Event = event1,
EventId = 1,
User = user
};

var comment2 = new Comment
{
Id = 2,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
Event = event1,
EventId = 1,
User = user
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
Expand Down Expand Up @@ -210,6 +158,7 @@ public void CreateComment_CommentCreated_CommentReturned()
Id = 1,
Name = "Event",
Description = "Description",
Slug = "Slug",
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
Expand All @@ -221,16 +170,16 @@ public void CreateComment_CommentCreated_CommentReturned()
var commentDto = new CommentDto
{
Content = "Content",
UserId = 1,
EventId = 1
Username = user.Username,
EventSlug = event1.Slug,
};

var result = commentService.CreateComment(commentDto);
var result = commentService.CreateComment(commentDto, event1.Id, user.Id);

Assert.IsNotNull(result);
Assert.AreEqual("Content", result.Content);
Assert.AreEqual(1, result.EventId);
Assert.AreEqual(1, result.UserId);
Assert.AreEqual("Slug", result.EventSlug);
Assert.AreEqual("Username", result.Username);
}

[TestMethod]
Expand All @@ -241,81 +190,7 @@ public void CreateComment_CommentDtoIsNull_NullReturned()

var commentService = new CommentService(mockContext.Object);

Assert.ThrowsException<ArgumentNullException>(() => commentService.CreateComment(null));
}

[TestMethod]
public void UpdateComment_CommentUpdated_CommentReturned()
{
var mockContext = new Mock<ReasnContext>();

var user = new User
{
Id = 1,
Username = "Username",
Email = "Email",
Password = "Password",
};

var event1 = new Event
{
Id = 1,
Name = "Event",
Description = "Description",
};

var comment = new Comment
{
Id = 1,
Content = "Content",
EventId = event1.Id,
UserId = user.Id
};

mockContext.Setup(c => c.Users).ReturnsDbSet([user]);
mockContext.Setup(c => c.Events).ReturnsDbSet([event1]);
mockContext.Setup(c => c.Comments).ReturnsDbSet([comment]);

var commentService = new CommentService(mockContext.Object);

var result = commentService.UpdateComment(1, new CommentDto
{
Content = "UpdatedContent",
EventId = 2,
UserId = 1
});

Assert.IsNotNull(result);
Assert.AreEqual("UpdatedContent", result.Content);
Assert.AreEqual(1, result.EventId);
Assert.AreEqual(1, result.UserId);
}

[TestMethod]
public void UpdateComment_CommentDoesNotExist_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Comments).ReturnsDbSet([]);

var commentService = new CommentService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => commentService.UpdateComment(1, new CommentDto
{
Content = "UpdatedContent",
EventId = 2,
UserId = 1
}));
}

[TestMethod]
public void UpdateComment_CommentDtoIsNull_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Comments).ReturnsDbSet([]);

var commentService = new CommentService(mockContext.Object);

Assert.ThrowsException<ArgumentNullException>(() => commentService.UpdateComment(1, null));
Assert.ThrowsException<ArgumentNullException>(() => commentService.CreateComment(null, 1, 1));
}

[TestMethod]
Expand Down
12 changes: 5 additions & 7 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public void UpdateEvent_EventExists_EventUpdated()
var eventDto = new EventDto()
{
Name = "name1",
AddressId = 1,
Description = "description2",
OrganizerId = 1,
StartAt = DateTime.Now,
Expand Down Expand Up @@ -82,7 +81,7 @@ public void UpdateEvent_EventExists_EventUpdated()
mockContext.Setup(c => c.Parameters).ReturnsDbSet(new List<Parameter>());
mockContext.Setup(c => c.Comments).ReturnsDbSet(new List<Comment>());
mockContext.Setup(c => c.Participants).ReturnsDbSet(new List<Participant>());
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

var result = eventService.UpdateEvent(1, eventDto);
Assert.AreEqual("name1", result.Name);
Expand All @@ -102,7 +101,6 @@ public void UpdateEvent_EventDoesNotExist_NullReturned()
var eventDto = new EventDto()
{
Name = "name1",
AddressId = 1,
Description = "description2",
OrganizerId = 1,
StartAt = DateTime.Now,
Expand Down Expand Up @@ -144,7 +142,7 @@ public void UpdateEvent_EventDoesNotExist_NullReturned()
UpdatedAt =DateTime.Now
}});

var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

Assert.ThrowsException<NotFoundException>(() => eventService.UpdateEvent(1, eventDto));
}
Expand Down Expand Up @@ -205,7 +203,7 @@ public void GetEventById_EventExists_EventReturned()
UpdatedAt =DateTime.Now }});


var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

var result = eventService.GetEventById(1);
Assert.IsNotNull(result);
Expand Down Expand Up @@ -250,7 +248,7 @@ public void GetEventById_EventDoesNotExist_NullReturned()
}});


var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

Assert.ThrowsException<NotFoundException>(() => eventService.GetEventById(1));
}
Expand Down Expand Up @@ -320,7 +318,7 @@ public void DeleteEvent_EventExists_EventDeleted()
mockContext.Setup(c => c.Comments).ReturnsDbSet(new List<Comment>());
mockContext.Setup(c => c.Participants).ReturnsDbSet(new List<Participant>());

var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object));
var eventService = new EventService(mockContext.Object, new ParameterService(mockContext.Object), new TagService(mockContext.Object), new CommentService(mockContext.Object), new AddressService(mockContext.Object), new ImageService(mockContext.Object));

eventService.DeleteEvent(1);

Expand Down
Loading
Loading