From f553f3b80aa22070d2603f41f78e93e14ec2dbc1 Mon Sep 17 00:00:00 2001 From: White Rabbit Date: Mon, 19 Aug 2024 20:35:32 +0400 Subject: [PATCH] perform model version run --- .../test_model_version_runs_controller.rb | 20 +++++++++++++++++++ .../test_model_version_runs/show.html.erb | 5 ++++- app/views/test_runs/show.html.erb | 6 +++++- config/routes.rb | 4 +++- ...test_model_version_runs_controller_spec.rb | 14 +++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/app/controllers/test_model_version_runs_controller.rb b/app/controllers/test_model_version_runs_controller.rb index 68b4f0b..b87c9f0 100644 --- a/app/controllers/test_model_version_runs_controller.rb +++ b/app/controllers/test_model_version_runs_controller.rb @@ -9,6 +9,21 @@ def show @test_results = @test_model_version_run.test_results.includes(:assertion_results).decorate end + # POST /test_runs/1/test_model_version_runs/2/perform or /test_runs/1/test_model_version_runs/2/perform.json + def perform + respond_to do |format| + if @test_model_version_run.state == 'pending' + perform_all_test_model_version_run_jobs + + notice = "Test model version run was successfully performed." + format.html { redirect_to perform_test_run_test_model_version_run_path(@test_run, @test_model_version_run), notice: } + format.json { render :show, status: :created } + else + # TODO: render errors + end + end + end + private # Use callbacks to share common setup or constraints between actions. @@ -19,4 +34,9 @@ def set_test_model_version_run def set_test_run @test_run = current_user.test_runs.find(params[:test_run_id]) end + + def perform_all_test_model_version_run_jobs + jobs = Array.new(@test_run.calls) { TestModelVersionRunJob.new(@test_model_version_run.id) } + ActiveJob.perform_all_later(jobs) + end end diff --git a/app/views/test_model_version_runs/show.html.erb b/app/views/test_model_version_runs/show.html.erb index 2817b98..e6cd5dc 100644 --- a/app/views/test_model_version_runs/show.html.erb +++ b/app/views/test_model_version_runs/show.html.erb @@ -5,7 +5,10 @@ <%= render @test_model_version_run %> - <%= link_to "Back to test run", test_run_path(@test_run), class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium w-56" %> +
+ <%= button_to 'Run', perform_test_run_test_model_version_run_path(@test_run, @test_model_version_run), method: :post, class: 'rounded-lg py-3 px-5 bg-blue-500 text-white font-medium mr-2' %> + <%= button_to "Back to test run", test_run_path(@test_run), class: "rounded-lg py-3 px-5 bg-gray-100 font-medium" %> +

Test Results

diff --git a/app/views/test_runs/show.html.erb b/app/views/test_runs/show.html.erb index c29cdbb..4036712 100644 --- a/app/views/test_runs/show.html.erb +++ b/app/views/test_runs/show.html.erb @@ -18,6 +18,7 @@ Model Version State Created at + <% if @test_model_version_runs.empty? %> - +

No model versions runs found.

@@ -52,6 +53,9 @@ <%= time_ago_in_words(test_model_version_run.created_at) %> ago + + <%= button_to 'Run', perform_test_run_test_model_version_run_path(@test_run, test_model_version_run), method: :post, class: 'bg-blue-500 text-white font-bold py-2 px-4 rounded' %> + <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index f57cb9f..f414c20 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,9 @@ end resources :test_runs, except: %i[edit update destroy] do - resources :test_model_version_runs, only: %i[show] + resources :test_model_version_runs, only: %i[show] do + post :perform, on: :member + end end resources :compare, only: [:index] do diff --git a/spec/controllers/test_model_version_runs_controller_spec.rb b/spec/controllers/test_model_version_runs_controller_spec.rb index cba07fd..be06c6b 100644 --- a/spec/controllers/test_model_version_runs_controller_spec.rb +++ b/spec/controllers/test_model_version_runs_controller_spec.rb @@ -32,4 +32,18 @@ expect(assigns(:test_run)).to eq(test_run) end end + + describe 'POST #perform' do + subject(:perform!) { post :run, params: { test_run_id: test_run.id, id: test_model_version_run.id } } + + before do + allow(ActiveJob).to receive(:perform_all_later) + end + + context 'with pending test_model_version_run' do + it 'enqueues a TestModelVersionRunJob' do + expect { perform! }.to change(TestModelVersionRunJob, :count).by(3) + end + end + end end