From bc8aea786e698f6851063f572a3cf5e7a9abd37d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Geuken?= Date: Mon, 3 Apr 2017 12:05:23 +0200 Subject: [PATCH] [ci] Add uniqueness validation for bs request actions Using the inbuild rspec validation test helper didn't work, because of the association ot BsRequestType named 'type'. ===== 1) BsRequestAction validates uniquness of type among bs requests Failure/Error: type_to_class_name(type_name.to_sym) || super ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'an arbitrary value'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite BsRequestAction.inheritance_column to use another column for that information. # ./app/models/bs_request_action.rb:87:in `find_sti_class' # ./spec/models/bs_request_action_spec.rb:7:in `block (2 levels) in ' # ./spec/support/logging.rb:4:in `block (2 levels) in ' # ------------------ # --- Caused by: --- # NameError: # wrong constant name an arbitrary value # ./app/models/bs_request_action.rb:87:in `find_sti_class' --- src/api/spec/models/bs_request_action_spec.rb | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/api/spec/models/bs_request_action_spec.rb b/src/api/spec/models/bs_request_action_spec.rb index e599cbc22cad..eb492d3f1b02 100644 --- a/src/api/spec/models/bs_request_action_spec.rb +++ b/src/api/spec/models/bs_request_action_spec.rb @@ -1,5 +1,37 @@ require 'rails_helper' RSpec.describe BsRequestAction do + context 'uniqueness validation of type' do + let(:bs_request) { create(:bs_request) } + let(:action_attributes) { + { + bs_request: bs_request, + type: 'submit', + target_project: 'target_prj', + target_package: 'target_pkg' + } + } + let!(:bs_request_action) { create(:bs_request_action, action_attributes) } + + it { expect(bs_request_action).to be_valid } + + it 'validates uniqueness of type among bs requests, target_project and target_package' do + duplicated_bs_request_action = build(:bs_request_action, action_attributes) + expect(duplicated_bs_request_action).not_to be_valid + expect(duplicated_bs_request_action.errors.full_messages.to_sentence).to eq('Type has already been taken') + end + + RSpec.shared_examples 'it skips validation for type' do |type| + context "type '#{type}'" do + it 'allows multiple bs request actions' do + expect(build(:bs_request_action, action_attributes.merge(type: 'add_role'))).to be_valid + end + end + end + + it_should_behave_like 'it skips validation for type', 'add_role' + it_should_behave_like 'it skips validation for type', 'maintenance_incident' + end + it { should belong_to(:bs_request).touch(true) } end