Skip to content

Commit

Permalink
Update JSON deserialisation and fake DB tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RuaridhAnswer committed Dec 13, 2023
1 parent 6eb57e1 commit 86a69a1
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.24" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.14" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
Expand Down
4 changes: 2 additions & 2 deletions tests/Answer.King.IntegrationTests/POCO/ErrorResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Answer.King.IntegrationTests.POCO;
internal class ErrorResponse
{
public required object Errors { get; set; }
public required int Status { get; set; }
}
public required int Status { get; set; }
}
84 changes: 15 additions & 69 deletions tests/Answer.King.IntegrationTests/Tests/AnswerKingPostOrder.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Net.Http;
using System.Text.Json;
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;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Testing;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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]
Expand All @@ -35,29 +21,31 @@ public void GetAllOrders()
.StatusCode(200);
}

[TestCase("Single_Line_Order")]
[TestCase("Multiple_Products_Order")]
[TestCase("Multiple_Same_Product_Multiple_Lines_Order")]
[TestCase("Multiple_Same_Product_Same_Line_Order")]
public void ProductOrder_Success(string name)
[TestCase("Single_Line_Order", "5.99")]
[TestCase("Multiple_Products_Order", "18.97")]
[TestCase("Multiple_Same_Product_Multiple_Lines_Order", "8.97")]
[TestCase("Multiple_Same_Product_Same_Line_Order", "99.9")]
public void ProductOrder_Success(string name, string orderTotal)
{
var orderRequest = DataHelper.GetOrderData(name);

var orderResponse = (Domain.Orders.Order)Given(this.Client)
JObject orderResponse = (JObject)Given(this.Client)
.Body(orderRequest)
.When()
.Post("https://localhost:44333/api/orders")
.Then()
.StatusCode(201)
.DeserializeTo(typeof(Domain.Orders.Order));
.DeserializeTo(typeof(JObject));

Snapshot.Match(orderResponse, matchOptions => matchOptions
.IgnoreField("Id")
.IgnoreField("CreatedOn")
.IgnoreField("LastUpdated"));
.IgnoreField("id")
.IgnoreField("createdOn")
.IgnoreField("lastUpdated"));

orderResponse?.SelectToken("orderTotal")?.ToString().Should().Be(orderTotal);
}


[TestCase("Fail_Retired_Product_Order")]
[TestCase("Fail_Invalid_Product_Id")]
[TestCase("Fail_No_Product_Line")]
Expand All @@ -76,14 +64,7 @@ public void FailedOrders(string name)
.StatusCode(400)
.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);
Snapshot.Match(orderResponse);
}

[Test]
Expand All @@ -96,41 +77,6 @@ public void FailedOrder_NoBody()
.StatusCode(400)
.DeserializeTo(typeof(ErrorResponse));

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


//public void InvalidProductIdRestAssured()
// response.Should().NotBeNull() // Response exists
// .And.ContainAll("error", "'product Id' must not be empty.") // The error message
// .And.NotContain("createOrder"); // Method name should not be revealed
//}

//public void InvalidNoProductLineRestAssured()
// response.Should().NotBeNull() // Response exists
// .And.ContainAll("error", "'product Id' must not be empty.") // The error message
// .And.NotContain("createOrder"); // Method name should not be revealed


//public void InvalidNoQuantityLineRestAssured()
// response.Should().NotBeNull() // Response exists
// .And.ContainAll("error", "'quantity' must not be empty.") // The error message
// .And.NotContain("createOrder"); // Method name should not be revealed



//public void InvalidNoQuantityRestAssured()

// response.Should().NotBeNull() // Response exists
// .And.ContainAll("error", "'quantity' must not be empty.") // The error message
// .And.NotContain("createOrder"); // Method name should not be revealed


//public void InvalidNegativeQuantityRestAssured()

// response.Should().NotBeNull() // Response exists
// .And.ContainAll("error", "'quantity' must be greater than or equal to '0'.") // The error message
// .And.NotContain("createOrder"); // Method name should not be revealed

}
96 changes: 96 additions & 0 deletions tests/Answer.King.IntegrationTests/Tests/DataBaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
namespace Answer.King.IntegrationTests.Tests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Extensions;
using Npgsql;

