Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Commit

Permalink
more conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
erniebrodeur committed Nov 16, 2018
1 parent 2de9db4 commit e682400
Show file tree
Hide file tree
Showing 22 changed files with 237 additions and 376 deletions.
2 changes: 1 addition & 1 deletion lib/cts/mpx/driver/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def raise_unless_reference?(argument)
# @raise [ArgumentError] if the keyword is not suppplied
# @return [nil]
def raise_unless_required_keyword?(keyword: nil)
raise ArgumentError, "#{keyword} is a required keyword." unless keyword && keyword
raise ArgumentError, "#{keyword} is a required keyword." unless keyword
end
end
end
Expand Down
13 changes: 0 additions & 13 deletions lib/cts/mpx/entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ class Entries

attribute name: 'collection', kind_of: Array

# Create a new entries collection from a page
# @param [Page] page the page object to process
# @raise [ArgumentError] if :page is not available
# @return [Entries] a new entries collection
def self.create_from_page(page)
Exceptions.raise_unless_argument_error? page, Page
entries = page.entries.each do |e|
entry = Entry.create(fields: Fields.create_from_data(data: e, xmlns: page.xmlns))
entry.id = entry.fields['id'] if entry.fields['id']
end
Entries.create(collection: entries)
end

# Addressable method, indexed by entry object
# @param [Entry] key the entry to return
# @return [Self.collection,Entry,nil] Can return the collection, a single entry, or nil if nothing found
Expand Down
2 changes: 1 addition & 1 deletion lib/cts/mpx/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def initialize
# List of entries created from the page
# @return [Entries] populated Entries object
def entries
Entries.create_from_page @page
@page.to_mpx_entries
end

# Run the query
Expand Down
7 changes: 4 additions & 3 deletions spec/cts/mpx/driver/assemblers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Cts
module Mpx
module Driver
describe Assemblers do
include_context "with web parameters"

let(:user) { User.create username: 'a', password: 'a', token: 'token' }

describe "Module method signatures" do
Expand All @@ -26,7 +28,6 @@ module Driver
before { Registry.initialize }

it { is_expected.to require_keyword_arguments(:host, params) }

it { expect { described_class.host(params) }.to raise_error_without_user_token(params[:user]) }

context "when the service reference does not present a url" do
Expand All @@ -41,7 +42,7 @@ module Driver
end

describe "::path" do
let(:params) { { service: Parameters.web_service, endpoint: Parameters.web_endpoint } }
let(:params) { { service: web_service, endpoint: web_endpoint } }

it { is_expected.to require_keyword_arguments(:path, params) }

Expand Down Expand Up @@ -84,7 +85,7 @@ module Driver
end

describe "::query" do
let(:params) { { user: user, service: Parameters.web_service, endpoint: Parameters.web_endpoint } }
let(:params) { { user: user, service: web_service, endpoint: web_endpoint } }

it { expect { described_class.query(params) }.to raise_error_without_user_token(params[:user]) }
it { is_expected.to require_keyword_arguments(:query, params) }
Expand Down
3 changes: 2 additions & 1 deletion spec/cts/mpx/driver/connections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Cts
module Mpx
module Driver
describe Connections do
let(:uri) { Parameters.account_id }
include_context "with basic parameters"
let(:uri) { account_id }
let(:connection) { Excon.new uri }

before do
Expand Down
4 changes: 2 additions & 2 deletions spec/cts/mpx/driver/exceptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Driver
end

it "is expected to return nil" do
expect(described_class.raise_unless_account_id(Parameters.account_id)).to be nil
expect(described_class.raise_unless_account_id(account_id)).to be nil
end
end

Expand Down Expand Up @@ -52,7 +52,7 @@ module Driver
end

it "is expected to return nil" do
expect(described_class.raise_unless_reference?(Parameters.account_id)).to be nil
expect(described_class.raise_unless_reference?(account_id)).to be nil
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/cts/mpx/driver/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module Driver

describe '#call' do
let(:request_method) { :get }
let(:request) { Request.create method: request_method, url: Parameters.account_id }
let(:socket) { Excon.new Parameters.account_id }
let(:request) { Request.create method: request_method, url: account_id }
let(:socket) { Excon.new account_id }
let(:excon_response) { Excon::Response.new }

before do
Expand Down Expand Up @@ -60,7 +60,7 @@ module Driver
it "is expected to call Connections[] for a connection" do
allow(Connections).to receive(:[]).and_call_original
request.call
expect(Connections).to have_received(:[]).with Parameters.account_id
expect(Connections).to have_received(:[]).with account_id
end

