spec/models/entity_spec.rb in maestrano-connector-rails-0.2.20 vs spec/models/entity_spec.rb in maestrano-connector-rails-0.3.0

- old
+ new

@@ -6,40 +6,11 @@ subject { Maestrano::Connector::Rails::Entity } describe 'entities_list' do it { expect(subject.entities_list).to eql(%w(entity1 entity2))} end - end - describe 'instance methods' do - subject { Maestrano::Connector::Rails::Entity.new } - - describe 'Mapper methods' do - before(:each) { - class AMapper - extend HashMapper - def self.set_organization(organization_id) - end - end - allow(subject).to receive(:mapper_class).and_return(AMapper) - } - - describe 'map_to_external' do - it 'calls the setter normalize' do - expect(AMapper).to receive(:normalize).with({}) - subject.map_to_external({}, nil) - end - end - - describe 'map_to_connec' do - it 'calls the setter denormalize' do - expect(AMapper).to receive(:denormalize).with({}) - subject.map_to_connec({}, nil) - end - end - end - # IdMap methods describe 'idmaps mehtods' do before { allow(subject).to receive(:connec_entity_name).and_return('Ab') allow(subject).to receive(:external_entity_name).and_return('Ab') @@ -59,11 +30,11 @@ let(:organization) { create(:organization) } let(:connec_entity) { {'id' => 'lala'} } before { allow(subject).to receive(:object_name_from_external_entity_hash).and_return('name_e') allow(subject).to receive(:object_name_from_connec_entity_hash).and_return('name_c') - allow(subject).to receive(:get_id_from_external_entity_hash).and_return('id') + allow(subject).to receive(:id_from_external_entity_hash).and_return('id') } it { expect(Maestrano::Connector::Rails::IdMap).to receive(:create).with(n_hash.merge(connec_id: 'lala', name: 'name_c', organization_id: organization.id)) subject.create_idmap_from_connec_entity(connec_entity, organization) @@ -71,49 +42,130 @@ it { expect(Maestrano::Connector::Rails::IdMap).to receive(:create).with(n_hash.merge(external_id: 'id', name: 'name_e', organization_id: organization.id)) subject.create_idmap_from_external_entity({}, organization) } end - end - # Connec! methods - describe 'connec_methods' do - let(:organization) { create(:organization) } - let(:client) { Maestrano::Connec::Client.new(organization.uid) } - let(:connec_name) { 'Person' } - let(:external_name) { 'external_name' } - let(:sync) { create(:synchronization) } + describe 'normalized_connec_entity_name' do before { allow(subject).to receive(:connec_entity_name).and_return(connec_name) } + context 'for a singleton resource' do + before { + allow(subject).to receive(:singleton?).and_return(true) + } - describe 'normalized_connec_entity_name' do - context 'for a singleton resource' do - before { - allow(subject).to receive(:singleton?).and_return(true) - } - + context 'for a simple name' do + let(:connec_name) { 'Person' } it { expect(subject.normalized_connec_entity_name).to eql('person') } end - context 'for a non singleton resource' do - before { - allow(subject).to receive(:singleton?).and_return(false) - } + context 'for a complex name' do + let(:connec_name) { 'Credit Note' } + it { expect(subject.normalized_connec_entity_name).to eql('credit_note') } + end + end + context 'for a non singleton resource' do + before { + allow(subject).to receive(:singleton?).and_return(false) + } + + context 'for a simple name' do + let(:connec_name) { 'Person' } it { expect(subject.normalized_connec_entity_name).to eql('people') } end + + context 'for a complex name' do + let(:connec_name) { 'Credit Note' } + it { expect(subject.normalized_connec_entity_name).to eql('credit_notes') } + end end + end + describe 'id_from_external_entity_hash' do + it { expect{ subject.id_from_external_entity_hash(nil) }.to raise_error('Not implemented') } + end + + describe 'last_update_date_from_external_entity_hash' do + it { expect{ subject.last_update_date_from_external_entity_hash(nil) }.to raise_error('Not implemented') } + end + + # Entity specific methods + describe 'singleton?' do + it 'is false by default' do + expect(subject.singleton?).to be false + end + end + + describe 'connec_entity_name' do + it { expect{ subject.connec_entity_name }.to raise_error('Not implemented') } + end + + describe 'external_entity_name' do + it { expect{ subject.external_entity_name }.to raise_error('Not implemented') } + end + + describe 'mapper_class' do + it { expect{ subject.mapper_class }.to raise_error('Not implemented') } + end + + describe 'object_name_from_connec_entity_hash' do + it { expect{ subject.object_name_from_connec_entity_hash({}) }.to raise_error('Not implemented') } + end + + describe 'object_name_from_external_entity_hash' do + it { expect{ subject.object_name_from_external_entity_hash({}) }.to raise_error('Not implemented') } + end + end + + describe 'instance methods' do + subject { Maestrano::Connector::Rails::Entity.new } + + describe 'Mapper methods' do + before(:each) { + class AMapper + extend HashMapper + end + allow(subject.class).to receive(:mapper_class).and_return(AMapper) + } + + describe 'map_to_external' do + it 'calls the mapper normalize' do + expect(AMapper).to receive(:normalize).with({}) + subject.map_to_external({}, nil) + end + end + + describe 'map_to_connec' do + it 'calls the mapper denormalize' do + expect(AMapper).to receive(:denormalize).with({}) + subject.map_to_connec({}, nil) + end + end + end + + # Connec! methods + describe 'connec_methods' do + let(:organization) { create(:organization) } + let(:client) { Maestrano::Connec::Client.new(organization.uid) } + let(:connec_name) { 'Person' } + let(:external_name) { 'external_name' } + let(:sync) { create(:synchronization) } + before { + allow(subject.class).to receive(:connec_entity_name).and_return(connec_name) + allow(subject.class).to receive(:external_entity_name).and_return(external_name) + } + describe 'get_connec_entities' do describe 'with response' do context 'for a singleton resource' do before { allow(client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {person: []}.to_json, {})) - allow(subject).to receive(:singleton?).and_return(true) + allow(subject.class).to receive(:singleton?).and_return(true) } it 'calls get with a singularize url' do expect(client).to receive(:get).with("/#{connec_name.downcase}") subject.get_connec_entities(client, nil, organization) @@ -186,11 +238,11 @@ end describe 'push_entities_to_connec_to' do let(:organization) { create(:organization) } let(:idmap1) { create(:idmap, organization: organization) } - let(:idmap2) { create(:idmap, organization: organization, connec_id: nil, connec_entity: nil, last_push_to_connec: nil) } + let(:idmap2) { create(:idmap, organization: organization, connec_id: nil, last_push_to_connec: nil) } let(:entity1) { {name: 'John'} } let(:entity2) { {name: 'Jane'} } let(:entity_with_idmap1) { {entity: entity1, idmap: idmap1} } let(:entity_with_idmap2) { {entity: entity2, idmap: idmap2} } let(:entities_with_idmaps) { [entity_with_idmap1, entity_with_idmap2] } @@ -208,13 +260,18 @@ idmap1.reload expect(idmap1.last_push_to_connec).to_not eql(old_push_date) idmap2.reload expect(idmap2.connec_id).to eql(id) - expect(idmap2.connec_entity).to eql(connec_name.downcase) expect(idmap2.last_push_to_connec).to_not be_nil end + + it 'stores an errr if any in the idmap' do + subject.push_entities_to_connec_to(client, entities_with_idmaps, '', organization) + idmap1.reload + expect(idmap1.message).to_not be nil + end end describe 'create_connec_entity' do let(:entity) { {name: 'John'} } @@ -249,15 +306,15 @@ describe 'map_to_external_with_idmap' do let(:organization) { create(:organization) } let(:id) { '765e-zer4' } let(:mapped_entity) { {'first_name' => 'John'} } before { - allow(subject).to receive(:connec_entity_name).and_return(connec_name) - allow(subject).to receive(:external_entity_name).and_return(external_name) + allow(subject.class).to receive(:connec_entity_name).and_return(connec_name) + allow(subject.class).to receive(:external_entity_name).and_return(external_name) allow(subject).to receive(:map_to_external).and_return(mapped_entity) - allow(subject).to receive(:object_name_from_connec_entity_hash).and_return('name') - allow(subject).to receive(:object_name_from_external_entity_hash).and_return('name') + allow(subject.class).to receive(:object_name_from_connec_entity_hash).and_return('name') + allow(subject.class).to receive(:object_name_from_external_entity_hash).and_return('name') } context 'when entity has an idmap' do let!(:idmap) { create(:idmap, organization: organization, external_entity: external_name.downcase, connec_entity: connec_name.downcase, connec_id: id, last_push_to_external: 3.hour.ago)} @@ -290,11 +347,11 @@ end context 'when entity has no idmap' do let(:entity) { {'id' => id, 'name' => 'John', 'updated_at' => 5.hour.ago } } before { - allow(subject).to receive(:object_name_from_connec_entity_hash).and_return('human readable stuff') + allow(subject.class).to receive(:object_name_from_connec_entity_hash).and_return('human readable stuff') } it { expect{ subject.map_to_external_with_idmap(entity, organization) }.to change{Maestrano::Connector::Rails::IdMap.count}.by(1) } it 'returns the entity with its new idmap' do @@ -327,19 +384,19 @@ it { expect{ subject.get_external_entities(nil, nil, organization) }.to raise_error('Not implemented') } end describe 'push_entities_to_external' do it 'calls push_entities_to_external_to' do - allow(subject).to receive(:external_entity_name).and_return(external_name) + allow(subject.class).to receive(:external_entity_name).and_return(external_name) expect(subject).to receive(:push_entities_to_external_to).with(nil, entities_with_idmaps, external_name, organization) subject.push_entities_to_external(nil, entities_with_idmaps, organization) end end describe 'push_entities_to_external_to' do it 'calls push_entity_to_external for each entity' do - allow(subject).to receive(:connec_entity_name).and_return(connec_name) + allow(subject.class).to receive(:connec_entity_name).and_return(connec_name) expect(subject).to receive(:push_entity_to_external).twice subject.push_entities_to_external_to(nil, entities_with_idmaps, external_name, organization) end end @@ -368,11 +425,10 @@ it 'updates the idmap external id, entity and last push' do allow(subject).to receive(:create_external_entity).and_return('999111') subject.push_entity_to_external(nil, entity_with_idmap2, external_name, organization) idmap2.reload expect(idmap2.external_id).to eql('999111') - expect(idmap2.external_entity).to eql(external_name) expect(idmap2.last_push_to_external).to_not be_nil end end end @@ -385,18 +441,10 @@ describe 'update_external_entity' do let(:organization) { create(:organization) } it { expect{ subject.update_external_entity(nil, nil, nil, nil, organization) }.to raise_error('Not implemented') } end - - describe 'get_id_from_external_entity_hash' do - it { expect{ subject.get_id_from_external_entity_hash(nil) }.to raise_error('Not implemented') } - end - - describe 'get_last_update_date_from_external_entity_hash' do - it { expect{ subject.get_last_update_date_from_external_entity_hash(nil) }.to raise_error('Not implemented') } - end end # General methods describe 'consolidate_and_map_data' do @@ -404,23 +452,23 @@ let(:external_name) { 'External_name' } let(:connec_name) { 'Connec_name' } let(:id) { '56882' } let(:date) { 2.hour.ago } before { - allow(subject).to receive(:get_id_from_external_entity_hash).and_return(id) - allow(subject).to receive(:get_last_update_date_from_external_entity_hash).and_return(date) - allow(subject).to receive(:external_entity_name).and_return(external_name) - allow(subject).to receive(:connec_entity_name).and_return(connec_name) + allow(subject.class).to receive(:id_from_external_entity_hash).and_return(id) + allow(subject.class).to receive(:last_update_date_from_external_entity_hash).and_return(date) + allow(subject.class).to receive(:external_entity_name).and_return(external_name) + allow(subject.class).to receive(:connec_entity_name).and_return(connec_name) } context 'for a singleton method' do before { - allow(subject).to receive(:singleton?).and_return(true) + allow(subject.class).to receive(:singleton?).and_return(true) allow(subject).to receive(:map_to_connec).and_return({map: 'connec'}) allow(subject).to receive(:map_to_external).and_return({map: 'external'}) - allow(subject).to receive(:object_name_from_connec_entity_hash).and_return('connec human name') - allow(subject).to receive(:object_name_from_external_entity_hash).and_return('external human name') + allow(subject.class).to receive(:object_name_from_connec_entity_hash).and_return('connec human name') + allow(subject.class).to receive(:object_name_from_external_entity_hash).and_return('external human name') } it { expect(subject.consolidate_and_map_data([], [], organization)).to eql({connec_entities: [], external_entities: []}) } context 'with no idmap' do @@ -488,11 +536,11 @@ let(:entities) { [entity] } let(:human_name) { 'alien' } before { allow(subject).to receive(:map_to_connec).and_return(mapped_entity) - allow(subject).to receive(:object_name_from_external_entity_hash).and_return(human_name) + allow(subject.class).to receive(:object_name_from_external_entity_hash).and_return(human_name) } context 'when entity has no idmap' do it 'creates an idmap and returns the mapped entity with its new idmap' do @@ -584,37 +632,9 @@ end end end - end - - - # Entity specific methods - describe 'singleton?' do - it 'is false by default' do - expect(subject.singleton?).to be false - end - end - - describe 'connec_entity_name' do - it { expect{ subject.connec_entity_name }.to raise_error('Not implemented') } - end - - describe 'external_entity_name' do - it { expect{ subject.external_entity_name }.to raise_error('Not implemented') } - end - - describe 'mapper_class' do - it { expect{ subject.mapper_class }.to raise_error('Not implemented') } - end - - describe 'object_name_from_connec_entity_hash' do - it { expect{ subject.object_name_from_connec_entity_hash({}) }.to raise_error('Not implemented') } - end - - describe 'object_name_from_external_entity_hash' do - it { expect{ subject.object_name_from_external_entity_hash({}) }.to raise_error('Not implemented') } end end end \ No newline at end of file