Skip to content

Commit

Permalink
Revert changes
Browse files Browse the repository at this point in the history
  • Loading branch information
adanmauri committed Jun 21, 2023
1 parent cbccaab commit db26f18
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 76 deletions.
1 change: 0 additions & 1 deletion src/ModelSelectionGUI.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module ModelSelectionGUI
using ModelSelection

include("status_codes.jl")
include("strings.jl")
include("types.jl")
include("utils.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ bad_request_exception("Invalid request parameters.")
```
"""
function bad_request_exception(message)
html(message, status = HTTP_400_BAD_REQUEST)
html(message, status = 400)
end
25 changes: 16 additions & 9 deletions src/responses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,32 @@ function job_results_response(job::ModelSelectionJob, resulttype::Symbol)
return HTTP.Response(200, headers, body = body)
end

result = nothing
data = nothing
if resulttype == ALLSUBSETREGRESSION
for res in job.modelselection_data.results
if typeof(res) == ModelSelection.AllSubsetRegression.AllSubsetRegressionResult
result = res
for result in job.modelselection_data.results
if typeof(result) ==
ModelSelection.AllSubsetRegression.AllSubsetRegressionResult
filename = get_csv_filename(job.filename, result)
data = get_csv_from_result(filename, result)
break
end
end
elseif resulttype == CROSSVALIDATION
for res in job.modelselection_data.results
if typeof(res) == ModelSelection.CrossValidation.CrossValidationResult
result = res
for result in job.modelselection_data.results
if typeof(result) == ModelSelection.CrossValidation.CrossValidationResult
filename = get_csv_filename(job.filename, result)
data = get_csv_from_result(filename, result)
break
end
end
end

filename = get_csv_filename(job.filename, result)
data = get_csv_from_result(filename, result)
if data === nothing
return bad_request_exception(
@sprintf("The job does not have a result of type %s", resulttype)
) # FIXME: Move to constant JOB_HAS_NOT_RESULTTYPE
end

body = data[DATA]
filename = data[FILENAME]
headers = Dict(
Expand Down
4 changes: 0 additions & 4 deletions src/status_codes.jl

This file was deleted.

45 changes: 14 additions & 31 deletions test/integration/test_files.jl
Original file line number Diff line number Diff line change
@@ -1,51 +1,34 @@
const DATA_FILENAME = "data.csv"
const DOTENV = "integration/.testenv"
const URL = "$(ModelSelectionGUI.SERVER_URL):$(ModelSelectionGUI.SERVER_PORT)/upload-file"

const FILENAME = String(ModelSelectionGUI.FILENAME)
const FILEHASH = String(ModelSelectionGUI.FILEHASH)
const DATANAMES = String(ModelSelectionGUI.DATANAMES)
const NOBS = String(ModelSelectionGUI.NOBS)

@testset "Files" begin
@testset "Upload file successfuly" begin
using HTTP, JSON, CSV, DataFrames
stop()
@testset "Upload file" begin
using HTTP, JSON
URL = "/upload-file"
url = "$(ModelSelectionGUI.SERVER_URL):$(ModelSelectionGUI.SERVER_PORT)$(URL)"
reset_envvars()
start(dotenv = DOTENV)

file = open(DATA_FILENAME, "r")
request_body = HTTP.Form(Dict(:data => HTTP.Multipart(DATA_FILENAME, file, ModelSelectionGUI.CSV_MIME)))
response = HTTP.post(URL, [], request_body)
body = JSON.parse(String(response.body))
body = HTTP.Form(Dict(:data => HTTP.Multipart(DATA_FILENAME, file, "text/csv")))
response = HTTP.post(url, [], body)
msg = String(response.body)
body = JSON.parse(msg)

data = CSV.read(DATA_FILENAME, DataFrame)
FILENAME = String(ModelSelectionGUI.FILENAME)
FILEHASH = String(ModelSelectionGUI.FILEHASH)
DATANAMES = String(ModelSelectionGUI.DATANAMES)
NOBS = String(ModelSelectionGUI.NOBS)

@test response.status == ModelSelectionGUI.HTTP_200_OK
@test response.status == 200
@test haskey(body, FILENAME)
@test body[FILENAME] isa String
@test body[FILENAME] == DATA_FILENAME
@test haskey(body, FILEHASH)
@test body[FILEHASH] isa String
@test haskey(body, DATANAMES)
@test body[DATANAMES] isa Vector
for i in 1:lastindex(names(data))
@test names(data)[i] == body[DATANAMES][i]
end
@test haskey(body, NOBS)
@test body[NOBS] isa Int64
@test body[NOBS] == nrow(data)
stop()
end
@testset "Upload file fail: invalid mime" begin
using HTTP, JSON, CSV, DataFrames
stop()
reset_envvars()
start(dotenv = DOTENV)

file = open(DATA_FILENAME, "r")
request_body = HTTP.Form(Dict(:data => HTTP.Multipart(DATA_FILENAME, file, ModelSelectionGUI.PLAIN_MIME)))
@test HTTP.post(URL, [], request_body).status == ModelSelectionGUI.HTTP_400_BAD_REQUEST

stop()
end
end
27 changes: 4 additions & 23 deletions test/integration/test_jobs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const DATA_FILENAME = "data.csv"
@testset "POST /job-enqueue/:filehash" begin
using HTTP, JSON
filehash = "adbc7420-1597-4b1b-a798-fafd9ee5f671"
url = "$(ModelSelectionGUI.SERVER_URL):$(ModelSelectionGUI.SERVER_PORT)/job-enqueue/"
url = "$(ModelSelectionGUI.SERVER_URL):$(ModelSelectionGUI.SERVER_PORT)/job-enqueue/$(filehash)"
filename = DATA_FILENAME
tempfile = DATA_FILENAME
ttest = true
Expand All @@ -20,7 +20,7 @@ const DATA_FILENAME = "data.csv"
reset_envvars()
start(dotenv = DOTENV)
response = HTTP.post(
"$(url)$(filehash)",
url,
["Content-Type" => "application/json"],
JSON.json(body)
)
Expand Down Expand Up @@ -75,31 +75,14 @@ const DATA_FILENAME = "data.csv"
@test body[EQUATION] == equation

@test haskey(body, MSG)

body = Dict(:equation => equation, :ttest => ttest)
test_url = "$(url)$(filehash)"
@test (HTTP.post(test_url, ["Content-Type" => "application/json"], JSON.json(body))).status == 400

body = Dict(:estimator => estimator, :ttest => ttest)
test_url = "$(url)$(filehash)"
@test (HTTP.post(test_url, ["Content-Type" => "application/json"], JSON.json(body))).status == 400

test_url = "$(url)invalid"
@test (HTTP.post(test_url, ["Content-Type" => "application/json"], JSON.json(body))).status == 400

filehash = "adbc7420-1597-4b1b-a798-fafd9ee5f672"
ModelSelectionGUI.add_job_file(filehash, "invalid", "data.csv")
test_url = "$(url)$(filehash)"
@test (HTTP.post(test_url, ["Content-Type" => "application/json"], JSON.json(body))).status == 400

stop()
end

@testset "GET /job/:id" begin
using HTTP, JSON, Dates
id = "83ecac9e-678d-4c80-9314-0ae4a67d5ace"
filehash = "adbc7420-1597-4b1b-a798-fafd9ee5f671"
url = "$(ModelSelectionGUI.SERVER_URL):$(ModelSelectionGUI.SERVER_PORT)/job/"
url = "$(ModelSelectionGUI.SERVER_URL):$(ModelSelectionGUI.SERVER_PORT)/job/$(id)"
filename = DATA_FILENAME
tempfile = DATA_FILENAME
estimator = :ols
Expand Down Expand Up @@ -129,7 +112,7 @@ const DATA_FILENAME = "data.csv"

reset_envvars()
start(dotenv = DOTENV)
response = HTTP.get("$(url)$(id)")
response = HTTP.get(url)
body = String(response.body)
body = JSON.parse(body)

Expand Down Expand Up @@ -189,8 +172,6 @@ const DATA_FILENAME = "data.csv"
@test body[MSG] isa String
@test body[MSG] == msg

@test (HTTP.get("$(url)invalid")).status == 400

ModelSelectionGUI.clear_jobs_queue()
ModelSelectionGUI.clear_current_job()
ModelSelectionGUI.clear_jobs_finished()
Expand Down
14 changes: 7 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ using ModelSelectionGUI
using ModelSelectionGUI: ModelSelectionJob

tests = [
# "unit/test_types.jl",
# "unit/test_utils.jl",
# "unit/test_variables.jl",
# "unit/test_exceptions.jl",
# "unit/test_responses.jl",
# "integration/test_server.jl",
"unit/test_types.jl",
"unit/test_utils.jl",
"unit/test_variables.jl",
"unit/test_exceptions.jl",
"unit/test_responses.jl",
"integration/test_server.jl",
"integration/test_files.jl",
#"integration/test_jobs.jl",
"integration/test_jobs.jl",
]

for test in tests
Expand Down
3 changes: 3 additions & 0 deletions test/unit/test_responses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,8 @@ const DATA_FILENAME = "data.csv"
response = ModelSelectionGUI.job_results_response(job, :crossvalidation)
@test response.status == 200
@test Dict(response.headers)["Content-Type"] == ModelSelectionGUI.CSV_MIME

response = ModelSelectionGUI.job_results_response(job, :invalid)
@test response.status == 400
end
end

0 comments on commit db26f18

Please sign in to comment.