spec/spigot/translator_spec.rb in spigot-0.2.0 vs spec/spigot/translator_spec.rb in spigot-0.2.1

- old
+ new

@@ -1,52 +1,52 @@ require 'spec_helper' describe Spigot::Translator do context '#initialize' do before{ Spigot::Mapping::User.basic } - it 'requires a service' do + it 'does not require a service' do expect{ - Spigot::Translator.new(nil, Struct) - }.to raise_error(Spigot::InvalidServiceError) + Spigot::Translator.new(Struct, nil) + }.to_not raise_error(Spigot::InvalidServiceError) end it 'requires a resource' do expect{ - Spigot::Translator.new(:github, nil) + Spigot::Translator.new(nil, :github) }.to raise_error(Spigot::InvalidResourceError) end it 'accepts an instance or class resource' do - by_class = Spigot::Translator.new(:github, User) - by_instance = Spigot::Translator.new(:github, User.new) + by_class = Spigot::Translator.new(User) + by_instance = Spigot::Translator.new(User.new) expect(by_class.resource).to eq(by_instance.resource) end end context '.id' do - let(:subject){ Spigot::Translator.new(:github, User.new, Spigot::Data::User.basic) } + let(:subject){ Spigot::Translator.new(User.new, :github, 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 - let(:subject){ Spigot::Translator.new(:github, User.new, Spigot::Data::User.basic) } + let(:subject){ Spigot::Translator.new(User.new, :github, 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 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) } + let(:subject){ Spigot::Translator.new(User.new, :github, 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 @@ -56,11 +56,11 @@ end end context 'with a nested map' do let(:data){ Spigot::Data::User.nested } - let(:subject){ Spigot::Translator.new(:github, User.new, data) } + let(:subject){ Spigot::Translator.new(User.new, :github, data) } before{ Spigot::Mapping::User.nested } it 'traverses into the nested hash' do expect(subject.format).to eq({ name: 'Dean Martin', @@ -70,11 +70,11 @@ end end context "nested twice" do let(:data){Spigot::Data::User.double_nested} - let(:subject){Spigot::Translator.new(:github, User.new, data)} + let(:subject){Spigot::Translator.new(User.new, :github, data)} before{ Spigot::Mapping::User.nested_twice } it 'traverses multiple levels' do expect(subject.format).to eq({ name: 'Dean Martin', @@ -85,11 +85,11 @@ end end context 'with an array of values' do let(:data){Spigot::Data::User.array} - let(:subject){Spigot::Translator.new(:github, User.new, data)} + let(:subject){Spigot::Translator.new(User.new, :github, data)} before{ Spigot::Mapping::User.basic } it 'returns an array of formatted data' do subject.format.should eq([ {name: "Dean Martin", username: "classyasfuck"}, @@ -98,11 +98,11 @@ end end context 'with a nested array of values' do let(:data){ Spigot::Data::User.nested_array } - let(:subject){Spigot::Translator.new(:github, User.new, data)} + let(:subject){Spigot::Translator.new(User.new, :github, data)} before{ Spigot::Mapping::User.nested_array } it 'handles a nested array of values' do subject.format.should eq({ name: 'Rockafella', @@ -112,11 +112,11 @@ end end context 'with a namedspaced resource' do let(:data){ Spigot::Data::Post.basic } - let(:subject){Spigot::Translator.new(:github, Wrapper::Post.new, data)} + let(:subject){Spigot::Translator.new(Wrapper::Post.new, :github, data)} before{ Spigot::Mapping::Post.basic } it 'accesses the wrapper/post key' do subject.format.should eq({ title: 'Brief Article', @@ -125,39 +125,77 @@ end end context 'with an interpolated value' do let(:data){ Spigot::Data::User.basic } - let(:subject){ Spigot::Translator.new(:github, User.new, data) } + let(:subject){ Spigot::Translator.new(User.new, :github, data) } before{ Spigot::Mapping::User.interpolated } it 'reads one layer' do expect(subject.format).to eq({name: 'Dean Martin', username: '@classyasfuck'}) end end context 'with a nested interpolated value' do let(:data){ Spigot::Data::User.nested } - let(:subject){ Spigot::Translator.new(:github, User.new, data) } + let(:subject){ Spigot::Translator.new(User.new, :github, 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 + + context 'without a service' do + let(:data){ Spigot::Data::User.basic } + let(:subject){ Spigot::Translator.new(User.new, nil, data) } + before{ Spigot::Mapping::User.serviceless } + it 'reads one layer' do + expect(subject.format).to eq({name: 'Dean Martin', username: 'classyasfuck'}) + end + + it 'does not use the any definition with an invalid service' do + subject = Spigot::Translator.new(User.new, :github, data) + expect { + subject.format + }.to raise_error(Spigot::InvalidServiceError) + end + end + + context 'multiple resources without a service' do + before{ Spigot::Mapping::User.multiple_serviceless } + it 'reads one layer' do + user = Spigot::Translator.new(User.new, nil, Spigot::Data::User.basic) + post = Spigot::Translator.new(Post.new, nil, Spigot::Data::Post.basic) + + expect(user.format).to eq({name: 'Dean Martin', username: 'classyasfuck'}) + expect(post.format).to eq({headline: 'Brief Article', body: 'lorem ipsum'}) + end + end + + context 'with and without service' do + before{ Spigot::Mapping::User.service_and_serviceless } + it 'prefers the service definition' do + service = Spigot::Translator.new(User.new, :github, Spigot::Data::User.basic) + no_service = Spigot::Translator.new(User.new, nil, Spigot::Data::User.basic) + + expect(service.format).to eq({name: 'classyasfuck', username: 'Dean Martin'}) + expect(no_service.format).to eq({name: 'Dean Martin', username: 'classyasfuck'}) + end + end end end context '#lookup' do - let(:subject){Spigot::Translator.new(:github, User.new, {a: '1'})} + let(:subject){Spigot::Translator.new(User.new, :github, {a: '1'})} it 'returns the value at a given key' do subject.lookup(:a).should eq('1') end end context '#conditions' do let(:data){ Spigot::Data::User.basic } - let(:subject){Spigot::Translator.new(:github, User.new, data)} + let(:subject){Spigot::Translator.new(User.new, :github, data)} context 'without conditions specified' do before{ Spigot::Mapping::User.basic } it 'should return a hash' do subject.conditions.should eq({'github_id' => nil}) @@ -175,10 +213,10 @@ end end end context '#options' do - let(:subject){ Spigot::Translator.new(:github, User.new, {remote_id: '987'}) } + let(:subject){ Spigot::Translator.new(User.new, :github, {remote_id: '987'}) } context 'without options provided' do before{ Spigot::Mapping::User.basic } it '#primary_key is the name of the service_id' do subject.primary_key.should eq('github_id')