Skip to content

Commit 445e014

Browse files
committed
💁
1 parent c9acb4c commit 445e014

File tree

20 files changed

+308
-515
lines changed

20 files changed

+308
-515
lines changed
Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System.Text.Json;
12
using Amazon.Lambda.APIGatewayEvents;
23
using Amazon.Lambda.TestUtilities;
34
using CommandQuery.AWSLambda;
5+
using CommandQuery.Sample.Contracts.Commands;
46
using FluentAssertions;
57
using Microsoft.Extensions.DependencyInjection;
68
using NUnit.Framework;
@@ -9,43 +11,34 @@ namespace CommandQuery.Sample.AWSLambda.Tests
911
{
1012
public class CommandTests
1113
{
12-
public class when_using_the_real_function
14+
[SetUp]
15+
public void SetUp()
1316
{
14-
[SetUp]
15-
public void SetUp()
16-
{
17-
var serviceCollection = new ServiceCollection();
18-
new Startup().ConfigureServices(serviceCollection);
19-
var serviceProvider = serviceCollection.BuildServiceProvider();
17+
var serviceCollection = new ServiceCollection();
18+
new Startup().ConfigureServices(serviceCollection);
19+
var serviceProvider = serviceCollection.BuildServiceProvider();
2020

21-
Subject = new Command(serviceProvider.GetRequiredService<ICommandFunction>());
22-
}
23-
24-
[Test]
25-
public async Task should_work()
26-
{
27-
var request = GetRequest("{ 'Value': 'Foo' }");
28-
var context = new TestLambdaContext();
29-
30-
var result = await Subject.Post(request, context, "FooCommand");
31-
32-
result.Should().NotBeNull();
33-
}
34-
35-
[Test]
36-
public async Task should_handle_errors()
37-
{
38-
var request = GetRequest("{ 'Value': 'Foo' }");
39-
var context = new TestLambdaContext();
21+
Subject = new Command(serviceProvider.GetRequiredService<ICommandFunction>());
22+
Context = new TestLambdaContext();
23+
}
4024

41-
var result = await Subject.Post(request, context, "FailCommand");
25+
[Test]
26+
public async Task should_handle_command()
27+
{
28+
var response = await Subject.Post(GetRequest(new FooCommand { Value = "Foo" }), Context, "FooCommand");
29+
response.StatusCode.Should().Be(200);
30+
}
4231

43-
result.ShouldBeError("The command type 'FailCommand' could not be found");
44-
}
32+
[Test]
33+
public async Task should_handle_errors()
34+
{
35+
var response = await Subject.Post(GetRequest(new FooCommand()), Context, "FooCommand");
36+
response.ShouldBeError("Value cannot be null or empty");
37+
}
4538

46-
static APIGatewayProxyRequest GetRequest(string content) => new() { Body = content };
39+
static APIGatewayProxyRequest GetRequest(object body) => new() { Body = JsonSerializer.Serialize(body) };
4740

48-
Command Subject = null!;
49-
}
41+
Command Subject = null!;
42+
TestLambdaContext Context = null!;
5043
}
5144
}
Lines changed: 36 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json;
2+
using System.Web;
13
using Amazon.Lambda.APIGatewayEvents;
24
using Amazon.Lambda.Core;
35
using Amazon.Lambda.TestUtilities;
@@ -11,90 +13,52 @@ namespace CommandQuery.Sample.AWSLambda.Tests
1113
{
1214
public class QueryTests
1315
{
14-
public class when_using_the_real_function_via_Post
16+
[SetUp]
17+
public void SetUp()
1518
{
16-
[SetUp]
17-
public void SetUp()
18-
{
19-
var serviceCollection = new ServiceCollection();
20-
new Startup().ConfigureServices(serviceCollection);
21-
var serviceProvider = serviceCollection.BuildServiceProvider();
22-
23-
Subject = new Query(serviceProvider.GetRequiredService<IQueryFunction>());
24-
Request = GetRequest("POST", content: "{ \"Id\": 1 }");
25-
Context = new TestLambdaContext();
26-
}
27-
28-
[Test]
29-
public async Task should_work()
30-
{
31-
var result = await Subject.Post(Request, Context, "BarQuery");
32-
var value = result.As<Bar>()!;
33-
34-
value.Id.Should().Be(1);
35-
value.Value.Should().NotBeEmpty();
36-
}
37-
38-
[Test]
39-
public async Task should_handle_errors()
40-
{
41-
var result = await Subject.Post(Request, Context, "FailQuery");
42-
43-
result.ShouldBeError("The query type 'FailQuery' could not be found");
44-
}
19+
var serviceCollection = new ServiceCollection();
20+
new Startup().ConfigureServices(serviceCollection);
21+
var serviceProvider = serviceCollection.BuildServiceProvider();
4522

46-
Query Subject = null!;
47-
APIGatewayProxyRequest Request = null!;
48-
ILambdaContext Context = null!;
23+
Subject = new Query(serviceProvider.GetRequiredService<IQueryFunction>());
24+
Context = new TestLambdaContext();
4925
}
5026

51-
public class when_using_the_real_function_via_Get
27+
[Test]
28+
public async Task should_handle_query_via_Post()
5229
{
53-
[SetUp]
54-
public void SetUp()
55-
{
56-
var serviceCollection = new ServiceCollection();
57-
new Startup().ConfigureServices(serviceCollection);
58-
var serviceProvider = serviceCollection.BuildServiceProvider();
59-
60-
Subject = new Query(serviceProvider.GetRequiredService<IQueryFunction>());
61-
Request = GetRequest("GET", query: new Dictionary<string, IList<string>> { { "Id", new List<string> { "1" } } });
62-
Context = new TestLambdaContext();
63-
}
64-
65-
[Test]
66-
public async Task should_work()
67-
{
68-
var result = await Subject.Get(Request, Context, "BarQuery");
69-
var value = result.As<Bar>()!;
70-
71-
value.Id.Should().Be(1);
72-
value.Value.Should().NotBeEmpty();
73-
}
74-
75-
[Test]
76-
public async Task should_handle_errors()
77-
{
78-
var result = await Subject.Get(Request, Context, "FailQuery");
79-
80-
result.ShouldBeError("The query type 'FailQuery' could not be found");
81-
}
30+
var response = await Subject.Post(GetRequest("POST", new BarQuery { Id = 1 }), Context, "BarQuery");
31+
var value = response.Body<Bar>()!;
32+
value.Id.Should().Be(1);
33+
}
8234

83-
Query Subject = null!;
84-
APIGatewayProxyRequest Request = null!;
85-
ILambdaContext Context = null!;
35+
[Test]
36+
public async Task should_handle_query_via_Get()
37+
{
38+
var response = await Subject.Get(GetRequest("GET", "?Id=1"), Context, "BarQuery");
39+
var value = response.Body<Bar>()!;
40+
value.Id.Should().Be(1);
8641
}
8742

88-
static APIGatewayProxyRequest GetRequest(string method, string? content = null, Dictionary<string, IList<string>>? query = null)
43+
static APIGatewayProxyRequest GetRequest(string method, object body) => new()
8944
{
90-
var request = new APIGatewayProxyRequest
45+
HttpMethod = method,
46+
Body = JsonSerializer.Serialize(body),
47+
};
48+
49+
static APIGatewayProxyRequest GetRequest(string method, string query)
50+
{
51+
var collection = HttpUtility.ParseQueryString(query);
52+
var parameters = collection.AllKeys.ToDictionary(k => k!, k => collection.GetValues(k)!.ToList() as IList<string>);
53+
54+
return new()
9155
{
9256
HttpMethod = method,
93-
Body = content,
94-
MultiValueQueryStringParameters = query
57+
MultiValueQueryStringParameters = parameters
9558
};
96-
97-
return request;
9859
}
60+
61+
Query Subject = null!;
62+
ILambdaContext Context = null!;
9963
}
10064
}

