Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nu-hin committed Apr 1, 2014
1 parent 426b8cc commit 3bd10ca
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.sublime*
/vendor/bundle
/coverage
.coveralls.yml
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ rvm:
install: "bundle install --path vendor/bundle"
cache:
directories:
- vendor/bundle
script: "CODECLIMATE_REPO_TOKEN=93631233647d94015cf8e05386117a48a1a23106c2b990e19c96f0f462992e14 bundle exec rspec"
- vendor/bundle
script: "bundle exec rspec"

3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ gemspec

group :test do
gem 'rspec'
gem 'coveralls'
gem 'codeclimate-test-reporter', require: nil
gem 'coveralls', require: false
gem 'factory_girl', '~> 4.0'
end
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
atomic (1.1.16)
codeclimate-test-reporter (0.3.0)
simplecov (>= 0.7.1, < 1.0.0)
coveralls (0.7.0)
multi_json (~> 1.3)
rest-client
Expand Down Expand Up @@ -61,7 +59,6 @@ PLATFORMS

DEPENDENCIES
axiomus_api!
codeclimate-test-reporter
coveralls
factory_girl (~> 4.0)
rspec
24 changes: 20 additions & 4 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@

factory :dpd_address, class: AxiomusApi::DpdAddress, parent: :ems_address do
region 'обл Ростовская'

trait :without_region do
region nil
end
end

factory :region_courier_address, class: AxiomusApi::RegionCourierAddress do
Expand All @@ -103,10 +107,6 @@
end

factory :order, class: AxiomusApi::Order, parent: :base_order do
trait :incl_deliv_sum do
incl_deliv_sum rand(100.0..200.00)
end

sms {rand(1..2) ? generate(:sms) : nil}
d_date {Time.now + rand(1..10)*24*60*60}
b_time {rand(10..19)}
Expand All @@ -115,6 +115,14 @@
city 0
garden_ring {generate :boolean}
from_mkad {rand(1..2) == 2 && garden_ring !='yes' && city > 0 ? rand(1..40) : nil}

trait :incl_deliv_sum do
incl_deliv_sum rand(100.0..200.00)
end

trait :with_empty_address do
address ''
end
end

factory :carry_order, class: AxiomusApi::CarryOrder, parent: :base_order do
Expand Down Expand Up @@ -164,6 +172,10 @@
address {build(:post_address)}
services {build(:post_services)}
contacts {generate(:sms)}

trait :with_region_services do
services {build(:region_services)}
end
end

factory :ems_order, class: AxiomusApi::EmsOrder, parent: :base_order do
Expand Down Expand Up @@ -195,6 +207,10 @@
d_date {(Time.now + rand(5..15)*24*60*60).strftime('%Y-%m-%d')}
b_time {rand(10..17)}
e_time {b_time + 1}

trait :without_address do
address nil
end
end

end
26 changes: 26 additions & 0 deletions spec/lib/axiomus_api/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require_relative '../../spec_helper'

describe 'AxiomusApi::Base' do

it 'should raise on wrong xml options' do
expect do
class C < AxiomusApi::Base
xml_field :some_field, unknown_option: 21
end
end.to raise_error
end

it 'should raise if no tag name' do

class C < AxiomusApi::Base
xml_field :value
end

c = C.new
c.value = '32'

expect{c.tag_name}.to raise_error

end

end
22 changes: 22 additions & 0 deletions spec/lib/axiomus_api/serializable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require_relative '../../spec_helper'

describe 'AxiomusApi::Serializable' do

it 'should serialize value as text node' do

class Node < AxiomusApi::Base
xml_element :node
xml_field :value, xml_type: :text
xml_attribute :id
end

n = Node.new
n.id = 1
n.value = 'Some value'

expect(n.to_xml()).to eq('<node id="1">Some value</node>')

end


end
37 changes: 35 additions & 2 deletions spec/lib/axiomus_api/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
end

