Skip to content

Commit

Permalink
EES-5738 Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyoungman committed Dec 16, 2024
1 parent cc25b5f commit 6765db1
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,51 @@ await TestApp.AddTestData<ContentDbContext>(context =>
AssertResultsForExpectedReleaseFiles(publication1Release1Version1Files, pagedResult.Results);
}

[Fact]
public async Task FilterByGeographicLevel_Success()
{
var (publication1, publication2) = _fixture
.DefaultPublication()
// Publications each have a published release version
.WithReleases(_fixture.DefaultRelease(publishedVersions: 1)
.Generate(1))
.WithTheme(_fixture.DefaultTheme())
.GenerateTuple2();

var publication1Release1Version1Files = _fixture.DefaultReleaseFile()
.WithReleaseVersion(publication1.ReleaseVersions[0])
.WithFiles(_fixture.DefaultFile(FileType.Data)
.WithDataSetFileMeta(_fixture.DefaultDataSetFileMeta()
.WithGeographicLevels(
[GeographicLevel.Country, GeographicLevel.LocalAuthority, GeographicLevel.Institution]))
.WithDataSetFileGeographicLevels(
[GeographicLevel.Country, GeographicLevel.LocalAuthority, GeographicLevel.Institution])
.GenerateList(1))
.GenerateList();

var publication2Release1Version1Files = GenerateDataSetFilesForReleaseVersion(publication2.ReleaseVersions[0]);

await TestApp.AddTestData<ContentDbContext>(context =>
{
context.ReleaseFiles.AddRange(publication1Release1Version1Files);
context.ReleaseFiles.AddRange(publication2Release1Version1Files);
});

MemoryCacheService
.SetupNotFoundForAnyKey<ListDataSetFilesCacheKey, PaginatedListViewModel<DataSetFileSummaryViewModel>>();

var query = new DataSetFileListRequest(GeographicLevel: GeographicLevel.Institution.GetEnumValue());
var response = await ListDataSetFiles(query);

MockUtils.VerifyAllMocks(MemoryCacheService);

var pagedResult = response.AssertOk<PaginatedListViewModel<DataSetFileSummaryViewModel>>();

pagedResult.AssertHasExpectedPagingAndResultCount(
expectedTotalResults: 1);
AssertResultsForExpectedReleaseFiles(publication1Release1Version1Files, pagedResult.Results);
}

