|
| 1 | +using System.Reflection; |
| 2 | +using GovUk.Education.ExploreEducationStatistics.Content.Search.FunctionApp.Clients; |
| 3 | +using GovUk.Education.ExploreEducationStatistics.Content.Search.FunctionApp.Clients.ContentApi; |
| 4 | +using GovUk.Education.ExploreEducationStatistics.Content.Search.FunctionApp.Exceptions; |
| 5 | + |
| 6 | +namespace GovUk.Education.ExploreEducationStatistics.Content.Search.FunctionApp.Tests.Clients; |
| 7 | + |
| 8 | +public class ContentApiClientTests |
| 9 | +{ |
| 10 | + private IContentApiClient GetSut(Action<HttpClient>? modifyHttpClient = null) |
| 11 | + { |
| 12 | + var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(10) }; |
| 13 | + modifyHttpClient?.Invoke(httpClient); |
| 14 | + return new ContentApiClient(httpClient); |
| 15 | + } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Separately assert that each of the public properties of two instances of an object are equal. |
| 19 | + /// This provides a finer grained explanation of where two objects differ in equality. |
| 20 | + /// </summary> |
| 21 | + private void AssertAllPropertiesMatch<T>(T expected, T actual) |
| 22 | + { |
| 23 | + AssertAll(typeof(T) |
| 24 | + .GetProperties(BindingFlags.Public | BindingFlags.Instance) |
| 25 | + .Select(propertyInfo => (Action)(() => Assert.Equal(propertyInfo.GetValue(expected), propertyInfo.GetValue(actual))))); |
| 26 | + Assert.Equal(expected, actual); // Belt and braces |
| 27 | + } |
| 28 | + |
| 29 | + private void AssertAll(params IEnumerable<Action>[] assertions) => |
| 30 | + Assert.All(assertions.SelectMany(a => a), assertion => assertion()); |
| 31 | + |
| 32 | + public class BasicTests : ContentApiClientTests |
| 33 | + { |
| 34 | + [Fact] |
| 35 | + public void Can_instantiate_SUT() => Assert.NotNull(GetSut()); |
| 36 | + } |
| 37 | + |
| 38 | + public abstract class LocalDevelopmentIntegrationTests : ContentApiClientTests |
| 39 | + { |
| 40 | + /// <summary> |
| 41 | + /// Ensure ContentAPI is running locally on port 5010 |
| 42 | + /// </summary> |
| 43 | + public class CallLocalService : LocalDevelopmentIntegrationTests |
| 44 | + { |
| 45 | + private IContentApiClient GetSut() => |
| 46 | + base.GetSut(httpClient => httpClient.BaseAddress = new Uri("http://localhost:5010")); |
| 47 | + |
| 48 | + [Fact(Skip = "This test is only for local development")] |
| 49 | + public async Task GetExampleSeedDocument() |
| 50 | + { |
| 51 | + // ARRANGE |
| 52 | + var sut = GetSut(); |
| 53 | + var publicationSlug = "seed-publication-permanent-and-fixed-period-exclusions-in-england"; |
| 54 | + |
| 55 | + // ACT |
| 56 | + var actual = await sut.GetPublicationLatestReleaseSearchViewModelAsync(publicationSlug); |
| 57 | + |
| 58 | + // ASSERT |
| 59 | + Assert.NotNull(actual); |
| 60 | + var expected = new ReleaseSearchViewModelDto |
| 61 | + { |
| 62 | + ReleaseVersionId = new Guid("46c5d916-ee40-49bd-cfdc-08dc1c5c621e"), |
| 63 | + Published = DateTimeOffset.Parse("2018-07-18T23:00:00Z"), |
| 64 | + PublicationTitle = "Seed publication - Permanent and fixed-period exclusions in England", |
| 65 | + Summary = "Read national statistical summaries, view charts and tables and download data files.", |
| 66 | + Theme = "Seed theme - Pupils and schools", |
| 67 | + Type = "OfficialStatistics", |
| 68 | + TypeBoost = 5, |
| 69 | + PublicationSlug = "seed-publication-permanent-and-fixed-period-exclusions-in-england", |
| 70 | + ReleaseSlug = "2016-17", |
| 71 | + HtmlContent = "<html>\n <head>\n <title>Seed publication - Permanent and fixed-period exclusions in England</title>\n </head>\n <body>\n<h1>Seed publication - Permanent and fixed-period exclusions in England</h1>\n<h2>Academic year 2016/17</h2>\n<h3>Summary</h3>\n<p>Read national statistical summaries, view charts and tables and download data files.</p>\n<h3>Headlines</h3>\n<p>The rate of permanent exclusions has increased since last year from 0.08 per cent of pupil enrolments in 2015/16 to 0.10 per cent in 2016/17.</p>\n<h3>About this release</h3>\n<p>The statistics and data cover permanent and fixed period exclusions and school-level exclusions during the 2016/17 academic year in the following state-funded school types as reported in the school census.</p>\n<h3>Permanent exclusions</h3>\n<p>The number of permanent exclusions has increased across all state-funded primary, secondary and special schools to 7,720 - up from 6,685 in 2015/16.</p>\n<h3>Fixed-period exclusions</h3>\n<p>The number of fixed-period exclusions has increased across all state-funded primary, secondary and special schools to 381,865 - up from 339,360 in 2015/16.</p>\n<h3>Number and length of fixed-period exclusions</h3>\n<p>The number of pupils with one or more fixed-period exclusion has increased across state-funded primary, secondary and special schools to 183,475 (2.29% of pupils) up from 167,125 (2.11% of pupils) in 2015/16.</p>\n<h3>Reasons for exclusions</h3>\n<p>All reasons (except bullying and theft) saw an increase in permanent exclusions since 2015/16.</p>\n<h3>Exclusions by pupil characteristics</h3>\n<p>There was a similar pattern to previous years where the following groups (where higher exclusion rates are expected) showed an increase in exclusions since 2015/16.</p>\n<h3>Independent exclusion reviews</h3>\n<p>There were 560 reviews lodged with independent review panels in maintained primary, secondary and special schools and academies of which 525 (93.4%) were determined and 45 (8.0%) resulted in an offer of reinstatement.</p>\n<h3>Pupil referral units exclusions</h3>\n<p>The permanent exclusion rate in pupil referral units decreased to 0.13 - down from 0.14% in 2015/16.</p>\n<h3>Regional and local authority (LA) breakdown</h3>\n<p>There's considerable variation in the permanent exclusion and fixed-period exclusion rate at the LA level.</p>\n </body>\n</html>\n" |
| 72 | + }; |
| 73 | + AssertAllPropertiesMatch(expected, actual); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public class CallUnknownService : LocalDevelopmentIntegrationTests |
| 78 | + { |
| 79 | + private IContentApiClient GetSut() => |
| 80 | + base.GetSut(httpClient => httpClient.BaseAddress = new Uri("http://localhost:8123")); // Cause a 404 |
| 81 | + |
| 82 | + [Fact(Skip = "This test is only for local development")] |
| 83 | + public async Task UnknownEndpointShouldThrow() |
| 84 | + { |
| 85 | + // ARRANGE |
| 86 | + var sut = GetSut(); |
| 87 | + var publicationSlug = "seed-publication-permanent-and-fixed-period-exclusions-in-england"; |
| 88 | + |
| 89 | + // ACT |
| 90 | + var exception = await Record.ExceptionAsync(() => sut.GetPublicationLatestReleaseSearchViewModelAsync(publicationSlug)); |
| 91 | + |
| 92 | + // ASSERT |
| 93 | + Assert.NotNull(exception); |
| 94 | + var unableToGetPublicationLatestReleaseSearchViewModelException = Assert.IsType<UnableToGetPublicationLatestReleaseSearchViewModelException>(exception); |
| 95 | + Assert.Contains(publicationSlug, unableToGetPublicationLatestReleaseSearchViewModelException.Message); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | +} |
0 commit comments