Skip to content

Commit

Permalink
Use _ in R bmi
Browse files Browse the repository at this point in the history
  • Loading branch information
sverhoeven committed Oct 7, 2024
1 parent f016e01 commit 73e3a9a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
74 changes: 37 additions & 37 deletions R/remotebmi/R/route.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ create_route <- function(model) {
update_until <- function(request, response, keys, ...) {
request$parse(json = reqres::parse_json())
time <- request$body
model$updateUntil(time)
model$update_until(time)
response$status <- 204L
return(FALSE)
}
Expand All @@ -55,87 +55,87 @@ create_route <- function(model) {
get_component_name <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- list(name = model$getComponentName())
response$body <- list(name = model$get_component_name())
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_output_var_names <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getOutputVarNames()
response$body <- model$get_output_var_names()
response$format(json = reqres::format_json())
return(FALSE)
}

get_output_item_count <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getOutputItemCount()
response$body <- model$get_output_item_count()
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_input_var_names <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getInputVarNames()
response$body <- model$get_input_var_names()
response$format(json = reqres::format_json())
return(FALSE)
}

get_input_item_count <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getInputItemCount()
response$body <- model$get_input_item_count()
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_time_units <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- list(units = model$getTimeUnits())
response$body <- list(units = model$get_time_units())
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_time_step <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getTimeStep()
response$body <- model$get_time_step()
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_current_time <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getCurrentTime()
response$body <- model$get_current_time()
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_start_time <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getStartTime()
response$body <- model$get_start_time()
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_end_time <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getEndTime()
response$body <- model$get_end_time()
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_var_grid <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getVarGrid(last_segment(request$path))
response$body <- model$get_var_grid(last_segment(request$path))
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}
Expand All @@ -144,7 +144,7 @@ create_route <- function(model) {
response$status <- 200L
response$type <- "application/json"
name <- last_segment(request$path)
rawType <- model$getVarType(name) # nolint: object_name_linter.
rawType <- model$get_var_type(name) # nolint: object_name_linter.
type <- ifelse(rawType == "float64", "double", rawType)
# TODO map other types to double, float, int32 or int64
response$body <- list(type = type)
Expand All @@ -155,23 +155,23 @@ create_route <- function(model) {
get_var_itemsize <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getVarItemSize(last_segment(request$path))
response$body <- model$get_var_itemsize(last_segment(request$path))
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_var_units <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- list(units = model$getVarUnits(last_segment(request$path)))
response$body <- list(units = model$get_var_units(last_segment(request$path)))
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_var_nbytes <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getVarNBytes(last_segment(request$path))
response$body <- model$get_var_nbytes(last_segment(request$path))
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}
Expand All @@ -180,15 +180,15 @@ create_route <- function(model) {
response$status <- 200L
response$type <- "application/json"
name <- last_segment(request$path)
response$body <- list(location = model$getVarLocation(name))
response$body <- list(location = model$get_var_location(name))
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_value <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getValue(last_segment(request$path))
response$body <- model$get_value(last_segment(request$path))
response$format(json = reqres::format_json())
return(FALSE)
}
Expand All @@ -198,150 +198,150 @@ create_route <- function(model) {
response$status <- 200L
response$type <- "application/json"
name <- last_segment(request$path)
response$body <- model$getValueAtIndices(name, request$body)
response$body <- model$get_value_at_indices(name, request$body)
response$format(json = reqres::format_json())
return(FALSE)
}

set_value <- function(request, response, keys, ...) {
request$parse(json = reqres::parse_json())
model$setValue(last_segment(request$path), request$body)
model$set_value(last_segment(request$path), request$body)
response$status <- 204L
return(FALSE)
}

set_value_at_indices <- function(request, response, keys, ...) {
request$parse(json = reqres::parse_json())
name <- last_segment(request$path)
model$setValueAtIndices(name, request$body$indices, request$body$values)
model$set_value_at_indices(name, request$body$indices, request$body$values)
response$status <- 204L
return(FALSE)
}

get_grid_rank <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridRank(keys$grid)
response$body <- model$get_grid_rank(keys$grid)
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_grid_type <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- list(type = model$getGridType(keys$grid))
response$body <- list(type = model$get_grid_type(keys$grid))
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_grid_size <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridSize(keys$grid)
response$body <- model$get_grid_size(keys$grid)
response$format(json = reqres::format_json(auto_unbox = TRUE))
return(FALSE)
}

get_grid_x <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridX(keys$grid)
response$body <- model$get_grid_x(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_y <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridY(keys$grid)
response$body <- model$get_grid_y(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_z <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridZ(keys$grid)
response$body <- model$get_grid_z(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_origin <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridOrigin(keys$grid)
response$body <- model$get_grid_origin(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_shape <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridShape(keys$grid)
response$body <- model$get_grid_shape(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_spacing <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridSpacing(keys$grid)
response$body <- model$get_grid_spacing(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_node_count <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridNodeCount(keys$grid)
response$body <- model$get_grid_node_count(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_edge_count <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridEdgeCount(keys$grid)
response$body <- model$get_grid_edge_count(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_face_count <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridFaceCount(keys$grid)
response$body <- model$get_grid_face_count(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_edge_nodes <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridEdgeNodes(keys$grid)
response$body <- model$get_grid_edge_nodes(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_face_edges <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridFaceEdges(keys$grid)
response$body <- model$get_grid_face_edges(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_face_nodes <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridFaceNodes(keys$grid)
response$body <- model$get_grid_face_nodes(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}

get_grid_nodes_per_face <- function(request, response, keys, ...) {
response$status <- 200L
response$type <- "application/json"
response$body <- model$getGridNodesPerFace(keys$grid)
response$body <- model$get_grid_nodes_per_face(keys$grid)
response$format(json = reqres::format_json())
return(FALSE)
}
Expand Down
29 changes: 19 additions & 10 deletions R/remotebmi/tests/testthat/test-route.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
library(R6)
library(bmi)

# Poor mans mock
bmi_initialize_called_with <<- ""
bmi_get_var_units_called_with <<- ""
# Mock model object
mock_model <- list(
bmi_initialize = function(config_file) {
bmi_initialize_called_with <<- config_file
},
getComponentName = function() "Mock Component",
getOutputVarNames = function() c("var1", "var2"),
getVarUnits = function(name) {
bmi_get_var_units_called_with <<- name
return("unit1")
}
# Modelled after
# https://github.com/eWaterCycle/grpc4bmi/blob/main/test/fake_models.py
MockedBmi <- R6Class("MockedBmi",
inherit = AbstractBmi,
public = list(
bmi_initialize = function(config_file) {
bmi_initialize_called_with <<- config_file
},
get_component_name = function() "Mock Component",
get_output_var_names = function() c("var1", "var2"),
get_var_units = function(name) {
bmi_get_var_units_called_with <<- name
return("unit1")
}
)
)
mock_model <- MockedBmi$new()

route <- create_route(mock_model)
formatter <- reqres::format_json(auto_unbox = TRUE)
Expand Down

0 comments on commit 73e3a9a

Please sign in to comment.