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-38] - Create services (address, comment, objecttype, participant, role, user*) #19

Merged
merged 37 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
8bca223
Created services classes and added TODO methods for each class
mkoper02 Mar 27, 2024
45b76ee
Created basic CRUD methods
mkoper02 Mar 29, 2024
56fe5a2
Changed methods returing types, checks for null
mkoper02 Apr 12, 2024
2c68854
Checks if user already exists in db etc., created tests for services
mkoper02 Apr 15, 2024
b752730
Fixed unit test
mkoper02 Apr 15, 2024
fa27fa5
Changed type taken by the services, new, improved tests for services
mkoper02 Apr 25, 2024
b6f34d6
Merge branch 'main' into RSN-38
mkoper02 Apr 29, 2024
e5d67f2
Final fixes and changed unit tests for methods with foreign keys
mkoper02 Apr 29, 2024
034e2cb
Changed delete method to return bool, fixed typos and other problems …
mkoper02 May 6, 2024
a01b96f
Merge branch 'main' into RSN-14
mkoper02 May 6, 2024
34c24f4
Merge branch 'RSN-14' into RSN-38
mkoper02 May 6, 2024
1a58a27
Added brackets one line ifs and added delete method for ObjectTypeSer…
mkoper02 May 6, 2024
1c69f11
Added brackets to one line ifs, updated delete method in ObjectTypeSe…
mkoper02 May 6, 2024
335f9bf
Revert "Added brackets to one line ifs, updated delete method in Obje…
mkoper02 May 6, 2024
26af1ae
Created services classes and added TODO methods for each class
mkoper02 Mar 27, 2024
564fd4c
Created basic CRUD methods
mkoper02 Mar 29, 2024
b390aea
Changed methods returing types, checks for null
mkoper02 Apr 12, 2024
904ae6d
Checks if user already exists in db etc., created tests for services
mkoper02 Apr 15, 2024
7d5b65b
Fixed unit test
mkoper02 Apr 15, 2024
e0d0c42
Changed type taken by the services, new, improved tests for services
mkoper02 Apr 25, 2024
1e514cf
Final fixes and changed unit tests for methods with foreign keys
mkoper02 Apr 29, 2024
bee2375
Changed delete method to return bool, fixed typos and other problems …
mkoper02 May 6, 2024
8894dec
Added brackets one line ifs and added delete method for ObjectTypeSer…
mkoper02 May 6, 2024
25ef6c8
Updated services after scaffold
mkoper02 May 29, 2024
22b3a17
Merge branch 'RSN-38' of https://github.com/wzarek/Reasn into RSN-38
mkoper02 May 29, 2024
2cf6e78
Workflow fixes
mkoper02 Jun 2, 2024
db62ad4
Unit test fix
mkoper02 Jun 2, 2024
6a56302
Working user service
mkoper02 Jun 2, 2024
e09b930
Merge branch 'RSN-14' into RSN-38
mkoper02 Jun 3, 2024
1935260
Deleted test controller
mkoper02 Jun 5, 2024
fd8fa07
Transaction for user update service
mkoper02 Jun 6, 2024
2acf442
Updated services and created mappers
mkoper02 Jun 7, 2024
4ee5848
Services throw errors
mkoper02 Jun 8, 2024
e3ffc9a
Mappers function name change
mkoper02 Jun 8, 2024
9efd65e
Update based on review
mkoper02 Jun 8, 2024
a9bcad9
Added GetUserByUsername function
mkoper02 Jun 9, 2024
d34ef81
Changed exceptions directory location
mkoper02 Jun 9, 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
2 changes: 1 addition & 1 deletion Database/init-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE SCHEMA users;
CREATE SCHEMA common;

CREATE TYPE common.participant_status AS ENUM ('Interested', 'Participating');
CREATE TYPE common.event_status AS ENUM ('Completed', 'In progress', 'Approved', 'Waiting for approval');
CREATE TYPE common.event_status AS ENUM ('Completed', 'InProgress', 'Approved', 'WaitingForApproval');
CREATE TYPE users.role AS ENUM ('User', 'Organizer', 'Admin');
CREATE TYPE common.object_type AS ENUM ('Event', 'User');

