Skip to content

Commit

Permalink
[RSN-14] - Create DB services (#62)
Browse files Browse the repository at this point in the history
* Created services classes and added TODO methods for each class

* [RSN-38] - Create services (address, comment, objecttype, participant, role, user*) (#19)

* Created services classes and added TODO methods for each class

* Created basic CRUD methods

* Changed methods returing types, checks for null

* Checks if user already exists in db etc., created tests for services

* Fixed unit test

* Changed type taken by the services, new, improved tests for services

* Final fixes and changed unit tests for methods with foreign keys

* Changed delete method to return bool, fixed typos and other problems based on PR review

* Added brackets one line ifs and added delete method for ObjectTypeService

* Revert "Added brackets to one line ifs, updated delete method in ObjectTypeService"

This reverts commit 1c69f11, reversing
changes made to 1a58a27.

* Created services classes and added TODO methods for each class

* Created basic CRUD methods

* Changed methods returing types, checks for null

* Checks if user already exists in db etc., created tests for services

* Fixed unit test

* Changed type taken by the services, new, improved tests for services

* Final fixes and changed unit tests for methods with foreign keys

* Changed delete method to return bool, fixed typos and other problems based on PR review

* Added brackets one line ifs and added delete method for ObjectTypeService

* Updated services after scaffold

Updated services and tests, User service not finished, enums not working

* Workflow fixes

* Unit test fix

* Working user service

* Deleted test controller

* Transaction for user update service

* Updated services and created mappers

* Services throw errors

Services now throw errors if something is not right (dto null, data not found in db), updated unit tests and created custom exception classes

* Mappers function name change

* Update based on review

* Added GetUserByUsername function

* Changed exceptions directory location

* [RSN-37] - Created services for Event Tag Parameter Interest Image (#42)

* 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]>

* Update Entity Framework version

* Changed exceptions location

* ReasnContext update

---------

Co-authored-by: Owczarek Kamil <[email protected]>
  • Loading branch information
mkoper02 and bilimig authored Jun 19, 2024
1 parent 5020d29 commit 14ea120
Show file tree
Hide file tree
Showing 46 changed files with 4,159 additions and 129 deletions.
5 changes: 5 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/ReasnAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<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>
<ProjectReference Include="..\ReasnAPI\ReasnAPI.csproj" />
</ItemGroup>
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.Exceptions;
using ReasnAPI.Models.Database;
using ReasnAPI.Models.DTOs;
using ReasnAPI.Services;

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

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

[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

0 comments on commit 14ea120

Please sign in to comment.