Skip to content

Commit

Permalink
Merge pull request #5211 from bgeuken/tests_for_staging_projects
Browse files Browse the repository at this point in the history
Tests for staging projects
  • Loading branch information
bgeuken committed Jun 26, 2018
2 parents a2ee21e + 5134868 commit 990fea4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/api/app/models/obs_factory/staging_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@ class StagingProject
NAME_PREFIX = ":Staging:"
ADI_NAME_PREFIX = ":Staging:adi:"

def initialize(project, distribution)
self.project = project
self.distribution = distribution
end

# Find all staging projects for a given distribution
#
# @param [Boolean] only_letter only letter stagings, otherwise all stagings
# @return [Array] array of StagingProject objects
def self.for(distribution, only_letter=true)
wildcard = only_letter ? "_" : "%"
::Project.where(["name like ?", "#{distribution.root_project_name}#{NAME_PREFIX}#{wildcard}"]).map { |p| StagingProject.new(p, distribution) }
::Project.where(["name like ?", "#{distribution.root_project_name}#{NAME_PREFIX}#{wildcard}"]).
map { |project| StagingProject.new(project: project, distribution: distribution) }
end

# Find a staging project by distribution and id
Expand All @@ -34,9 +30,7 @@ def self.for(distribution, only_letter=true)
def self.find(distribution, id)
project = ::Project.find_by_name("#{distribution.root_project_name}#{NAME_PREFIX}#{id}")
if project
StagingProject.new(project, distribution)
else
nil
StagingProject.new(project: project, distribution: distribution)
end
end

Expand All @@ -58,10 +52,7 @@ def description
#
# @return [Boolean] true if the project is adi staging project
def adi_staging?
if name =~ /#{ADI_NAME_PREFIX}/
return true
end
false
/#{ADI_NAME_PREFIX}/.match?(name)
end

# Part of the name shared by all the staging projects belonging to the same
Expand Down Expand Up @@ -108,8 +99,8 @@ def obsolete_requests
def subproject
return @subprojects[0] unless @subprojects.nil?
@subprojects = []
::Project.where(["name like ?", "#{name}:%"]).map do |p|
p = StagingProject.new(p, distribution)
::Project.where(["name like ?", "#{name}:%"]).map do |project|
p = StagingProject.new(project: project, distribution: distribution)
p.parent = self
@subprojects << p
end
Expand Down
42 changes: 42 additions & 0 deletions src/api/spec/models/obs_factory/staging_project_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'rails_helper'

RSpec.describe ObsFactory::StagingProject do
let(:distribution) { ObsFactory::Distribution.new(create(:project, name: 'openSUSE:Factory')) }

describe '::find' do
subject { ObsFactory::StagingProject.find(distribution, '42') }

context 'when there is a matching project' do
let!(:project) { create(:project, name: "openSUSE:Factory:Staging:42") }

it 'returns the staging project' do
is_expected.to be_kind_of ObsFactory::StagingProject
expect(subject.name).to eq 'openSUSE:Factory:Staging:42'
expect(subject.project).to eq project
expect(subject.distribution).to eq distribution
end
end

context 'when there is no matching project' do
it { is_expected.to be_nil }
end
end

describe '#adi_staging?' do
let(:project) { create(:project, name: "openSUSE:Factory:Staging:adi:42") }

subject { ObsFactory::StagingProject.new(project: project, distribution: distribution) }

context "when the project name includes 'Staging:adi'" do
let(:project) { create(:project, name: "openSUSE:Factory:Staging:adi:42") }

it { expect(subject.adi_staging?).to be true }
end

context "when the project name does not include 'Staging:adi'" do
let(:project) { create(:project, name: "openSUSE:Factory:Staging:42") }

it { expect(subject.adi_staging?).to be false }
end
end
end

0 comments on commit 990fea4

Please sign in to comment.