From 33f7a05d3e31f6c04a2e5ee9a1f927608836a536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 19 Oct 2022 02:34:37 +0200 Subject: [PATCH 1/2] Fix crash running machine learning with no data dir The scripts crashed when the `data` folder wasn't present, which is the common situation in development environments or production environments not using Capistrano, since this folder isn't under version control. --- app/models/machine_learning.rb | 7 +++++++ spec/models/machine_learning_spec.rb | 9 --------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/models/machine_learning.rb b/app/models/machine_learning.rb index c58b8361a..76be044b8 100644 --- a/app/models/machine_learning.rb +++ b/app/models/machine_learning.rb @@ -195,17 +195,24 @@ class MachineLearning private + def create_data_folder + FileUtils.mkdir_p DATA_FOLDER + end + def export_proposals_to_json + create_data_folder filename = DATA_FOLDER.join(MachineLearning.proposals_filename) Proposal::Exporter.new.to_json_file(filename) end def export_budget_investments_to_json + create_data_folder filename = DATA_FOLDER.join(MachineLearning.investments_filename) Budget::Investment::Exporter.new(Array.new).to_json_file(filename) end def export_comments_to_json + create_data_folder filename = DATA_FOLDER.join(MachineLearning.comments_filename) Comment::Exporter.new.to_json_file(filename) end diff --git a/spec/models/machine_learning_spec.rb b/spec/models/machine_learning_spec.rb index ebbb1c607..da0788e15 100644 --- a/spec/models/machine_learning_spec.rb +++ b/spec/models/machine_learning_spec.rb @@ -303,9 +303,6 @@ describe MachineLearning do describe "#export_proposals_to_json" 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) last_proposal = create(:proposal) @@ -332,9 +329,6 @@ describe MachineLearning do describe "#export_budget_investments_to_json" 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) last_budget_investment = create(:budget_investment) @@ -359,9 +353,6 @@ describe MachineLearning do describe "#export_comments_to_json" 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) last_comment = create(:comment) From 669f1906403f6cca246b62199a1b7f3a821e6f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 19 Oct 2022 03:20:11 +0200 Subject: [PATCH 2/2] Simplify test stubbing machine learning files --- spec/system/admin/machine_learning_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/system/admin/machine_learning_spec.rb b/spec/system/admin/machine_learning_spec.rb index ebc83de6d..32a59f281 100644 --- a/spec/system/admin/machine_learning_spec.rb +++ b/spec/system/admin/machine_learning_spec.rb @@ -207,8 +207,7 @@ describe "Machine learning" do end scenario "Show output files info on settins page" do - require "fileutils" - FileUtils.mkdir_p Rails.root.join("public", "machine_learning", "data") + FileUtils.mkdir_p MachineLearning::DATA_FOLDER allow_any_instance_of(MachineLearning).to receive(:run) do MachineLearningJob.first.update!(finished_at: 2.minutes.from_now)