samples/CommandQuery.Sample.AWSLambda.Tests/ShouldExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ public static void ShouldBeError(this APIGatewayProxyResponse result, string mes
1111
{
1212
result.Should().NotBeNull();
1313
result.StatusCode.Should().NotBe(200);
14-
var value = JsonSerializer.Deserialize<Error>(result.Body)!;
14+
var value = result.Body<Error>()!;
1515
value.Should().NotBeNull();
1616
value.Message.Should().Be(message);
1717
}
18+
19+
public static T? Body<T>(this APIGatewayProxyResponse result)
20+
{
21+
return JsonSerializer.Deserialize<T>(result.Body);
22+
}
1823
}
1924
}

samples/CommandQuery.Sample.AWSLambda.Tests/TestExtensions.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

samples/CommandQuery.Sample.AWSLambda/Command.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class Command(ICommandFunction commandFunction)
1010
{
1111
[LambdaFunction(Policies = "AWSLambdaBasicExecutionRole", MemorySize = 256, Timeout = 30)]
1212
[RestApi(LambdaHttpMethod.Post, "/command/{commandName}")]
13-
public async Task<APIGatewayProxyResponse> Post(APIGatewayProxyRequest request, ILambdaContext context, string commandName) =>
13+
public async Task<APIGatewayProxyResponse> Post(
14+
APIGatewayProxyRequest request,
15+
ILambdaContext context,
16+
string commandName) =>
1417
await commandFunction.HandleAsync(commandName, request, context.Logger);
1518
}

