-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
services/app/apps/codebattle/lib/codebattle/utils/populate_tasks.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
76 changes: 76 additions & 0 deletions
76
services/app/apps/codebattle/test/codebattle/utils/populate_tasks_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |