Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/cadet/assessments/library.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ defmodule Cadet.Assessments.Library do
field(:variant, :string, default: nil)
field(:exec_time_ms, :integer, default: 1000)
field(:globals, :map, default: %{})
field(:language_options, :map, default: %{})
embeds_one(:external, ExternalLibrary, on_replace: :update)
end

@required_fields ~w(chapter)a
@optional_fields ~w(globals variant exec_time_ms)a
@optional_fields ~w(globals variant language_options exec_time_ms)a
@required_embeds ~w(external)a

def changeset(library, params \\ %{}) do
Expand Down
27 changes: 18 additions & 9 deletions lib/cadet/jobs/xml_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ defmodule Cadet.Updater.XMLParser do

@spec process_questions(String.t()) :: {:ok, [map()]} | {:error, String.t()}
defp process_questions(xml) do
default_library = xpath(xml, ~x"//TASK/DEPLOYMENT"e)
default_grading_library = xpath(xml, ~x"//TASK/GRADERDEPLOYMENT"e)
default_library = xpath(xml, ~x"//TASK/PROGRAMMINGLANGUAGE"e)
default_grading_library = xpath(xml, ~x"//TASK/GRADERPROGRAMMINGLANGUAGE"e)

questions_params =
xml
Expand Down Expand Up @@ -278,22 +278,23 @@ defmodule Cadet.Updater.XMLParser do

@spec process_question_library(map(), any(), any()) :: map() | {:error, String.t()}
defp process_question_library(question, default_library, default_grading_library) do
library = xpath(question[:entity], ~x"./DEPLOYMENT"o) || default_library
library = xpath(question[:entity], ~x"./PROGRAMMINGLANGUAGE"o) || default_library

grading_library =
xpath(question[:entity], ~x"./GRADERDEPLOYMENT"o) || default_grading_library || library
xpath(question[:entity], ~x"./GRADERPROGRAMMINGLANGUAGE"o) || default_grading_library ||
library

if library do
question
|> Map.put(:library, process_question_library(library))
|> Map.put(:grading_library, process_question_library(grading_library))
|> Map.put(:library, parse_programming_language(library))
|> Map.put(:grading_library, parse_programming_language(grading_library))
else
{:error, "Missing DEPLOYMENT"}
{:error, "Missing PROGRAMMINGLANGUAGE"}
end
end

@spec process_question_library(any()) :: map()
defp process_question_library(library_entity) do
@spec parse_programming_language(any()) :: map()
defp parse_programming_language(library_entity) do
globals =
library_entity
|> xpath(
Expand All @@ -313,6 +314,13 @@ defmodule Cadet.Updater.XMLParser do
symbols: ~x"./SYMBOL/text()"sl
)

options_list =
library_entity
|> xpath(~x"./OPTION"el, key: ~x"./@key"s, value: ~x"./@value"s)

options_map =
options_list |> Map.new(&{&1.key, &1.value})

library_entity
|> xpath(
~x"."e,
Expand All @@ -322,6 +330,7 @@ defmodule Cadet.Updater.XMLParser do
)
|> Map.put(:globals, globals)
|> Map.put(:external, external)
|> Map.put(:language_options, options_map)
end

@spec process_charlist(charlist() | nil) :: String.t() | nil
Expand Down
3 changes: 2 additions & 1 deletion lib/cadet_web/helpers/assessments_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ defmodule CadetWeb.AssessmentsHelpers do
variant: :variant,
execTimeMs: :exec_time_ms,
globals: :globals,
external: &build_external_library(%{external_library: &1.external})
external: &build_external_library(%{external_library: &1.external}),
languageOptions: :language_options
})
end

Expand Down
8 changes: 4 additions & 4 deletions test/cadet/updater/xml_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,21 @@ defmodule Cadet.Updater.XMLParserTest do
end
end

test "missing DEPLOYMENT", %{
test "missing PROGRAMMINGLANGUAGE", %{
questions: questions,
course: course,
assessments_with_config: assessments_with_config
} do
for {assessment, assessment_config} <- assessments_with_config do
xml = XMLGenerator.generate_xml_for(assessment, questions, no_deployment: true)
xml = XMLGenerator.generate_xml_for(assessment, questions, no_programminglanguage: true)

assert capture_log(fn ->
assert(
XMLParser.parse_xml(xml, course.id, assessment_config.id) ==
{:error, {:bad_request, "Missing DEPLOYMENT"}}
{:error, {:bad_request, "Missing PROGRAMMINGLANGUAGE"}}
)
end) =~
"Missing DEPLOYMENT"
"Missing PROGRAMMINGLANGUAGE"
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down Expand Up @@ -347,6 +348,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down Expand Up @@ -398,6 +400,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down Expand Up @@ -1327,6 +1330,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down Expand Up @@ -1368,6 +1372,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"content" => &1.question.question.content,
Expand Down Expand Up @@ -1419,6 +1424,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down
25 changes: 14 additions & 11 deletions test/support/xml_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ defmodule Cadet.Test.XMLGenerator do
) do
assessment_wide_library =
if opts[:library] do
process_library(opts[:library], using: &deployment/2, no_deployment: opts[:no_deployment])
process_library(opts[:library],
using: &programminglanguage/2,
no_deployment: opts[:no_programminglanguage]
)
else
[]
end
Expand All @@ -28,8 +31,8 @@ defmodule Cadet.Test.XMLGenerator do
if opts[:grading_library] do
process_library(
opts[:grading_library],
using: &graderdeployment/2,
no_deployment: opts[:no_deployment]
using: &graderprogramminglanguage/2,
no_deployment: opts[:no_programminglanguage]
)
else
[]
Expand Down Expand Up @@ -63,13 +66,13 @@ defmodule Cadet.Test.XMLGenerator do
process_question_by_question_type(question) ++
process_library(
question.library,
using: &deployment/2,
no_deployment: opts[:no_deployment]
using: &programminglanguage/2,
no_deployment: opts[:no_programminglanguage]
) ++
process_library(
question.grading_library,
using: &graderdeployment/2,
no_deployment: opts[:no_deployment]
using: &graderprogramminglanguage/2,
no_deployment: opts[:no_programminglanguage]
)
)
end
Expand Down Expand Up @@ -171,12 +174,12 @@ defmodule Cadet.Test.XMLGenerator do
{"VOTING", map_permit_keys(raw_attr, ~w(assessment_number reveal_hours token_divider)a)}
end

defp deployment(raw_attrs, children) do
{"DEPLOYMENT", map_permit_keys(raw_attrs, ~w(interpreter)a), children}
defp programminglanguage(raw_attrs, children) do
{"PROGRAMMINGLANGUAGE", map_permit_keys(raw_attrs, ~w(interpreter)a), children}
end

defp graderdeployment(raw_attrs, children) do
{"GRADERDEPLOYMENT", map_permit_keys(raw_attrs, ~w(interpreter)a), children}
defp graderprogramminglanguage(raw_attrs, children) do
{"GRADERPROGRAMMINGLANGUAGE", map_permit_keys(raw_attrs, ~w(interpreter)a), children}
end

defp external(raw_attrs, children) do
Expand Down
Loading