Expand Down
12 changes: 6 additions & 6 deletions Database/init-dev-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ SELECT setval('users.user_id_seq', (SELECT MAX(id) FROM users.user));

INSERT INTO events.event ("id", "name", "address_id", "description", "organizer_id", "start_at", "end_at", "created_at", "updated_at", "slug", "status") VALUES
(1, 'Tech Conference', 1, 'Annual tech conference', 1, '2023-10-01 09:00:00', '2023-10-02 17:00:00', '2023-09-01 08:00:00', '2023-09-01 08:00:00', 'tech-conference', 'Approved'),
(2, 'Health Symposium', 2, 'Health and wellness symposium', 2, '2023-11-05 09:00:00', '2023-11-06 17:00:00', '2023-10-05 08:00:00', '2023-10-05 08:00:00', 'health-symposium', 'In progress'),
(3, 'Koncert Rockowy', 3, 'Występ ulubionych zespołów rockowych', 2, CURRENT_TIMESTAMP - '1 day'::INTERVAL, CURRENT_TIMESTAMP + '4 hours'::INTERVAL, '2023-09-01 08:00:00', '2023-09-01 08:00:00', 'koncert-rockowy', 'Waiting for approval'),
(2, 'Health Symposium', 2, 'Health and wellness symposium', 2, '2023-11-05 09:00:00', '2023-11-06 17:00:00', '2023-10-05 08:00:00', '2023-10-05 08:00:00', 'health-symposium', 'InProgress'),
(3, 'Koncert Rockowy', 3, 'Występ ulubionych zespołów rockowych', 2, CURRENT_TIMESTAMP - '1 day'::INTERVAL, CURRENT_TIMESTAMP + '4 hours'::INTERVAL, '2023-09-01 08:00:00', '2023-09-01 08:00:00', 'koncert-rockowy', 'WaitingForApproval'),
(4, 'Konferencja IT', 4, 'Coroczna konferencja technologiczna', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + '3 hours'::INTERVAL, '2023-10-02 17:00:00', '2023-10-02 18:00:00', 'konferencja-it', 'Completed'),
(5, 'Mecz Piłki Nożnej', 5, 'Mecz drużynowej rywalizacji w piłce nożnej', 3, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + '3 hours'::INTERVAL, '2022-12-01 08:00:00', '2023-09-01 08:30:00', 'mecz-pilki-noznej', 'In progress'),
(6, 'Festiwal Elektroniczny', 6, 'Największe hity muzyki elektronicznej', 4, CURRENT_TIMESTAMP - '4 day'::INTERVAL, CURRENT_TIMESTAMP - '2 days'::INTERVAL, '2023-09-01 08:00:00', '2023-09-02 08:00:00', 'festiwal-elektroniczny', 'Waiting for approval'),
(5, 'Mecz Piłki Nożnej', 5, 'Mecz drużynowej rywalizacji w piłce nożnej', 3, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + '3 hours'::INTERVAL, '2022-12-01 08:00:00', '2023-09-01 08:30:00', 'mecz-pilki-noznej', 'InProgress'),
(6, 'Festiwal Elektroniczny', 6, 'Największe hity muzyki elektronicznej', 4, CURRENT_TIMESTAMP - '4 day'::INTERVAL, CURRENT_TIMESTAMP - '2 days'::INTERVAL, '2023-09-01 08:00:00', '2023-09-02 08:00:00', 'festiwal-elektroniczny', 'WaitingForApproval'),
(7, 'Koncert Hip-Hopowy', 7, 'Najnowsze hity hip-hopu w wykonaniu gwiazd', 6, CURRENT_TIMESTAMP + '2 days'::INTERVAL, CURRENT_TIMESTAMP + '2 days'::INTERVAL + '4 hours'::INTERVAL, '2023-10-08 17:00:00', '2023-11-01 09:19:22', 'koncert-hip-hopowy', 'Completed'),
(8, 'Wieczór Jazzowy', 8, 'Relaksujące dźwięki jazzu w kameralnej atmosferze', 8, CURRENT_TIMESTAMP + '1 day'::INTERVAL, CURRENT_TIMESTAMP + '1 day'::INTERVAL + '5 hours'::INTERVAL, '2023-09-01 08:00:00', '2023-09-01 08:00:00', 'wieczor-jazzowy', 'In progress'),
(9, 'Koncert Klasyczny', 9, 'Muzyka klasyczna w wykonaniu renomowanych artystów', 10, CURRENT_TIMESTAMP + '1 day'::INTERVAL, CURRENT_TIMESTAMP + '1 day'::INTERVAL + '3 hours'::INTERVAL, '2023-01-01 15:00:00', '2023-02-01 08:00:00', 'koncert-klasyczny', 'Waiting for approval'),
(8, 'Wieczór Jazzowy', 8, 'Relaksujące dźwięki jazzu w kameralnej atmosferze', 8, CURRENT_TIMESTAMP + '1 day'::INTERVAL, CURRENT_TIMESTAMP + '1 day'::INTERVAL + '5 hours'::INTERVAL, '2023-09-01 08:00:00', '2023-09-01 08:00:00', 'wieczor-jazzowy', 'InProgress'),
(9, 'Koncert Klasyczny', 9, 'Muzyka klasyczna w wykonaniu renomowanych artystów', 10, CURRENT_TIMESTAMP + '1 day'::INTERVAL, CURRENT_TIMESTAMP + '1 day'::INTERVAL + '3 hours'::INTERVAL, '2023-01-01 15:00:00', '2023-02-01 08:00:00', 'koncert-klasyczny', 'WaitingForApproval'),
(10, 'Turniej w League of Legends', 10, 'Turniej LAN dla miłośnikow esportu i gry League of Legends', 10, CURRENT_TIMESTAMP - '1 day'::INTERVAL, CURRENT_TIMESTAMP - '3 hours'::INTERVAL, '2023-01-01 15:00:00', '2023-02-01 08:00:00', 'turniej-lol', 'Completed');
SELECT setval('events.event_id_seq', (SELECT MAX(id) FROM events.event));

