Skip to content

Commit

Permalink
created test for interest status parameter and tag
Browse files Browse the repository at this point in the history
  • Loading branch information
bilimig committed Apr 17, 2024
1 parent 556d637 commit 184d3f4
Show file tree
Hide file tree
Showing 13 changed files with 849 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/ReasnAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

<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>
Expand Down
38 changes: 38 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/ImageServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ReasnAPI.Models.Database;
using ReasnAPI.Models.DTOs;
using ReasnAPI.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Linq.Expressions;
using Moq;
using Moq.EntityFrameworkCore;


namespace ReasnAPI.Tests.Services
{
[TestClass]
public class ImageServiceTests
{
[TestMethod]
public void CreateImage_ImageDoesNotExist_ImageCreated()
{
var imageDto = new ImageDto
{
ImageData = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 },
};

var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Images).ReturnsDbSet(new List<Image>());

var imageService = new ImageService(mockContext.Object);

var result = imageService.CreateImage(imageDto);
Assert.IsNotNull(result);
}

}
}
197 changes: 197 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/Services/InterestServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ReasnAPI.Models.Database;
using ReasnAPI.Models.DTOs;
using ReasnAPI.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Linq.Expressions;
using Moq;
using Moq.EntityFrameworkCore;

namespace ReasnAPI.Tests.Services
{
[TestClass]
public class InterestServiceTests
{
[TestMethod]
public void CreateInterest_InterestDoesNotExist_InterestCreated()
{
var interestDto = new InterestDto
{
Name = "TestInterest"
};

var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest>());

var interestService = new InterestService(mockContext.Object);

var result = interestService.CreateInterest(interestDto);

Assert.AreEqual(interestDto.Name, result.Name);
}

[TestMethod]
public void CreateInterest_InterestExists_InterestNotCreated()
{
var interestDto = new InterestDto
{
Name = "TestInterest"
};

var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest> { new Interest { Name = "TestInterest" } });

var interestService = new InterestService(mockContext.Object);

var result = interestService.CreateInterest(interestDto);

Assert.IsNull(result);
}

[TestMethod]
public void GetAllInterests_InterestExists_InterestsReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest> { new Interest { Name = "TestInterest" } });

var interestService = new InterestService(mockContext.Object);

var result = interestService.GetAllInterests();

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

[TestMethod]
public void GetAllInterests_InterestDoesNotExists_NothingReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest>());

var interestService = new InterestService(mockContext.Object);

var result = interestService.GetAllInterests();

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

[TestMethod]
public void GetInterestById_InterestDoesNotExist_NullReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest>());

var interestService = new InterestService(mockContext.Object);

var result = interestService.GetInterestById(1);

Assert.IsNull(result);
}

[TestMethod]
public void GetInterestById_InterestExists_InterestReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest> { new Interest { Id = 1, Name = "TestInterest"} });

var interestService = new InterestService(mockContext.Object);

var result = interestService.GetInterestById(1);

Assert.AreEqual("TestInterest", result.Name);
}

[TestMethod]
public void UpdateInterest_InterestExists_InterestUpdated()
{
var interestDto = new InterestDto
{
Name = "TestInterest1"
};

var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest> { new Interest { Id = 1, Name = "TestInterest" } });

var interestService = new InterestService(mockContext.Object);

var result = interestService.UpdateInterest(1, interestDto);

Assert.AreEqual("TestInterest1", result.Name);
}

[TestMethod]
public void UpdateInterest_InterestDoesNotExist_NullReturned()
{
var interestDto = new InterestDto
{
Name = "TestInterest"
};

var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest>());

var interestService = new InterestService(mockContext.Object);

var result = interestService.UpdateInterest(1, interestDto);

Assert.IsNull(result);
}

[TestMethod]
public void DeleteInterest_InterestExists_InterestDeleted()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest> { new Interest { Id = 1, Name = "TestInterest" } });

var interestService = new InterestService(mockContext.Object);

interestService.DeleteInterest(1);

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

[TestMethod]
public void DeleteInterest_InterestDoesNotExist_NothingDeleted()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest>());

var interestService = new InterestService(mockContext.Object);

interestService.DeleteInterest(1);

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

[TestMethod]
public void GetInterestsByFilter_InterestExists_InterestReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest> { new Interest { Name = "TestInterest" } });

var interestService = new InterestService(mockContext.Object);

var result = interestService.GetInterestsByFilter(i => i.Name == "TestInterest").ToList();

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

}

[TestMethod]
public void GetInterestsByFilter_InterestDoesNotExist_NothingReturned()
{
var mockContext = new Mock<ReasnContext>();
mockContext.Setup(c => c.Interests).ReturnsDbSet(new List<Interest>());

var interestService = new InterestService(mockContext.Object);

var result = interestService.GetInterestsByFilter(i => i.Name == "TestInterest").ToList();

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

}
}
Loading

0 comments on commit 184d3f4

Please sign in to comment.