-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClimateApiTests.cs
113 lines (95 loc) · 4.03 KB
/
ClimateApiTests.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Servirtium.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using Microsoft.Extensions.Logging;
using Xunit;
namespace Servirtium.Climate.Demo
{
public abstract class ClimateApiTests
{
protected static ILoggerFactory loggerFactory = LoggerFactory.Create((builder) => builder
.AddConsole()
.AddDebug());
internal abstract IEnumerable<(IServirtiumServer, ClimateApi)> GenerateTestServerClientPairs(string script);
private void RunTest(string script, Action<ClimateApi> verification)
{
foreach ((IServirtiumServer server, ClimateApi api) in GenerateTestServerClientPairs(script))
{
try
{
server.Start();
verification(api);
}
finally
{
server.Stop().Wait();
}
}
}
public virtual void AverageRainfallForGreatBritainFrom1980to1999Exists()
{
RunTest(
"averageRainfallForGreatBritainFrom1980to1999Exists.md",
(api) => Assert.Equal(988.8454972331015, api.GetAveAnnualRainfall(1980, 1999, "gbr").Result, 0)
);
}
public virtual void AverageRainfallForFranceFrom1980to1999Exists()
{
RunTest(
"averageRainfallForFranceFrom1980to1999Exists.md",
(api) => Assert.Equal(913.7986955122727, api.GetAveAnnualRainfall(1980, 1999, "fra").Result, 0)
);
}
public virtual void AverageRainfallForEgyptFrom1980to1999Exists()
{
RunTest(
"averageRainfallForEgyptFrom1980to1999Exists.md",
(api) => Assert.Equal(54.58587712129825, api.GetAveAnnualRainfall(1980, 1999, "egy").Result, 0)
);
}
public virtual void AverageRainfallForGreatBritainFrom1985to1995DoesNotExist()
{
RunTest(
"averageRainfallForGreatBritainFrom1985to1995DoesNotExist.md",
(api) =>
{
var e = Assert.Throws<AggregateException>(() => api.GetAveAnnualRainfall(1985, 1995, "gbr").Wait());
Assert.Equal("date range 1985-1995 not supported", e.InnerExceptions[0].Message);
}
);
}
public virtual void AverageRainfallForMiddleEarthFrom1980to1999DoesNotExist()
{
RunTest(
"averageRainfallForMiddleEarthFrom1980to1999DoesNotExist.md",
(api) =>
{
var e = Assert.Throws<AggregateException>(() => api.GetAveAnnualRainfall(1980, 1999, "mde").Wait());
Assert.Equal("mde not recognized by climateweb", e.InnerExceptions[0].Message);
}
);
}
public virtual void AverageRainfallForGreatBritainAndFranceFrom1980to1999CanBeCalculatedFromTwoRequests()
{
RunTest(
"averageRainfallForGreatBritainAndFranceFrom1980to1999CanBeCalculatedFromTwoRequests.md",
(api) => Assert.Equal(951.3220963726872, api.GetAveAnnualRainfall(1980, 1999, "gbr", "fra").Result, 0)
);
}
public virtual void AverageRainfallForNeptuneServiceNotFound()
{
RunTest(
"averageRainfallForNeptuneFrom1980to1999NotFoundError.md",
(api) => {
var exception = Assert.Throws<AggregateException>(() => api.GetPlanetaryRainfall(1980, 1999, "neptune").Result);
Assert.IsType<HttpRequestException>(exception.InnerExceptions[0]);
Assert.Matches($@"^GET Request to http://(.+)/climateweb/rest/v1/planet/annualavg/pr/1980/1999/neptune\.xml failed, status {HttpStatusCode.NotFound}", exception.InnerExceptions[0].Message);
}
);
}
}
}