Expand Down
6 changes: 6 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/ReasnAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.Playwright.MSTest" Version="1.27.1" />
<PackageReference Include="Moq.EntityFrameworkCore" Version="8.0.1.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ReasnAPI\ReasnAPI.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.Playwright.MSTest" />
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
Expand Down
294 changes: 294 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/AddressServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
using Moq;
using Moq.EntityFrameworkCore;
using ReasnAPI.Models.Database;
using ReasnAPI.Models.DTOs;
using ReasnAPI.Services;
using ReasnAPI.Services.Exceptions;

namespace ReasnAPI.Tests.Services;

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

var address = new Address
{
Id = 1,
City = "City",
Country = "Country",
State = "State",
Street = "Street",
ZipCode = "ZipCode"
};

var fakeAddress = new FakeDbSet<Address> { address };

mockContext.Setup(c => c.Addresses).Returns(fakeAddress);

var addressService = new AddressService(mockContext.Object);

var result = addressService.GetAddressById(1);

Assert.IsNotNull(result);
Assert.AreEqual("City", result.City);
Assert.AreEqual("Country", result.Country);
Assert.AreEqual("State", result.State);
Assert.AreEqual("Street", result.Street);
Assert.AreEqual("ZipCode", result.ZipCode);
}

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

var addressService = new AddressService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => addressService.GetAddressById(1));
}

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

var address1 = new Address
{
Id = 1,
City = "City",
Country = "Country",
State = "State",
Street = "Street",
ZipCode = "ZipCode"
};

var address2 = new Address
{
Id = 2,
City = "City",
Country = "Country",
State = "State",
Street = "Street",
ZipCode = "ZipCode"
};

mockContext.Setup(c => c.Addresses).ReturnsDbSet([address1, address2]);

var addressService = new AddressService(mockContext.Object);

var result = addressService.GetAllAddresses();

Assert.IsNotNull(result);
Assert.AreEqual(2, result.Count());
}

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

var addressService = new AddressService(mockContext.Object);

var result = addressService.GetAllAddresses();

