File: test-data_unite.R

package info (click to toggle)
r-cran-datawizard 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,300 kB
  • sloc: sh: 13; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 5,078 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
d_unite <- data.frame(
  x = c(NA, 1:3),
  y = c(letters[1:3], NA_character_),
  z = 6:9,
  m = c("X", NA_character_, "Y", "Z"),
  n = c("NATION", "COUNTRY", "NATION", NA_character_),
  stringsAsFactors = FALSE
)


# for following tests, we need to check for correct column names,
# and correct values in new variable

test_that("data_unite: simple use case", {
  # basic
  out <- data_unite(d_unite, new_column = "xyz")
  expect_identical(colnames(out), "xyz")
  expect_identical(
    out$xyz,
    c("NA_a_6_X_NATION", "1_b_7_NA_COUNTRY", "2_c_8_Y_NATION", "3_NA_9_Z_NA")
  )
  # use existing column name
  out <- data_unite(d_unite, new_column = "x")
  expect_identical(colnames(out), "x")
  expect_identical(
    out$x,
    c("NA_a_6_X_NATION", "1_b_7_NA_COUNTRY", "2_c_8_Y_NATION", "3_NA_9_Z_NA")
  )
  # select
  out <- data_unite(d_unite, new_column = "xyz", select = c("x", "n"))
  expect_identical(colnames(out), c(setdiff(colnames(d_unite), c("x", "n")), "xyz"))
  expect_identical(
    out$xyz,
    c("NA_NATION", "1_COUNTRY", "2_NATION", "3_NA")
  )
  # select, use existing column name
  out <- data_unite(d_unite, new_column = "x", select = c("x", "n"))
  expect_identical(colnames(out), c(setdiff(colnames(d_unite), c("x", "n")), "x"))
  expect_identical(
    out$x,
    c("NA_NATION", "1_COUNTRY", "2_NATION", "3_NA")
  )
})


test_that("data_unite: remove_na", {
  # basic
  out <- data_unite(d_unite, new_column = "xyz", remove_na = TRUE)
  expect_identical(colnames(out), "xyz")
  expect_identical(
    out$xyz,
    c("a_6_X_NATION", "1_b_7_COUNTRY", "2_c_8_Y_NATION", "3_9_Z")
  )
  # use existing column name
  out <- data_unite(d_unite, new_column = "x", remove_na = TRUE)
  expect_identical(colnames(out), "x")
  expect_identical(
    out$x,
    c("a_6_X_NATION", "1_b_7_COUNTRY", "2_c_8_Y_NATION", "3_9_Z")
  )
  # select
  out <- data_unite(d_unite, new_column = "xyz", remove_na = TRUE, select = c("x", "n"))
  expect_identical(colnames(out), c(setdiff(colnames(d_unite), c("x", "n")), "xyz"))
  expect_identical(
    out$xyz,
    c("NATION", "1_COUNTRY", "2_NATION", "3")
  )
  # select, use existing column name
  out <- data_unite(d_unite, new_column = "x", remove_na = TRUE, select = c("x", "n"))
  expect_identical(colnames(out), c(setdiff(colnames(d_unite), c("x", "n")), "x"))
  expect_identical(
    out$x,
    c("NATION", "1_COUNTRY", "2_NATION", "3")
  )
})


test_that("data_unite: append", {
  # basic
  out <- data_unite(d_unite, new_column = "xyz", append = TRUE)
  expect_identical(colnames(out), c("x", "y", "z", "m", "n", "xyz"))
  expect_identical(
    out$xyz,
    c("NA_a_6_X_NATION", "1_b_7_NA_COUNTRY", "2_c_8_Y_NATION", "3_NA_9_Z_NA")
  )
  # remove NA
  out <- data_unite(d_unite, new_column = "xyz", remove_na = TRUE, append = TRUE)
  expect_identical(colnames(out), c("x", "y", "z", "m", "n", "xyz"))
  expect_identical(
    out$xyz,
    c("a_6_X_NATION", "1_b_7_COUNTRY", "2_c_8_Y_NATION", "3_9_Z")
  )
  # append, using existing column name
  expect_message({
    out <- data_unite(d_unite, new_column = "x", append = TRUE)
  })
  expect_identical(colnames(out), c("x", "y", "z", "m", "n"))
  expect_identical(
    out$x,
    c("NA_a_6_X_NATION", "1_b_7_NA_COUNTRY", "2_c_8_Y_NATION", "3_NA_9_Z_NA")
  )
  # append, using existing column name, and remove NA
  expect_message({
    out <- data_unite(d_unite, new_column = "x", remove_na = TRUE, append = TRUE)
  })
  expect_identical(colnames(out), c("x", "y", "z", "m", "n"))
  expect_identical(
    out$x,
    c("a_6_X_NATION", "1_b_7_COUNTRY", "2_c_8_Y_NATION", "3_9_Z")
  )
})


test_that("data_unite: combine select and append", {
  # basic
  out <- data_unite(d_unite, new_column = "xyz", append = TRUE, select = c("x", "n"))
  expect_identical(colnames(out), c("x", "y", "z", "m", "n", "xyz"))
  expect_identical(
    out$xyz,
    c("NA_NATION", "1_COUNTRY", "2_NATION", "3_NA")
  )
  # remove NA
  out <- data_unite(d_unite, new_column = "xyz", remove_na = TRUE, append = TRUE, select = c("x", "n"))
  expect_identical(colnames(out), c("x", "y", "z", "m", "n", "xyz"))
  expect_identical(
    out$xyz,
    c("NATION", "1_COUNTRY", "2_NATION", "3")
  )
  # append, using existing column name
  expect_message({
    out <- data_unite(d_unite, new_column = "x", append = TRUE, select = c("x", "n"))
  })
  expect_identical(colnames(out), c("x", "y", "z", "m", "n"))
  expect_identical(
    out$x,
    c("NA_NATION", "1_COUNTRY", "2_NATION", "3_NA")
  )
  # append, using existing column name, and remove NA
  expect_message({
    out <- data_unite(d_unite, new_column = "x", remove_na = TRUE, append = TRUE, select = c("x", "n"))
  })
  expect_identical(colnames(out), c("x", "y", "z", "m", "n"))
  expect_identical(
    out$x,
    c("NATION", "1_COUNTRY", "2_NATION", "3")
  )
})


test_that("data_unite: errors", {
  expect_error(data_unite(d_unite), regex = "No name")
  expect_error(data_unite(d_unite, new_column = c("a", "b")), regex = "a single string")
  expect_error(expect_warning(data_unite(d_unite, new_column = "a", select = "huhu")), regex = "At least")
})