Skip to content

Commit

Permalink
Merge branch 'release/1.48.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-nick17 committed Jan 18, 2023
2 parents 221a1b7 + e76adb9 commit 2d2a48f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 19 deletions.
58 changes: 42 additions & 16 deletions mapper/census.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"html/template"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -390,28 +391,46 @@ 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 = dims.Label
collapsibleContent.Subheading = cleanDimensionLabel(dims.Label)
collapsibleContent.Content = strings.Split(dims.Description, "\n")
collapsibleContentItems = append(collapsibleContentItems, collapsibleContent)
}
Expand All @@ -433,7 +452,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{
Expand Down Expand Up @@ -482,7 +501,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
Expand Down Expand Up @@ -535,6 +554,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) + `);`)
Expand Down
71 changes: 68 additions & 3 deletions mapper/census_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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[1].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)
Expand Down Expand Up @@ -249,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)
Expand Down Expand Up @@ -1367,3 +1420,15 @@ 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")
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)")
})
}

0 comments on commit 2d2a48f

Please sign in to comment.