spec/attune/client_spec.rb in attune-1.0.3 vs spec/attune/client_spec.rb in attune-1.0.5

- old
+ new

@@ -24,27 +24,27 @@ end end describe "API errors" do it "will raise timeout" do - stubs.post("anonymous", %[{"user_agent":"Mozilla/5.0"}]){ raise Faraday::Error::TimeoutError.new("test") } + stubs.post("anonymous", nil){ raise Faraday::Error::TimeoutError.new("test") } expect { - client.create_anonymous(user_agent: 'Mozilla/5.0') + client.anonymous.create }.to raise_exception(Faraday::Error::TimeoutError) stubs.verify_stubbed_calls end it "will raise ConnectionFailed" do - stubs.post("anonymous", %[{"user_agent":"Mozilla/5.0"}]){ raise Faraday::Error::ConnectionFailed.new("test") } + stubs.post("anonymous", nil){ raise Faraday::Error::ConnectionFailed.new("test") } expect { - client.create_anonymous(user_agent: 'Mozilla/5.0') + client.anonymous.create }.to raise_exception(Faraday::Error::ConnectionFailed) stubs.verify_stubbed_calls end it "will raise ConnectionFailed on Errno::ENOENT" do - stubs.post("anonymous", %[{"user_agent":"Mozilla/5.0"}]){ raise Errno::ENOENT.new("test") } + stubs.post("anonymous", nil){ raise Errno::ENOENT.new("test") } expect { - client.create_anonymous(user_agent: 'Mozilla/5.0') + client.anonymous.create }.to raise_exception(Faraday::Error::ConnectionFailed) end it "will raise AuthenticationException" do stubs.post("oauth/token", {:client_id=>"id", :client_secret=>"secret", grant_type: :client_credentials} @@ -59,11 +59,11 @@ describe "disabled" do context "with raise" do let(:options){ {disabled: true, exception_handler: :raise} } it "will raise DisalbedException" do expect { - client.create_anonymous(user_agent: 'Mozilla/5.0') + client.anonymous.create }.to raise_exception(Attune::DisabledException) end end context "with mock" do let(:options){ {disabled: true, exception_handler: :mock} } @@ -71,15 +71,15 @@ it "mocks get_auth_token" do result = client.get_auth_token("id", "secret") expect(result).to match(/^[a-z0-9\-]+$/) end it "mocks create_anonymous with an id" do - result = client.create_anonymous(id: '12345', user_agent: 'Mozilla/5.0') + result = client.anonymous.create(id: '12345', user_agent: 'Mozilla/5.0') expect(result).to eq('12345') end it "mocks create_anonymous with no id" do - result = client.create_anonymous(user_agent: 'Mozilla/5.0') + result = client.anonymous.create(user_agent: 'Mozilla/5.0') expect(result).to match(/^[a-z0-9\-]+$/) end describe "mocks get_rankings" do let(:entities) { %w[1001 1002 1003 1004] } let(:expected) do @@ -88,11 +88,11 @@ entities: entities.map { |e| e.to_s } } end before(:each) do - @result = client.get_rankings( + @result = client.entities.get_rankings( id: 'abcd123', view: 'b/mens-pants', collection: 'products', entities: entities ) @@ -155,106 +155,98 @@ expect(token).to eq('secret-token') end it "can create_anonymous generating an id" do - stubs.post("anonymous", %[{"user_agent":"Mozilla/5.0"}]){ [200, {location: 'urn:id:abcd123'}, nil] } - id = client.create_anonymous(user_agent: 'Mozilla/5.0') + stubs.post("anonymous", nil){ [200, {location: 'urn:id:abcd123'}, %[{"id": "abcd123"}]] } + result = client.anonymous.create stubs.verify_stubbed_calls - expect(id).to eq('abcd123') + expect(result.id).to eq('abcd123') end it "can bind" do - stubs.put("bindings/anonymous=abcd123&customer=foobar"){ [200, {}, nil] } - client.bind('abcd123', 'foobar') + stubs.put("anonymous/abcd123", %[{"customer":"foobar"}]){ [200, {}, nil] } + client.anonymous.update('abcd123', Attune::Model::Customer.new(customer:'foobar')) stubs.verify_stubbed_calls end - it "can create_anonymous using existing id" do - stubs.put("anonymous/abcd123", %[{"user_agent":"Mozilla/5.0"}]){ [200, {}, nil] } - id = client.create_anonymous(id: 'abcd123', user_agent: 'Mozilla/5.0') + it "get anonymous using existing id" do + stubs.get("anonymous/abcd123"){ [200, {}, %[{"customer":"foobar"}]] } + response = client.anonymous.get('abcd123') stubs.verify_stubbed_calls - expect(id).to eq('abcd123') + expect(response.customer).to eq('foobar') end describe "get_rankings" do before(:each) do - stubs.get("rankings/anonymous=abcd123&entities=1001%2C%2C1002%2C%2C1003%2C%2C1004&entity_collection=products&ip=none&view=b%2Fmens-pants"){ [200, {"attune-ranking"=>"test", "attune-cell"=>"test"}, %[{"ranking":["1004","1003","1002","1001"]}]] } - @rankings = client.get_rankings( - id: 'abcd123', + ranking_request = { + anonymous: 'abcd123', view: 'b/mens-pants', - collection: 'products', - entities: %w[1001, 1002, 1003, 1004] - ) + entity_type: 'products', + ids: %w[1001, 1002, 1003, 1004] + } + stubs.post("entities/ranking", ranking_request.to_json){ [200, nil, %[{"ranking":["1004","1003","1002","1001"]}]] } + @rankings = client.entities.get_rankings(ranking_request) stubs.verify_stubbed_calls end it "can get ranked entities" do - expect(@rankings[:entities]).to eq(%W[1004 1003 1002 1001]) + expect(@rankings.ranking).to eq(%W[1004 1003 1002 1001]) end - - it "can get ranking headers" do - expect(@rankings[:headers]).to eq({"attune-ranking"=>"test", "attune-cell"=>"test"}) - end end describe "multi_get_rankings" do - let(:req1){ CGI::escape 'anonymous=0cddbc0-6114-11e3-949a-0800200c9a66&view=b%2Fmens-pants&entity_collection=products&entities=1001%2C%2C1002%2C%2C1003%2C%2C1004&ip=none' } - let(:req2){ CGI::escape 'anonymous=0cddbc0-6114-11e3-949a-0800200c9a66&view=b%2Fmens-pants&entity_collection=products&entities=2001%2C%2C2002%2C%2C2003%2C%2C2004&ip=none' } - before(:each) do - stubs.get("/rankings?ids=anonymous%3D0cddbc0-6114-11e3-949a-0800200c9a66%26entities%3D1001%252C%252C1002%252C%252C1003%252C%252C1004%26entity_collection%3Dproducts%26ip%3Dnone%26view%3Db%252Fmens-pants&ids=anonymous%3D0cddbc0-6114-11e3-949a-0800200c9a66%26entities%3D2001%252C%252C2002%252C%252C2003%252C%252C2004%26entity_collection%3Dproducts%26ip%3Dnone%26view%3Db%252Fmens-pants") do - [200, {"attune-ranking"=>"test", "attune-cell"=>"test"}, <<-JSON] + batch_request = {requests: [ + { + anonymous: '0cddbc0-6114-11e3-949a-0800200c9a66', + view: 'b/mens-pants', + entity_type: 'products', + ids: %w[1001, 1002, 1003, 1004] + }, + { + anonymous: '0cddbc0-6114-11e3-949a-0800200c9a66', + view: 'b/mens-pants', + entity_type: 'products', + ids: %w[2001, 2002, 2003, 2004] + } + ]} + stubs.post("entities/ranking/many", batch_request.to_json) do + [200, nil, <<-JSON] { - "errors": {}, - "results": { - "anonymous=0cddbc0-6114-11e3-949a-0800200c9a66&entities=1001%2C%2C1002%2C%2C1003%2C%2C1004&entity_collection=products&ip=none&view=b%2Fmens-pants": { + "results": [ + { "ranking": [ "1004", "1003", "1002", "1001" ] }, - "anonymous=0cddbc0-6114-11e3-949a-0800200c9a66&entities=2001%2C%2C2002%2C%2C2003%2C%2C2004&entity_collection=products&ip=none&view=b%2Fmens-pants": { + { "ranking": [ "2004", "2003", "2002", "2001" ] } - } + ] } JSON end - @rankings = client.multi_get_rankings([ - { - id: '0cddbc0-6114-11e3-949a-0800200c9a66', - view: 'b/mens-pants', - collection: 'products', - entities: %w[1001, 1002, 1003, 1004] - }, - { - id: '0cddbc0-6114-11e3-949a-0800200c9a66', - view: 'b/mens-pants', - collection: 'products', - entities: %w[2001, 2002, 2003, 2004] - } - ]) + @results = client.entities.batch_get_rankings(batch_request) stubs.verify_stubbed_calls end it "can get ranked entities" do - expect(@rankings[:entities]).to eq [ + rankings = @results.results.map {|r| r.ranking } + expect(rankings).to eq [ %W[1004 1003 1002 1001], %W[2004 2003 2002 2001] ] end - - it "can get ranking headers" do - expect(@rankings[:headers]).to eq({"attune-ranking"=>"test", "attune-cell"=>"test"}) - end end end +