Skip to content

Commit 09d515a

Browse files
authored
Merge pull request #7 from AtlasOfLivingAustralia/test_identifiers
Added tests for ID generation functions #6
2 parents b4f5951 + 50e9c3d commit 09d515a

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
test_that("random_id() generates unique and random IDs", {
2+
df <- tibble::tibble(x = 1:5)
3+
4+
# Testing random ID generating unique ID for each row
5+
result <- df |>
6+
dplyr::mutate(id = random_id())
7+
expect_equal(nrow(result), 5)
8+
expect_true(all(nchar(result$id) == 36))
9+
expect_true(length(unique(result$id)) == 5)
10+
11+
# Testing with a single row (single ID generated)
12+
single_row_df <- tibble::tibble(x = 1)
13+
result <- single_row_df |>
14+
dplyr::mutate(id = random_id())
15+
expect_equal(nrow(result), 1)
16+
expect_true(nchar(result$id) == 36)
17+
18+
# Test with no rows (nothing returned)
19+
empty_df <- tibble::tibble(x = numeric(0))
20+
result <- empty_df |>
21+
dplyr::mutate(id = random_id())
22+
expect_equal(nrow(result), 0)
23+
24+
})
25+
26+
test_that("sequential_id generates sequential IDs correctly", {
27+
df <- tibble::tibble(x = 1:5)
28+
29+
# Default width
30+
result <- df |>
31+
dplyr::mutate(id = sequential_id())
32+
expect_equal(result$id, c("01", "02", "03", "04", "05"))
33+
34+
# Create sequential IDs of specified width
35+
result <- df |>
36+
dplyr::mutate(id = sequential_id(width = 4))
37+
expect_equal(result$id, c("0001", "0002", "0003", "0004", "0005"))
38+
39+
# Single row - single ID
40+
single_row_df <- tibble::tibble(x = 1)
41+
result <- single_row_df |>
42+
dplyr::mutate(id = sequential_id())
43+
expect_equal(result$id, "01")
44+
45+
# Single row - single ID of specified width
46+
single_row_df <- tibble::tibble(x = 1)
47+
result <- single_row_df |>
48+
dplyr::mutate(id = sequential_id(width = 6))
49+
expect_equal(result$id, "000001")
50+
51+
# No rows - empty character returned and warning generated
52+
empty_df <- tibble::tibble(x = numeric(0))
53+
expect_warning(result <- empty_df |> dplyr::mutate(id = sequential_id()), "no non-missing arguments to max; returning -Inf")
54+
expect_equal(result$id, character(0))
55+
})
56+
57+
test_that("composite_id generates composite strings correctly", {
58+
# Test with multiple inputs and default separator
59+
expect_equal(composite_id(1, "A", TRUE), "1-A-TRUE")
60+
# Test with a custom separator
61+
expect_equal(composite_id(1, "A", TRUE, sep = "_"), "1_A_TRUE")
62+
# Test with only one input
63+
expect_equal(composite_id("!@n$9m7"), "!@n$9m7")
64+
# Test with no input
65+
expect_equal(composite_id(sep = "-"), character(0))
66+
# Test with mixed input types
67+
expect_equal(composite_id(429, "te_b_st", NULL, 0.4, "\"", sep = "/"), "429/te_b_st//0.4/\"")
68+
69+
})

0 commit comments

Comments
 (0)