[Fact]
public async Task FilterByPublicationId_Success()
{
Expand Down Expand Up @@ -1675,6 +1720,7 @@ private async Task<HttpResponseMessage> ListDataSetFiles(
{ "themeId", request.ThemeId?.ToString() },
{ "publicationId", request.PublicationId?.ToString() },
{ "releaseId", request.ReleaseId?.ToString() },
{ "geographicLevel", request.GeographicLevel },
{ "latestOnly", request.LatestOnly?.ToString() },
{ "searchTerm", request.SearchTerm },
{ "sort", request.Sort?.ToString() },
Expand Down Expand Up @@ -1737,6 +1783,7 @@ private List<ReleaseFile> GenerateDataSetFilesForReleaseVersion(
.WithReleaseVersion(releaseVersion)
.WithFiles(_fixture.DefaultFile(FileType.Data)
.WithDataSetFileMeta(_fixture.DefaultDataSetFileMeta())
.WithDataSetFileGeographicLevels([GeographicLevel.Country])
.GenerateList(numberOfDataSets))
.GenerateList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ public static Generator<DataImport> WithSubjectId(

public static Generator<DataImport> WithDefaultFiles(
this Generator<DataImport> generator,
string dataFileName)
=> generator.ForInstance(s => s.SetDefaultFiles(dataFileName));
string dataFileName,
bool metaSet = true)
{
if (metaSet)
{
return generator.ForInstance(s => s.SetDefaultFiles(dataFileName));
}
return generator.ForInstance(s => s.SetDefaultFilesWithoutMeta(dataFileName));
}

public static Generator<DataImport> WithFile(
this Generator<DataImport> generator,
Expand Down Expand Up @@ -98,6 +105,29 @@ public static InstanceSetters<DataImport> SetDefaultFiles(
)
.Set(d => d.MetaFileId, (_, d) => d.MetaFile.Id);

public static InstanceSetters<DataImport> SetDefaultFilesWithoutMeta(
this InstanceSetters<DataImport> setters,
string dataFileName)
=> setters
.Set(
d => d.File,
(_, d, context) => context.Fixture
.DefaultFile(FileType.Data)
.WithDataSetFileMeta(null)
.WithDataSetFileGeographicLevels([])
.WithFilename($"{dataFileName}.csv")
.WithSubjectId(d.SubjectId)
)
.Set(d => d.FileId, (_, d) => d.File.Id)
.Set(
d => d.MetaFile,
(_, d, context) => context.Fixture
.DefaultFile(FileType.Metadata)
.WithFilename($"{dataFileName}.meta.csv")
.WithSubjectId(d.SubjectId)
)
.Set(d => d.MetaFileId, (_, d) => d.MetaFile.Id);

public static InstanceSetters<DataImport> SetFile(
this InstanceSetters<DataImport> setters,
File file)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using GovUk.Education.ExploreEducationStatistics.Common.Model.Data;
using GovUk.Education.ExploreEducationStatistics.Common.Tests.Fixtures;

namespace GovUk.Education.ExploreEducationStatistics.Content.Model.Tests.Fixtures;

public static class DataSetFileGeographicLevelGeneratorExtensions
{
public static Generator<DataSetFileGeographicLevel> DefaultDataSetFileGeographicLevel(this DataFixture fixture)
=> fixture.Generator<DataSetFileGeographicLevel>().WithDefaults();

public static Generator<DataSetFileGeographicLevel> WithDefaults(this Generator<DataSetFileGeographicLevel> generator)
=> generator.ForInstance(d => d.SetDefaults());

public static InstanceSetters<DataSetFileGeographicLevel> SetDefaults(
this InstanceSetters<DataSetFileGeographicLevel> setters)
=> setters
.SetDataSetFileVersionId(Guid.NewGuid())
.SetGeographicLevel(GeographicLevel.Country);

public static Generator<DataSetFileGeographicLevel> WithDataSetFileVersion(
this Generator<DataSetFileGeographicLevel> generator,
File dataSetFileVersion)
=> generator.ForInstance(s => s.SetDataSetFileVersion(dataSetFileVersion));

public static Generator<DataSetFileGeographicLevel> WithDataSetFileVersionId(
this Generator<DataSetFileGeographicLevel> generator,
Guid dataSetFileVersionId)
=> generator.ForInstance(s => s.SetDataSetFileVersionId(dataSetFileVersionId));

public static Generator<DataSetFileGeographicLevel> WithGeographicLevel(
this Generator<DataSetFileGeographicLevel> generator,
GeographicLevel geographicLevel)
=> generator.ForInstance(s => s.SetGeographicLevel(geographicLevel));

public static InstanceSetters<DataSetFileGeographicLevel> SetDataSetFileVersion(
this InstanceSetters<DataSetFileGeographicLevel> setters,
File dataSetFileVersion)
=> setters.Set(f => f.DataSetFileVersion, dataSetFileVersion);

public static InstanceSetters<DataSetFileGeographicLevel> SetDataSetFileVersionId(
this InstanceSetters<DataSetFileGeographicLevel> setters,
Guid dataSetFileVersionId)
=> setters.Set(f => f.DataSetFileVersionId, dataSetFileVersionId);

public static InstanceSetters<DataSetFileGeographicLevel> SetGeographicLevel(
this InstanceSetters<DataSetFileGeographicLevel> setters,
GeographicLevel geographicLevel)
=> setters.Set(f => f.GeographicLevel, geographicLevel);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GovUk.Education.ExploreEducationStatistics.Common.Model;
using GovUk.Education.ExploreEducationStatistics.Common.Model.Data;
using GovUk.Education.ExploreEducationStatistics.Common.Tests.Fixtures;
using Semver;

namespace GovUk.Education.ExploreEducationStatistics.Content.Model.Tests.Fixtures;

Expand Down Expand Up @@ -84,6 +85,11 @@ public static Generator<File> WithDataSetFileMeta(
DataSetFileMeta? dataSetFileMeta)
=> generator.ForInstance(s => s.SetDataSetFileMeta(dataSetFileMeta));

public static Generator<File> WithDataSetFileGeographicLevels(
this Generator<File> generator,
List<GeographicLevel> geographicLevels)
=> generator.ForInstance(s => s.SetDataSetFileGeographicLevels(geographicLevels));

public static InstanceSetters<File> SetDefaults(this InstanceSetters<File> setters, FileType? fileType)
=> fileType switch
{
Expand All @@ -110,7 +116,8 @@ public static InstanceSetters<File> SetDataFileDefaults(this InstanceSetters<Fil
.SetDefault(f => f.SubjectId)
.SetContentType("text/csv")
.SetDefault(f => f.DataSetFileId)
.Set(f => f.DataSetFileMeta, (_, _, context) => context.Fixture.DefaultDataSetFileMeta());
.Set(f => f.DataSetFileMeta, (_, _, context) => context.Fixture.DefaultDataSetFileMeta())
.SetDataSetFileGeographicLevels([GeographicLevel.Country, GeographicLevel.LocalAuthority, GeographicLevel.LocalAuthorityDistrict]);

public static InstanceSetters<File> SetMetaFileDefaults(this InstanceSetters<File> setters)
=> setters
Expand Down Expand Up @@ -200,4 +207,16 @@ public static InstanceSetters<File> SetDataSetFileMeta(
this InstanceSetters<File> setters,
DataSetFileMeta? dataSetFileMeta)
=> setters.Set(f => f.DataSetFileMeta, dataSetFileMeta);

public static InstanceSetters<File> SetDataSetFileGeographicLevels(
this InstanceSetters<File> setters,
List<GeographicLevel> geographicLevels)
=> setters.Set(
file => file.DataSetFileGeographicLevels,
(_, file) => geographicLevels.Select(
gl => new DataSetFileGeographicLevel
{
DataSetFileVersionId = file.Id,
GeographicLevel = gl,
}).ToList());
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class File : ICreatedTimestamp<DateTime?>

public int? DataSetFileVersion { get; set; }

public List<DataSetFileGeographicLevel>? DataSetFileGeographicLevels { get; set; }
public List<DataSetFileGeographicLevel> DataSetFileGeographicLevels { get; set; } = [];

public DataSetFileMeta? DataSetFileMeta { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,12 @@ internal static IQueryable<ReleaseFile> HavingReleaseVersionId(
return releaseVersionId.HasValue ? query.Where(rf => rf.ReleaseVersionId == releaseVersionId.Value) : query;
}

internal static IQueryable<ReleaseFile> HavingGeographicLevel( // @MarkFix write tests
internal static IQueryable<ReleaseFile> HavingGeographicLevel(
this IQueryable<ReleaseFile> query,
GeographicLevel? geographicLevel)
{
return geographicLevel.HasValue
? query.Where(rf => rf.File.DataSetFileGeographicLevels!.Any( // @MarkFix null allowing
? query.Where(rf => rf.File.DataSetFileGeographicLevels.Any(
gl => gl.GeographicLevel == geographicLevel
&& rf.FileId == gl.DataSetFileVersionId))
: query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using GovUk.Education.ExploreEducationStatistics.Data.Processor.Services;
using GovUk.Education.ExploreEducationStatistics.Data.Processor.Services.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Data.Processor.Tests.Services;
using GovUk.Education.ExploreEducationStatistics.Public.Data.Model.Migrations;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -106,7 +107,7 @@ public async Task ProcessStage3()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(_subject.Id)
.WithDefaultFiles("small-csv")
.WithDefaultFiles("small-csv", metaSet: false)
.WithStatus(STAGE_3)
.WithTotalRows(16)
.WithExpectedImportedRows(16)
Expand Down Expand Up @@ -325,7 +326,7 @@ public async Task ProcessStage3_FailsImportIfRowCountsDontMatch()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(_subject.Id)
.WithDefaultFiles("small-csv")
.WithDefaultFiles("small-csv", metaSet: false)
.WithStatus(STAGE_3)
.WithTotalRows(16)
.WithExpectedImportedRows(16)
Expand Down Expand Up @@ -454,7 +455,7 @@ public async Task ProcessStage3_PartiallyImportedAlready()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(_subject.Id)
.WithDefaultFiles("small-csv")
.WithDefaultFiles("small-csv", metaSet: false)
.WithStatus(STAGE_3)
.WithTotalRows(16)
.WithExpectedImportedRows(16)
Expand Down Expand Up @@ -604,7 +605,7 @@ public async Task ProcessStage3_PartiallyImportedAlready_BatchSizeChanged()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(_subject.Id)
.WithDefaultFiles("small-csv")
.WithDefaultFiles("small-csv", metaSet: false)
.WithStatus(STAGE_3)
.WithTotalRows(16)
.WithExpectedImportedRows(16)
Expand Down Expand Up @@ -749,7 +750,7 @@ public async Task ProcessStage3_IgnoredRows()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(_subject.Id)
.WithDefaultFiles("ignored-school-rows")
.WithDefaultFiles("ignored-school-rows", metaSet: false)
.WithStatus(STAGE_3)
.WithTotalRows(16)
.WithExpectedImportedRows(8)
Expand Down Expand Up @@ -894,7 +895,7 @@ public async Task ProcessStage3_IgnoredRows_PartiallyImported()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(_subject.Id)
.WithDefaultFiles("ignored-school-rows")
.WithDefaultFiles("ignored-school-rows", metaSet: false)
.WithStatus(STAGE_3)
.WithTotalRows(16)
.WithExpectedImportedRows(8)
Expand Down Expand Up @@ -1052,7 +1053,7 @@ public async Task ProcessStage3_Cancelling()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(_subject.Id)
.WithDefaultFiles("small-csv")
.WithDefaultFiles("small-csv", metaSet: false)
.WithStatus(STAGE_3)
.WithTotalRows(16)
.WithExpectedImportedRows(16)
Expand Down Expand Up @@ -1190,7 +1191,7 @@ public async Task ProcessStage3_CancelledAlready()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(_subject.Id)
.WithDefaultFiles("small-csv")
.WithDefaultFiles("small-csv", metaSet: false)
.WithStatus(CANCELLED)
.WithTotalRows(16)
.WithExpectedImportedRows(16)
Expand Down Expand Up @@ -1324,7 +1325,7 @@ public async Task ProcessStage3_AdditionalFiltersAndIndicators()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(_subject.Id)
.WithDefaultFiles("additional-filters-and-indicators")
.WithDefaultFiles("additional-filters-and-indicators", metaSet: false)
.WithStatus(STAGE_3)
.WithTotalRows(16)
.WithExpectedImportedRows(16)
Expand Down Expand Up @@ -1493,7 +1494,7 @@ public async Task ProcessStage3_SpecialFilterItemAndIndicatorValues()
var import = _fixture
.DefaultDataImport()
.WithSubjectId(subject.Id)
.WithDefaultFiles("small-csv-with-special-data")
.WithDefaultFiles("small-csv-with-special-data", metaSet: false)
.WithStatus(STAGE_3)
.WithTotalRows(5)
.WithExpectedImportedRows(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public async Task WriteDataSetMetaFile_Success()

var file = _fixture.DefaultFile(FileType.Data)
.WithDataSetFileMeta(null)
.WithDataSetFileGeographicLevels([])
.WithSubjectId(subject.Id)
.Generate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const testDataSetFileSummaries: DataSetFileSummary[] = [
to: '2020',
},
filters: ['Filter 1', 'Filter 2'],
geographicLevels: ['National', 'Regional'],
geographicLevels: ['National', 'Regional', 'Local authority'],
indicators: ['Indicator 1', 'Indicator 2'],
},
latestData: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ describe('DataCataloguePage', () => {
render(<DataCataloguePage />);

await waitFor(() => {
expect(screen.getByText('2 data sets')).toBeInTheDocument();
expect(screen.getByText('3 data sets')).toBeInTheDocument();
});

expect(mockRouter).toMatchObject({
Expand Down Expand Up @@ -1195,6 +1195,32 @@ describe('DataCataloguePage', () => {
).toBeInTheDocument();
});

test('filters by geographic level', async () => {
mockRouter.setCurrentUrl('/data-catalogue?geographicLevel=LA');

dataSetService.listDataSetFiles.mockResolvedValueOnce({
results: [testDataSetFileSummaries[1], testDataSetFileSummaries[2]],
paging: { ...testPaging, totalPages: 1, totalResults: 1 },
});
publicationService.getPublicationTree.mockResolvedValue(testThemes);
publicationService.listReleases.mockResolvedValue(testReleases);

render(<DataCataloguePage />);

await waitFor(() => {
expect(screen.getByText('1 data set')).toBeInTheDocument();
});

expect(mockRouter).toMatchObject({
pathname: '/data-catalogue',
query: { geographicLevel: 'LA' },
});

expect(screen.getByLabelText('Filter by Geographic level')).toHaveValue(
'LA',
);
});

test('filters by search term', async () => {
mockRouter.setCurrentUrl('/data-catalogue?searchTerm=find+me');

Expand Down

0 comments on commit 6765db1

Please sign in to comment.