spec/spigot/translator_spec.rb in spigot-0.1.0 vs spec/spigot/translator_spec.rb in spigot-0.2.0
- old
+ new
@@ -1,10 +1,10 @@
require 'spec_helper'
describe Spigot::Translator do
-
context '#initialize' do
+ before{ Spigot::Mapping::User.basic }
it 'requires a service' do
expect{
Spigot::Translator.new(nil, Struct)
}.to raise_error(Spigot::InvalidServiceError)
end
@@ -20,113 +20,129 @@
by_instance = Spigot::Translator.new(:github, User.new)
expect(by_class.resource).to eq(by_instance.resource)
end
end
- context '#format' do
- let(:subject){Spigot::Translator.new(:github, User.new, {a: '1'})}
-
- context 'with a missing map' do
- it 'raises error with a missing file' do
- expect {
- subject.format
- }.to raise_error(Spigot::MissingServiceError)
- end
+ context '.id' do
+ let(:subject){ Spigot::Translator.new(:github, User.new, Spigot::Data::User.basic) }
+ before{ Spigot::Mapping::User.basic }
+ it 'returns the value at the foreign_key' do
+ subject.stubs(:foreign_key).returns('id')
+ subject.id.should eq('123')
end
+ end
+ context '.format' do
context 'with a missing resource map' do
- before do
- subject.stubs(:translation_file).returns("missing:\n foo: 'bar'")
- end
-
+ let(:subject){ Spigot::Translator.new(:github, User.new, Spigot::Data::User.basic) }
it 'raises error with a missing resource map' do
expect{
subject.format
}.to raise_error(Spigot::MissingResourceError)
end
end
- context 'with a symbol keyed map' do
- let(:subject){Spigot::Translator.new(:github, User.new, Spigot::ApiData.basic_user)}
+ context 'with a valid resource map' do
+ context 'with a simple map' do
+ let(:data){ Spigot::Data::User.basic }
+ let(:subject){ Spigot::Translator.new(:github, User.new, data) }
+ before{ Spigot::Mapping::User.basic }
+ it 'returns empty hash from nil data' do
+ subject.data = {}
+ expect(subject.format).to eq({name: nil, username: nil})
+ end
- context 'with a basic mapping' do
- with_mapping(:basic_user, Spigot::Mapping::User.symbolized)
-
it 'reads one layer' do
- expect(subject.format).to eq({'name' => 'Dean Martin', 'username' => 'classyasfuck'})
+ expect(subject.format).to eq({name: 'Dean Martin', username: 'classyasfuck'})
end
end
- end
- context 'and a valid resource map' do
- with_mapping(:basic_user, Spigot::Mapping::User.basic)
+ context 'with a nested map' do
+ let(:data){ Spigot::Data::User.nested }
+ let(:subject){ Spigot::Translator.new(:github, User.new, data) }
+ before{ Spigot::Mapping::User.nested }
- it 'returns empty hash from nil data' do
- expect(subject.format).to eq({})
- end
- end
-
- context 'with basic user data' do
- let(:subject){Spigot::Translator.new(:github, User.new, Spigot::ApiData.basic_user)}
-
- context 'with a basic mapping' do
- with_mapping(:basic_user, Spigot::Mapping::User.basic)
-
- it 'reads one layer' do
- expect(subject.format).to eq({'name' => 'Dean Martin', 'username' => 'classyasfuck'})
+ it 'traverses into the nested hash' do
+ expect(subject.format).to eq({
+ name: 'Dean Martin',
+ username: 'classyasfuck',
+ contact: 'dino@amore.io'
+ })
end
end
- context 'with a mapping containing nested data' do
- let(:subject){Spigot::Translator.new(:github, User.new, Spigot::ApiData.nested_user)}
- with_mapping(:basic_user, Spigot::Mapping::User.nested)
+ context "nested twice" do
+ let(:data){Spigot::Data::User.double_nested}
+ let(:subject){Spigot::Translator.new(:github, User.new, data)}
+ before{ Spigot::Mapping::User.nested_twice }
- it 'traverses into the nested hash' do
+ it 'traverses multiple levels' do
expect(subject.format).to eq({
- 'name' => 'Dean Martin',
- 'username' => 'classyasfuck',
- 'contact' => 'dino@amore.io'
+ name: 'Dean Martin',
+ ip: "127.0.0.1",
+ username: 'classyasfuck',
+ contact: 'dino@amore.io'
})
end
+ end
- context 'twice' do
- with_mapping(:basic_user, Spigot::Mapping::User.nested_twice)
- let(:subject){Spigot::Translator.new(:github, User.new, Spigot::ApiData.double_nested_user)}
+ context 'with an array of values' do
+ let(:data){Spigot::Data::User.array}
+ let(:subject){Spigot::Translator.new(:github, User.new, data)}
+ before{ Spigot::Mapping::User.basic }
- it 'traverses multiple levels' do
- expect(subject.format.values).to include(*['Dean Martin','classyasfuck','dino@amore.io'])
- end
+ it 'returns an array of formatted data' do
+ subject.format.should eq([
+ {name: "Dean Martin", username: "classyasfuck"},
+ {name: "Frank Sinatra", username: "livetilidie"}
+ ])
end
end
- end
- context 'with an array of values' do
- let(:subject){Spigot::Translator.new(:github, User.new, Spigot::ApiData.user_array)}
- with_mapping(:basic_user, Spigot::Mapping::User.basic)
+ context 'with a nested array of values' do
+ let(:data){ Spigot::Data::User.nested_array }
+ let(:subject){Spigot::Translator.new(:github, User.new, data)}
+ before{ Spigot::Mapping::User.nested_array }
- it 'returns an array of formatted data' do
- expect(subject.format.length).to eq(2)
- expect(subject.format.map{|u| u['name']}).to include('Dean Martin', 'Frank Sinatra')
+ it 'handles a nested array of values' do
+ subject.format.should eq({
+ name: 'Rockafella',
+ user_count: 2,
+ users: [{name: "Dean Martin", username: "classyasfuck"}, {name: "Frank Sinatra", username: "livetilidie"}]
+ })
+ end
end
- end
- context 'with a nested array of values' do
- let(:subject){Spigot::Translator.new(:github, User.new, Spigot::ApiData.nested_user_array)}
- with_mapping(:basic_user, Spigot::Mapping::User.nested_array)
+ context 'with a namedspaced resource' do
+ let(:data){ Spigot::Data::Post.basic }
+ let(:subject){Spigot::Translator.new(:github, Wrapper::Post.new, data)}
+ before{ Spigot::Mapping::Post.basic }
- it 'handles a nested array of values' do
- expect(subject.format.keys).to include('name', 'users', 'user_count')
- expect(subject.format['users']).to be_a(Array)
+ it 'accesses the wrapper/post key' do
+ subject.format.should eq({
+ title: 'Brief Article',
+ description: 'lorem ipsum'
+ })
+ end
end
- end
- context 'with a namedspaced resource' do
- let(:subject){Spigot::Translator.new(:github, Wrapper::Post.new, Spigot::ApiData.post)}
- with_mapping(:namespaced_post, Spigot::Mapping::Post.namespaced)
+ context 'with an interpolated value' do
+ let(:data){ Spigot::Data::User.basic }
+ let(:subject){ Spigot::Translator.new(:github, User.new, data) }
+ before{ Spigot::Mapping::User.interpolated }
+ it 'reads one layer' do
+ expect(subject.format).to eq({name: 'Dean Martin', username: '@classyasfuck'})
+ end
+ end
- it 'accesses the wrapper/post key' do
- expect(subject.format).to eq({'title' => 'Regular Article', 'description' => 'dolor sit amet'})
+ context 'with a nested interpolated value' do
+ let(:data){ Spigot::Data::User.nested }
+ let(:subject){ Spigot::Translator.new(:github, User.new, data) }
+ before{ Spigot::Mapping::User.nested_interpolation }
+ it 'reads one layer' do
+ expect(subject.format).to eq({name: 'Dean Martin', contact: 'dino@amore.io', username: '@classyasfuck'})
+ end
end
end
end
context '#lookup' do
@@ -135,77 +151,58 @@
it 'returns the value at a given key' do
subject.lookup(:a).should eq('1')
end
end
- context '#id' do
- let(:subject){Spigot::Translator.new(:github, User.new, {id: '123'})}
-
- it 'returns the value at the foreign_key' do
- subject.stubs(:foreign_key).returns('id')
- subject.id.should eq('123')
- end
- end
-
context '#conditions' do
- let(:subject){Spigot::Translator.new(:github, User.new, Spigot::ApiData.user)}
- with_mapping(:user_with_conditions, Spigot::Mapping::User.with_options)
+ let(:data){ Spigot::Data::User.basic }
+ let(:subject){Spigot::Translator.new(:github, User.new, data)}
- it 'should return a hash' do
- previous_translations = Spigot.config.translations
- Spigot.configure{|c| c.translations = Spigot::Mapping::User.with_options }
- subject.conditions.should eq({"username"=>"classyasfuck"})
- Spigot.configure{|c| c.translations = previous_translations }
+ context 'without conditions specified' do
+ before{ Spigot::Mapping::User.basic }
+ it 'should return a hash' do
+ subject.conditions.should eq({'github_id' => nil})
+ end
end
context 'with conditions specified' do
- with_mapping(:user_with_conditions, Spigot::Mapping::User.with_conditions)
-
+ before{ Spigot::Mapping::User.with_conditions }
it 'can specify the keys used in the map options' do
- subject.conditions.should eq({"username"=>"classyasfuck", "name"=>"Dean Martin"})
+ subject.conditions.should eq({username: "classyasfuck"})
end
it 'can specify only one key' do
- subject.conditions.should eq({"username"=>"classyasfuck", "name"=>"Dean Martin"})
+ subject.conditions.should eq({username: "classyasfuck"})
end
end
end
context '#options' do
- let(:subject){Spigot::Translator.new(:github, User.new, {remote_id: '987'})}
+ let(:subject){ Spigot::Translator.new(:github, User.new, {remote_id: '987'}) }
context 'without options provided' do
- with_mapping(:basic_user, Spigot::Mapping::User.basic)
+ before{ Spigot::Mapping::User.basic }
+ it '#primary_key is the name of the service_id' do
+ subject.primary_key.should eq('github_id')
+ end
- context 'defaults' do
- it '#primary_key is the name of the service _id' do
- subject.primary_key.should eq('github_id')
- end
-
- it '#foreign_key is id' do
- subject.foreign_key.should eq('id')
- end
+ it '#foreign_key is id' do
+ subject.foreign_key.should eq('id')
end
end
context 'with options provided' do
- with_mapping(:user_with_options, Spigot::Mapping::User.with_options)
-
- it 'reads the options from the spigot key' do
- subject.options.should eq(Spigot::Mapping::User.with_options['user']['spigot'])
+ before{ Spigot::Mapping::User.with_options }
+ it 'reads a primary key from the mapping' do
+ subject.primary_key.should eq(:username)
end
- context '#primary_key' do
- it 'reads a primary key from the mapping' do
- subject.primary_key.should eq('username')
- end
+ it 'reads a foreign key from the mapping' do
+ subject.foreign_key.should eq(:login)
end
- context '#foreign_key' do
- it 'reads a foreign key from the mapping' do
- subject.foreign_key.should eq('login')
- end
+ it 'reads options' do
+ subject.options.should be_a_kind_of(Spigot::Map::Option)
end
end
end
-
end