Skip to content

Commit 0354534

Browse files
committed
Add tests raising test coverage to over 90%
1 parent 9b9287a commit 0354534

File tree

9 files changed

+166
-35
lines changed

9 files changed

+166
-35
lines changed

NEWS.Rmd

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ output: html_document
1414
might be a bit confusing for someone who is exploring the app and maybe doesn't have their own
1515
data yet
1616

17-
- The tests seemed a bit incomplete to me, it doesn't look like all functions are being tested,
18-
just the app function and the file format. Is this enough to test the whole code?
19-
2017
## v0.6.1
2118

19+
- Additional test raising test coverage to over 90%
2220
- Button to load sample data and get started right away without uploading any data
2321
- Update icon names using Font Awesome 6
2422
- All code comments are in English now

R/01-utils.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ make_names_vec <- function(x)
8787
nms <- stringr::str_replace_all(nms, "\\.|-|/|\\(|\\)|&|\\?", "_") # replace . - \ ? to _ (underscore)
8888
nms <- stringr::str_replace_all(nms, "[_]+", "_") # replace multiple underscores by one
8989
nms <- stringr::str_replace_all(nms, "[_]+$", "") # remove trailing underscores
90+
nms <- stringr::str_replace_all(nms, "ß", "ss")
9091
nms <- stringr::str_replace_all(nms, "\u00E4", "ae") # replace German umlauts by their two letter ASCII version
9192
nms <- stringr::str_replace_all(nms, "\u00FC", "ue")
9293
nms <- stringr::str_replace_all(nms, "\u00F6", "oe")

R/02-calculate.R

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ calculate_similarity <- function(x, min_matches = 6, align_poles = TRUE) #, use_
6666
if (align_poles) {
6767
x <- align_positive_poles(x)
6868
}
69-
69+
7070
s <- x[, i_ratings] # remove construct poles
7171
S <- as.matrix(s)
7272
pole_left <- x[, i_left]
@@ -98,7 +98,7 @@ calculate_similarity <- function(x, min_matches = 6, align_poles = TRUE) #, use_
9898
Mi_vec <- as.vector(Mi)
9999
R[, ] <- ifelse(M_vec > Mi_vec, M_vec, Mi_vec)
100100
diag(R) <- NA
101-
101+
102102
diag(M) <- NA # exlude match to self
103103
diag(Mi) <- NA # exlude match to self
104104

@@ -130,8 +130,8 @@ calculate_similarity <- function(x, min_matches = 6, align_poles = TRUE) #, use_
130130
names(pole_right) <- labels
131131
names(valence_left) <- labels
132132
names(valence_right) <- labels
133-
134-
list(R = R, # no of matches (indlucing optional construct reversal, i.e. only high no. of matches relevant
133+
134+
list(R = R, # no of matches (inclucing optional construct reversal, i.e. only high no. of matches relevant
135135
M = M, # matrix of matches without optional construct reversal, as described in paper, i.e. a very low and very high number of matches relevant
136136
MM = MM, # relatedness 0/1
137137
D = D, # direction of relation -1/1
@@ -192,7 +192,7 @@ clique_color_pals <- function(n, name = "Dark2", alpha = .1)
192192
)
193193
return(l)
194194
}
195-
195+
196196
pals <- RColorBrewer::brewer.pal.info
197197
n_max <- pals[name, ]$maxcolors # max umber of avaiabke colors in palette
198198
cols <- RColorBrewer::brewer.pal(n_max, name) # build palette
@@ -214,7 +214,7 @@ clique_color_pals <- function(n, name = "Dark2", alpha = .1)
214214
add_image_border <- function(color = NA)
215215
{
216216
if ( is.null(color) || is.na(color) )
217-
return(NULL)
217+
return(NULL)
218218
graphics::box(which = "outer", lty = "solid", col = color)
219219
}
220220

@@ -327,7 +327,7 @@ network_graph_images <- function(x,
327327

328328
nms_keep <- clique_lists %>% unlist %>% unique
329329
MM2 <- MM[nms_keep, nms_keep]
330-
330+
331331
# colorize edges by direction
332332
edges <- ends(g, E(g)) # edge from to as rowwise matrix
333333
edge_directions <- D[edges]
@@ -336,7 +336,7 @@ network_graph_images <- function(x,
336336
} else {
337337
edge_labels <- NULL
338338
}
339-
339+
340340
if (colorize_direction) {
341341
edge_colors <- recode(edge_directions, `1` = "darkgreen", `-1` = "red", .default = "grey")
342342
edge_label_colors <- edge_colors
@@ -345,8 +345,8 @@ network_graph_images <- function(x,
345345
edge_label_colors <- grey(.2)
346346
}
347347
E(g)$color <- edge_colors
348-
349-
348+
349+
350350
##__ all - abbreviated ----------------------------------------------
351351

352352
vertex.labels <- NULL
@@ -380,7 +380,7 @@ network_graph_images <- function(x,
380380
})
381381
add_image_border(image_border_color)
382382
dev.off()
383-
383+
384384

385385
# __ all - full labels ----------------------------------------------
386386

@@ -420,19 +420,19 @@ network_graph_images <- function(x,
420420
vertex.label.family = "sans",
421421
vertex.color = grey(.9),
422422
vertex.frame.color = grey(.5))
423-
423+
424424
})
425425
add_image_border(image_border_color)
426426
dev.off()
427427

