Skip to content

Commit

Permalink
base class updates
Browse files Browse the repository at this point in the history
  • Loading branch information
RuaridhAnswer committed Dec 11, 2023
1 parent fac8d53 commit 6eb57e1
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 42 deletions.
6 changes: 6 additions & 0 deletions Answer.King.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Answer.King.Infrastructure.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Answer.King.IntegrationTests", "tests\Answer.King.IntegrationTests\Answer.King.IntegrationTests.csproj", "{E794D3D2-E323-48AB-A507-FA361E060618}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A7366D48-A55F-477D-AC26-8D061A839D37}"
ProjectSection(SolutionItems) = preProject
.github\workflows\pull-request.yml = .github\workflows\pull-request.yml
.github\workflows\workflow-dispatch.yaml = .github\workflows\workflow-dispatch.yaml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
1 change: 1 addition & 0 deletions src/Answer.King.Api/Answer.King.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

<ItemGroup>
<InternalsVisibleTo Include="$(AssemblyName).UnitTests" />
<InternalsVisibleTo Include="$(AssemblyName).IntegrationTests" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="RestAssured.Net" Version="4.2.0" />
<PackageReference Include="Snapshooter.NUnit" Version="0.13.0" />
<PackageReference Include="LiteDB" Version="5.0.15" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Answer.King.Api\Answer.King.Api.csproj" />
<ProjectReference Include="..\..\src\Answer.King.Domain\Answer.King.Domain.csproj" />
<ProjectReference Include="..\src\Answer.King.Api\Answer.King.Api.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions tests/Answer.King.IntegrationTests/POCO/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Answer.King.IntegrationTests.POCO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

internal class ErrorResponse
{
public required object Errors { get; set; }
public required int Status { get; set; }
}
22 changes: 15 additions & 7 deletions tests/Answer.King.IntegrationTests/Tests/AnswerKingPostOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Xml.Linq;
using Answer.King.Api.RequestModels;
using Answer.King.Domain.Orders;
using Answer.King.IntegrationTests.POCO;
using Answer.King.IntegrationTests.TestData;
using Answer.King.IntegrationTests.Utilities;
using FluentAssertions;
Expand All @@ -16,14 +17,14 @@
using Snapshooter.NUnit;
using static RestAssured.Dsl;
using Order = Answer.King.Api.RequestModels.Order;
using LiteDB;
using System.Linq;

namespace Answer.King.IntegrationTests.Tests;

[TestFixture]

public class AnswerKingPostOrder : BaseTestClass

{
[Parallelizable(scope: ParallelScope.Self)]
[Test]
public void GetAllOrders()
{
Expand Down Expand Up @@ -54,6 +55,7 @@ public void ProductOrder_Success(string name)
.IgnoreField("Id")
.IgnoreField("CreatedOn")
.IgnoreField("LastUpdated"));

}

[TestCase("Fail_Retired_Product_Order")]
Expand All @@ -66,27 +68,33 @@ public void FailedOrders(string name)
{
var orderRequest = DataHelper.GetOrderData(name);

var orderResponse = (ProblemDetails)Given(this.Client)
var orderResponse = (ErrorResponse)Given(this.Client)
.Body(orderRequest)
.When()
.Post("https://localhost:44333/api/orders")
.Then()
.StatusCode(400)
.DeserializeTo(typeof(ProblemDetails));
.DeserializeTo(typeof(ErrorResponse));

Snapshot.Match(orderResponse, matchOptions => matchOptions
.IgnoreField("TraceId"));

//var database = new LiteDatabase($@"db\{this.testDb}");
//var collection = database.GetCollection<Category>("categories");
//var results = collection.Find(Query.All());
//var items = results.ToList();
//Console.WriteLine(results);
}

