Skip to content

Commit

Permalink
adapt test distance to test direction
Browse files Browse the repository at this point in the history
  • Loading branch information
robitalec committed Nov 1, 2024
1 parent 9caa05b commit 13cde68
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions tests/testthat/test-direction-to-leader.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Test distance_to_leader
context('test distance_to_leader')
# Test direction_to_leader
context('test direction_to_leader')

library(spatsoc)

Expand Down Expand Up @@ -28,72 +28,101 @@ DT <- copy(DT)[group != 868]
clean_DT <- copy(DT)

test_that('DT is required', {
expect_error(distance_to_leader(DT = NULL))
expect_error(direction_to_leader(DT = NULL))
})

test_that('arguments required, otherwise error detected', {
expect_error(distance_to_leader(DT, coords = NULL, group = group),
expect_error(direction_to_leader(DT, coords = NULL, group = group),
'coords req')
expect_error(distance_to_leader(DT, coords = coords, group = NULL),
expect_error(direction_to_leader(DT, coords = coords, group = NULL),
'group column name required')
})

test_that('column names must exist in DT', {
expect_error(distance_to_leader(DT, coords = rep('potato', 2), group = group),
expect_error(direction_to_leader(DT, coords = rep('potato', 2), group = group),
'potato field')
expect_error(distance_to_leader(DT, coords = coords, group = 'potato'),
expect_error(direction_to_leader(DT, coords = coords, group = 'potato'),
'group column')
copy_DT <- copy(DT)
setnames(copy_DT, 'rank_position_group_direction', 'potato')
expect_error(distance_to_leader(copy_DT, coords = coords, group = group),
expect_error(direction_to_leader(copy_DT, coords = coords, group = group),
'did you run leader?')
})

test_that('coords are correctly provided or error detected', {
expect_error(distance_to_leader(DT, coords = c('X', NULL), group = group),
expect_error(direction_to_leader(DT, coords = c('X', NULL), group = group),
'coords requires a vector')
copy_DT <- copy(DT)[, X := as.character(X)]
expect_error(distance_to_leader(copy_DT, coords = coords, group = group),
expect_error(direction_to_leader(copy_DT, coords = coords, group = group),
'coords must be numeric')
copy_DT <- copy(DT)[, X := as.character(X)]
expect_error(distance_to_leader(copy_DT, coords = coords,
expect_error(direction_to_leader(copy_DT, coords = coords,
group = group),
'coords must be numeric')
copy_DT <- copy(DT)[, rank_position_group_direction := NULL]
expect_error(distance_to_leader(copy_DT, coords = coords,
expect_error(direction_to_leader(copy_DT, coords = coords,
group = group))
})

test_that('message when distance_leader column overwritten', {
copyDT <- copy(clean_DT)[, distance_leader := 1]
test_that('message when direction_leader column overwritten', {
copyDT <- copy(clean_DT)[, direction_leader := 1]
expect_message(
distance_to_leader(copyDT, coords = coords, group = group),
'distance_leader column will be overwritten'
direction_to_leader(copyDT, coords = coords, group = group),
'direction_leader column will be overwritten'
)
})

test_that('no rows are added to the result DT', {
copyDT <- copy(clean_DT)

expect_equal(nrow(copyDT),
nrow(distance_to_leader(copyDT, coords = coords, group = group)))
nrow(direction_to_leader(copyDT, coords = coords, group = group)))
})

test_that('one column added to the result DT', {
copyDT <- copy(clean_DT)

expect_equal(ncol(copyDT) + 1,
ncol(distance_to_leader(copyDT, coords = coords, group = group)))
ncol(direction_to_leader(copyDT, coords = coords, group = group)))
})

test_that('column added to the result DT is a double', {
expect_type(
distance_to_leader(DT, coords = coords, group = group)$distance_leader,
direction_to_leader(DT, coords = coords, group = group)$direction_leader,
'double'
)
})

test_that('returns a data.table', {
expect_s3_class(distance_to_leader(DT, coords = coords, group = group),
expect_s3_class(direction_to_leader(DT, coords = coords, group = group),
'data.table')
})

expect_DT <- data.table(
ID = c('A', 'B'),
X = c(0, 10),
Y = c(0, 0),
group_direction = rep(as_units(0, 'rad'), 2),
group = c(1, 1)
)
centroid_group(expect_DT, coords = coords)
leader_direction_group(expect_DT, coords = coords,
return_rank = TRUE, group = group)
direction_to_leader(expect_DT, coords = c('X', 'Y'))

test_that('expected results for simple case', {
expect_lte(
expect_DT[, max(direction_leader, na.rm = TRUE)],
10
)

expect_equal(
expect_DT[is.na(direction_leader), .N],
1
)
expect_equal(
expect_DT[is.na(direction_leader), ID],
'B'
)
})

0 comments on commit 13cde68

Please sign in to comment.