From 33753775052bd733b98a9cb76c99e8849b2c967c Mon Sep 17 00:00:00 2001 From: lukestringer90 Date: Fri, 19 Mar 2021 08:36:34 +0000 Subject: [PATCH] Use exclude rather than include for filters Easier to compute in client apps --- Sources/PeakPivot/BuildPivot.swift | 2 +- Sources/PeakPivot/BuildPivotFilter.swift | 8 ++++---- .../PivotBuilderIntegrationTests.swift | 7 ++++--- .../PivotBuilderMultipleFilterTests.swift | 20 ++++++++++--------- .../PivotBuilderSingleFilterTests.swift | 16 ++++++++------- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Sources/PeakPivot/BuildPivot.swift b/Sources/PeakPivot/BuildPivot.swift index 5b34dfb..63c3aba 100644 --- a/Sources/PeakPivot/BuildPivot.swift +++ b/Sources/PeakPivot/BuildPivot.swift @@ -140,7 +140,7 @@ fileprivate extension BuildPivot { if let filters = self.filters { let matchingFilters = filters.filter { filter -> Bool in - return fieldName == filter.fieldName && !filter.include.contains(fieldValue) + return fieldName == filter.fieldName && filter.exclude.contains(fieldValue) } if matchingFilters.count > 0 { diff --git a/Sources/PeakPivot/BuildPivotFilter.swift b/Sources/PeakPivot/BuildPivotFilter.swift index 3ebf1bb..4b78853 100644 --- a/Sources/PeakPivot/BuildPivotFilter.swift +++ b/Sources/PeakPivot/BuildPivotFilter.swift @@ -13,12 +13,12 @@ public struct BuildPivotFilter { /// The field name to filter on public let fieldName: FieldName - /// The values of the field name to include - public let include: [FieldValue] + /// The values of the field name to exclude + public let exclude: [FieldValue] - public init(fieldName: FieldName, include: [FieldName]) { + public init(fieldName: FieldName, exclude: [FieldName]) { self.fieldName = fieldName - self.include = include + self.exclude = exclude } } diff --git a/Tests/PeakPivotTests/PivotBuilderIntegrationTests.swift b/Tests/PeakPivotTests/PivotBuilderIntegrationTests.swift index 3ef57ea..2943e39 100644 --- a/Tests/PeakPivotTests/PivotBuilderIntegrationTests.swift +++ b/Tests/PeakPivotTests/PivotBuilderIntegrationTests.swift @@ -15,7 +15,8 @@ class PivotBuilderIntegrationTests: PivotBuilderTestsBase { builder.percentagesEnabled = false builder.table = TestData.people builder.fields = ["age_range", "gender"] - builder.filters = [BuildPivotFilter(fieldName: "age_range", include: ["40s"])] + + builder.filters = [BuildPivotFilter(fieldName: "age_range", exclude: ["20s", "30s", "50s", "60s"])] builder.percentagesEnabled = true runBuilder() @@ -35,7 +36,7 @@ class PivotBuilderIntegrationTests: PivotBuilderTestsBase { builder.percentagesEnabled = false builder.table = TestData.people builder.fields = ["age_range", "gender"] - builder.filters = [BuildPivotFilter(fieldName: "age_range", include: ["40s", "60s"])] + builder.filters = [BuildPivotFilter(fieldName: "age_range", exclude: ["20s", "30s", "50s"])] builder.sortDescriptor = .byCount(ascending: false) builder.percentagesEnabled = true @@ -61,7 +62,7 @@ class PivotBuilderIntegrationTests: PivotBuilderTestsBase { builder.percentagesEnabled = true builder.sortDescriptor = .byCount(ascending: false) builder.fields = ["age_range", "gender", "title"] - builder.filters = [BuildPivotFilter(fieldName: "gender", include: ["Female"])] + builder.filters = [BuildPivotFilter(fieldName: "gender", exclude: ["Male", Blank])] runBuilder() diff --git a/Tests/PeakPivotTests/PivotBuilderMultipleFilterTests.swift b/Tests/PeakPivotTests/PivotBuilderMultipleFilterTests.swift index 3c4a808..3cfe0ba 100644 --- a/Tests/PeakPivotTests/PivotBuilderMultipleFilterTests.swift +++ b/Tests/PeakPivotTests/PivotBuilderMultipleFilterTests.swift @@ -13,11 +13,11 @@ import XCTest class PivotBuilderMultipleFilterTests: PivotBuilderTestsBase { - func testTwoLevels_TwoFilters_TwoIncludes() { + func testTwoLevels_TwoFilters() { builder.fields = ["title", "age_range"] builder.filters = [ - BuildPivotFilter(fieldName: "title", include: ["Dr", Blank]), - BuildPivotFilter(fieldName: "age_range", include: ["40s", "60s"]), + BuildPivotFilter(fieldName: "title", exclude: ["Honorable", "Mr", "Mrs", "Rev", "Ms"]), + BuildPivotFilter(fieldName: "age_range", exclude: ["20s", "30s", "50s"]) ] runBuilder() @@ -36,10 +36,11 @@ class PivotBuilderMultipleFilterTests: PivotBuilderTestsBase { XCTAssertEqual(pivotRows, expected) } - func testTwoLevels_TwoFilters_TwoIncludes_ZerosEnabled() { + func testTwoLevels_OneFilter_ZerosEnabled() { builder.fields = ["title", "age_range"] builder.filters = [ - BuildPivotFilter(fieldName: "age_range", include: ["50s"]), + BuildPivotFilter(fieldName: "age_range", exclude: ["20s", "30s", "40s", "60s"]) + ] builder.zeroRowsEnabled = true @@ -65,11 +66,12 @@ class PivotBuilderMultipleFilterTests: PivotBuilderTestsBase { } - func testTwoLevels_TwoFilters_TwoIncludes_ZerosDisabled() { + func testTwoLevels_TwoFilters_ZerosDisabled() { builder.fields = ["title", "age_range"] builder.filters = [ - BuildPivotFilter(fieldName: "age_range", include: ["50s"]), + BuildPivotFilter(fieldName: "age_range", exclude: ["20s", "30s", "40s", "60s"]) ] + builder.zeroRowsEnabled = false runBuilder() @@ -90,10 +92,10 @@ class PivotBuilderMultipleFilterTests: PivotBuilderTestsBase { } - func testTwoLevels_TwoFilters_TwoIncludes_ZerosDisabled_PercentagesEnabled() { + func testTwoLevels_TwoFilters_ZerosDisabled_PercentagesEnabled() { builder.fields = ["title", "age_range"] builder.filters = [ - BuildPivotFilter(fieldName: "age_range", include: ["50s"]), + BuildPivotFilter(fieldName: "age_range", exclude: ["20s", "30s", "40s", "60s"]) ] builder.zeroRowsEnabled = false builder.percentagesEnabled = true diff --git a/Tests/PeakPivotTests/PivotBuilderSingleFilterTests.swift b/Tests/PeakPivotTests/PivotBuilderSingleFilterTests.swift index 943ff78..ffba75c 100644 --- a/Tests/PeakPivotTests/PivotBuilderSingleFilterTests.swift +++ b/Tests/PeakPivotTests/PivotBuilderSingleFilterTests.swift @@ -11,9 +11,9 @@ import XCTest class PivotBuilderSingleFilterTests: PivotBuilderTestsBase { - func testThreeLevels_oneIncludesAtLevelOne() { + func testThreeLevels_fourExcludesAtLevelOne() { builder.fields = ["age_range", "gender", "title"] - builder.filters = [BuildPivotFilter(fieldName: "age_range", include: ["20s"])] + builder.filters = [BuildPivotFilter(fieldName: "age_range", exclude: ["30s", "40s", "50s", "60s"])] runBuilder() @@ -34,9 +34,10 @@ class PivotBuilderSingleFilterTests: PivotBuilderTestsBase { XCTAssertEqual(pivotRows, expected) } - func testThreeLevels_twoIncludesAtLevelOne() { + func testThreeLevels_threeExcludesAtLevelOne() { builder.fields = ["age_range", "gender", "title"] - builder.filters = [BuildPivotFilter(fieldName: "age_range", include: ["20s", "50s"])] +// builder.filters = [BuildPivotFilter(fieldName: "age_range", include: ["20s", "50s"])] + builder.filters = [BuildPivotFilter(fieldName: "age_range", exclude: ["30s", "40s", "60s"])] runBuilder() @@ -70,10 +71,11 @@ class PivotBuilderSingleFilterTests: PivotBuilderTestsBase { XCTAssertEqual(pivotRows, expected) } - func testThreeLevels_threeIncludesAtLevelTwo_sortByValueAscending_noPercentages() { + func testThreeLevels_twoExcludesAtLevelTwo_sortByValueAscending_noPercentages() { builder.sortDescriptor = .byCount(ascending: true) builder.fields = ["age_range", "gender"] - builder.filters = [BuildPivotFilter(fieldName: "age_range", include: ["20s", "30s", "60s"])] +// builder.filters = [BuildPivotFilter(fieldName: "age_range", include: ["20s", "30s", "60s"])] + builder.filters = [BuildPivotFilter(fieldName: "age_range", exclude: ["40s", "50s"])] runBuilder() @@ -100,7 +102,7 @@ class PivotBuilderSingleFilterTests: PivotBuilderTestsBase { func testOneLevels_blankInclude() { builder.fields = ["gender"] - builder.filters = [BuildPivotFilter(fieldName: "gender", include: [Blank])] + builder.filters = [BuildPivotFilter(fieldName: "gender", exclude: ["Female", "Male"])] runBuilder()