428-
428+
429429
##__ all - separate poles ----------------------------------------------
430430

431431
label_wrap_width <- 14
432432
cnames <- paste( prep_label(l$pole_left, label_max_length = label_max_length), "@",
433433
prep_label(l$pole_right, label_max_length = label_max_length))
434434
names(cnames) <- names(l$constructs)
435-
435+
436436
cns <- V(g)$name
437437
ii_keep <- match(cns, names(cnames))
438438
vertex.labels <-
@@ -441,7 +441,7 @@ network_graph_images <- function(x,
441441
vertex.size <- 22
442442
vertex.label.cex <- .5
443443
edge_labels <- NULL
444-
444+
445445
# find vertexes with negative relations only => we need to separate by direction
446446
D2 <- D[ii_keep, ii_keep]
447447
vertex_relations <- apply(D2, 2, function(x) {
@@ -457,10 +457,10 @@ network_graph_images <- function(x,
457457
names(vertex_relations) <- cns
458458
vertex_font_pole_1 <- 1 # recode(vertex_relations, "neg" = 2, .default = 1)
459459
vertex_font_pole_2 <- 1 # recode(vertex_relations, "neg" = 1, .default = 2)
460-
460+
461461
vertex.labels1 <- replace_all(vertex.labels, first = TRUE)
462462
vertex.labels2 <- replace_all(vertex.labels, first = FALSE)
463-
463+
464464
# colorize edges by direction
465465
edges <- ends(g, E(g)) # edge from to as rowwise matrix
466466
edge_directions <- D[edges]
@@ -514,7 +514,7 @@ network_graph_images <- function(x,
514514
vertex.label.font = vertex_font_pole_1,
515515
vertex.color = grey(.9),
516516
vertex.frame.color = grey(.5))
517-
517+
518518
set.seed(seed)
519519
igraph::plot.igraph(g, add = TRUE,
520520
mark.groups = NULL,
@@ -568,7 +568,7 @@ network_graph_images <- function(x,
568568
cnames[cns] %>%
569569
str_sub(start = 1, end = label_max_length) %>%
570570
str_wrap(width = label_wrap_width)
571-
571+
572572
# abbreviated construct labels
573573
vertex.labels <- NULL
574574
vertex.size = 30
@@ -622,7 +622,7 @@ network_graph_images <- function(x,
622622
vertex.label.cex <- .6
623623

624624
img_cliques_only_full_labels <- tempfile(fileext = ".png")
625-
625+
626626
png(img_cliques_only_full_labels, width = 20, height = 20, units = "cm", res = 300)
627627
with_par(img_par, {
628628
set.seed(seed)

R/03-excel.R

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ check_excel_input_test <- function(x)
120120
#'
121121
check_excel_input <- function(x)
122122
{
123-
tests <- tryCatch(
124-
check_excel_input_test(x),
125-
error = function(e) {
126-
data.frame(
127-
assert = "Excel file tests can be executed without error.",
128-
passed = FALSE,
129-
error = "When testing your Excel file format for correctness, the program crashed. The reason is unknown. Most likely, there is a problem in the Excel file you uploaded. Please check the Excel grid format for correctness."
130-
)
131-
}
132-
)
123+
suppressWarnings({ # so that NA coercions do not raise unwanted warnings
124+
tests <- tryCatch(
125+
check_excel_input_test(x),
126+
error = function(e) {
127+
data.frame(
128+
assert = "Excel file tests can be executed without error.",
129+
passed = FALSE,
130+
error = "When testing your Excel file format for correctness, the program crashed. The reason is unknown. Most likely, there is a problem in the Excel file you uploaded. Please check the Excel grid format for correctness."
131+
)
132+
}
133+
)
134+
})
133135
tests
134136
}
135137

codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ language: R
33
sudo: false
44
cache: packages
55
after_success:
6-
- Rscript -e 'covr::codecov()'
6+
- Rscript -e 'covr::codecov()'

tests/testthat/Rplots.pdf

3.37 KB
Binary file not shown.

tests/testthat/test-calculate.R

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
3+
4+
test_that("calculation functions work as expected", {
5+
6+
# count_matches --------------------
7+
8+
ci = matrix(c(1, 1, 0, 0), 2, 2, byrow = TRUE)
9+
cj = matrix(c(0, 1, 0, 1), 2, 2, byrow = TRUE)
10+
expect_equal(count_matches(ci, ci), 4)
11+
expect_equal(count_matches(ci, ci, inverse = TRUE), 0)
12+
13+
expect_equal(count_matches(cj, cj), 4)
14+
expect_equal(count_matches(cj, cj, inverse = TRUE), 0)
15+
16+
expect_equal(count_matches(cj, ci), 2)
17+
expect_equal(count_matches(cj, ci, inverse = TRUE), 2)
18+
19+
20+
# align_positive_poles --------------------
21+
22+
df <- data.frame(
23+
left = c("l1","l2","l3"),
24+
e1 = c(0, 1, NA),
25+
e2 = c(0, 1, 1),
26+
right = c("r1","r2","r3"),
27+
preferred = c(1, 1, 1))
28+
expect_equal(align_positive_poles(df), df)
29+
30+
df2 <- data.frame(
31+
left = c("r1","r2","r3"),
32+
e1 = c(1, 0, NA),
33+
e2 = c(1, 0, 0),
34+
right = c("l1","l2","l3"),
35+
preferred = c(0, 0, 0))
36+
expect_equal(align_positive_poles(df2), df)
37+
38+
39+
# calculate_similarity --------------------
40+
41+
# (these results are the input for the graph algortihm)
42+
file <- system.file("extdata", "sylvia.xlsx", package = "OpenRepGrid.ic")
43+
x <- read.xlsx(file)
44+
x_subset <- x[c(3, 7, 10), ] # only use three constructs for testing
45+
l <- calculate_similarity(x_subset)
46+
47+
# => no of matches (inclucing optional construct reversal)
48+
R = matrix(c(NA, 5, 6,
49+
5, NA, 6,
50+
6, 6, NA), nrow = 3, byrow=TRUE)
51+
expect_equal(l$R, R, check.attributes = FALSE)
52+
53+
# => # matrix of matches without optional construct reversal)
54+
M = matrix(c(NA, 2, 6,
55+
2, NA, 1,
56+
6, 1, NA), nrow = 3, byrow=TRUE)
57+
expect_equal(l$M, M, check.attributes = FALSE)
58+
59+
# relatedness 0/1
60+
MM = matrix(c(0, 0, 1,
61+
0, 0, 1,
62+
1, 1, 0), nrow = 3, byrow=TRUE)
63+
expect_equal(l$MM, MM, check.attributes = FALSE)
64+
65+
# direction of relation -1/1
66+
D = matrix(c(NA, NA, 1,
67+
NA, NA, -1,
68+
1, -1, NA), nrow = 3, byrow=TRUE)
69+
expect_equal(l$D, D, check.attributes = FALSE)
70+
})
71+
72+
73+
74+

tests/testthat/test-file-format.R renamed to tests/testthat/test-excel.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,19 @@ test_that("input file format is checked correctly", {
4545
tests <- check_excel_input(x)
4646
expect_false(all(tests$passed))
4747

48-
x <- read.xlsx(file, sheet = "format-empty")
48+
suppressWarnings({ # suppress read.xlsx empty sheet warning
49+
x <- read.xlsx(file, sheet = "format-empty")
50+
})
4951
tests <- check_excel_input(x)
5052
expect_false(all(tests$passed))
5153

5254
})
55+
56+
57+
test_that("Excel output file gets created", {
58+
file <- system.file("extdata", "sylvia.xlsx", package = "OpenRepGrid.ic")
59+
x <- openxlsx::read.xlsx(file)
60+
l <- network_graph_images(x, min_clique_size = 3, show_edges = TRUE, min_matches = 6)
61+
file_tmp <- create_excel_output(file, l)
62+
expect_true(file.exists(file_tmp))
63+
})

tests/testthat/test-utils.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
3+
4+
test_that("utility functions work as expected", {
5+
6+
# emptify_object
7+
expect_equal(emptify_object(mtcars), mtcars[integer(0), ])
8+
expect_equal(
9+
emptify_object(list(a = 1, b = 1:10)),
10+
list(a = list(), b = list())
11+
)
12+
13+
# dt_default
14+
dt = dt_default()
15+
expect_true(inherits(dt, "datatables"))
16+
17+
# cell_text_split
18+
res <- list(c("10","20","30"))
19+
expect_equal(cell_text_split("10, 20,30"), res)
20+
expect_equal( cell_text_split("; , 10 ,,, 20;30,," ), res)
21+
22+
# make_names_vec
23+
a <- c("ä", "ü", "ö", "Ä", "Ü", "Ö", "ß")
24+
b <- c("ae", "ue", "oe", "ae", "ue", "oe", "ss")
25+
expect_equal(make_names_vec(a), b)
26+
27+
a <- c("___", "_a_", "a___b", "a b c", " _ _ _A")
28+
b <-c("", "_a", "a_b", "a_b_c", "_a")
29+
expect_equal(make_names_vec(a), b)
30+
31+
# make_names
32+
df <- as.data.frame(matrix(NA, 0, 5))
33+
names(df) <- a
34+
expect_equal(names(make_names(df)), b)
35+
36+
# fnum
37+
expect_equal(fnum(0.1), "0.10")
38+
expect_equal(fnum(11), "11.00")
39+
expect_equal(fnum(1, 3), "1.000")
40+
41+
})
42+
43+
44+
45+

0 commit comments

Comments
 (0)