File: test-data_unique.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 (169 lines) | stat: -rw-r--r-- 3,446 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Preparations

df1 <- data.frame(
  id = c(1, 2, 3, 1, 3),
  year = c(2022, 2022, 2022, 2022, 2000),
  item1 = c(NA, 1, 1, 2, 3),
  item2 = c(NA, 1, 1, 2, 3),
  item3 = c(NA, 1, 1, 2, 3)
)

expected1 <- data.frame(
  id = c(1, 2, 3),
  year = c(2022, 2022, 2022),
  item1 = c(2, 1, 1),
  item2 = c(2, 1, 1),
  item3 = c(2, 1, 1)
)

expected2 <- data.frame(
  id = c(1, 2, 3),
  year = c(2022, 2022, 2022),
  item1 = c(NA, 1, 1),
  item2 = c(NA, 1, 1),
  item3 = c(NA, 1, 1)
)

expected3 <- data.frame(
  id = c(1, 2, 3),
  year = c(2022, 2022, 2000),
  item1 = c(2, 1, 3),
  item2 = c(2, 1, 3),
  item3 = c(2, 1, 3)
)

expected4 <- data.frame(
  id = c(1, 2, 3, 3),
  year = c(2022, 2022, 2022, 2000),
  item1 = c(2, 1, 1, 3),
  item2 = c(2, 1, 1, 3),
  item3 = c(2, 1, 1, 3)
)

# Testing

test_that("data_unique returns original data if no duplicates", {
  test <- data.frame(x = c(1, 2), y = c(3, 4))
  expect_identical(
    data_unique(test, c("x", "y"), verbose = FALSE),
    test
  )
  expect_identical(
    data_unique(test, "x", verbose = FALSE),
    test
  )
})

test_that("data_unique basic", {
  expect_identical(
    data_unique(df1, select = "id", verbose = FALSE),
    expected1
  )
})

test_that("data_unique basic method best", {
  expect_identical(
    data_unique(df1, select = "id", keep = "best", verbose = FALSE),
    expected1
  )
})

test_that("data_unique basic method first", {
  expect_identical(
    data_unique(df1, select = "id", keep = "first", verbose = FALSE),
    expected2
  )
})

test_that("data_unique basic method last", {
  expect_identical(
    data_unique(df1, select = "id", keep = "last", verbose = FALSE),
    expected3
  )
})

test_that("data_unique unquoted", {
  expect_identical(
    data_unique(df1, select = id, verbose = FALSE),
    expected1
  )
})

test_that("data_unique vector", {
  expect_identical(
    data_unique(df1, select = 1, verbose = FALSE),
    expected1
  )
})

test_that("data_unique select-helper", {
  expect_identical(
    data_unique(df1, select = contains("id"), verbose = FALSE),
    expected1
  )
})

test_that("data_unique multiple IDs", {
  x <- data_unique(df1, select = c("id", "year"), verbose = FALSE)
  rownames(x) <- NULL
  expect_identical(
    x,
    expected4
  )
})

test_that("data_unique multiple IDs formula", {
  x <- data_unique(df1, select = ~ id + year, verbose = FALSE)
  rownames(x) <- NULL
  expect_identical(
    x,
    expected4
  )
})

test_that("data_unique multiple IDs vector", {
  x <- data_unique(df1, select = 1:2, verbose = FALSE)
  rownames(x) <- NULL
  expect_identical(
    x,
    expected4
  )
})

test_that("data_unique preserve attributes", {
  attr(df1, "testing") <- "custom.attribute"
  x <- attributes(data_unique(df1, id, verbose = FALSE))
  expect_identical(
    x$testing,
    "custom.attribute"
  )
})

test_that("data_unique, arg 'verbose' works", {
  expect_message(
    data_unique(df1, select = ~ id + year),
    "removed, with method"
  )
})

test_that("data_unique works with groups", {
  df <- data.frame(
    g = c(1, 1, 2, 2),
    x = c(1, 1, 2, 1)
  )
  df <- data_group(df, "g")

  expected <- data.frame(
    g = c(1, 2, 2),
    x = c(1, 2, 1)
  )
  expected <- data_group(expected, "g")

  x <- data_unique(df, "x", verbose = FALSE)
  expect_identical(x, expected, ignore_attr = TRUE)

  y <- attributes(x)

  expect_identical(attributes(df)$class, y$class)
  expect_identical(attributes(df)$groups, y$groups)
})