//The tests in here are mocked data to practice how I could do DB testing and assertions with a working SQL DB

[TestFixture]
public class BeeKeeperDBTests
{
[Test]
public async Task OrderAssertionsProductId()
{

var connectionString = "Server=localhost;Port=5432;User Id=postgres;Password=penguin;Database=postgres;";
await using var dataSource = NpgsqlDataSource.Create(connectionString);
await using var command = dataSource.CreateCommand("SELECT product_id from public.orders WHERE order_id = 4");
await using var reader = await command.ExecuteReaderAsync();

List<Int32> resultList = new List<Int32>();

while (await reader.ReadAsync())
{
var result = reader.GetInt32(0);
resultList.Add(result);
}

string resultString = string.Join(", ", resultList);

//Assertions that can be done on a int list
resultList.Count().Should().Be(3);

//Assertions that can't be done on a list
resultString.Should().ContainAll("1", "2", "5");
Console.WriteLine(resultString);
}

[Test]
public async Task OrderAssertionsProductName()
{

var connectionString = "Server=localhost;Port=5432;User Id=postgres;Password=penguin;Database=postgres;";
await using var dataSource = NpgsqlDataSource.Create(connectionString);
await using var command = dataSource.CreateCommand("SELECT pro_name from public.orders WHERE order_id = 4");
await using var reader = await command.ExecuteReaderAsync();

List<string> resultList = new List<string>();

while (await reader.ReadAsync())
{
var result = reader.GetString(0);
resultList.Add(result);
}

string resultString = string.Join(", ", resultList);

//Assertions that can be done on a list
resultList.Should().Contain("Fish", "Chips", "Cheese Burger");
resultList.Should().NotContain("Gravy");

//Assertions that can't be done on a list
Console.WriteLine(resultString);
}

[Test]
public async Task OrderAssertionsProductQuantity()
{

var connectionString = "Server=localhost;Port=5432;User Id=postgres;Password=penguin;Database=postgres;";
await using var dataSource = NpgsqlDataSource.Create(connectionString);
await using var command = dataSource.CreateCommand("SELECT quantity from public.orders WHERE order_id = 4");
await using var reader = await command.ExecuteReaderAsync();

List<int> resultList = new List<int>();

while (await reader.ReadAsync())
{
var result = reader.GetInt32(0);
resultList.Add(result);
result.Should().Be(1);
}

string resultString = string.Join(", ", resultList);

//Assertions that can be done on a list
resultList.Count().Should().Be(3);

//Assertions that can't be done on a list
resultString.Should().Contain("1", Exactly.Thrice());
Console.WriteLine(resultString);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"id": 4,
"createdOn": "2023-12-13T14:41:20.0664133Z",
"lastUpdated": "2023-12-13T14:41:20.0665808Z",
"orderStatus": "Created",
"orderTotal": 18.97,
"lineItems": [
{
"product": {
"id": 1,
"name": "Fish",
"description": "Delicious and satisfying.",
"price": 5.99
},
"quantity": 1,
"subTotal": 5.99
},
{
"product": {
"id": 2,
"name": "Chips",
"description": "Nothing more to say.",
"price": 2.99
},
"quantity": 1,
"subTotal": 2.99
},
{
"product": {
"id": 5,
"name": "Cheese Burger",
"description": "Plain burger with cheese",
"price": 9.99
},
"quantity": 1,
"subTotal": 9.99
}
]
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"id": 4,
"createdOn": "2023-12-13T14:41:20.1602293Z",
"lastUpdated": "2023-12-13T14:41:20.1602512Z",
"orderStatus": "Created",
"orderTotal": 8.97,
"lineItems": [
{
"product": {
"id": 2,
"name": "Chips",
"description": "Nothing more to say.",
"price": 2.99
},
"quantity": 3,
"subTotal": 8.97
}
]
}

This file was deleted.

Loading

0 comments on commit 86a69a1

Please sign in to comment.