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-37] - Created services #42

Merged
merged 65 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
ea37bc7
Created CRUD services for Parameter, Status, Tag and C&U for Event
bilimig Mar 30, 2024
bbe1798
created CRUD Image, Interest, Event and Parameter Services
bilimig Mar 30, 2024
9b0381f
changed delete services to void isnted of bool
bilimig Apr 2, 2024
e101e09
Fixed GetByFilter service in Event, Image, Intrest, Parameter, Status…
bilimig Apr 3, 2024
690a93c
little changes
bilimig Apr 13, 2024
4332abe
fixing build issue and changing list to ienumerable
bilimig Apr 13, 2024
a7495d1
Fixing EventServices and standardizing syntax issues
bilimig Apr 14, 2024
6185066
created test for interest status parameter and tag
bilimig Apr 17, 2024
db6a70f
Created ImageServicesTests
bilimig Apr 18, 2024
ce92d22
Updated Parameter, Tag, Status services
bilimig May 1, 2024
63f3d77
fixed bug in imageservicetest
bilimig May 2, 2024
e760dbb
one more fix
bilimig May 2, 2024
196ef61
Updated services and tests
bilimig May 6, 2024
7784b40
Updated services
bilimig May 7, 2024
ae26b70
Updates after scafoled nr 1
bilimig May 27, 2024
fc48408
Updated services
bilimig May 30, 2024
ad78d8b
Updated EventService
bilimig May 30, 2024
f19e208
working event services
bilimig Jun 6, 2024
2428425
created mappers and small fixes
bilimig Jun 9, 2024
095dd7d
finished mappers
bilimig Jun 9, 2024
028d043
Added exceptions in services
mkoper02 Jun 9, 2024
687a5bd
fixed mappers according to our convension
bilimig Jun 9, 2024
17ef41a
Changed exceptions directory location
mkoper02 Jun 9, 2024
5cc818a
fixed build error
bilimig Jun 9, 2024
3021123
Exceptions changes
mkoper02 Jun 9, 2024
bf25afd
Unit tests fix
mkoper02 Jun 9, 2024
a52a1d3
updated services
bilimig Jun 10, 2024
c64189f
Slug creating method updated
bilimig Jun 10, 2024
342ab16
Update ImageService.cs
bilimig Jun 10, 2024
68f4c17
Update ImageService.cs
bilimig Jun 10, 2024
faa050a
updated image services and tests also updated slug method for event s…
bilimig Jun 10, 2024
7bda1bf
event service delete method updated
bilimig Jun 10, 2024
0f2a8c0
refactor and fixes
bilimig Jun 10, 2024
59b3bea
refactor
bilimig Jun 10, 2024
2a1b4a7
Update EventService.cs
bilimig Jun 10, 2024
8385ed8
last refactor
bilimig Jun 10, 2024
90305e2
removing spaces
bilimig Jun 10, 2024
3f10272
added geteventbyslug method to eventservice
bilimig Jun 10, 2024
a941d1d
Update ImageService.cs
bilimig Jun 10, 2024
9df5dcb
refactor
bilimig Jun 10, 2024
fc7e7c7
changed list to Enumerable
bilimig Jun 10, 2024
55bc25c
changed list to enumerable
bilimig Jun 10, 2024
1e4316a
folder refactor
bilimig Jun 10, 2024
dc4ffa9
updated services
bilimig Jun 11, 2024
12bbc02
Update EventService.cs
bilimig Jun 11, 2024
6886e31
changes
bilimig Jun 12, 2024
c343f40
Update EventServicesTest.cs
bilimig Jun 12, 2024
55b9e7d
updates
bilimig Jun 12, 2024
912344c
Update EventService.cs
bilimig Jun 12, 2024
e747b3d
Update EventService.cs
bilimig Jun 12, 2024
a79b31f
Update ImageService.cs
bilimig Jun 12, 2024
2f4128c
Updates
bilimig Jun 12, 2024
450849b
Update EventMapper.cs
bilimig Jun 12, 2024
f51885e
changes
bilimig Jun 12, 2024
631d8b7
Update ImageServiceTests.cs
bilimig Jun 12, 2024
9bb1ca6
changes
bilimig Jun 12, 2024
3c01c4f
Formating
bilimig Jun 12, 2024
ecbdf3f
Update EventService.cs
bilimig Jun 13, 2024
e74a0d2
Update EventService.cs
bilimig Jun 13, 2024
08462e1
changes
bilimig Jun 13, 2024
4223069
Created AddEventComment method
bilimig Jun 14, 2024
54001a6
Update EventService.cs
bilimig Jun 14, 2024
3a3274f
fixed updating and creating event
bilimig Jun 15, 2024
fc3c82f
Update EventService.cs
bilimig Jun 17, 2024
f5dace1
changed create slug and text for event
bilimig Jun 17, 2024
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
329 changes: 329 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/EventServicesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
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,
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,
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));

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 88 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 88 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));

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));

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));

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));

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,32 +10,35 @@

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.
}

public override EntityEntry<T> Add(T entity)
{
_data.Add(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.
}

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
Loading