Refactoring: DRY up TwitterSync.preferences_cleanup spec

This commit is contained in:
Ryan Lue 2019-12-11 10:32:45 +08:00 committed by Thorsten Eckel
parent e26fe9cc50
commit b990145875

View file

@ -2,74 +2,67 @@ require 'rails_helper'
RSpec.describe TwitterSync do RSpec.describe TwitterSync do
describe '.preferences_cleanup' do describe '.preferences_cleanup' do
describe 'sanitizing Twitter preferences' do shared_examples 'for normalizing input' do
context 'when given as a bare hash' do it 'is converted (from bare hash)' do
it 'automatically adds empty hashes at :geo and :place' do expect(described_class.preferences_cleanup(raw_preferences)).to include(clean_preferences)
expect(described_class.preferences_cleanup({}))
.to eq({ geo: {}, place: {} })
end end
it 'does not modify values at :mention_ids' do it 'is converted (from article.preferences hash)' do
expect(described_class.preferences_cleanup({ mention_ids: [1_234_567_890] })) expect(described_class.preferences_cleanup(twitter: raw_preferences)).to match({ twitter: hash_including(clean_preferences) })
.to include({ mention_ids: [1_234_567_890] })
end
it 'converts geo: instance_of(Twitter::NullOjbect) to empty hash' do
expect(described_class.preferences_cleanup({ geo: Twitter::NullObject.new }))
.to include(geo: {})
end
it 'converts geo: instance_of(Twitter::Geo.new) to matching hash' do
expect(described_class.preferences_cleanup({ geo: Twitter::Geo.new(coordinates: [1, 1]) }))
.to include(geo: { coordinates: [1, 1] })
end
it 'converts place: instance_of(Twitter::NullOjbect) to empty hash' do
expect(described_class.preferences_cleanup({ place: Twitter::NullObject.new }))
.to include(place: {})
end
it 'converts place: instance_of(Twitter::Place.new) to matching hash' do
place_data = { country: 'da', name: 'do', woeid: 1, id: 1 }
expect(described_class.preferences_cleanup({ place: Twitter::Place.new(place_data) }))
.to include(place: place_data)
end end
end end
context 'when given nested in an article preferences hash' do describe ':geo key' do
it 'automatically adds empty hashes at :geo and :place' do context 'when absent' do
expect(described_class.preferences_cleanup({ twitter: {} })) let(:raw_preferences) { {} }
.to eq(twitter: { geo: {}, place: {} }) let(:clean_preferences) { { geo: {} } }
include_examples 'for normalizing input'
end end
it 'does not modify values at :mention_ids' do context 'when instance_of(Twitter::NullOjbect)' do
expect(described_class.preferences_cleanup({ twitter: { mention_ids: [1_234_567_890] } })) let(:raw_preferences) { { geo: Twitter::NullObject.new } }
.to include(twitter: hash_including(mention_ids: [1_234_567_890])) let(:clean_preferences) { { geo: {} } }
include_examples 'for normalizing input'
end end
it 'converts geo: instance_of(Twitter::NullOjbect) to empty hash' do context 'when instance_of(Twitter::Geo.new)' do
expect(described_class.preferences_cleanup({ twitter: { geo: Twitter::NullObject.new } })) let(:raw_preferences) { { geo: Twitter::Geo.new(coordinates: [1, 1]) } }
.to include(twitter: hash_including(geo: {})) let(:clean_preferences) { { geo: { coordinates: [1, 1] } } }
include_examples 'for normalizing input'
end
end end
it 'converts geo: instance_of(Twitter::Geo.new) to matching hash' do describe ':place key' do
expect(described_class.preferences_cleanup({ twitter: { geo: Twitter::Geo.new(coordinates: [1, 1]) } })) context 'when absent' do
.to include(twitter: hash_including(geo: { coordinates: [1, 1] })) let(:raw_preferences) { {} }
let(:clean_preferences) { { place: {} } }
include_examples 'for normalizing input'
end end
it 'converts place: instance_of(Twitter::NullOjbect) to empty hash' do context 'when instance_of(Twitter::NullOjbect)' do
expect(described_class.preferences_cleanup({ twitter: { place: Twitter::NullObject.new } })) let(:raw_preferences) { { place: Twitter::NullObject.new } }
.to include(twitter: hash_including(place: {})) let(:clean_preferences) { { place: {} } }
include_examples 'for normalizing input'
end end
it 'converts place: instance_of(Twitter::Place.new) to matching hash' do context 'when instance_of(Twitter::Place.new)' do
place_data = { country: 'da', name: 'do', woeid: 1, id: 1 } let(:raw_preferences) { { place: Twitter::Place.new({ country: 'da', name: 'do', woeid: 1, id: 1 }) } }
let(:clean_preferences) { { place: { country: 'da', name: 'do', woeid: 1, id: 1 } } }
expect(described_class.preferences_cleanup({ twitter: { place: Twitter::Place.new(place_data) } })) include_examples 'for normalizing input'
.to include(twitter: hash_including(place: place_data)) end
end end
end
describe ':mention_ids key' do
let(:raw_preferences) { { mention_ids: [1_234_567_890] } }
let(:clean_preferences) { { mention_ids: [1_234_567_890] } }
include_examples 'for normalizing input'
end end
end end
end end