Sha256: 04b4cf367d5a4d416d7c14bd500e2d11484a8d1bbd7cea3d5d8df216e34b0749

Contents?: true

Size: 1.74 KB

Versions: 11

Compression:

Stored size: 1.74 KB

Contents

require 'rails_helper'

describe LHS::Record do
  let(:datastore) { 'http://local.ch/v2' }

  before(:each) do
    LHC.config.placeholder(:datastore, datastore)
    class Record < LHS::Record
      endpoint '{+datastore}/content-ads/{campaign_id}/feedbacks'
      endpoint '{+datastore}/content-ads/{campaign_id}/feedbacks/{id}'
      endpoint '{+datastore}/feedbacks'
      endpoint '{+datastore}/feedbacks/{id}'
    end
  end

  context 'find_by' do
    it 'finds a single record' do
      stub_request(:get, "#{datastore}/feedbacks/z12f-3asm3ngals?limit=1")
        .to_return(status: 200, body: load_json(:feedback))
      record = Record.find_by(id: 'z12f-3asm3ngals')
      expect(record.source_id).to be_kind_of String
      expect(record).to be_kind_of Record
    end

    it 'returns nil if no record was found' do
      stub_request(:get, "#{datastore}/feedbacks/something-inexistent?limit=1")
        .to_return(status: 404)
      expect(
        Record.find_by(id: 'something-inexistent')
      ).to eq nil
    end

    it 'return first item by parameters' do
      json = load_json(:feedbacks)
      stub_request(:get, "#{datastore}/feedbacks?has_reviews=true&limit=1")
        .to_return(status: 200, body: json)
      expect(
        Record.find_by(has_reviews: true).id
      ).to eq json['items'].first['id']
    end

    it 'returns nil if empty id' do
      expect { Record.find_by(id: '') }.to raise_error LHS::Unprocessable
    end
  end

  context 'find_by!' do
    it 'raises if nothing was found with parameters' do
      stub_request(:get, "#{datastore}/feedbacks?has_reviews=true&limit=1")
        .to_return(status: 200, body: { items: [] }.to_json)
      expect { Record.find_by!(has_reviews: true) }
        .to raise_error LHC::NotFound
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
lhs-15.2.3 spec/record/find_by_spec.rb
lhs-15.2.3.pre.favorites.1 spec/record/find_by_spec.rb
lhs-15.2.2.pre.favorites.1 spec/record/find_by_spec.rb
lhs-15.2.2 spec/record/find_by_spec.rb
lhs-15.2.1 spec/record/find_by_spec.rb
lhs-15.2.0 spec/record/find_by_spec.rb
lhs-15.1.1 spec/record/find_by_spec.rb
lhs-15.1.0 spec/record/find_by_spec.rb
lhs-15.0.2 spec/record/find_by_spec.rb
lhs-15.0.1 spec/record/find_by_spec.rb
lhs-15.0.0 spec/record/find_by_spec.rb