-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathQueryTests.cs
64 lines (56 loc) · 2.02 KB
/
QueryTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Text.Json;
using System.Web;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.TestUtilities;
using CommandQuery.AWSLambda;
using CommandQuery.Sample.Contracts.Queries;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
namespace CommandQuery.Sample.AWSLambda.Tests
{
public class QueryTests
{
[SetUp]
public void SetUp()
{
var serviceCollection = new ServiceCollection();
new Startup().ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
Subject = new Query(serviceProvider.GetRequiredService<IQueryFunction>());
Context = new TestLambdaContext();
}
[Test]
public async Task should_handle_query_via_Post()
{
var response = await Subject.Post(GetRequest("POST", new BarQuery { Id = 1 }), Context, "BarQuery");
var value = response.Body<Bar>()!;
value.Id.Should().Be(1);
}
[Test]
public async Task should_handle_query_via_Get()
{
var response = await Subject.Get(GetRequest("GET", "?Id=1"), Context, "BarQuery");
var value = response.Body<Bar>()!;
value.Id.Should().Be(1);
}
static APIGatewayProxyRequest GetRequest(string method, object body) => new()
{
HttpMethod = method,
Body = JsonSerializer.Serialize(body),
};
static APIGatewayProxyRequest GetRequest(string method, string query)
{
var collection = HttpUtility.ParseQueryString(query);
var parameters = collection.AllKeys.ToDictionary(k => k!, k => collection.GetValues(k)!.ToList() as IList<string>);
return new()
{
HttpMethod = method,
MultiValueQueryStringParameters = parameters
};
}
Query Subject = null!;
ILambdaContext Context = null!;
}
}