[Test]
public void FailedOrder_NoBody()
{
var response = (ProblemDetails)Given(this.Client)
var response = (ErrorResponse)Given(this.Client)
.When()
.Post("https://localhost:44333/api/orders")
.Then()
.StatusCode(400)
.DeserializeTo(typeof(ProblemDetails));
.DeserializeTo(typeof(ErrorResponse));

Snapshot.Match(response, matchOptions => matchOptions
.IgnoreField("TraceId"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"Type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"Title": "One or more validation errors occurred.",
"Status": 400,
"Detail": null,
"Instance": null,
"Extensions": {}
"Errors": {
"": [
"A non-empty request body is required."
],
"createOrder": [
"The createOrder field is required."
]
},
"Status": 400
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"Type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"Title": "One or more validation errors occurred.",
"Status": 400,
"Detail": null,
"Instance": null,
"Extensions": {}
"Errors": {
"lineItems[0].productId": [
"'product Id' must not be empty."
]
},
"Status": 400
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"Type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"Title": "One or more validation errors occurred.",
"Status": 400,
"Detail": null,
"Instance": null,
"Extensions": {}
"Errors": {
"lineItems[0].quantity": [
"'quantity' must be greater than or equal to '0'."
]
},
"Status": 400
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"Type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"Title": "One or more validation errors occurred.",
"Status": 400,
"Detail": null,
"Instance": null,
"Extensions": {}
"Errors": {
"lineItems[0].productId": [
"'product Id' must not be empty."
]
},
"Status": 400
}
21 changes: 16 additions & 5 deletions tests/Answer.King.IntegrationTests/Utilities/BaseTestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@ namespace Answer.King.IntegrationTests.Utilities;
using System;
using System.IO;
using System.Net.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public class BaseTestClass : IDisposable
{
public HttpClient Client;

[OneTimeSetUp]
public string testDb { get; set; } = $"Answer.King.{Guid.NewGuid()}.db";

[SetUp]
public void Setup()
{
File.Delete(Path.Combine(TestContext.CurrentContext.TestDirectory, "db/Answer.King.db"));
File.Delete(Path.Combine(TestContext.CurrentContext.TestDirectory, "db/Answer.King-log.db"));
this.Client = new WebApplicationFactory<Program>().CreateClient();
this.Client = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
{
builder.UseSetting("ConnectionStrings:AnswerKing", $"filename=db/{this.testDb};Connection=Shared");
}).CreateClient();
}

[OneTimeTearDown]
[TearDown]
public void TearDown()
{
if (Directory.Exists(Path.Combine(TestContext.CurrentContext.TestDirectory, "db")))
{
File.Delete(Path.Combine(TestContext.CurrentContext.TestDirectory, $"db/{testDb}"));
File.Delete(Path.Combine(TestContext.CurrentContext.TestDirectory, "db/Answer.King-log.db"));
}
this.Client.Dispose();
}

Expand Down
11 changes: 6 additions & 5 deletions tests/Answer.King.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
"System.Configuration.ConfigurationManager": "4.4.0"
}
},
"LiteDB": {
"type": "Direct",
"requested": "[5.0.15, )",
"resolved": "5.0.15",
"contentHash": "nucyfCOGSATH553BxplxExP3BOqEwmHt0B57426EIaQjD3CC1Odb52VVCGgTxyYaD2oe3B/cJk8jDo6XiBJqPg=="
},
"Microsoft.AspNetCore.Mvc.Testing": {
"type": "Direct",
"requested": "[7.0.14, )",
Expand Down Expand Up @@ -176,11 +182,6 @@
"resolved": "1.11.54",
"contentHash": "bgB79N4urc4D7hrD4R+Qk6CjPiYOfs6vfXNsmw3nCcfQEQvBQGjfAcl5k/tB1ZP1hA0d7xXs0jK8xAXV36IVUg=="
},
"LiteDB": {
"type": "Transitive",
"resolved": "5.0.15",
"contentHash": "nucyfCOGSATH553BxplxExP3BOqEwmHt0B57426EIaQjD3CC1Odb52VVCGgTxyYaD2oe3B/cJk8jDo6XiBJqPg=="
},
"Microsoft.AspNetCore.Hosting.Abstractions": {
"type": "Transitive",
"resolved": "2.2.0",
Expand Down

0 comments on commit 6eb57e1

Please sign in to comment.