use shared examples for tests

pull/2550/head
Jan Sandbrink 10 years ago
parent c1ed80f3a3
commit 3be8944c06
  1. 95
      spec/lib/api/v3/support/schema_examples.rb
  2. 49
      spec/lib/api/v3/utilities/custom_field_injector_spec.rb
  3. 70
      spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb

@ -0,0 +1,95 @@
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
shared_examples_for 'has basic schema properties' do
it 'exists' do
is_expected.to have_json_path(path)
end
it 'has a type' do
is_expected.to be_json_eql(type.to_json).at_path("#{path}/type")
end
it 'has a name' do
is_expected.to be_json_eql(name.to_json).at_path("#{path}/name")
end
it 'indicates if it is required' do
is_expected.to be_json_eql(required.to_json).at_path("#{path}/required")
end
it 'indicates if it is writable' do
is_expected.to be_json_eql(writable.to_json).at_path("#{path}/writable")
end
end
shared_examples_for 'links to allowed values directly' do
it 'has the expected number of links' do
is_expected.to have_json_size(hrefs.size).at_path("#{path}/_links/allowedValues")
end
it 'contains links to the allowed values' do
index = 0
hrefs.each do |href|
href_path = "#{path}/_links/allowedValues/#{index}/href"
is_expected.to be_json_eql(href.to_json).at_path(href_path)
index += 1
end
end
it 'has the expected number of embedded values' do
is_expected.to have_json_size(hrefs.size).at_path("#{path}/_embedded/allowedValues")
end
it 'embeds the allowed values' do
index = 0
hrefs.each do |href|
href_path = "#{path}/_embedded/allowedValues/#{index}/_links/self/href"
is_expected.to be_json_eql(href.to_json).at_path(href_path)
index += 1
end
end
end
shared_examples_for 'links to allowed values via collection link' do
it 'contains the link to the allowed values' do
is_expected.to be_json_eql(href.to_json).at_path("#{path}/_links/allowedValues/href")
end
end
shared_examples_for 'does not link to allowed values' do
it 'contains no link to the allowed values' do
is_expected.to_not have_json_path("#{path}/_links/allowedValues")
end
it 'does not embed allowed values' do
is_expected.to_not have_json_path("#{path}/_embedded/allowedValues")
end
end

@ -31,7 +31,8 @@ require 'spec_helper'
describe ::API::V3::Utilities::CustomFieldInjector do
let(:custom_field) {
FactoryGirl.build(:custom_field,
field_format: 'bool')
field_format: 'bool',
is_required: true)
}
describe 'TYPE_MAP' do
@ -43,44 +44,30 @@ describe ::API::V3::Utilities::CustomFieldInjector do
end
describe ':inject_schema' do
let(:schema_class) { Class.new(::API::Decorators::Schema) }
let(:rendered_json) { schema_class.new(nil).to_json }
let(:modified_class) { Class.new(::API::Decorators::Schema) }
let(:cf_path) { "customField#{custom_field.id}" }
subject { described_class.new(schema_class) }
let(:injector) { described_class.new(modified_class) }
subject { modified_class.new(nil).to_json }
before do
subject.inject_schema(custom_field)
injector.inject_schema(custom_field)
end
it 'injects the schema' do
expect(rendered_json).to have_json_path(cf_path)
end
it 'sets the type' do
expect(rendered_json).to be_json_eql('Boolean'.to_json).at_path("#{cf_path}/type")
end
it 'sets the name' do
expect(rendered_json).to be_json_eql(custom_field.name.to_json).at_path("#{cf_path}/name")
end
it 'marks the field as writable' do
expect(rendered_json).to be_json_eql(true.to_json).at_path("#{cf_path}/writable")
end
context 'when the custom field is required' do
let(:custom_field) { FactoryGirl.build(:custom_field, is_required: true) }
it 'marks the field as required' do
expect(rendered_json).to be_json_eql(true.to_json).at_path("#{cf_path}/required")
describe 'basic custom field' do
it_behaves_like 'has basic schema properties' do
let(:path) { cf_path }
let(:type) { 'Boolean' }
let(:name) { custom_field.name }
let(:required) { true }
let(:writable) { true }
end
end
context 'when the custom field is not required' do
let(:custom_field) { FactoryGirl.build(:custom_field, is_required: false) }
context 'when the custom field is not required' do
let(:custom_field) { FactoryGirl.build(:custom_field, is_required: false) }
it 'marks the field as required' do
expect(rendered_json).to be_json_eql(false.to_json).at_path("#{cf_path}/required")
it 'marks the field as not required' do
is_expected.to be_json_eql(false.to_json).at_path("#{cf_path}/required")
end
end
end
end

@ -48,72 +48,6 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
end
shared_examples_for 'has basic schema properties' do
it 'exists' do
is_expected.to have_json_path(path)
end
it 'has a type' do
is_expected.to be_json_eql(type.to_json).at_path("#{path}/type")
end
it 'has a name' do
is_expected.to be_json_eql(name.to_json).at_path("#{path}/name")
end
it 'indicates if it is required' do
is_expected.to be_json_eql(required.to_json).at_path("#{path}/required")
end
it 'indicates if it is writable' do
is_expected.to be_json_eql(writable.to_json).at_path("#{path}/writable")
end
end
shared_examples_for 'links to allowed values directly' do
it 'has the expected number of links' do
is_expected.to have_json_size(hrefs.size).at_path("#{path}/_links/allowedValues")
end
it 'contains links to the allowed values' do
index = 0
hrefs.each do |href|
href_path = "#{path}/_links/allowedValues/#{index}/href"
is_expected.to be_json_eql(href.to_json).at_path(href_path)
index += 1
end
end
it 'has the expected number of embedded values' do
is_expected.to have_json_size(hrefs.size).at_path("#{path}/_embedded/allowedValues")
end
it 'embeds the allowed values' do
index = 0
hrefs.each do |href|
href_path = "#{path}/_embedded/allowedValues/#{index}/_links/self/href"
is_expected.to be_json_eql(href.to_json).at_path(href_path)
index += 1
end
end
end
shared_examples_for 'links to allowed values via collection link' do
it 'contains the link to the allowed values' do
is_expected.to be_json_eql(href.to_json).at_path("#{path}/_links/allowedValues/href")
end
end
shared_examples_for 'does not link to allowed values' do
it 'contains no link to the allowed values' do
is_expected.to_not have_json_path("#{path}/_links/allowedValues")
end
it 'does not embed allowed values' do
is_expected.to_not have_json_path("#{path}/_embedded/allowedValues")
end
end
describe '_type' do
it_behaves_like 'has basic schema properties' do
let(:path) { '_type' }
@ -482,8 +416,8 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
allow(::API::V3::Utilities::CustomFieldInjector).to receive(:new).and_return(injector)
end
it 'contains the custom field' do
expect(injector).to receive(:inject_schema).with(custom_field)
it 'uses a custom field injector' do
expect(injector).to receive(:inject_schema).with(custom_field, wp_schema: schema)
representer.to_json
end
end

Loading…
Cancel
Save