ORDER_MODES.each do |method|
order_class = order_factory_name(method)

describe "##{method}" do
it 'should handle successful request' do
HttpMocking.enqueue_response(DummyData.order_success_response(method))
response = @session.send(method, build(:base_order))
response = @session.send(method, build(order_class))
expect(response.code).to eq 0
end

it 'should raise on error' do
HttpMocking.enqueue_response(DummyData.order_error_response(method))
expect{@session.send(method, build(:base_order))}.to raise_error(AxiomusApi::Errors::RequestError)
expect{@session.send(method, build(order_class))}.to raise_error(AxiomusApi::Errors::RequestError)
end
end
end
Expand All @@ -44,4 +46,35 @@
end
end

it 'should accept a block' do
HttpMocking.enqueue_response(DummyData::REGIONS_SUCCESS_RESPONSE)
res = nil

AxiomusApi.test_session do |s|
res = s.get_regions
end

expect(res.regions.count).to eq 1
expect(res.regions.first.code).to eq '21'
end

describe '#send_order_request' do
it 'should raise on invalid order' do
order = build(:dpd_order, :without_address)
HttpMocking.enqueue_response(DummyData.order_success_response(:new_dpd))
expect{@session.send_order_request(:new_dpd, order)}.to raise_error(AxiomusApi::Errors::ValidationError)
end
end

describe '#labels_link' do
it 'should return correct URL' do
order_nums = []
10.times {order_nums << rand(1..10000)}
url = @session.labels_link(order_nums)
order_nums.each do |on|
expect(url).to include(on.to_s)
end
end
end

end
36 changes: 36 additions & 0 deletions spec/lib/axiomus_api/validated_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require_relative '../../spec_helper'

describe 'AxiomusApi::Validated' do

it 'should validate required fields' do
address = build(:dpd_address, :without_region)
expect(address).not_to be_valid
expect(address.validation_errors).to have(1).items
end

it 'should validate empty fields' do
order = build(:order, :with_empty_address)
expect(order).not_to be_valid
expect(order.validation_errors).to have(1).items
end

it 'should validate wrong type fields' do
order = build(:post_order, :with_region_services)
expect(order).not_to be_valid
expect(order.validation_errors).to have(1).items
end

it 'should validate nested fields' do
order = build(:dpd_order)
order.address.region = nil
expect(order).not_to be_valid
expect(order.validation_errors).to have(1).items
end

it 'should validate correct items' do
order = build(:post_order)
expect(order).to be_valid
expect(order.validation_errors).to have(0).items
end

end
4 changes: 3 additions & 1 deletion spec/lib/axiomus_api_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require_relative '../spec_helper'

describe 'AxiomusApi' do

it 'should assign logger' do
AxiomusApi.logger = Logger.new($stdout)
end
end
17 changes: 13 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require 'coveralls'
require 'codeclimate-test-reporter'
require 'factory_girl'

Coveralls.wear!
CodeClimate::TestReporter.start

require 'factory_girl'

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
Expand All @@ -13,6 +11,17 @@
:new_self_export, :update_self_export, :new_post, :update_post, :new_dpd, :update_dpd, :new_ems, :update_ems,
:new_region_courier, :update_region_courier, :new_region_pickup, :update_region_pickup]


def order_factory_name(order_mode)
match = /_(.+)$/.match(order_mode)

if match.nil?
:order
else
"#{match[1]}_order".to_sym
end
end

require_relative '../lib/axiomus_api.rb'
require_relative 'support/dummy_data.rb'
require_relative 'support/http_mocking.rb'
Expand Down
4 changes: 0 additions & 4 deletions spec/support/http_mocking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

module HttpMocking

def self.clear_response_queue
@responses = []
end

def self.enqueue_response(response_body, code = '200', msg='OK')
@responses ||= []
@responses.push([response_body, code, msg])
Expand Down

0 comments on commit 3bd10ca

Please sign in to comment.