Assert.IsNotNull(result);
Assert.AreEqual(0, result.Count());
}

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

var address1 = new Address
{
Id = 1,
City = "City",
Country = "Country",
State = "State",
Street = "Street",
ZipCode = "ZipCode"
};

var address2 = new Address
{
Id = 2,
City = "City",
Country = "Country",
State = "State",
Street = "Street",
ZipCode = "ZipCode"
};

mockContext.Setup(c => c.Addresses).ReturnsDbSet([address1, address2]);
var addressService = new AddressService(mockContext.Object);

var result = addressService.GetAddressesByFilter(r => r.Id == 1).ToList();

Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
}

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

var addressService = new AddressService(mockContext.Object);

var result = addressService.GetAddressesByFilter(r => r.Id == 1);

Assert.IsNotNull(result);
Assert.AreEqual(0, result.Count());
}

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

var addressService = new AddressService(mockContext.Object);

var addressDto = new AddressDto
{
City = "City",
Country = "Country",
State = "State",
Street = "Street",
ZipCode = "ZipCode"
};

var result = addressService.CreateAddress(addressDto);

Assert.IsNotNull(result);
Assert.AreEqual("City", result.City);
Assert.AreEqual("Country", result.Country);
Assert.AreEqual("State", result.State);
Assert.AreEqual("Street", result.Street);
Assert.AreEqual("ZipCode", result.ZipCode);
}

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

var addressService = new AddressService(mockContext.Object);

Assert.ThrowsException<ArgumentNullException>(() => addressService.CreateAddress(null));

Check warning on line 187 in Server/ReasnAPI/ReasnAPI.Tests/Services/AddressServiceTests.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 187 in Server/ReasnAPI/ReasnAPI.Tests/Services/AddressServiceTests.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 187 in Server/ReasnAPI/ReasnAPI.Tests/Services/AddressServiceTests.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Cannot convert null literal to non-nullable reference type.
}

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

var address = new Address
{
Id = 1,
City = "City",
Country = "Country",
State = "State",
Street = "Street",
ZipCode = "ZipCode"
};

mockContext.Setup(c => c.Addresses).ReturnsDbSet([address]);

var addressService = new AddressService(mockContext.Object);

var addressDto = new AddressDto
{
City = "City2",
Country = "Country2",
State = "State2",
Street = "Street2",
ZipCode = "ZipCode2"
};

var result = addressService.UpdateAddress(1, addressDto);

Assert.IsNotNull(result);
Assert.AreEqual("City2", result.City);
Assert.AreEqual("Country2", result.Country);
Assert.AreEqual("State2", result.State);
Assert.AreEqual("Street2", result.Street);
Assert.AreEqual("ZipCode2", result.ZipCode);
}

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

var addressService = new AddressService(mockContext.Object);

var addressDto = new AddressDto
{
City = "City2",
Country = "Country2",
State = "State2",
Street = "Street2",
ZipCode = "ZipCode2"
};

Assert.ThrowsException<NotFoundException>(() => addressService.UpdateAddress(1, addressDto));
}

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

var addressService = new AddressService(mockContext.Object);

Assert.ThrowsException<ArgumentNullException>(() => addressService.UpdateAddress(1, null));

Check warning on line 256 in Server/ReasnAPI/ReasnAPI.Tests/Services/AddressServiceTests.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 256 in Server/ReasnAPI/ReasnAPI.Tests/Services/AddressServiceTests.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 256 in Server/ReasnAPI/ReasnAPI.Tests/Services/AddressServiceTests.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Cannot convert null literal to non-nullable reference type.
}

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

var address = new Address
{
Id = 1,
City = "City",
Country = "Country",
State = "State",
Street = "Street",
ZipCode = "ZipCode"
};

mockContext.Setup(c => c.Addresses).ReturnsDbSet([address]);

var addressService = new AddressService(mockContext.Object);

addressService.DeleteAddress(1);

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

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

var addressService = new AddressService(mockContext.Object);

Assert.ThrowsException<NotFoundException>(() => addressService.DeleteAddress(1));
mockContext.Verify(c => c.SaveChanges(), Times.Never);
}
}
Loading