it "is expected to call connection with the provided method" do
Expand Down
46 changes: 3 additions & 43 deletions spec/cts/mpx/driver/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,7 @@ module Cts
module Mpx
module Driver
describe Response do
let(:response) { Cts::Mpx::Driver::Response.create original: excon_response }
let(:excon_response) { Excon::Response.new body: body, headers: headers, status: 200 }
let(:headers) do
{ "Access-Control-Allow-Origin" => "*",
"X-Cache" => "MISS from data.media.theplatform.com:80",
"Cache-Control" => "max-age=0",
"Date" => "Tue, 15 May 2018 00:25:24 GMT",
"Content-Type" => "application/json; charset=UTF-8",
"Content-Length" => "2050",
"Server" => "Jetty(8.1.16.2)" }
end
let(:service_exception) do
{
"title" => "ObjectNotFoundException",
"description" => "Could not find object with ID q",
"isException" => true,
"responseCode" => 404,
"correlationId" => "9a23ea0a-0975-4633-9af0-5f7fe3e83f2b",
"serverStackTrace" => "com.theplatform.data.api.exception.ObjectNotFoundException: ..."
}
end

let(:body) { Oj.dump page_hash }
let(:page_hash) do
{
"$xmlns" => {
"pl1" => "http://access.auth.theplatform.com/account/data/Account/2051509925"
},
"startIndex" => 1,
"itemsPerPage" => 1,
"entryCount" => 1,
"entries" => [
{
"id" => "http://data.media.theplatform.com/media/data/AccountSettings/357280835864",
"guid" => "cxotgIy9yxyqcr6ccO8DCe56lDkbqTSe",
"added" => 1_415_856_204_000,
"ownerId" => "http://access.auth.theplatform.com/account/data/Account/2460535675"
}
]
}
end
include_context "with request and response"

it { is_expected.to be_a_kind_of Creatable }

Expand Down Expand Up @@ -75,7 +35,7 @@ module Driver
it "is expected to call Oj.load against original.body" do
allow(Oj).to receive(:load).and_call_original
response.data
expect(Oj).to have_received(:load).with(body)
expect(Oj).to have_received(:load).with(excon_body)
end

it "is expected to return a hash" do
Expand Down Expand Up @@ -123,7 +83,7 @@ module Driver
it "is expected to call Page.create with the entries and xmlns values extracted from the body." do
allow(Page).to receive(:create)
response.page
expect(Page).to have_received(:create).with entries: page_hash['entries'], xmlns: page_hash['$xmlns']
expect(Page).to have_received(:create).with entries: [], xmlns: nil
end

it "is expected to return an instance of Page" do
Expand Down
98 changes: 21 additions & 77 deletions spec/cts/mpx/entries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@
module Cts
module Mpx
describe Entries do
let(:entries) { described_class.create collection: [entry] }
let(:entry) { Entry.new }
include_context "with media entries"
include_context "with field"

let(:other_entries) { Entries.create(collection: [other_entry]) }
let(:other_entry) { Entry.new }
let(:page) { Driver::Page.create params }
let(:params) { { xmlns: { "ns": "a_value" }, entries: [{ "id" => Parameters.account_id, "guid" => "123" }] } }

it { is_expected.to be_a_kind_of Enumerable }
it { is_expected.to be_a_kind_of Creatable }
it { is_expected.to have_attributes(collection: []) }

describe 'Class methods' do
it { expect(described_class).to respond_to(:create_from_page).with(1).arguments }
end

describe "Instance methods" do
describe "Responds to" do
it { is_expected.to respond_to(:add).with(1).argument }
it { is_expected.to respond_to(:each) }
it { is_expected.to respond_to(:remove).with(1).argument }
Expand All @@ -28,125 +23,74 @@ module Mpx
it { is_expected.to respond_to(:-).with(1).argument }
end

describe '::create_from_page' do
it { expect { described_class.create_from_page 1 }.to raise_argument_error(1, Driver::Page) }

context 'when the field "id" is present' do
let(:entry) { Entry.new }

it "is expected to set entry.id with entry.field['id']" do
allow(Entry).to receive(:new).and_return entry
expect { described_class.create_from_page page }.to change(entry, :id).to Parameters.account_id
end
end

