Skip to content

Commit

Permalink
Merge pull request #60 from best-practice-and-impact/frequency-table-…
Browse files Browse the repository at this point in the history
…unit-tests

Frequency table unit tests
  • Loading branch information
CHCRowley authored Oct 9, 2023
2 parents 2b42d6f + 2f97ea5 commit af6fc5a
Show file tree
Hide file tree
Showing 47 changed files with 5,940 additions and 17 deletions.
3 changes: 3 additions & 0 deletions R/frequency-tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,9 @@ summarise_heard_of_RAP_by_prof <- function(data) {
1))

rownames(frequencies) <- NULL
names(frequencies$n) <- NULL

frequencies$value <- recode(frequencies$value, !!!prof_names)

return(frequencies)

Expand Down
16 changes: 16 additions & 0 deletions docs/data_collection.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/department-for-digital-culture-media-and-sport.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/department-for-education.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/department-for-international-trade.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/department-for-transport-excl-agencies-.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/department-for-work-and-pensions.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/forestry-commission.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/government-actuary-s-department.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/hm-revenue-and-customs.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/ministry-of-defence.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/ministry-of-justice-excl-agencies-.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/national-records-of-scotland.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/nhs.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/northern-ireland-statistics-and-research-agency.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/office-for-national-statistics.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/public-health-scotland.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/scottish-government-excl-agencies-.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/uk-health-security-agency.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/departments/welsh-government.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/professions/data-scientists.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/professions/digital-and-data.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/professions/government-actuarys-department.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/professions/government-economic-service.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/professions/government-geography.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/professions/government-operational-research.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/professions/government-social-research.html

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions docs/professions/government-statician-group.html

Large diffs are not rendered by default.

