Skip to content

Commit

Permalink
populate tasks from json files
Browse files Browse the repository at this point in the history
  • Loading branch information
kjubybot committed Feb 24, 2025
1 parent d981186 commit 6e99fab
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Codebattle.Utils.PopulateTasks do
@moduledoc false

def from_dir!(dir) do
dir
|> File.ls!()
|> Enum.each(&process_file(dir, &1))
end

defp process_file(dir, file) do
dir
|> Path.join(file)
|> File.read!()
|> Jason.decode!(keys: :atoms)
|> Codebattle.Task.upsert!()
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
defmodule Codebattle.Utils.PopulateTasksTest do
use Codebattle.DataCase, async: true

test "from_dir!" do
dir = Temp.mkdir!()

task1 = """
{
"name": "testtask1",
"level": "elementary",
"examples": "none",
"origin": "user",
"state": "active",
"description_ru": "",
"description_en": "none",
"tags": [],
"visibility": "public",
"input_signature": [{
"argument_name": "ping",
"type": {
"name": "string"
}
}],
"output_signature": {
"type": {
"name": "string"
}
},
"asserts": [{
"arguments": "ping",
"expected": "pong"
}]
}
"""

task2 = """
{
"name": "testtask2",
"level": "medium",
"examples": "none",
"origin": "user",
"state": "active",
"description_ru": "",
"description_en": "none",
"tags": [],
"visibility": "public",
"input_signature": [{
"argument_name": "ping",
"type": {
"name": "string"
}
}],
"output_signature": {
"type": {
"name": "string"
}
},
"asserts": [{
"arguments": "ping",
"expected": "pong"
}]
}
"""

dir
|> Path.join("task1.json")
|> File.write!(task1)

dir
|> Path.join("task2.json")
|> File.write!(task2)

assert Codebattle.Utils.PopulateTasks.from_dir!(dir) == :ok
assert %{name: "testtask2"} = Codebattle.Task |> Ecto.Query.where(name: "testtask2") |> Codebattle.Repo.one!()
end
end

0 comments on commit 6e99fab

Please sign in to comment.