Merge pull request #5014 from consul/machine_learning_crash

Fix crash running machine learning with no data dir
This commit is contained in:
Javi Martín
2022-10-19 11:56:40 +02:00
committed by GitHub
3 changed files with 8 additions and 11 deletions

View File

@@ -195,17 +195,24 @@ class MachineLearning
private private
def create_data_folder
FileUtils.mkdir_p DATA_FOLDER
end
def export_proposals_to_json def export_proposals_to_json
create_data_folder
filename = DATA_FOLDER.join(MachineLearning.proposals_filename) filename = DATA_FOLDER.join(MachineLearning.proposals_filename)
Proposal::Exporter.new.to_json_file(filename) Proposal::Exporter.new.to_json_file(filename)
end end
def export_budget_investments_to_json def export_budget_investments_to_json
create_data_folder
filename = DATA_FOLDER.join(MachineLearning.investments_filename) filename = DATA_FOLDER.join(MachineLearning.investments_filename)
Budget::Investment::Exporter.new(Array.new).to_json_file(filename) Budget::Investment::Exporter.new(Array.new).to_json_file(filename)
end end
def export_comments_to_json def export_comments_to_json
create_data_folder
filename = DATA_FOLDER.join(MachineLearning.comments_filename) filename = DATA_FOLDER.join(MachineLearning.comments_filename)
Comment::Exporter.new.to_json_file(filename) Comment::Exporter.new.to_json_file(filename)
end end

View File

@@ -303,9 +303,6 @@ describe MachineLearning do
describe "#export_proposals_to_json" do describe "#export_proposals_to_json" do
it "creates a JSON file with all proposals" do it "creates a JSON file with all proposals" do
require "fileutils"
FileUtils.mkdir_p Rails.root.join("public", "machine_learning", "data")
first_proposal = create(:proposal) first_proposal = create(:proposal)
last_proposal = create(:proposal) last_proposal = create(:proposal)
@@ -332,9 +329,6 @@ describe MachineLearning do
describe "#export_budget_investments_to_json" do describe "#export_budget_investments_to_json" do
it "creates a JSON file with all budget investments" do it "creates a JSON file with all budget investments" do
require "fileutils"
FileUtils.mkdir_p Rails.root.join("public", "machine_learning", "data")
first_budget_investment = create(:budget_investment) first_budget_investment = create(:budget_investment)
last_budget_investment = create(:budget_investment) last_budget_investment = create(:budget_investment)
@@ -359,9 +353,6 @@ describe MachineLearning do
describe "#export_comments_to_json" do describe "#export_comments_to_json" do
it "creates a JSON file with all comments" do it "creates a JSON file with all comments" do
require "fileutils"
FileUtils.mkdir_p Rails.root.join("public", "machine_learning", "data")
first_comment = create(:comment) first_comment = create(:comment)
last_comment = create(:comment) last_comment = create(:comment)

View File

@@ -207,8 +207,7 @@ describe "Machine learning" do
end end
scenario "Show output files info on settins page" do scenario "Show output files info on settins page" do
require "fileutils" FileUtils.mkdir_p MachineLearning::DATA_FOLDER
FileUtils.mkdir_p Rails.root.join("public", "machine_learning", "data")
allow_any_instance_of(MachineLearning).to receive(:run) do allow_any_instance_of(MachineLearning).to receive(:run) do
MachineLearningJob.first.update!(finished_at: 2.minutes.from_now) MachineLearningJob.first.update!(finished_at: 2.minutes.from_now)