Skip to content

Commit

Permalink
[RSN-37] - Created services for Event Tag Parameter Interest Image (#42)
Browse files Browse the repository at this point in the history
* Created CRUD services for Parameter, Status, Tag and C&U for Event

* created CRUD Image, Interest, Event and Parameter Services

Also added geting all elements and by filter(not done properly yet)

* changed delete services to void isnted of bool

* Fixed GetByFilter service in Event, Image, Intrest, Parameter, Status and Tag Services

* little changes

* fixing build issue and changing list to ienumerable

* Fixing EventServices and standardizing syntax issues

* created test for interest status parameter and tag

* Created ImageServicesTests

* Updated Parameter, Tag, Status services

Checked if any of Parameters, Tags or Statuses are used before delating/updating.

* fixed bug in imageservicetest

* one more fix

* Updated services and tests

Changed delete methods to bool, updated mocks on tests.

* Updated services

* Updates after scafoled nr 1

* Updated services

Changed using bridge tables to using colections in EF.

* Updated EventService

added a logic for parameters in event

* working event services

* created mappers and small fixes

* finished mappers

* Added exceptions in services

* fixed mappers according to our convension

* Changed exceptions directory location

* fixed build error

* Exceptions changes

* Unit tests fix

* updated services

* Slug creating method updated

* Update ImageService.cs

* Update ImageService.cs

* updated image services and tests also updated slug method for event services

* event service delete method updated

* refactor and fixes

* refactor

* Update EventService.cs

* last refactor

* removing spaces

* added geteventbyslug method to eventservice

* Update ImageService.cs

* refactor

* changed list to Enumerable

* changed list to enumerable

* folder refactor

* updated services

* Update EventService.cs

* changes

* Update EventServicesTest.cs

* updates

* Update EventService.cs

* Update EventService.cs

* Update ImageService.cs

* Updates

* Update EventMapper.cs

* changes

* Update ImageServiceTests.cs

* changes

* Formating

* Update EventService.cs

* Update EventService.cs

* changes

* Created AddEventComment method

* Update EventService.cs

* fixed updating and creating event

* Update EventService.cs

* changed create slug and text for event

---------

Co-authored-by: Maciej Koperdowski <[email protected]>
  • Loading branch information
bilimig and mkoper02 authored Jun 17, 2024
1 parent 2afbdf3 commit 3599d87
Show file tree
Hide file tree
Showing 23 changed files with 2,283 additions and 144 deletions.
331 changes: 331 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
using ReasnAPI.Models.DTOs;
using ReasnAPI.Services;
using ReasnAPI.Models.Database;
using Moq;
using Moq.EntityFrameworkCore;
using ReasnAPI.Models.Enums;
using ReasnAPI.Exceptions;


namespace ReasnAPI.Tests.Services
{
[TestClass]
public class EventServicesTest
{

[TestMethod]
public void UpdateEvent_EventExists_EventUpdated()
{
var tagDto = new TagDto() { Name = "tesTag" };
var tagList = new List<TagDto>
{
tagDto
};
var eventDto = new EventDto()
{
Name = "name1",
AddressId = 1,
Description = "description2",
OrganizerId = 1,
StartAt = DateTime.Now,
EndAt = DateTime.Now,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
Slug = "name1",
Status = EventStatus.Completed,
Tags = tagList,
};

var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Events).ReturnsDbSet(new List<Event>{
new Event()
{
Id = 1,
Name = "name",
AddressId = 1,
Description = "description",
OrganizerId = 1,
StartAt = DateTime.Now,
EndAt = DateTime.Now,
CreatedAt = DateTime.Now,
Slug = "name1",
UpdatedAt = DateTime.Now,
Status = EventStatus.Completed,
}});
mockContext.Setup(c => c.Tags).ReturnsDbSet(new List<Tag>());

mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List<Address>{
new Address()
{
Id = 1,
City = "city",
Country = "country",
State = "state",
Street = "street",
ZipCode = "test123"
}});
mockContext.Setup(c => c.Users).ReturnsDbSet(new List<User>{
new User()
{
Id = 1,
Name = "test",
Email = "[email protected]",
AddressId = 1,
CreatedAt = DateTime.Now,
IsActive = true, Role = UserRole.Admin,
Password ="test123",
Phone = "123123123",
Surname ="test",
Username ="test",
UpdatedAt =DateTime.Now }});

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 result = eventService.UpdateEvent(1, eventDto);
Assert.AreEqual("name1", result.Name);
Assert.AreEqual("description2", result.Description);
Assert.AreEqual(1, result.Tags.Count);

Check warning on line 90 in Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 90 in Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Dereference of a possibly null reference.

Check warning on line 90 in Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Dereference of a possibly null reference.

}

[TestMethod]
public void UpdateEvent_EventDoesNotExist_NullReturned()
{
var tagDto = new TagDto() { Name = "tesTag" };
var tagList = new List<TagDto>
{
tagDto
};
var eventDto = new EventDto()
{
Name = "name1",
AddressId = 1,
Description = "description2",
OrganizerId = 1,
StartAt = DateTime.Now,
EndAt = DateTime.Now,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
Status = EventStatus.Completed,
Tags = tagList,
};

var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Events).ReturnsDbSet(new List<Event>());
mockContext.Setup(c => c.Tags).ReturnsDbSet(new List<Tag>());

mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List<Address>{
new Address()
{
Id = 1,
City = "city",
Country = "country",
State = "state",
Street = "street",
ZipCode = "test123"
}});
mockContext.Setup(c => c.Users).ReturnsDbSet(new List<User>{
new User()
{
Id = 1,
Name = "test",
Email = "[email protected]",
AddressId = 1,
CreatedAt = DateTime.Now,
IsActive = true,
Role = UserRole.Admin,
Password ="test123",
Phone = "123123123",
Surname ="test",
Username ="test",
UpdatedAt =DateTime.Now
}});

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