it "is expected to call page.entries.each" do
allow(page.entries).to receive(:each).and_call_original
described_class.create_from_page page
expect(page.entries).to have_received(:each)
end

it "is expected to call #{Entries}.new " do
allow(Entries).to receive(:new).and_call_original
described_class.create_from_page page
expect(Entries).to have_received(:new)
end

it "is expected to call #{Fields}.create_from_data(data: entry_hash, xmlns: xmlns_hash)" do
allow(Fields).to receive(:create_from_data).and_call_original
allow(page.entries).to receive(:each).and_call_original
described_class.create_from_page page
expect(Fields).to have_received(:create_from_data)
end

it "is expected to call #{Entry}.new with Fields: created_fields" do
allow(Entry).to receive(:new).and_call_original
described_class.create_from_page page
expect(Entry).to have_received(:new)
end

it "is expected to add the new entry to the new entries" do
e = Entries.new
allow(Entries).to receive(:new).and_return(e)
allow(e).to receive(:add).and_call_original
expect { described_class.create_from_page(page) }.to(change { e.entries })
end

it "is expected to return self" do
expect(entries.add(entry)).to eq entries
end
end

describe '::[] (addressable method)' do
let(:id) { Parameters.account_id }

context "when no key is supplied" do
it "is expected to return the entire collection" do
expect(entries[]).to eq entries.collection
expect(media_entries[]).to eq media_entries.collection
end
end

context "when a value could not be found" do
it "is expected to return nil" do
expect(entries['aslkdj']).to eq nil
expect(media_entries['aslkdj']).to eq nil
end
end

it "is expected to return the value" do
entry.id = id
expect(entries[id]).to eq entry
media_entry.id = media_id
expect(media_entries[media_id]).to eq media_entry
end
end

describe '::+' do
it { expect(entries + other_entries).to be_an_instance_of(Entries) }
it { expect(media_entries + other_entries).to be_an_instance_of(Entries) }

it "expected to be the aggreation of the two entries" do
expect((entries + other_entries).collection).to eq [entry, other_entry]
expect((media_entries + other_entries).collection).to eq [media_entry, other_entry]
end
end

describe '::-' do
it { expect(entries - other_entries).to be_an_instance_of(Entries) }
it { expect(media_entries - other_entries).to be_an_instance_of(Entries) }

it "expected to be the aggreation of the two entries" do
expect((entries - other_entries).collection).to eq [entry, other_entry]
expect((media_entries - other_entries).collection).to eq [media_entry, other_entry]
end
end

describe '::add' do
context "when the provided argument is not a kind of #{Entry}" do
it { expect { entries.add 1 }.to raise_argument_error(1, Entry) }
it { expect { media_entries.add 1 }.to raise_argument_error(1, Entry) }
end

context 'when the provided entry is already in @collections' do
it { expect { entries.add entry }.not_to change(entries, :collection) }
it { expect { media_entries.add media_entry }.not_to change(media_entries, :collection) }
end

it "is expected to add the argument to @collection" do
entries.remove entry
expect { entries.add entry }.to change(entries, :collection).to([entry])
media_entries.remove media_entry
expect { media_entries.add media_entry }.to change(media_entries, :collection).to([media_entry])
end

it "is expected to return self" do
expect(entries.add(entry)).to eq entries
expect(media_entries.add(media_entry)).to eq media_entries
end
end

describe '::each' do
specify { expect { |b| entries.each(&b) }.to yield_control.once }
specify { expect { |b| media_entries.each(&b) }.to yield_control.once }
end

describe '::reset' do
it { expect { entries.reset }.to change(entries, :collection).to([]) }
it { expect { media_entries.reset }.to change(media_entries, :collection).to([]) }
end

describe '::remove' do
context "when the provided field is not a kind of Field" do
it { expect { entries.add 1 }.to raise_argument_error(1, Entry) }
it { expect { media_entries.add 1 }.to raise_argument_error(1, Entry) }
end

it { expect { entries.remove entry }.to change(entries, :collection).to([]) }
it { expect { media_entries.remove media_entry }.to change(media_entries, :collection).to([]) }
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion spec/cts/mpx/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Cts
module Mpx
describe Entry do
include_context "with basic parameters"
include_context "with field and fields"
include_context "with field"
include_context "with media entry"
include_context "with page"

let(:data) { { id: media_id } }
let(:empty_entry) { described_class.new }
Expand Down
Loading

0 comments on commit e682400

Please sign in to comment.