From 9b87f1c932b55029ca68fe510a73a83da3475acc Mon Sep 17 00:00:00 2001 From: Tom Ridd Date: Tue, 10 Jan 2023 09:53:41 +0000 Subject: [PATCH 1/4] add clean dimension functionality to dataset landing page --- mapper/census.go | 14 +++++++++++--- mapper/census_test.go | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/mapper/census.go b/mapper/census.go index e2f334c..0658b72 100644 --- a/mapper/census.go +++ b/mapper/census.go @@ -7,6 +7,7 @@ import ( "html/template" "net/http" "net/url" + "regexp" "sort" "strconv" "strings" @@ -411,7 +412,7 @@ func populateCollapsible(Dimensions []dataset.VersionDimension, isFilterOutput b for _, dims := range Dimensions { if dims.Description != "" { var collapsibleContent coreModel.CollapsibleItem - collapsibleContent.Subheading = dims.Label + collapsibleContent.Subheading = cleanDimensionLabel(dims.Label) collapsibleContent.Content = strings.Split(dims.Description, "\n") collapsibleContentItems = append(collapsibleContentItems, collapsibleContent) } @@ -433,7 +434,7 @@ func mapCensusOptionsToDimensions(dims []dataset.VersionDimension, opts []datase pDim.Description = dimension.Description pDim.IsAreaType = helpers.IsBoolPtr(dimension.IsAreaType) pDim.ShowChange = pDim.IsAreaType && isFlex || isMultivariate - pDim.Title = dimension.Label + pDim.Title = cleanDimensionLabel(dimension.Label) pDim.ID = dimension.ID if dimension.QualityStatementText != "" && dimension.QualityStatementURL != "" { qs = append(qs, datasetLandingPageCensus.Panel{ @@ -482,7 +483,7 @@ func mapFilterOutputDims(dims []sharedModel.FilterDimension, queryStrValues []st isAreaType = true } pDim := sharedModel.Dimension{} - pDim.Title = dim.Label + pDim.Title = cleanDimensionLabel(dim.Label) pDim.ID = dim.ID pDim.Name = dim.Name pDim.IsAreaType = isAreaType @@ -535,6 +536,13 @@ func generateTruncatePath(path, dimID string, q url.Values) string { return truncatePath } +// cleanDimensionlabel is a helper function that parses dimension labels from cantabular into display text +func cleanDimensionLabel(label string) string { + matcher := regexp.MustCompile(`(\(\d+ ((C|c)ategories|(C|c)ategory)\))`) + result := matcher.ReplaceAllString(label, "") + return strings.TrimSpace(result) +} + func getDataLayerJavaScript(analytics map[string]string) template.JS { jsonStr, _ := json.Marshal(analytics) return template.JS(`dataLayer.push(` + string(jsonStr) + `);`) diff --git a/mapper/census_test.go b/mapper/census_test.go index 492ce61..41fbcf9 100644 --- a/mapper/census_test.go +++ b/mapper/census_test.go @@ -214,6 +214,41 @@ func TestCreateCensusDatasetLandingPage(t *testing.T) { So(page.ShowCensusBranding, ShouldBeTrue) }) + Convey("Census dataset landing page formats dimension labels", t, func() { + withDimensionLabelsDetails := dataset.Version{ + ReleaseDate: "01-01-2021", + Downloads: map[string]dataset.Download{}, + Edition: "2021", + Version: 1, + Links: dataset.Links{}, + Dimensions: []dataset.VersionDimension{ + { + Description: "A description on one line", + Name: "dim_1", + ID: "dim_1", + Label: "Label 1 (1 Category)", + IsAreaType: helpers.ToBoolPtr(true), + QualityStatementText: "This is a quality notice statement", + QualityStatementURL: "#", + }, + { + Description: "A description on one line \n Then a line break", + Name: "dim_2", + Label: "Label 2 (100 categories)", + ID: "dim_2", + QualityStatementText: "This is another quality notice statement", + QualityStatementURL: "#", + }, + }, + } + + page := CreateCensusDatasetLandingPage(true, context.Background(), req, pageModel, datasetModel, withDimensionLabelsDetails, datasetOptions, "", false, []dataset.Version{versionOneDetails}, 1, "/a/version/1", "", []string{}, 50, false, false, false, map[string]filter.Download{}, []sharedModel.FilterDimension{}, serviceMessage, emergencyBanner) + + So(page.Collapsible.CollapsibleItems[2].Subheading, ShouldEqual, "Label 1") + So(page.Collapsible.CollapsibleItems[3].Subheading, ShouldEqual, "Label 2") + So(page.DatasetLandingPage.Dimensions[0].Title, ShouldEqual, "Label 1") + }) + Convey("Census dataset landing page maps correctly with filter output", t, func() { datasetModel.Type = "flexible" page := CreateCensusDatasetLandingPage(true, context.Background(), req, pageModel, datasetModel, versionOneDetails, datasetOptions, "", false, []dataset.Version{versionOneDetails}, 1, "/a/version/1", "", []string{}, 50, false, true, true, filterOutput, fDims, serviceMessage, emergencyBanner) @@ -1367,3 +1402,12 @@ func TestCreateCensusDatasetLandingPage(t *testing.T) { }) } + +func TestCleanDimensionsLabel(t *testing.T) { + Convey("Removes categories count from label - case insensitive", t, func() { + So(cleanDimensionLabel("Example (100 categories)"), ShouldEqual, "Example") + So(cleanDimensionLabel("Example (7 Categories)"), ShouldEqual, "Example") + So(cleanDimensionLabel("Example (1 category)"), ShouldEqual, "Example") + So(cleanDimensionLabel("Example (1 Category)"), ShouldEqual, "Example") + }) +} From bc61f7f8491294f2961e452cb282869c8e502e0f Mon Sep 17 00:00:00 2001 From: Tom Ridd Date: Tue, 10 Jan 2023 10:02:38 +0000 Subject: [PATCH 2/4] test clean dimension label functionality on filter outputs page --- mapper/census_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/mapper/census_test.go b/mapper/census_test.go index 41fbcf9..5e18f87 100644 --- a/mapper/census_test.go +++ b/mapper/census_test.go @@ -284,6 +284,24 @@ func TestCreateCensusDatasetLandingPage(t *testing.T) { So(page.SearchNoIndexEnabled, ShouldBeTrue) }) + Convey("Census dataset landing page formats dimension labels with filter output", t, func() { + datasetModel.Type = "flexible" + fDimsWithCounts := []sharedModel.FilterDimension{ + { + ModelDimension: filter.ModelDimension{ + Label: "Label 1 (100 categories)", + Options: []string{"An option", "and another"}, + IsAreaType: helpers.ToBoolPtr(true), + Name: "Geography", + }, + OptionsCount: 2, + }, + } + page := CreateCensusDatasetLandingPage(true, context.Background(), req, pageModel, datasetModel, versionOneDetails, datasetOptions, "", false, []dataset.Version{versionOneDetails}, 1, "/a/version/1", "", []string{}, 50, false, true, true, filterOutput, fDimsWithCounts, serviceMessage, emergencyBanner) + + So(page.DatasetLandingPage.Dimensions[0].Title, ShouldEqual, "Label 1") + }) + Convey("Release date and hasOtherVersions is mapped correctly when v2 of Census DLP dataset is loaded", t, func() { req := httptest.NewRequest("", "/datasets/cantabular-1/editions/2021/versions/2", nil) page := CreateCensusDatasetLandingPage(true, context.Background(), req, pageModel, datasetModel, versionTwoDetails, datasetOptions, versionOneDetails.ReleaseDate, true, []dataset.Version{versionOneDetails, versionTwoDetails}, 2, "/a/version/123", "", []string{}, 50, false, false, false, map[string]filter.Download{}, []sharedModel.FilterDimension{}, serviceMessage, emergencyBanner) From c25c398f7f09ee2602a4a5975ddd3f434c23f306 Mon Sep 17 00:00:00 2001 From: Tom Ridd Date: Tue, 10 Jan 2023 16:30:12 +0000 Subject: [PATCH 3/4] additional test coverage --- mapper/census_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mapper/census_test.go b/mapper/census_test.go index 5e18f87..94037d1 100644 --- a/mapper/census_test.go +++ b/mapper/census_test.go @@ -1427,5 +1427,8 @@ func TestCleanDimensionsLabel(t *testing.T) { So(cleanDimensionLabel("Example (7 Categories)"), ShouldEqual, "Example") So(cleanDimensionLabel("Example (1 category)"), ShouldEqual, "Example") So(cleanDimensionLabel("Example (1 Category)"), ShouldEqual, "Example") + So(cleanDimensionLabel(""), ShouldEqual, "") + So(cleanDimensionLabel("Example 1 category"), ShouldEqual, "Example 1 category") + So(cleanDimensionLabel("Example (something in brackets) (1 Category)"), ShouldEqual, "Example (something in brackets)") }) } From 2a01f0070be3140ebdf566b15d54d666a0721d7d Mon Sep 17 00:00:00 2001 From: Nick Lupton <19624419+mr-nick17@users.noreply.github.com> Date: Thu, 12 Jan 2023 14:21:16 +0000 Subject: [PATCH 4/4] 5486 ordering dimensions within collapsible --- mapper/census.go | 46 ++++++++++++++++++++++++++++++------------- mapper/census_test.go | 8 ++++---- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/mapper/census.go b/mapper/census.go index 0658b72..22d75c9 100644 --- a/mapper/census.go +++ b/mapper/census.go @@ -391,26 +391,44 @@ func orderDownloads(downloads []sharedModel.Download) []sharedModel.Download { } func populateCollapsible(Dimensions []dataset.VersionDimension, isFilterOutput bool) []coreModel.CollapsibleItem { + // TODO: This helper func will be re-written when filter output mapping work is done var collapsibleContentItems []coreModel.CollapsibleItem - collapsibleContentItems = append(collapsibleContentItems, coreModel.CollapsibleItem{ - Subheading: AreaType, - SafeHTML: coreModel.Localisation{ - LocaleKey: "VariableInfoAreaType", - Plural: 1, + collapsibleContentItems = append(collapsibleContentItems, []coreModel.CollapsibleItem{ + { + Subheading: AreaType, + SafeHTML: coreModel.Localisation{ + LocaleKey: "VariableInfoAreaType", + Plural: 1, + }, }, - }) - collapsibleContentItems = append(collapsibleContentItems, coreModel.CollapsibleItem{ - Subheading: Coverage, - SafeHTML: coreModel.Localisation{ - LocaleKey: "VariableInfoCoverage", - Plural: 1, + { + Subheading: Coverage, + SafeHTML: coreModel.Localisation{ + LocaleKey: "VariableInfoCoverage", + Plural: 1, + }, }, - }) + }...) // TODO: Temporarily removing mapping on filter output pages until API is updated if !isFilterOutput { for _, dims := range Dimensions { - if dims.Description != "" { + if helpers.IsBoolPtr(dims.IsAreaType) && dims.Description != "" { + collapsibleContentItems = append(collapsibleContentItems[:1], []coreModel.CollapsibleItem{ + { + Subheading: cleanDimensionLabel(dims.Label), + Content: strings.Split(dims.Description, "\n"), + }, + { + Subheading: Coverage, + SafeHTML: coreModel.Localisation{ + LocaleKey: "VariableInfoCoverage", + Plural: 1, + }, + }, + }...) + } + if !helpers.IsBoolPtr(dims.IsAreaType) && dims.Description != "" { var collapsibleContent coreModel.CollapsibleItem collapsibleContent.Subheading = cleanDimensionLabel(dims.Label) collapsibleContent.Content = strings.Split(dims.Description, "\n") @@ -536,7 +554,7 @@ func generateTruncatePath(path, dimID string, q url.Values) string { return truncatePath } -// cleanDimensionlabel is a helper function that parses dimension labels from cantabular into display text +// cleanDimensionLabel is a helper function that parses dimension labels from cantabular into display text func cleanDimensionLabel(label string) string { matcher := regexp.MustCompile(`(\(\d+ ((C|c)ategories|(C|c)ategory)\))`) result := matcher.ReplaceAllString(label, "") diff --git a/mapper/census_test.go b/mapper/census_test.go index 94037d1..3aa21da 100644 --- a/mapper/census_test.go +++ b/mapper/census_test.go @@ -189,9 +189,9 @@ func TestCreateCensusDatasetLandingPage(t *testing.T) { So(page.HasContactDetails, ShouldBeTrue) So(page.DatasetLandingPage.LatestVersionURL, ShouldBeBlank) So(page.Collapsible.CollapsibleItems[0].Subheading, ShouldEqual, "Area type") - So(page.Collapsible.CollapsibleItems[1].Subheading, ShouldEqual, "Coverage") - So(page.Collapsible.CollapsibleItems[2].Subheading, ShouldEqual, versionOneDetails.Dimensions[0].Label) - So(page.Collapsible.CollapsibleItems[2].Content[0], ShouldEqual, versionOneDetails.Dimensions[0].Description) + So(page.Collapsible.CollapsibleItems[1].Subheading, ShouldEqual, versionOneDetails.Dimensions[0].Label) + So(page.Collapsible.CollapsibleItems[1].Content[0], ShouldEqual, versionOneDetails.Dimensions[0].Description) + So(page.Collapsible.CollapsibleItems[2].Subheading, ShouldEqual, "Coverage") So(page.Collapsible.CollapsibleItems[3].Subheading, ShouldEqual, versionOneDetails.Dimensions[1].Label) So(page.Collapsible.CollapsibleItems[3].Content, ShouldResemble, strings.Split(versionOneDetails.Dimensions[1].Description, "\n")) So(page.Collapsible.CollapsibleItems, ShouldHaveLength, 4) @@ -244,7 +244,7 @@ func TestCreateCensusDatasetLandingPage(t *testing.T) { page := CreateCensusDatasetLandingPage(true, context.Background(), req, pageModel, datasetModel, withDimensionLabelsDetails, datasetOptions, "", false, []dataset.Version{versionOneDetails}, 1, "/a/version/1", "", []string{}, 50, false, false, false, map[string]filter.Download{}, []sharedModel.FilterDimension{}, serviceMessage, emergencyBanner) - So(page.Collapsible.CollapsibleItems[2].Subheading, ShouldEqual, "Label 1") + So(page.Collapsible.CollapsibleItems[1].Subheading, ShouldEqual, "Label 1") So(page.Collapsible.CollapsibleItems[3].Subheading, ShouldEqual, "Label 2") So(page.DatasetLandingPage.Dimensions[0].Title, ShouldEqual, "Label 1") })