Assert.ThrowsException<NotFoundException>(() => eventService.UpdateEvent(1, eventDto));
}

[TestMethod]
public void GetEventById_EventExists_EventReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Events).ReturnsDbSet(new List<Event>
{
new Event()
{
Id = 1,
Name = "name",
Slug = "name",
AddressId = 1,
Description = "description",
OrganizerId = 1,
StartAt = DateTime.Now,
EndAt = DateTime.Now,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
Status = EventStatus.Completed,
}});
mockContext.Setup(c => c.Tags).ReturnsDbSet(new List<Tag>
{
new Tag()
{
Id = 1,
Name = "name"
}
});

mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List<Address>{
new Address()
{
Id = 1,
City = "city",
Country = "country",
State = "state",
Street = "street",
ZipCode = "test123"
}});
mockContext.Setup(c => c.Users).ReturnsDbSet(new List<User>{
new User()
{
Id = 1,
Name = "test",
Email = "[email protected]",
AddressId = 1,
CreatedAt = DateTime.Now,
IsActive = true,
Role = UserRole.User,
Password ="test123",
Phone = "123123123",
Surname ="test",
Username ="test",
UpdatedAt =DateTime.Now }});


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

var result = eventService.GetEventById(1);
Assert.IsNotNull(result);

}

[TestMethod]

public void GetEventById_EventDoesNotExist_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Events).ReturnsDbSet(new List<Event>());
mockContext.Setup(c => c.Tags).ReturnsDbSet(new List<Tag>());

mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List<Address>
{
new Address()
{
Id = 1,
City = "city",
Country = "country",
State = "state",
Street = "street",
ZipCode = "test123"
}});
mockContext.Setup(c => c.Users).ReturnsDbSet(new List<User>
{
new User()
{
Id = 1,
Name = "test",
Email = "[email protected]",
AddressId = 1,
CreatedAt = DateTime.Now,
IsActive = true,
Role = UserRole.Admin,
Password ="test123",
Phone = "123123123",
Surname ="test",
Username ="test",
UpdatedAt =DateTime.Now
}});


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

Assert.ThrowsException<NotFoundException>(() => eventService.GetEventById(1));
}

[TestMethod]

