spec/apitizer/helper_spec.rb in apitizer-0.0.1 vs spec/apitizer/helper_spec.rb in apitizer-0.0.2
- old
+ new
@@ -1,22 +1,22 @@
require 'spec_helper'
-describe Apitizer::Helper do
+RSpec.describe Apitizer::Helper do
extend ResourceHelper
let(:subject_module) { Apitizer::Helper }
describe '.member_action?' do
restful_member_actions.each do |action|
it "returns true for the #{ action } member action" do
- expect(subject_module.member_action?(action)).to be_true
+ expect(subject_module.member_action?(action)).to be true
end
end
restful_collection_actions.each do |action|
it "returns false for the #{ action } collection action" do
- expect(subject_module.member_action?(action)).to be_false
+ expect(subject_module.member_action?(action)).to be false
end
end
it 'raises exceptions when encounters unknown actions' do
expect { subject_module.member_action?(:rock) }.to \
@@ -32,56 +32,85 @@
eq(a: 4, b: { c: 5, d: 3, e: 6 })
end
end
describe '.build_query' do
- it 'handels ordinary parameters' do
- queries = [
- 'title=Meaning+of+Life&author=Random+Number+Generator',
- 'author=Random+Number+Generator&title=Meaning+of+Life'
- ]
+ it 'handles ordinary parameters' do
query = subject_module.build_query(
title: 'Meaning of Life', author: 'Random Number Generator')
- expect(queries).to include(query)
+ expect(query).to \
+ eq('title=Meaning+of+Life&author=Random+Number+Generator')
end
- it 'handles parameters whose values are ordinary lists' do
+ it 'handles parameters whose values are ordinary arrays' do
query = subject_module.build_query(keywords: [ 'hitchhiker', 'galaxy' ])
expect(query).to eq('keywords[]=hitchhiker&keywords[]=galaxy')
end
- it 'handles parameters whose values are object lists' do
- queries = [
- 'genres[0][name]=Comedy&genres[1][name]=Fiction',
- 'genres[1][name]=Fiction&genres[0][name]=Comedy'
- ]
+ it 'handles parameters whose values are object arrays' do
query = subject_module.build_query(
- genres: { 0 => { name: 'Comedy' }, 1 => { name: 'Fiction' } })
- expect(queries).to include(query)
+ genres: [ { name: 'Comedy' }, { name: 'Fiction' } ])
+ expect(query).to \
+ eq('genres[0][name]=Comedy&genres[1][name]=Fiction')
end
+ it 'ignores parameters whose values are empty ordinary arrays' do
+ query = subject_module.build_query(title: 'Pulp Fiction', keywords: [])
+ expect(query).to eq('title=Pulp+Fiction')
+ end
+
+ it 'ignores parameters whose values are empty object arrays' do
+ query = subject_module.build_query(title: 'Pulp Fiction', genres: {})
+ expect(query).to eq('title=Pulp+Fiction')
+ end
+
+ it 'ignores deeply nested empty structures' do
+ query = subject_module.build_query(title: 'Pulp Fiction',
+ genres: [ { :name => { :language => [ nil, nil, [], {} ] } } ])
+ expect(query).to eq('title=Pulp+Fiction')
+ end
+
it 'converts integers to decimal strings' do
query = subject_module.build_query(page: 42)
expect(query).to eq('page=42')
end
- it 'converts integers in object lists to decimal strings' do
- queries = [
- 'primes[0][value]=2&primes[1][value]=3',
- 'primes[1][value]=3&primes[0][value]=2'
- ]
+ it 'converts integers in object arrays to decimal strings' do
query = subject_module.build_query(
- primes: { 0 => { value: 2 }, 1 => { value: 3 } })
- expect(queries).to include(query)
+ primes: [ { value: 2 }, { value: 3 } ])
+ expect(query).to \
+ eq('primes[0][value]=2&primes[1][value]=3')
end
it 'converts the logical true to the string true' do
query = subject_module.build_query(published: true)
expect(query).to eq('published=true')
end
it 'converts the logical false to the string false' do
query = subject_module.build_query(published: false)
expect(query).to eq('published=false')
+ end
+
+ it 'handles an arbitrary object that responds to #to_a' do
+ object = double(to_a: [ 'one', 'two' ])
+ query = subject_module.build_query(digits: object)
+ expect(query).to eq('digits[]=one&digits[]=two')
+ end
+
+ it 'handles an arbitrary object that responds to #to_h' do
+ object = double(to_h: { one: 1, two: 2 })
+ query = subject_module.build_query(digits: object)
+ expect(query).to eq('digits[one]=1&digits[two]=2')
+ end
+
+ it 'raises an exception for an unknown objects without #to_a and #to_h' do
+ expect { subject_module.build_query(a: { b: Class.new.new }) }.to \
+ raise_error(ArgumentError)
+ end
+
+ it 'returns a string encoded in UTF-8' do
+ query = subject_module.build_query(alpha: 'omega')
+ expect(query.encoding.to_s).to match(/^UTF-8$/i)
end
end
end