Skip to content

Commit

Permalink
Merge pull request #37 from dtacled/feature/minor-code-quality-improv…
Browse files Browse the repository at this point in the history
…ements

Feature/minor code quality improvements
  • Loading branch information
TreyBilling authored Jan 14, 2025
2 parents 3bfcaa3 + d8b041c commit 11f8607
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
53 changes: 25 additions & 28 deletions R/acled_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,20 @@
#' @export

acled_api <- function(email = NULL,
key = NULL,
country = NULL,
regions = NULL,
start_date = floor_date(Sys.Date(), "year") - years(1),
end_date = Sys.Date(),
timestamp = NULL,
event_types = NULL,
population = "none",
key = NULL,
country = NULL,
regions = NULL,
start_date = floor_date(Sys.Date(), "year") - years(1),
end_date = Sys.Date(),
timestamp = NULL,
event_types = NULL,
population = "none",
inter_numeric = FALSE,
monadic = FALSE,
...,
acled_access = TRUE,
prompt = TRUE,
log = F) {

prompt = TRUE,
log = F) {
# Acled Acess and credentials ----

if ((acled_access %in% c(TRUE, T)) & (is.null(email) | is.null(key))) { # Access is true, and credentials are null
Expand Down Expand Up @@ -142,7 +141,7 @@ acled_api <- function(email = NULL,
stop("One or more requested region numbers not in the ACLED country list. The full list of ACLED regions is available at 'acledR::acled_regions'.")
}

if(!population %in% c("none", "best", "full")) {
if (!population %in% c("none", "best", "full")) {
stop("The 'population' argument must be one of 'none', 'best', or 'full'.")
}

Expand Down Expand Up @@ -257,10 +256,6 @@ acled_api <- function(email = NULL,
time = .data$t_end - .data$t_start
)

n_countries <- length(unique(out$country))
# country_days <- as.numeric(sum(out$time))



# Note for how much data is being requested
size_note <- paste(
Expand All @@ -273,7 +268,7 @@ acled_api <- function(email = NULL,

message(size_note)

if (str_detect(as.character(end_date), "2024") | str_detect(as.character(start_date), "2024")){
if (str_detect(as.character(end_date), "2024") | str_detect(as.character(start_date), "2024")) {
message("Your request appears include dates in 2024. Please note that estimates of 2024 are based on 2023 estimates. ")
}

Expand Down Expand Up @@ -327,7 +322,7 @@ acled_api <- function(email = NULL,
## country

countries_internal <- vector("list", length = length(out_groups))
for (i in 1:length(out_groups)) {
for (i in seq_along(out_groups)) {
countries_internal[[i]] <- paste0("&country=", paste(gsub("\\s{1}", "%20", out_groups[[i]]$country), collapse = ":OR:country="))
countries_internal[[i]] <- paste0(countries_internal[[i]], "&country_where=%3D")
}
Expand Down Expand Up @@ -445,7 +440,7 @@ acled_api <- function(email = NULL,

# Population argument

if(population == "none") {
if (population == "none") {
population_internal <- ""
} else if (population == "best") {
population_internal <- "&population=true"
Expand All @@ -455,28 +450,30 @@ acled_api <- function(email = NULL,

# Inter argument

if(inter_numeric == TRUE) {
if (inter_numeric == TRUE) {
inter_internal <- "&inter_num=1"
} else {
inter_internal <- "&inter_num=0"
}

# Loop through country bins to define each api call
url_internal <- vector("list", length = length(out_groups))
for(i in 1:length(out_groups)) {
url_internal[[i]] <- paste0(base_url, monadic_internal,
email_internal, key_internal,
countries_internal[[i]],
dates_internal, timestamp_internal,
event_types_internal, population_internal,
inter_internal, ..., "&limit=0")
for (i in seq_along(out_groups)) {
url_internal[[i]] <- paste0(
base_url, monadic_internal,
email_internal, key_internal,
countries_internal[[i]],
dates_internal, timestamp_internal,
event_types_internal, population_internal,
inter_internal, ..., "&limit=0"
)
}


# Loop through the api requests
response <- vector("list", length = length(out_groups))
message("Processing API request")
for (i in 1:length(out_groups)) {
for (i in seq_along(out_groups)) {
response[[i]] <- httr::GET(url_internal[[i]])

if (response[[i]][["status_code"]] == 500) {
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-acled_transform_longer.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# Test data type ----
# Test that the function returns a tibble
test_that("returns a tibble", {
data <- data.frame(actor1 = c("Actor D"), actor2 = c("Actor C"), assoc_actor_1 = c("Actor A; Actor B"), assoc_actor_2 = c("Hello"),inter1 = "1", inter2="2",sub_event_type = "Protests", source_scale= "khapow",source= 'khapow')
data <- data.frame(actor1 = "Actor D", actor2 = "Actor C", assoc_actor_1 = "Actor A; Actor B", assoc_actor_2 = "Hello", inter1 = "1", inter2 = "2", sub_event_type = "Protests", source_scale = "khapow", source = "khapow")
output <- acled_transform_longer(data, "full_actors")
expect_s3_class(output, c("tbl_df", "data.frame"))
})

# Test functionalities ----
# Test that the function returns the correct number of rows for full_actors type
test_that("returns correct number of rows for full_actors type", {
data <- data.frame(actor1 = c("Actor D"), actor2 = c("Actor C"), assoc_actor_1 = c("Actor A; Actor B"), assoc_actor_2 = c("Hello"), inter1 = "1", inter2="2",sub_event_type = "Protests", source_scale= "khapow",source= 'khapow')
data <- data.frame(actor1 = "Actor D", actor2 = "Actor C", assoc_actor_1 = "Actor A; Actor B", assoc_actor_2 = "Hello", inter1 = "1", inter2 = "2", sub_event_type = "Protests", source_scale = "khapow", source = "khapow")
output <- acled_transform_longer(data, "full_actors")
expect_equal(nrow(output), 5)
})

# Test that the function returns the correct number of rows for main_actors type
test_that("returns correct number of rows for main_actors type", {
data <- data.frame(actor1 = c("Actor D"), actor2 = c("Actor C"), assoc_actor_1 = c("Actor A; Actor B"), assoc_actor_2 = c(""),inter1 = "1", inter2="2", sub_event_type = "Protests", source_scale= "khapow",source= 'khapow')
data <- data.frame(actor1 = "Actor D", actor2 = "Actor C", assoc_actor_1 = "Actor A; Actor B", assoc_actor_2 = "", inter1 = "1", inter2 = "2", sub_event_type = "Protests", source_scale = "khapow", source = "khapow")
output <- acled_transform_longer(data, "main_actors")
expect_equal(nrow(output), 2)
})

# Test that the function returns the correct number of rows for assoc_actors type
test_that("returns correct number of rows for assoc_actors type", {
data <- data.frame(actor1 = c("Actor D"), actor2 = c("Actor C"), assoc_actor_1 = c("Actor A; Actor B"), assoc_actor_2 = c("hello"), inter1 = "1", inter2="2",sub_event_type = "Protests", source_scale= "khapow",source= 'khapow')
data <- data.frame(actor1 = "Actor D", actor2 = "Actor C", assoc_actor_1 = "Actor A; Actor B", assoc_actor_2 = "hello", inter1 = "1", inter2 = "2", sub_event_type = "Protests", source_scale = "khapow", source = "khapow")
output <- acled_transform_longer(data, "assoc_actors")
expect_equal(nrow(output), 3)
})

# Test that the function returns the correct number of rows for source type
test_that("returns correct number of rows for source type", {
data <- data.frame(actor1 = c("Actor D"), actor2 = c("Actor C"), assoc_actor_1 = c("Actor A; Actor B"), assoc_actor_2 = c("Hello"), inter1 = "1", inter2="2",sub_event_type = "Protests", source_scale= "khapow", source = c("Source A; Source B"))
data <- data.frame(actor1 = "Actor D", actor2 = "Actor C", assoc_actor_1 = "Actor A; Actor B", assoc_actor_2 = "Hello", inter1 = "1", inter2 = "2", sub_event_type = "Protests", source_scale = "khapow", source = "Source A; Source B")
output <- acled_transform_longer(data, "source")
expect_equal(nrow(output), 2)
})
Expand All @@ -39,25 +39,25 @@ test_that("returns correct number of rows for source type", {
# Test errors----
# Test that the function throws an error for an invalid type parameter
test_that("throws an error for invalid type parameter", {
data <- data.frame(actor1 = c("Actor A; Actor B"), actor2 = c("Actor C"), assoc_actor_1 = c("Actor D"), assoc_actor_2 = c(""),inter1 = "1", inter2="2")
data <- data.frame(actor1 = "Actor A; Actor B", actor2 = "Actor C", assoc_actor_1 = "Actor D", assoc_actor_2 = "", inter1 = "1", inter2 = "2")
expect_error(acled_transform_longer(data, "invalid_type"))
})

# Test that if columns are missing, it will throw an error for an invalid data frame

test_that("if columns are missing, it will throw an error for an invalid data frame", {
data <- data.frame(actor1 = c("Actor A; Actor B"), actor2 = c("Actor C"),inter1 = "1", inter2="2", assoc_actor_1 = c("Actor D"), assoc_actor_2 = c(""))
data <- data.frame(actor1 = "Actor A; Actor B", actor2 = "Actor C", inter1 = "1", inter2 = "2", assoc_actor_1 = "Actor D", assoc_actor_2 = "")
expect_error(acled_transform_longer(data, "full_actors"), "Some columns are missing. Please make sure your data frame includes: actor1, actor2, assoc_actor_1, assoc_actor_2, sub_event_type, source_scale, source.")
})

# Test that you get an error if the actor1 or actor 2 columns have more than one value per row.
test_that("error if the actor1 or actor 2 columns have more than one value per row", {
data <- data.frame(actor1 = c("Actor A; Actor B"), actor2 = c("Actor C"), inter1 = "1", inter2="2",assoc_actor_1 = c("Actor D"), assoc_actor_2 = c(""), sub_event_type = "Protests", source_scale= "", source = c("Source A; Source B"))
data <- data.frame(actor1 = "Actor A; Actor B", actor2 = "Actor C", inter1 = "1", inter2 = "2", assoc_actor_1 = "Actor D", assoc_actor_2 = "", sub_event_type = "Protests", source_scale = "", source = "Source A; Source B")
expect_error(acled_transform_longer(data, "main_actors", "*column seems to include more than one result per row. That is inconsistent with our column structure."))
})

test_that("warning when there are empty rows in the assoc actors column", {
data <- data.frame(actor1 = c("Actor D"), actor2 = c("Actor C"), inter1 = "1", inter2="2",assoc_actor_1 = c("Actor A; Actor B"), assoc_actor_2 = c(""), sub_event_type = "Protests", source_scale= "",source= '')
data <- data.frame(actor1 = "Actor D", actor2 = "Actor C", inter1 = "1", inter2 = "2", assoc_actor_1 = "Actor A; Actor B", assoc_actor_2 = "", sub_event_type = "Protests", source_scale = "", source = "")
expect_warning(acled_transform_longer(data, "assoc_actors"), "There are empty rows in the assoc_actor column.")
expect_warning(acled_transform_longer(data, "full_actors"), "There are empty rows in the actor column.")
})

0 comments on commit 11f8607

Please sign in to comment.