njtierney / maxcovr

Tools in R to make it easier to solve the Maximal Coverage Location Problem
https://njtierney.github.io/maxcovr/
GNU General Public License v3.0
43 stars 12 forks source link

update tests to not copy across relevant dplyr code #101

Open njtierney opened 2 weeks ago

njtierney commented 2 weeks ago

Currently there are tests like

# compare this to the dplyr method ============================================

facility <- mutate(york, key = 1) |>
    rename(lat_facility = lat,
                  long_facility = long) |>
    # create an ID for each row
    mutate(facility_id = 1:n()) |>
    slice(1:100)

user <- mutate(york_crime, key = 1) |>
    rename(lat_user = lat,
                  long_user = long) |>
    mutate(user_id = 1:n()) |>
    slice(1:100)

my_dist_dplyr <- user |>
    left_join(facility,
                     by = "key",
              relationship = "many-to-many") |>
    mutate(distance = spherical_distance(lat1 = lat_user,
                                                long1 = long_user,
                                                lat2 = lat_facility,
                                                long2 = long_facility)) |>
    # drop key
    select(-key) |>
    select(user_id,
                  facility_id,
                  distance) |>
    spread(key = "facility_id",
                  value = "distance",
                  sep = "_") |>
    # drop the ID column (for proper comparison)
    select(-user_id) |>
    as.matrix()

test_that("cpp distance matrix produces same numeric result as using dplyr",{
    # I still need to make a method that gives the big matrix names
    expect_equal(my_dist_cpp,my_dist_dplyr)
})

Which involve hand copying relevant code over - this is not a very robust way to test the dplyr/underlying methods in the software