Commit f5f52da8 authored by Shinya Maeda's avatar Shinya Maeda

Add spec for Releases::Source and Link models

Releases::Source and Releases::Link are covered by tests
parent 8a75453e
......@@ -31,7 +31,7 @@ class Release < ActiveRecord::Base
end
def assets_count
links.size + sources.size
links&.size.to_i + sources&.size.to_i
end
def sources
......
......@@ -21,13 +21,13 @@ module Releases
Gitlab::Routing
.url_helpers
.project_archive_url(project,
id: File.join(tag_name, archive_path),
id: File.join(tag_name, archive_prefix),
format: format)
end
private
def archive_path
def archive_prefix
"#{project.path}-#{tag_name.tr('/', '-')}"
end
end
......
......@@ -18,6 +18,7 @@
"links": {
"type": "array",
"items": {
"id": "integer",
"name": "string",
"url": "string",
"external": "boolean"
......
......@@ -10,10 +10,35 @@ RSpec.describe Release do
describe 'associations' do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:author).class_name('User') }
it { is_expected.to have_many(:links).class_name('Releases::Link') }
end
describe 'validation' do
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:description) }
end
describe '#assets_count' do
subject { release.assets_count }
it 'returns the number of sources' do
is_expected.to eq(Releases::Source::FORMATS.count)
end
context 'when a links exists' do
let!(:link) { create(:release_link, release: release) }
it 'counts the link as an asset' do
is_expected.to eq(1 + Releases::Source::FORMATS.count)
end
end
end
describe '#sources' do
subject { release.sources }
it 'returns sources' do
is_expected.to all(be_a(Releases::Source))
end
end
end
require 'rails_helper'
RSpec.describe Releases::Link do
let(:release) { create(:release, project: project) }
let(:project) { create(:project) }
describe 'associations' do
it { is_expected.to belong_to(:release) }
end
describe 'validation' do
it { is_expected.to validate_presence_of(:url) }
it { is_expected.to validate_presence_of(:name) }
context 'when url is invalid' do
let(:link) { build(:release_link, url: 'hoge') }
it 'will be invalid' do
expect(link).to be_invalid
end
end
context 'when duplicate name is added to a release' do
let!(:link) { create(:release_link, name: 'alpha', release: release) }
it 'raises an error' do
expect do
create(:release_link, name: 'alpha', release: release)
end.to raise_error(ActiveRecord::RecordInvalid)
end
end
end
describe '.sorted' do
subject { described_class.sorted }
let!(:link_1) { create(:release_link, name: 'alpha', release: release, created_at: 1.day.ago) }
let!(:link_2) { create(:release_link, name: 'beta', release: release, created_at: 2.days.ago) }
it 'returns a list of links by created_at order' do
is_expected.to eq([link_1, link_2])
end
end
describe '#internal?' do
subject { link.internal? }
let(:link) { build(:release_link, release: release, url: url) }
let(:url) { "#{project.web_url}/-/jobs/140463678/artifacts/download" }
it { is_expected.to be_truthy }
context 'when link does not include project web url' do
let(:url) { 'https://google.com/-/jobs/140463678/artifacts/download' }
it { is_expected.to be_falsy }
end
end
describe '#external?' do
subject { link.external? }
let(:link) { build(:release_link, release: release, url: url) }
let(:url) { 'https://google.com/-/jobs/140463678/artifacts/download' }
it { is_expected.to be_truthy }
end
end
require 'rails_helper'
RSpec.describe Releases::Source do
set(:project) { create(:project, :repository, name: 'finance-cal') }
let(:tag_name) { 'v1.0' }
describe '.all' do
subject { described_class.all(project, tag_name) }
it 'returns all formats of sources' do
expect(subject.map(&:format))
.to match_array(described_class::FORMATS)
end
end
describe '#url' do
subject { source.url }
let(:source) do
described_class.new(project: project, tag_name: tag_name, format: format)
end
let(:format) { 'zip' }
it 'returns zip archived source url' do
is_expected
.to eq("#{project.web_url}/-/archive/v1.0/finance-cal-v1.0.zip")
end
context 'when ref is directory structure' do
let(:tag_name) { 'beta/v1.0' }
it 'converts slash to dash' do
is_expected
.to eq("#{project.web_url}/-/archive/beta/v1.0/finance-cal-beta-v1.0.zip")
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment