Skip to content

Commit

Permalink
tidy code
Browse files Browse the repository at this point in the history
  • Loading branch information
rmbielby committed Nov 1, 2023
1 parent eb2a4e0 commit e0f8113
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
44 changes: 22 additions & 22 deletions R/mainTests.r
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ la_col_present <- function(data) {
col_completed <- function(x, row, level_rows) {
y <- x + 1
col <- paste(row[y])

if (any(!is.na(level_rows[[col]] %>% .[. != ""]))) {
return(col)
}
Expand All @@ -979,8 +979,8 @@ overcompleted_cols <- function(data, meta) {

overcomplete_regional_cols <- function(matrixRow) {
# Start by filtering the data down to remove the geographic level being tested and any lower levels we don't care about
levels_incompatible_with_region <- c("National", "Local skills improvement plan area")
levels_incompatible_with_region <- c("National", "Local skills improvement plan area")

level_rows <- data %>%
filter(geographic_level %in% levels_incompatible_with_region)

Expand All @@ -990,7 +990,7 @@ overcompleted_cols <- function(data, meta) {

# Apply over every column in the matrixRow (geographic_level) being tested

pre_output <- sapply(c(1:length(cols)), col_completed, row=matrixRow, level_rows=level_rows)
pre_output <- sapply(c(1:length(cols)), col_completed, row = matrixRow, level_rows = level_rows)

return(pre_output)
}
Expand All @@ -1003,7 +1003,7 @@ overcompleted_cols <- function(data, meta) {

# Start by filtering the data down to remove the geographic level being tested, lad rows and any lower levels we don't care about
levels_compatible_with_la <- c("Local authority", "Local authority district", "School", "Provider", "Institution", "Planning area")

level_rows <- data %>%
filter(!geographic_level %in% levels_compatible_with_la)

Expand All @@ -1013,31 +1013,31 @@ overcompleted_cols <- function(data, meta) {

# Apply over every column in the matrixRow (geographic_level) being tested

pre_output <- sapply(c(1:length(cols)), col_completed, row=matrixRow, level_rows=level_rows)
pre_output <- sapply(c(1:length(cols)), col_completed, row = matrixRow, level_rows = level_rows)

return(pre_output)
}

# ----------------------------------------------------------------------------------------------------------------------------------
# checking whether LSIP columns are completed for national, regional or mid-geography rows (ignoring LAD)

overcomplete_lsip_cols <- function(matrixRow) {
# This is a test that could benefit from more detail, and maybe a table in the error feedback

# Start by filtering the data down to remove the geographic level being tested, lad rows and any lower levels we don't care about
levels_compatible_with_lsip <- c("Local skills improvement plan area", "Local authority district", "School", "Provider", "Institution", "Planning area")

level_rows <- data %>%
filter(!geographic_level %in% levels_compatible_with_lsip)

# Extract the columns for the geographic level that is being tested

cols <- matrixRow[2:4] %>% .[!is.na(.)]

# Apply over every column in the matrixRow (geographic_level) being tested
pre_output <- sapply(c(1:length(cols)), col_completed, row=matrixRow, level_rows=level_rows)

pre_output <- sapply(c(1:length(cols)), col_completed, row = matrixRow, level_rows = level_rows)

return(pre_output)
}
# ----------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -1056,7 +1056,7 @@ overcompleted_cols <- function(data, meta) {

# Apply over every column in the matrixRow (geographic_level) being tested

pre_output <- sapply(c(1:length(cols)), col_completed, row=matrixRow, level_rows=level_rows)
pre_output <- sapply(c(1:length(cols)), col_completed, row = matrixRow, level_rows = level_rows)

return(pre_output)
}
Expand Down Expand Up @@ -1096,9 +1096,9 @@ overcompleted_cols <- function(data, meta) {
# Apply over every column in the matrixRow (geographic_level) being tested

if (sch_prov_only_filter == TRUE) {
pre_output <- sapply(c(1, 3), col_completed, row=matrixRow, level_rows=level_rows)
pre_output <- sapply(c(1, 3), col_completed, row = matrixRow, level_rows = level_rows)
} else {
pre_output <- sapply(c(1:length(cols)), col_completed, row=matrixRow, level_rows=level_rows)
pre_output <- sapply(c(1:length(cols)), col_completed, row = matrixRow, level_rows = level_rows)
}

return(pre_output)
Expand All @@ -1110,7 +1110,7 @@ overcompleted_cols <- function(data, meta) {
regional_matrix <- matrix(geography_matrix[2, ], nrow = 1)
la_matrix <- matrix(geography_matrix[3, ], nrow = 1)
lsip_matrix <- matrix(geography_matrix[7, ], nrow = 1)

overcomplete_geographies <- c(
unlist(apply(regional_matrix, 1, overcomplete_regional_cols)),
unlist(apply(la_matrix, 1, overcomplete_la_cols)),
Expand Down Expand Up @@ -1419,7 +1419,7 @@ la_combinations <- function(data) {

lsip_combinations <- function(data) {
level_description <- "Local skills improvement plan area"
level_line <- geography_dataframe %>% filter(geographic_level == level_description)
level_line <- geography_dataframe %>% filter(geographic_level == level_description)
if (!level_line$code_field %in% names(data)) {
output <- list(
"message" = paste(level_line$code_field, "columns are not present in this data file."),
Expand Down Expand Up @@ -1453,12 +1453,12 @@ lsip_combinations <- function(data) {
} else {
if (length(invalid_values) == 1) {
output <- list(
"message" = paste0("The following ", level_line$code_field," and ", level_line$name_field," combination is invalid for rows within the '", level_description, "' geographic_level: '", paste0(invalid_values), "'. <br> - We do not expect any combinations outside of the <a href='https://github.com/dfe-analytical-services/dfe-published-data-qa/blob/master/data/lsips.csv' target='_blank'>standard geographies lookup</a> (case sensitive), please check your name and code combinations against this lookup."),
"message" = paste0("The following ", level_line$code_field, " and ", level_line$name_field, " combination is invalid for rows within the '", level_description, "' geographic_level: '", paste0(invalid_values), "'. <br> - We do not expect any combinations outside of the <a href='https://github.com/dfe-analytical-services/dfe-published-data-qa/blob/master/data/lsips.csv' target='_blank'>standard geographies lookup</a> (case sensitive), please check your name and code combinations against this lookup."),
"result" = "FAIL"
)
} else {
output <- list(
"message" = paste0("The following ", level_line$code_field," / ", level_line$name_field," combinations are invalid for rows within the '", level_description, "' geographic_level: '", paste0(invalid_values, collapse = "', '"), "'. <br> - We do not expect any combinations outside of the <a href='https://github.com/dfe-analytical-services/dfe-published-data-qa/blob/master/data/lsips.csv' target='_blank'>standard geographies lookup</a> (case sensitive), please check your name and code combinations against this lookup."),
"message" = paste0("The following ", level_line$code_field, " / ", level_line$name_field, " combinations are invalid for rows within the '", level_description, "' geographic_level: '", paste0(invalid_values, collapse = "', '"), "'. <br> - We do not expect any combinations outside of the <a href='https://github.com/dfe-analytical-services/dfe-published-data-qa/blob/master/data/lsips.csv' target='_blank'>standard geographies lookup</a> (case sensitive), please check your name and code combinations against this lookup."),
"result" = "FAIL"
)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-screenFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ context("Quoted blanks")

test_that("QuotedBlanks-overcompleted_cols", {
screeningOutput <- testOther("../../tests/testthat/otherData/quoted_blank_geographies.csv")

expect_equal(screeningOutput$results %>% filter(test == "overcompleted_cols") %>% pull(result) %>% unlist(use.names = FALSE), "PASS")
})

Expand Down

0 comments on commit e0f8113

Please sign in to comment.