Add test for repository template [SCI-11708]

This commit is contained in:
Andrej 2025-04-03 09:21:11 +02:00
parent 4753bef936
commit 17837c0f8e
8 changed files with 106 additions and 3 deletions

View file

@ -2,7 +2,7 @@
class RepositoryTemplatesController < ApplicationController
before_action :check_read_permissions
before_action :load_repository_template
before_action :load_repository_template, only: :list_repository_columns
def index
repository_templates = current_team.repository_templates.order(:id)
@ -24,6 +24,7 @@ class RepositoryTemplatesController < ApplicationController
def load_repository_template
@repository_template = current_team.repository_templates.find_by(id: params[:id])
render_404 unless @repository_template
end
def check_read_permissions

View file

@ -22,13 +22,13 @@ class Repository < RepositoryBase
class_name: 'User',
inverse_of: :restored_repositories,
optional: true
belongs_to :repository_template, inverse_of: :repositories, optional: true
has_many :repository_snapshots,
class_name: 'RepositorySnapshot',
foreign_key: :parent_id,
inverse_of: :original_repository
has_many :repository_ledger_records, as: :reference, dependent: :nullify
has_many :repository_table_filters, dependent: :destroy
belongs_to :repository_template, inverse_of: :repositories, optional: true
before_save :sync_name_with_snapshots, if: :name_changed?
before_destroy :refresh_report_references_on_destroy, prepend: true

View file

@ -8,6 +8,7 @@ describe RepositoriesController, type: :controller do
let!(:user) { controller.current_user }
let!(:team) { create :team, created_by: user }
let(:action) { post :create, params: params, format: :json }
let(:repository_template) { create :repository_template, team: team }
describe 'index' do
let(:repository) { create :repository, team: team, created_by: user }
@ -29,7 +30,7 @@ describe RepositoriesController, type: :controller do
end
describe 'POST create' do
let(:params) { { repository: { name: 'My Repository' } } }
let(:params) { { repository: { name: 'My Repository', repository_template_id: repository_template.id } } }
it 'calls create activity for creating inventory' do
expect(Activities::CreateActivityService)
@ -43,6 +44,13 @@ describe RepositoriesController, type: :controller do
expect { action }
.to(change { Activity.count })
end
it 'returns success response' do
expect { action }.to change(Repository, :count).by(1)
expect(response).to have_http_status(:success)
expect(response.media_type).to eq 'application/json'
expect(Repository.order(created_at: :desc).first.repository_template).to eq repository_template
end
end
describe 'DELETE destroy' do

View file

@ -0,0 +1,49 @@
# frozen_string_literal: true
require 'rails_helper'
describe RepositoryTemplatesController, type: :controller do
login_user
let!(:user) { controller.current_user }
let!(:team) { create :team, created_by: user }
describe 'index' do
let(:action) { get :index, format: :json }
it 'correct JSON format' do
action
expect(response).to have_http_status(:success)
expect(response.media_type).to eq 'application/json'
parsed_response = JSON.parse(response.body)
expect(parsed_response['data'].count).to eq(4)
end
end
describe 'list_repository_columns' do
let(:repository_template) { team.repository_templates.last }
let(:action) { get :list_repository_columns, params: { id: repository_template } ,format: :json }
let(:action_invalid) { get :list_repository_columns, params: { id: -1 } ,format: :json }
it 'correct JSON format' do
action
expect(response).to have_http_status(:success)
expect(response.media_type).to eq 'application/json'
parsed_response = JSON.parse(response.body)
expect(parsed_response['name']).to eq(repository_template.name)
expect(parsed_response['columns'].count).to eq(repository_template.column_definitions.count)
end
it 'invalid id' do
action_invalid
expect(response).to have_http_status(:not_found)
end
end
end

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
FactoryBot.define do
factory :repository_template do
association :team
end
end

View file

@ -24,6 +24,7 @@ describe Repository, type: :model do
describe 'Relations' do
it { should belong_to :team }
it { should belong_to(:created_by).class_name('User') }
it { should belong_to(:repository_template).optional }
it { should have_many :repository_rows }
it { should have_many :repository_table_states }
it { should have_many :report_elements }

View file

@ -0,0 +1,29 @@
# frozen_string_literal: true
require 'rails_helper'
describe RepositoryTemplate, type: :model do
let(:repository_template) { build :repository_template }
it 'is valid' do
expect(repository_template).to be_valid
end
it 'should be of class Repository Template' do
expect(subject.class).to eq RepositoryTemplate
end
describe 'Database table' do
it { should have_db_column :name }
it { should have_db_column :team_id }
it { should have_db_column :column_definitions }
it { should have_db_column :predefined }
it { should have_db_column :created_at }
it { should have_db_column :updated_at }
end
describe 'Relations' do
it { should belong_to :team }
it { should have_many(:repositories).dependent(:destroy) }
end
end

View file

@ -37,6 +37,7 @@ describe Team, type: :model do
it { should have_many(:team_shared_objects).dependent(:destroy) }
it { should have_many :shared_repositories }
it { should have_many(:shareable_links).dependent(:destroy) }
it { should have_many(:repository_templates).dependent(:destroy) }
end
describe 'Validations' do
@ -87,5 +88,12 @@ describe Team, type: :model do
expect(team.shareable_links.count).to eq(0)
end
end
context 'repository templates after team create' do
it 'create repository templates after team create' do
expect_any_instance_of(Team).to receive(:create_default_repository_templates)
team.save!
end
end
end
end