Skip to content
Permalink
Browse files

WIP zoltar2 conversion

  • Loading branch information...
matthewcornell committed Oct 15, 2019
1 parent 3ee63f7 commit bc37cb4224600f8a3d4f3c62781bb61dde77b1d2
Showing with 75 additions and 15 deletions.
  1. +40 −3 R/connection.R
  2. +8 −0 tests/testthat/example-model-config.json
  3. +27 −12 tests/testthat/test-connection.R
@@ -29,6 +29,11 @@ url_for_project_id <- function(zoltar_connection, project_id) {
}


url_for_project_models <- function(zoltar_connection, project_id) {
paste0(zoltar_connection$host, '/api/project/', project_id, '/models/')
}


url_for_model_id <- function(zoltar_connection, model_id) {
paste0(zoltar_connection$host, '/api/model/', model_id)
}
@@ -289,12 +294,13 @@ delete_project <- function(zoltar_connection, project_id) {

#' Create a project
#'
#' Creates the project using the passed list. Fails if a project with the passed name already exists.
#' Creates the project using the passed project configuration list. Fails if a project with the passed name already
#' exists.
#'
#' @return project_id of the newly-created project
#' @param zoltar_connection A `ZoltarConnection` object as returned by \code{\link{new_connection}}
#' @param project_config A `list` containing a Zoltar project configuration. todo xx document.
# An example: example-project-config.json
#' @param project_config A `list` containing a Zoltar project configuration. note that this list validated by the
#' server and not here. An example: example-project-config.json. todo xx document
#' @export
#' @examples \dontrun{
#' new_project_id <- create_project(conn, jsonlite::read_json("example-project-config.json"))
@@ -452,6 +458,37 @@ delete_model <- function(zoltar_connection, model_id) {
}


#' Create a model
#'
#' Creates the model in the passed project using the passed list. Fails if a model with the passed name already exists.
#'
#' @return model_id of the newly-created model
#' @param zoltar_connection A `ZoltarConnection` object as returned by \code{\link{new_connection}}
#' @param project_id ID of a project in zoltar_connection's projects. this is the project the new model will be created
#' in
#' @param model_config A `list` containing a Zoltar model configuration. todo xx document.
# An example: example-model-config.json
#' @export
#' @examples \dontrun{
#' new_model_id <- create_model(conn, 46L, jsonlite::read_json("example-model-config.json"))
#' }
create_model <- function(zoltar_connection, project_id, model_config) {
models_url <- url_for_project_models(zoltar_connection, project_id)
response <- httr::POST(
url=models_url,
add_auth_headers(zoltar_connection),
body=list(model_config=model_config))
# the Zoltar API returns 400 if there was an error POSTing. the content is JSON with a $error key that contains the
# error message
json_response <- httr::content(response, "parsed")
if (response$status_code == 400) {
stop(json_response$error, call. = FALSE)
}

json_response$id # throw away rest of json and let model_info() reload/refresh it
}


#
# ---- forecast functions ----
#
@@ -0,0 +1,8 @@
{
"name": "a model_name",
"abbreviation": "an abbreviation",
"team_name": "a team_name",
"description": "a description",
"home_url": "http://example.com/",
"aux_data_url": "http://example.com/"
}
@@ -61,6 +61,12 @@ test_that("url_for_project_id(zoltar_connection, project_id) returns a URL", {
})


test_that("url_for_project_models(zoltar_connection, project_id) returns a URL", {
zoltar_connection <- new_connection("http://example.com")
expect_equal(url_for_project_models(zoltar_connection, 1L), "http://example.com/api/project/1/models/")
})


test_that("url_for_model_id(zoltar_connection, project_id) returns a URL", {
zoltar_connection <- new_connection("http://example.com")
expect_equal(url_for_model_id(zoltar_connection, 1L), "http://example.com/api/model/1")
@@ -147,10 +153,10 @@ test_that("httr functions re-authenticate expired tokens", {

zoltar_connection <- new_connection("http://example.com")
mock_authenticate(zoltar_connection) # default token (mock_token) is expired
webmockr::stub_request('get', uri='http://example.com')
webmockr::stub_request('delete', uri='http://example.com/api/forecast/0')
webmockr::stub_request('post', uri='http://example.com/api/model/0/forecasts/')
webmockr::stub_request('post', uri = 'http://example.com/api-token-auth/')
webmockr::stub_request("get", uri="http://example.com")
webmockr::stub_request("delete", uri="http://example.com/api/forecast/0")
webmockr::stub_request("post", uri="http://example.com/api/model/0/forecasts/")
webmockr::stub_request("post", uri = "http://example.com/api-token-auth/")
mockery::stub(upload_forecast, 'httr::upload_file', NULL)

m <- mock()
@@ -182,7 +188,7 @@ test_that("httr functions re-authenticate expired tokens", {
test_that("upload_forecast() does not call add_headers() for unauthenticated connection", {
zoltar_connection <- new_connection("http://example.com") # unauthenticated
mockery::stub(upload_forecast, 'httr::upload_file', NULL)
webmockr::stub_request('post', uri='http://example.com/api/model/0/forecasts/')
webmockr::stub_request("post", uri="http://example.com/api/model/0/forecasts/")
m <- mock()
testthat::with_mock("httr::add_headers" = m, {
upload_forecast(zoltar_connection, 0L, NULL, list()) # model_id, timezero_date, forecast_data
@@ -193,7 +199,7 @@ test_that("upload_forecast() does not call add_headers() for unauthenticated con

test_that("delete_forecast() does not call add_headers() for unauthenticated connection", {
zoltar_connection <- new_connection("http://example.com") # unauthenticated
webmockr::stub_request('delete', uri='http://example.com/api/forecast/0')
webmockr::stub_request("delete", uri="http://example.com/api/forecast/0")
m <- mock()
testthat::with_mock("httr::add_headers" = m, {
delete_forecast(zoltar_connection, forecast_id=0L)
@@ -205,7 +211,7 @@ test_that("delete_forecast() does not call add_headers() for unauthenticated con

test_that("get_resource() does not call add_headers() for unauthenticated connection", {
zoltar_connection <- new_connection("http://example.com") # unauthenticated
webmockr::stub_request('get', uri='http://example.com')
webmockr::stub_request("get", uri="http://example.com")
m <- mock()
testthat::with_mock("httr::add_headers" = m, {
get_resource(zoltar_connection, "http://example.com")
@@ -216,7 +222,7 @@ test_that("get_resource() does not call add_headers() for unauthenticated connec

test_that("scores() does not call add_headers() for unauthenticated connection", {
zoltar_connection <- new_connection("http://example.com") # unauthenticated
webmockr::stub_request('get', uri='http://example.com/api/project/0/score_data/')
webmockr::stub_request("get", uri="http://example.com/api/project/0/score_data/")
m <- mock()
testthat::with_mock("httr::add_headers" = m, {
scores(zoltar_connection, 0L)
@@ -227,7 +233,7 @@ test_that("scores() does not call add_headers() for unauthenticated connection",

test_that("download_forecast() does not call add_headers() for unauthenticated connection", {
zoltar_connection <- new_connection("http://example.com") # unauthenticated
webmockr::stub_request('get', uri='http://example.com/api/forecast/0/data/')
webmockr::stub_request("get", uri="http://example.com/api/forecast/0/data/")
m <- mock()
testthat::with_mock("httr::add_headers" = m, {
download_forecast(zoltar_connection, 0L)
@@ -368,7 +374,7 @@ test_that("models(zoltar_connection, project_id) can handle NULL owner in projec
test_that("create_project() creates a Project", {
zoltar_connection <- new_connection("http://example.com")
project_info <- two_projects_json[[1]]
webmockr::stub_request('post', uri='http://example.com/api/projects/') %>%
webmockr::stub_request("post", uri="http://example.com/api/projects/") %>%
to_return(
body=project_info,
status=200,
@@ -457,7 +463,7 @@ test_that("upload_forecast() returns an UploadFileJob id, and upload_info() is c
zoltar_connection <- new_connection("http://example.com")
upload_file_job_json <- jsonlite::read_json("upload-file-job-2.json")
mockery::stub(upload_forecast, 'httr::upload_file', NULL)
webmockr::stub_request('post', uri='http://example.com/api/model/1/forecasts/') %>%
webmockr::stub_request("post", uri="http://example.com/api/model/1/forecasts/") %>%
to_return(
body=upload_file_job_json,
status=200,
@@ -506,7 +512,16 @@ test_that("delete_forecast(zoltar_connection, forecast_id) passes correct URL",


test_that("create_model() creates a Model", {
fail("todo xx")
zoltar_connection <- new_connection("http://example.com")
model_info <- jsonlite::read_json("model-1.json")
webmockr::stub_request("post", uri="http://example.com/api/project/1/models/") %>%
to_return(
body=model_info,
status=200,
headers=list('Content-Type'='application/json; charset=utf-8'))
model_config <- jsonlite::read_json("example-model-config.json")
new_model_id <- create_model(zoltar_connection, project_id=1L, model_config)
expect_equal(new_model_id, 1L)
})


0 comments on commit bc37cb4

Please sign in to comment.
You can’t perform that action at this time.