Skip to content

Commit

Permalink
Merge branch 'filter-exclude'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukestringer90 committed Apr 25, 2021
2 parents a7b0ad5 + 3375377 commit 57aabed
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Sources/PeakPivot/BuildPivot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions Sources/PeakPivot/BuildPivotFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
7 changes: 4 additions & 3 deletions Tests/PeakPivotTests/PivotBuilderIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand All @@ -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()

Expand Down
20 changes: 11 additions & 9 deletions Tests/PeakPivotTests/PivotBuilderMultipleFilterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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
Expand Down
16 changes: 9 additions & 7 deletions Tests/PeakPivotTests/PivotBuilderSingleFilterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand Down

0 comments on commit 57aabed

Please sign in to comment.