public void DeleteEvent_EventExists_EventDeleted()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Events).ReturnsDbSet(new List<Event>
{
new Event()
{
Id = 1,
Name = "name",
Slug = "name",
AddressId = 1,
Description = "description",
OrganizerId = 1,
StartAt = DateTime.Now,
EndAt = DateTime.Now,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
Status = EventStatus.Completed,
Tags = new List<Tag> { new Tag { Id = 1, Name = "name" } },
Parameters = new List<Parameter> { new Parameter { Key = "key", Value = "value" } },
Comments = new List<Comment> { new Comment { Id = 1, Content = "content" } },
Participants = new List<Participant> { new Participant { Id = 1, UserId = 1 } }
}
});
mockContext.Setup(c => c.Tags).ReturnsDbSet(new List<Tag>
{
new Tag()
{
Id = 1,
Name = "name"
}
});
mockContext.Setup(c => c.Addresses).ReturnsDbSet(new List<Address>
{
new Address()
{
Id = 1,
City = "city",
Country = "country",
State = "state",
Street = "street",
ZipCode = "test123"
}
});
mockContext.Setup(c => c.Users).ReturnsDbSet(new List<User>{
new User()
{
Id = 1,
Name = "test",
Email = "[email protected]",
AddressId = 1,
CreatedAt = DateTime.Now,
IsActive = true,
Role = UserRole.User,
Password ="test123",
Phone = "123123123",
Surname ="test",
Username ="test",
UpdatedAt =DateTime.Now }});
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));

eventService.DeleteEvent(1);

mockContext.Verify(c => c.SaveChanges(), Times.AtLeastOnce);
}

}
}
17 changes: 10 additions & 7 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class FakeDbSet<T> : DbSet<T>, IAsyncEnumerable<T>

public FakeDbSet()
{
_data = [];
_data = new List<T>();
}

public override T? Find(params object[] keyValues)
public override T Find(params object[] keyValues)

Check warning on line 16 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Nullability of reference types in type of parameter 'keyValues' doesn't match overridden member.

Check warning on line 16 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Nullability of reference types in type of parameter 'keyValues' doesn't match overridden member.

Check warning on line 16 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Nullability of reference types in type of parameter 'keyValues' doesn't match overridden member.
{
return _data.FirstOrDefault();

Check warning on line 18 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Possible null reference return.

Check warning on line 18 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Possible null reference return.

Check warning on line 18 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Possible null reference return.
}
Expand All @@ -24,18 +24,21 @@ public override EntityEntry<T> Add(T entity)
return null; // Return null for simplicity, adjust as needed

Check warning on line 24 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Possible null reference return.

Check warning on line 24 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Possible null reference return.

Check warning on line 24 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Possible null reference return.
}

public override IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)

Check warning on line 27 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

'FakeDbSet<T>.GetAsyncEnumerator(CancellationToken)' hides inherited member 'DbSet<T>.GetAsyncEnumerator(CancellationToken)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 27 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

'FakeDbSet<T>.GetAsyncEnumerator(CancellationToken)' hides inherited member 'DbSet<T>.GetAsyncEnumerator(CancellationToken)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 27 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

'FakeDbSet<T>.GetAsyncEnumerator(CancellationToken)' hides inherited member 'DbSet<T>.GetAsyncEnumerator(CancellationToken)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
{
return new AsyncEnumerator<T>(_data.GetEnumerator());
}

public override Microsoft.EntityFrameworkCore.Metadata.IEntityType EntityType => throw new NotImplementedException();

// Implement other methods...

private class AsyncEnumerator<T>(IEnumerator<T> enumerator) : IAsyncEnumerator<T>
private class AsyncEnumerator<T> : IAsyncEnumerator<T>

Check warning on line 34 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'FakeDbSet<T>'

Check warning on line 34 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'FakeDbSet<T>'

Check warning on line 34 in Server/ReasnAPI/ReasnAPI.Tests/Services/FakeDbSet.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'FakeDbSet<T>'
{
private readonly IEnumerator<T> _enumerator = enumerator;
private readonly IEnumerator<T> _enumerator;

public AsyncEnumerator(IEnumerator<T> enumerator)
{
_enumerator = enumerator;
}

public ValueTask DisposeAsync()
{
Expand Down
Loading

0 comments on commit 3599d87

Please sign in to comment.