Skip to content

Commit e1578b0

Browse files
committed
#29 unit tests for asynchronous testing have been created
1 parent 80258e5 commit e1578b0

File tree

6 files changed

+515
-6
lines changed

6 files changed

+515
-6
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using NUnit.Framework;
2+
using SubSonic.Extensions.Test.Models;
3+
using SubSonic.Extensions.Test;
4+
using SubSonic.Tests.DAL.SUT;
5+
using System;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace SubSonic.Tests.DAL
13+
{
14+
using FluentAssertions;
15+
using Linq;
16+
using System.Threading;
17+
18+
[TestFixture]
19+
public class AsynchronousTests
20+
: BaseTestFixture
21+
{
22+
public override void SetupTestFixture()
23+
{
24+
base.SetupTestFixture();
25+
26+
string
27+
people_equal = @"SELECT [T1].[ID], [T1].[FirstName], [T1].[MiddleInitial], [T1].[FamilyName], [T1].[FullName]
28+
FROM [dbo].[Person] AS [T1]
29+
WHERE ([T1].[ID] == @id_1)",
30+
people_greater_than = @"SELECT [T1].[ID], [T1].[FirstName], [T1].[MiddleInitial], [T1].[FamilyName], [T1].[FullName]
31+
FROM [dbo].[Person] AS [T1]
32+
WHERE ([T1].[ID] > @id_1)",
33+
people_less_than = @"SELECT [T1].[ID], [T1].[FirstName], [T1].[MiddleInitial], [T1].[FamilyName], [T1].[FullName]
34+
FROM [dbo].[Person] AS [T1]
35+
WHERE ([T1].[ID] > @id_1)";
36+
37+
Context.Database.Instance.AddCommandBehavior(people_greater_than, cmd => People.Where(x => x.ID > cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());
38+
Context.Database.Instance.AddCommandBehavior(people_equal, cmd => People.Where(x => x.ID == cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());
39+
Context.Database.Instance.AddCommandBehavior(people_less_than, cmd => People.Where(x => x.ID < cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());
40+
}
41+
42+
[Test]
43+
public async Task ShouldBeAbleToGetSingleAsync()
44+
{
45+
Person person = await Context.People.Where(x => x.ID == 1)
46+
.AsAsyncSubSonicQueryable()
47+
.SingleAsync();
48+
49+
person.ID.Should().Be(1);
50+
}
51+
52+
[Test]
53+
public void ShouldBeAbleToThrowWhenSingleAsyncHasMoreThanOne()
54+
{
55+
FluentActions.Invoking(async () =>
56+
{
57+
await Context.People.Where(x => x.ID > 0)
58+
.AsAsyncSubSonicQueryable()
59+
.SingleAsync();
60+
}).Should().Throw<InvalidOperationException>();
61+
}
62+
63+
[Test]
64+
public void ShouldBeAbleToThrowSingleAsyncOnNull()
65+
{
66+
FluentActions.Invoking(async () =>
67+
{
68+
await Context.People.Where(x => x.ID == -1)
69+
.AsAsyncSubSonicQueryable()
70+
.SingleAsync();
71+
}).Should().Throw<InvalidOperationException>();
72+
}
73+
74+
[Test]
75+
public async Task ShouldBeAbleToGetSingleOrDefaultAsync()
76+
{
77+
Person person = await Context.People.Where(x => x.ID == 1)
78+
.AsAsyncSubSonicQueryable()
79+
.SingleOrDefaultAsync();
80+
81+
person.ID.Should().Be(1);
82+
}
83+
84+
[Test]
85+
public void ShouldBeAbleToNotThrowSingleOrDefaultAsyncOnNull()
86+
{
87+
FluentActions.Invoking(async () =>
88+
{
89+
Person person = await Context.People.Where(x => x.ID == -1)
90+
.AsAsyncSubSonicQueryable()
91+
.SingleOrDefaultAsync();
92+
93+
person.Should().BeNull();
94+
95+
}).Should().NotThrow();
96+
}
97+
98+
[Test]
99+
public async Task ShouldBeAbleToGetFirstAsync(CancellationToken cancellationToken)
100+
{
101+
Person person = await Context.People.Where(x => x.ID > 0)
102+
.AsAsyncSubSonicQueryable()
103+
.FirstAsync(cancellationToken);
104+
105+
person.ID.Should().Be(1);
106+
}
107+
108+
[Test]
109+
public void ShouldBeAbleToThrowFirstAsyncOnNull()
110+
{
111+
FluentActions.Invoking(async () =>
112+
{
113+
await Context.People.Where(x => x.ID < 1)
114+
.AsAsyncSubSonicQueryable()
115+
.FirstAsync();
116+
}).Should().Throw<InvalidOperationException>();
117+
}
118+
119+
[Test]
120+
public async Task ShouldBeAbleToGetFirstOrDefaultAsync()
121+
{
122+
Person person = await Context.People.Where(x => x.ID > 2)
123+
.AsAsyncSubSonicQueryable()
124+
.FirstOrDefaultAsync();
125+
126+
person.ID.Should().Be(3);
127+
}
128+
129+
[Test]
130+
public void ShouldBeAbleToNotThrowFirstOrDefaultAsyncOnNull()
131+
{
132+
FluentActions.Invoking(async () =>
133+
{
134+
Person person = await Context.People.Where(x => x.ID < 1)
135+
.AsAsyncSubSonicQueryable()
136+
.SingleAsync();
137+
138+
person.Should().BeNull();
139+
140+
}).Should().NotThrow();
141+
}
142+
143+
[Test]
144+
public async Task ShouldBeAbleToLoadResultSet()
145+
{
146+
FluentActions.Invoking(async () =>
147+
{
148+
ISubSonicCollection<Person> people = await Context.People
149+
.AsAsyncSubSonicQueryable()
150+
.LoadAsync();
151+
152+
await foreach(Person person in people)
153+
{
154+
person.FullName.Should().Be(String.Format("{0}, {1}{2}",
155+
person.FamilyName, person.FirstName,
156+
string.IsNullOrEmpty(person.MiddleInitial?.Trim()) ? "" : $" {person.MiddleInitial}."));
157+
}
158+
159+
}).Should().NotThrow();
160+
}
161+
}
162+
}

SubSonic/Infrastructure/Builders/DbSqlQueryBuilder/DbSqlQueryBuilderAsyncQueryProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System.Linq.Expressions;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

45
namespace SubSonic.Infrastructure.Builders
56
{
67
public partial class DbSqlQueryBuilder
78
{
8-
public async Task<TResult> ExecuteAsync<TResult>(Expression expression)
9+
public async Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
910
{
1011
if (expression is null)
1112
{
@@ -15,7 +16,7 @@ public async Task<TResult> ExecuteAsync<TResult>(Expression expression)
1516
throw Error.NotImplemented();
1617
}
1718

18-
public async Task<object> ExecuteAsync(Expression expression)
19+
public async Task<object> ExecuteAsync(Expression expression, CancellationToken cancellationToken)
1920
{
2021
if (expression is null)
2122
{

SubSonic/Infrastructure/Builders/DbSqlTableTypeProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace SubSonic.Infrastructure.Builders
1212
using Linq.Expressions;
1313
using Logging;
1414
using Schema;
15+
using System.Threading;
1516

1617
public class DbSqlTableTypeProvider
1718
: ISubSonicQueryProvider
@@ -131,7 +132,7 @@ public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
131132
}
132133
}
133134

134-
public async Task<TResult> ExecuteAsync<TResult>(Expression expression)
135+
public async Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
135136
{
136137
if (expression is null)
137138
{
@@ -141,7 +142,7 @@ public async Task<TResult> ExecuteAsync<TResult>(Expression expression)
141142
throw Error.NotImplemented();
142143
}
143144

144-
public async Task<object> ExecuteAsync(Expression expression)
145+
public async Task<object> ExecuteAsync(Expression expression, CancellationToken cancellationToken)
145146
{
146147
if (expression is null)
147148
{

SubSonic/Interfaces/IAsyncSubSonicQueryProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq;
22
using System.Linq.Expressions;
3+
using System.Threading;
34
using System.Threading.Tasks;
45

56
namespace SubSonic.Interfaces
@@ -12,13 +13,13 @@ public interface IAsyncSubSonicQueryProvider
1213
/// </summary>
1314
/// <param name="expression"></param>
1415
/// <returns></returns>
15-
Task<object> ExecuteAsync(Expression expression);
16+
Task<object> ExecuteAsync(Expression expression, CancellationToken cancellationToken);
1617
/// <summary>
1718
/// Executes the expression asynchronously
1819
/// </summary>
1920
/// <typeparam name="TResult"></typeparam>
2021
/// <param name="Expression"></param>
2122
/// <returns></returns>
22-
Task<TResult> ExecuteAsync<TResult>(Expression Expression);
23+
Task<TResult> ExecuteAsync<TResult>(Expression Expression, CancellationToken cancellationToken);
2324
}
2425
}

0 commit comments

Comments
 (0)