Skip to content

Commit

Permalink
Merge pull request #11 from Shuttle/async
Browse files Browse the repository at this point in the history
Async
  • Loading branch information
eben-roux authored Apr 30, 2024
2 parents cafcba4 + bf05f1c commit 0a09aa6
Show file tree
Hide file tree
Showing 84 changed files with 2,425 additions and 2,083 deletions.
234 changes: 84 additions & 150 deletions README.md

Large diffs are not rendered by default.

87 changes: 43 additions & 44 deletions Shuttle.Core.Data.Tests/AssemblerExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,48 @@
using NUnit.Framework;
using Shuttle.Core.Data.Tests.Fakes;

namespace Shuttle.Core.Data.Tests
namespace Shuttle.Core.Data.Tests;

[TestFixture]
public class AssemblerExtensionsFixture
{
[TestFixture]
public class AssemblerExtensionsFixture
{
[Test]
public void Should_be_able_to_assemble_item()
{
var now = DateTime.Now;
var mappedData = new MappedData();

var orderTable = new DataTable();

orderTable.Columns.Add("OrderNumber", typeof(string));
orderTable.Columns.Add("OrderDate", typeof(DateTime));

const string orderNumber = "ON-10";

mappedData.Add(new MappedRow<Order>(orderTable.Rows.Add(orderNumber, now), new Order(orderNumber, now)));

var orderLineTable = new DataTable();

orderLineTable.Columns.Add("OrderNumber", typeof(string));
orderLineTable.Columns.Add("ProductId", typeof(string));
orderLineTable.Columns.Add("Quantity", typeof(int));
orderLineTable.Columns.Add("UnitCost", typeof(double));

mappedData.Add(new List<MappedRow<OrderLine>>
{
new MappedRow<OrderLine>(orderLineTable.Rows.Add(orderNumber, "SKU-1", 5, 10), new OrderLine("SKU-1", 5, 10)),
new MappedRow<OrderLine>(orderLineTable.Rows.Add(orderNumber, "SKU-2", 1, 65), new OrderLine("SKU-2", 1, 65)),
new MappedRow<OrderLine>(orderLineTable.Rows.Add(orderNumber, "SKU-3", 10, 10.5), new OrderLine("SKU-3", 10, 10.5))
});

var order = new OrderAssembler().AssembleItem(mappedData);

Assert.AreEqual(orderNumber, order.OrderNumber);
Assert.AreEqual(now, order.OrderDate);
Assert.AreEqual(3, order.Lines.Count());
Assert.AreEqual(50, order.Lines.ElementAt(0).TotalCost());
Assert.AreEqual(65, order.Lines.ElementAt(1).TotalCost());
Assert.AreEqual(105, order.Lines.ElementAt(2).TotalCost());
Assert.AreEqual(220, order.Total());
}
}
[Test]
public void Should_be_able_to_assemble_item()
{
var now = DateTime.Now;
var mappedData = new MappedData();

var orderTable = new DataTable();

orderTable.Columns.Add("OrderNumber", typeof(string));
orderTable.Columns.Add("OrderDate", typeof(DateTime));

const string orderNumber = "ON-10";

mappedData.Add(new MappedRow<Order>(orderTable.Rows.Add(orderNumber, now), new Order(orderNumber, now)));

var orderLineTable = new DataTable();

orderLineTable.Columns.Add("OrderNumber", typeof(string));
orderLineTable.Columns.Add("ProductId", typeof(string));
orderLineTable.Columns.Add("Quantity", typeof(int));
orderLineTable.Columns.Add("UnitCost", typeof(double));

mappedData.Add(new List<MappedRow<OrderLine>>
{
new(orderLineTable.Rows.Add(orderNumber, "SKU-1", 5, 10), new OrderLine("SKU-1", 5, 10)),
new(orderLineTable.Rows.Add(orderNumber, "SKU-2", 1, 65), new OrderLine("SKU-2", 1, 65)),
new(orderLineTable.Rows.Add(orderNumber, "SKU-3", 10, 10.5), new OrderLine("SKU-3", 10, 10.5))
});

var order = new OrderAssembler().AssembleItem(mappedData);

Assert.AreEqual(orderNumber, order.OrderNumber);
Assert.AreEqual(now, order.OrderDate);
Assert.AreEqual(3, order.Lines.Count());
Assert.AreEqual(50, order.Lines.ElementAt(0).TotalCost());
Assert.AreEqual(65, order.Lines.ElementAt(1).TotalCost());
Assert.AreEqual(105, order.Lines.ElementAt(2).TotalCost());
Assert.AreEqual(220, order.Total());
}
}
52 changes: 52 additions & 0 deletions Shuttle.Core.Data.Tests/AsyncFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Shuttle.Core.Data.Tests;

public class AsyncFixture : MappingFixture
{
private readonly Query _rowsQuery = new(@"
select
Id,
Name,
Age
from
BasicMapping
");

[Test]
public void Should_be_able_to_use_the_same_database_context_across_tasks_async()
{
var tasks = new List<Task>();

using (GetDatabaseContext())
{
for (int i = 0; i < 10; i++)
{
tasks.Add(GetDatabaseGateway().GetRowsAsync(_rowsQuery));
}

Task.WaitAll(tasks.ToArray());
}
}

[Test]
public async Task Should_be_able_to_use_the_same_database_context_across_synchronized_tasks_async()
{
using (GetDatabaseContext())
{
await GetRowsAsync(0);
}
}

private async Task GetRowsAsync(int depth)
{
if (depth < 5)
{
await GetRowsAsync(depth + 1);
}

_ = await GetDatabaseGateway().GetRowsAsync(_rowsQuery);
}
}
Loading

0 comments on commit 0a09aa6

Please sign in to comment.