samples/CommandQuery.Sample.AWSLambda/Query.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ public class Query(IQueryFunction queryFunction)
1010
{
1111
[LambdaFunction(Policies = "AWSLambdaBasicExecutionRole", MemorySize = 256, Timeout = 30)]
1212
[RestApi(LambdaHttpMethod.Get, "/query/{queryName}")]
13-
public async Task<APIGatewayProxyResponse> Get(APIGatewayProxyRequest request, ILambdaContext context, string queryName) =>
13+
public async Task<APIGatewayProxyResponse> Get(
14+
APIGatewayProxyRequest request,
15+
ILambdaContext context,
16+
string queryName) =>
1417
await queryFunction.HandleAsync(queryName, request, context.Logger);
1518

1619
[LambdaFunction(Policies = "AWSLambdaBasicExecutionRole", MemorySize = 256, Timeout = 30)]
1720
[RestApi(LambdaHttpMethod.Post, "/query/{queryName}")]
18-
public async Task<APIGatewayProxyResponse> Post(APIGatewayProxyRequest request, ILambdaContext context, string queryName) =>
21+
public async Task<APIGatewayProxyResponse> Post(
22+
APIGatewayProxyRequest request,
23+
ILambdaContext context,
24+
string queryName) =>
1925
await queryFunction.HandleAsync(queryName, request, context.Logger);
2026
}
2127
}
Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Net;
2-
using System.Text;
2+
using CommandQuery.Sample.Contracts.Commands;
33
using FluentAssertions;
44
using Microsoft.AspNetCore.Mvc.Testing;
55
using NUnit.Framework;
@@ -8,46 +8,35 @@ namespace CommandQuery.Sample.AspNetCore.Tests
88
{
99
public class CommandControllerTests
1010
{
11-
public class when_using_the_real_controller
11+
[SetUp]
12+
public void SetUp()
1213
{
13-
[SetUp]
14-
public void SetUp()
15-
{
16-
Factory = new WebApplicationFactory<Program>();
17-
Client = Factory.CreateClient();
18-
}
19-
20-
[TearDown]
21-
public void TearDown()
22-
{
23-
Client.Dispose();
24-
Factory.Dispose();
25-
}
26-
27-
[Test]
28-
public async Task should_work()
29-
{
30-
var content = new StringContent("{ \"Value\": \"Foo\" }", Encoding.UTF8, "application/json");
31-
32-
var result = await Client.PostAsync("/api/command/FooCommand", content);
33-
34-
result.EnsureSuccessStatusCode();
35-
(await result.Content.ReadAsStringAsync()).Should().BeEmpty();
36-
}
37-
38-
[Test]
39-
public async Task should_handle_errors()
40-
{
41-
var content = new StringContent("{ \"Value\": \"Foo\" }", Encoding.UTF8, "application/json");
14+
Factory = new WebApplicationFactory<Program>();
15+
Client = Factory.CreateClient();
16+
}
4217

43-
var result = await Client.PostAsync("/api/command/FailCommand", content);
18+
[TearDown]
19+
public void TearDown()
20+
{
21+
Client.Dispose();
22+
Factory.Dispose();
23+
}
4424

45-
result.StatusCode.Should().Be(HttpStatusCode.NotFound);
46-
(await result.Content.ReadAsStringAsync()).Should().BeEmpty();
47-
}
25+
[Test]
26+
public async Task should_handle_command()
27+
{
28+
var response = await Client.PostAsJsonAsync("/api/command/FooCommand", new FooCommand { Value = "Foo" });
29+
response.StatusCode.Should().Be(HttpStatusCode.OK);
30+
}
4831

49-
WebApplicationFactory<Program> Factory = null!;
50-
HttpClient Client = null!;
32+
[Test]
33+
public async Task should_handle_errors()
34+
{
35+
var response = await Client.PostAsJsonAsync("/api/command/FooCommand", new FooCommand { Value = "" });
36+
await response.ShouldBeErrorAsync("Value cannot be null or empty");
5137
}
38+
39+
WebApplicationFactory<Program> Factory = null!;
40+
HttpClient Client = null!;
5241
}
5342
}

0 commit comments

Comments
 (0)