144 changes: 144 additions & 0 deletions docs/summary.html

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions tests/testthat/test-calculate_freqs.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ expected <- data.frame(name = rep(c("Question 1",
levels = c("test1", "test2", "test3")),
n = c(1, 0, 0, 0, 1, 0, 0.5, 0, 0.5))

test_that("create_tidy_freq_table validation works", {

expect_error(calculate_freqs(data = dummy_data,
questions = questions,
levels = levels),
"Missing input: labels needed for mutli-column frequencies.")

})

test_that("create_tidy_freq_table missing data is handled correctly", {

got <- calculate_freqs(data = dummy_data,
Expand Down Expand Up @@ -52,3 +61,36 @@ test_that("create_tidy_freq_table count output is as expected", {
expect_equal(got, expected)

})

test_that("create_tidy_freq_table single question proportion output is as expected", {

questions <- "Q1"

got <- calculate_freqs(data = dummy_data,
questions = questions,
levels = levels)

expected <- data.frame(value = factor(c("test1", "test2", "test3"),
levels = c("test1", "test2", "test3")),
n = c(1, 0, 0))

expect_equal(got, expected)

})

test_that("create_tidy_freq_table single question count output is as expected", {

questions <- "Q1"

got <- calculate_freqs(data = dummy_data,
questions = questions,
levels = levels,
prop = FALSE)

expected <- data.frame(value = factor(c("test1", "test2", "test3"),
levels = c("test1", "test2", "test3")),
n = c(1, 0, 0))

expect_equal(got, expected)

})
27 changes: 26 additions & 1 deletion tests/testthat/test-freq_subplots.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ got <- freq_subplots(dummy_data, xlab = "x", ylab = "y",

for(i in 1:length(unique(dummy_data[[2]]))){
j = 2*i - 1
testthat::test_that("expected outputs achieved",
testthat::test_that("expected outputs for vertical chart achieved",
{
# x values
testthat::expect_equal(c(got$x$data[[j]]$x), factor(levels1, levels = levels1))
Expand All @@ -43,3 +43,28 @@ for(i in 1:length(unique(dummy_data[[2]]))){
testthat::expect_equal(got$x$data[[j]]$title, factor(i, levels = c(1, 2, 3)))
})
}

got <- freq_subplots(dummy_data, xlab = "x", ylab = "y",
height = 500, width = 300, nrows = 3,
y_margin = .3, x_margin = .3, orientation = "h")

for(i in 1:length(unique(dummy_data[[2]]))){
j = 2*i - 1
testthat::test_that("expected outputs for horizontal chart achieved",
{
# x values
testthat::expect_equal(c(got$x$data[[j]]$y), factor(levels1, levels = rev(levels1)))

# y values
testthat::expect_equal(c(got$x$data[[j]]$x), dummy_data[dummy_data$Q2 == i, "n"])

# Bar colors
testthat::expect_equal(got$x$data[[j]]$marker$color, "#004556")

# Plot orientation
testthat::expect_equal(got$x$data[[j]]$orientation, "h")

# Title
testthat::expect_equal(got$x$data[[j]]$title, factor(i, levels = c(1, 2, 3)))
})
}
46 changes: 43 additions & 3 deletions tests/testthat/test-plot_freqs.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ testthat::test_that("validity checks work",
testthat::expect_error(plot_freqs(dummy_data, font_size = "x"), "Unexpected input - font_size is not numeric.")
})

got <- plot_freqs(dummy_data, n = 100, xlab = "x", ylab = "y", break_q_names_col = TRUE)

testthat::test_that("expected outputs achieved",
testthat::test_that("expected outputs for vertical chart achieved",
{

got <- plot_freqs(dummy_data, n = 100, xlab = "x", ylab = "y",
break_q_names_col = TRUE,
type = "bar",
orientation = "v")

# x and y values
testthat::expect_equal(c(got$x$attrs[[1]]$x), factor(c("This is test<br>number one", "This is test<br>2", "test3"),
levels = c("This is test<br>number one", "This is test<br>2", "test3")))
Expand Down Expand Up @@ -49,3 +53,39 @@ testthat::test_that("expected outputs achieved",
testthat::expect_equal(got$x$layoutAttrs[[1]]$annotations$font$size, 12)

})

testthat::test_that("expected outputs for horizontal chart achieved",
{

got <- plot_freqs(dummy_data, n = 100, xlab = "x", ylab = "y",
break_q_names_col = TRUE,
type = "line",
orientation = "h")

# x and y values
testthat::expect_equal(c(got$x$attrs[[1]]$y), factor(c("This is test<br>number one", "This is test<br>2", "test3"),
levels = rev(c("This is test<br>number one", "This is test<br>2", "test3"))))
testthat::expect_equal(c(got$x$attrs[[1]]$x), c(0.5, 0.2, 0.8))

# Bar colors
testthat::expect_equal(got$x$attrs[[1]]$marker$color, "#004556")

# Plot orientation
testthat::expect_equal(got$x$attrs[[1]]$orientation, "h")

# Sample size
testthat::expect_equal(got$x$layoutAttrs[[1]]$annotations$text, "Sample size = 100")

# Axis labels
testthat::expect_equal(got$x$layoutAttrs[[1]]$xaxis$title, "y")
testthat::expect_equal(got$x$layoutAttrs[[2]]$annotations$text, "x")

# Font sizes
testthat::expect_equal(got$x$layoutAttrs[[1]]$xaxis$titlefont$size, 14.4)
testthat::expect_equal(got$x$layoutAttrs[[1]]$xaxis$tickfont$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$yaxis$titlefont$size, 14.4)
testthat::expect_equal(got$x$layoutAttrs[[1]]$yaxis$tickfont$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$hoverlabel$font$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$annotations$font$size, 12)

})
48 changes: 42 additions & 6 deletions tests/testthat/test-plot_grouped.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
levels1 <- c("test1", "test2", "test3", "test4", "test5")
levels1 <- c("This is test number one", "This is test 2", "test3", "test4", "test5")

dummy_data <- data.frame(Q1 = factor(levels1,
levels = levels1),
Expand All @@ -10,20 +10,24 @@ testthat::test_that("validity checks work",
{
testthat::expect_error(plot_grouped(as.list(dummy_data)), "Unexpected input - data is not a data.frame.")
testthat::expect_error(plot_grouped(dplyr::mutate(dummy_data, Q3 = Q1)), "Unexpected input - data does not contain 3 columns.")
testthat::expect_error(plot_grouped(dplyr::mutate(dummy_data, n = as.character(n))), "Unexpected input - data column 3 is not numeric.")
testthat::expect_error(plot_grouped(dummy_data, xlab = 1), "Unexpected input - labels should be single character strings.")
testthat::expect_error(plot_grouped(dummy_data, ylab = 1), "Unexpected input - labels should be single character strings.")
testthat::expect_error(plot_grouped(dummy_data, xlab = c("1", "2")), "Unexpected input - labels should be single character strings.")
testthat::expect_error(plot_grouped(dummy_data, ylab = c("1", "2")), "Unexpected input - labels should be single character strings.")
testthat::expect_error(plot_grouped(dummy_data, font_size = "x"), "Unexpected input - font_size is not numeric.")
})

got <- plot_grouped(dummy_data, n = 100, xlab = "x", ylab = "y")

testthat::test_that("expected outputs achieved",
testthat::test_that("expected outputs for vertical chart achieved",
{

got <- plot_grouped(dummy_data, n = 100, xlab = "x", ylab = "y",
break_q_names_col = TRUE,
orientation = "v")

# x and y values
testthat::expect_equal(c(got$x$attrs[[1]]$x), factor(levels1,
levels = levels1))
testthat::expect_equal(c(got$x$attrs[[1]]$x), factor(c("This is test<br>number one", "This is test<br>2", "test3", "test4", "test5"),
levels = c("This is test<br>number one", "This is test<br>2", "test3", "test4", "test5")))
testthat::expect_equal(c(got$x$attrs[[1]]$y), c(0.2, 0.5, 0.3, 0.8, 0.6))

# Bar colors
Expand All @@ -45,3 +49,35 @@ testthat::test_that("expected outputs achieved",
testthat::expect_equal(got$x$layoutAttrs[[1]]$annotations$font$size, 12)

})

testthat::test_that("expected outputs for horizontal chart achieved",
{

got <- plot_grouped(dummy_data, n = 100, xlab = "x", ylab = "y",
break_q_names_col = TRUE,
orientation = "h")

# x and y values
testthat::expect_equal(c(got$x$attrs[[1]]$y), factor(rev(c("This is test<br>number one", "This is test<br>2", "test3", "test4", "test5")),
levels = rev(c("This is test<br>number one", "This is test<br>2", "test3", "test4", "test5"))))
testthat::expect_equal(c(got$x$attrs[[1]]$x), rev(c(0.2, 0.5, 0.3, 0.8, 0.6)))

# Bar colors
testthat::expect_equal(got$x$attrs[[1]]$marker$color, rev(c("#FF6900", "#FF6900", "#FF6900", "#004556", "#004556")))

# Sample size
testthat::expect_equal(got$x$layoutAttrs[[1]]$annotations$text, "Sample size = 100")

# Axis labels
testthat::expect_equal(got$x$layoutAttrs[[1]]$xaxis$title, "y")
testthat::expect_equal(got$x$layoutAttrs[[2]]$annotations$text, "x")

# Font sizes
testthat::expect_equal(got$x$layoutAttrs[[1]]$xaxis$titlefont$size, 14.4)
testthat::expect_equal(got$x$layoutAttrs[[1]]$xaxis$tickfont$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$yaxis$titlefont$size, 14.4)
testthat::expect_equal(got$x$layoutAttrs[[1]]$yaxis$tickfont$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$hoverlabel$font$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$annotations$font$size, 12)

})
56 changes: 49 additions & 7 deletions tests/testthat/test-plot_stacked.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

dummy_data <- data.frame(Q1 = rep(c("test1",
"test2",
dummy_data <- data.frame(Q1 = rep(c("This is test number one",
"This is test 2",
"test3"), each = 3),
Q2 = factor(rep(c(1, 2, 3), 3),
levels = c(1, 2, 3)),
Expand All @@ -17,13 +17,16 @@ testthat::test_that("validity checks work",
testthat::expect_error(plot_stacked(dummy_data, font_size = "x"), "Unexpected input - font_size is not numeric.")
})

got <- plot_stacked(dummy_data, n = 100, xlab = "x", ylab = "y")

testthat::test_that("expected outputs achieved",
testthat::test_that("expected outputs for horizontal chart achieved",
{

got <- plot_stacked(dummy_data, n = 100, xlab = "x", ylab = "y",
orientation = "h",
break_q_names_col = TRUE,)

# x and y values
testthat::expect_equal(c(got$x$attrs[[1]]$y), factor(rep(c("test1", "test2", "test3"), each = 3),
levels = c("test3", "test2", "test1")))
testthat::expect_equal(c(got$x$attrs[[1]]$y), factor(rep(c("This is test<br>number one", "This is test<br>2", "test3"), each = 3),
levels = c("test3", "This is test<br>2", "This is test<br>number one")))
testthat::expect_equal(c(got$x$attrs[[1]]$x), c(0.1, 0.3, 0.6, 0.5, 0.25, 0.25, 0.33, 0.33, 0.34))

# Bar colors
Expand All @@ -49,3 +52,42 @@ testthat::test_that("expected outputs achieved",
testthat::expect_equal(got$x$layoutAttrs[[1]]$annotations$font$size, 12)

})



testthat::test_that("expected outputs for vertical chart achieved",
{

got <- plot_stacked(dummy_data, n = 100, xlab = "x", ylab = "y",
orientation = "v",
type = "line",
break_q_names_col = TRUE,)

# x and y values
testthat::expect_equal(c(got$x$attrs[[1]]$x), factor(rep(c("This is test<br>number one", "This is test<br>2", "test3"), each = 3),
levels = rev(c("test3", "This is test<br>2", "This is test<br>number one"))))
testthat::expect_equal(c(got$x$attrs[[1]]$y), c(0.1, 0.3, 0.6, 0.5, 0.25, 0.25, 0.33, 0.33, 0.34))

# Bar colors
testthat::expect_equal(got$x$attrs[[1]]$marker$color, c("#004556", "#004556", "#004556", "#AFAFAF", "#AFAFAF", "#AFAFAF", "#FF6900", "#FF6900", "#FF6900"))

# Plot orientation
testthat::expect_equal(got$x$attrs[[1]]$orientation, "v")

# Sample size
testthat::expect_equal(got$x$layoutAttrs[[1]]$annotations$text, "Sample size = 100")

# Axis labels
testthat::expect_equal(got$x$layoutAttrs[[1]]$xaxis$title, "x")
testthat::expect_equal(got$x$layoutAttrs[[2]]$annotations$text, "y")

# Font sizes
testthat::expect_equal(got$x$layoutAttrs[[1]]$legend$font$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$xaxis$titlefont$size, 14.4)
testthat::expect_equal(got$x$layoutAttrs[[1]]$xaxis$tickfont$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$yaxis$titlefont$size, 14.4)
testthat::expect_equal(got$x$layoutAttrs[[1]]$yaxis$tickfont$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$hoverlabel$font$size, 12)
testthat::expect_equal(got$x$layoutAttrs[[1]]$annotations$font$size, 12)

})
29 changes: 29 additions & 0 deletions tests/testthat/test-rap_awareness_over_time.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

dummy_data <- data.frame(year = rep(c(2020, 2021, 2022), each=2),
heard_of_RAP = rep(c("Yes", "No"), times=3))

test_that("summarise_rap_awareness_over_time missing data is handled correctly", {

dummy_data[1,2] <- NA

got <- summarise_rap_awareness_over_time(dummy_data)

expect_false(any(is.na.data.frame(got)))

})

test_that("summarise_rap_awareness_over_time output is as expected", {

got <- summarise_rap_awareness_over_time(dummy_data)

expected <- data.frame(Var1 = factor(c("Yes", "Yes", "Yes"),
levels = c("No", "Yes")),
Var2 = factor(c("2020", "2021", "2022"),
levels = c("2020", "2021", "2022")),
Freq = c(1, 1, 1),
n = c(2, 2, 2)) %>%
get_ci(freq_col = 3, n_col = 4)

expect_equal(got, expected)

})
51 changes: 51 additions & 0 deletions tests/testthat/test-summarise_cap_change_by_CS_grade.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
dummy_data <- data.frame(CS_grade = c(NA,
rep("Higher Executive Officer (or equivalent)", 10),
rep("Senior Executive Officer (or equivalent)", 10),
rep("Grade 7 (or equivalent)", 5),
rep("Grade 6 (or equivalent)", 5)),
coding_ability_change = c(NA,
rep(c("Significantly worse",
"Slightly worse",
"No change",
"Slightly better",
"Significantly better"),
times = 6)))

test_that("summarise_cap_change_by_CS_grade missing data is handled correctly", {

got <- summarise_cap_change_by_CS_grade(dummy_data)

expect_false(any(is.na.data.frame(got)))

})

test_that("summarise_cap_change_by_CS_grade output is as expected", {

got <- summarise_cap_change_by_CS_grade(dummy_data)

expected <- data.frame(CS_grade = factor(rep(c("Higher Executive Officer (or equivalent)",
"Senior Executive Officer (or equivalent)",
"Grade 6 and 7"),
each = 5),
levels = c("Higher Executive Officer (or equivalent)",
"Senior Executive Officer (or equivalent)",
"Grade 6 and 7")),
coding_ability_change = factor(rep(c("Significantly worse",
"Slightly worse",
"No change",
"Slightly better",
"Significantly better"), 3),
levels = c("Significantly worse",
"Slightly worse",
"No change",
"Slightly better",
"Significantly better")),
n = c(0.20, 0.20, 0.20, 0.20, 0.20,
0.20, 0.20, 0.20, 0.20, 0.20,
0.20, 0.20, 0.20, 0.20, 0.20))


expect_equal(got, expected)


})
Loading

0 comments on commit af6fc5a

Please sign in to comment.