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

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
erniebrodeur committed Nov 20, 2018
1 parent 283e451 commit 636249b
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 216 deletions.
5 changes: 1 addition & 4 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(%r{spec\/spec_*}) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)
end
5 changes: 3 additions & 2 deletions lib/cts/mpx/driver/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ def raise_unless_reference?(argument)
# @param [Object] keyword keyword to assure is supplied
# @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
def raise_unless_required_keyword?(a_binding, keyword)
value = a_binding.local_variable_get(keyword)
raise ArgumentError, "#{keyword} is a required keyword." unless value
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cts/mpx/driver/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def raise_if_not_a_hash(objects)
# @return [nil] nil
#
def required_arguments(keywords, a_binding)
keywords.each { |arg| Exceptions.raise_unless_required_keyword?(keyword: a_binding.local_variable_get(arg)) }
keywords.each { |keyword| Exceptions.raise_unless_required_keyword?(a_binding, keyword) }
nil
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/cts/mpx/services/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def get(user: nil, account_id: nil, service: nil, fields: nil, endpoint: nil, so
# @return [Response] Response of the call
def post(user: nil, account_id: nil, service: nil, endpoint: nil, extra_path: nil, query: {}, page: nil, headers: {}, method: :post)
prep_call(user: user, account_id: account_id, service: service, query: query, headers: headers, required_arguments: ['user', 'service', 'endpoint', 'page'], page: page, binding: binding)

host = Driver::Assemblers.host user: user, service: service
path = Driver::Assemblers.path service: service, endpoint: endpoint, extra_path: extra_path
query = Driver::Assemblers.query user: user, account_id: account_id, service: service, endpoint: endpoint, query: query
Expand Down
2 changes: 0 additions & 2 deletions spec/cts/mpx/driver/exceptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module Driver

describe "::raise_unless_account_id" do
context "when the argument is not an account_id" do

it "is expected to raise a ArgumentError with is not a valid account_id " do
expect { described_class.raise_unless_account_id(reference) }.to raise_error ArgumentError, /#{reference} is not a valid account_id/
end
Expand Down Expand Up @@ -46,7 +45,6 @@ module Driver
describe "::raise_unless_reference?" do
it { expect(described_class).to respond_to(:raise_unless_reference?).with(1).argument }
context "when the argument is not a reference" do

it "is expected to raise a ArgumentError with is not a valid reference " do
expect { described_class.raise_unless_reference?(reference) }.to raise_error ArgumentError, /#{reference} is not a valid reference/
end
Expand Down
2 changes: 0 additions & 2 deletions spec/cts/mpx/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ module Mpx
expect(media_entry.save(user: user)).to be media_entry
end


context "when fields['ownerId'] is not set" do

before { entry.fields['ownerId'] = nil }

it { expect { entry.save user: user }.to raise_error ArgumentError, "fields['ownerId'] must be set" }
Expand Down
2 changes: 1 addition & 1 deletion spec/cts/mpx/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Cts
module Mpx
describe Query do
include_context "with fields"
include_context "with fields objects"
include_context "with media objects"
include_context "with user objects"
include_context "with empty objects"
Expand Down
228 changes: 153 additions & 75 deletions spec/cts/mpx/services/data_spec.rb
Original file line number Diff line number Diff line change
@@ -1,104 +1,182 @@
require 'spec_helper'

module Cts
module Mpx
module Services
describe Data do
include_context "with request and response objects"
include_context "with media objects"
include_context "with user objects"

let(:root_domain) { Driver.load_json_file('config/root_registry_sea1.json')['resolveDomainResponse'] }
let(:call_params) { { user: user, service: media_service, endpoint: media_endpoint, query: {} } }
let(:with_params) { { method: call_method, url: "http://data.media.theplatform.com/media/data/Media/feed", query: {}, headers: {} } }

before do
allow(Driver::Request).to receive(:new).and_return request
allow(request).to receive(:call).and_return response
allow(Registry).to receive(:fetch_and_store_domain).and_return root_domain
Registry.initialize
end
module Cts::Mpx::Services
describe Data do
include_context "with data parameters"
include_context "with request and response objects"
include_context "with media objects"
include_context "with user objects"

let(:root_domain) { Driver.load_json_file('config/root_registry_sea1.json')['resolveDomainResponse'] }
let(:call_params) { { user: user, service: media_service, endpoint: media_endpoint, query: {} } }
let(:with_params) { { method: call_method, url: "http://data.media.theplatform.com/media/data/Media/feed", query: { form: "cjson", schema: "1.7.0", token: "carpe diem" }, headers: {} } }

before do
allow(Driver::Request).to receive(:new).and_return request
allow(request).to receive(:call).and_return response
allow(Registry).to receive(:fetch_and_store_domain).and_return root_domain
Registry.initialize
end

it { expect(described_class.class).to be Module }
it { expect(described_class.class).to be Module }

describe "Responds to" do
it { is_expected.to respond_to(:[]).with(0..1).arguments }
it { is_expected.to respond_to(:delete).with_keywords(:user, :account_id, :service, :endpoint, :sort, :extra_path, :range, :ids, :query, :headers, :count, :entries) }
it { is_expected.to respond_to(:get).with_keywords(:user, :account_id, :service, :endpoint, :sort, :extra_path, :range, :ids, :query, :headers, :count, :entries, :fields) }
it { is_expected.to respond_to(:post).with_keywords(:user, :account_id, :service, :endpoint, :extra_path, :query, :page, :headers) }
it { is_expected.to respond_to(:put).with_keywords(:user, :account_id, :service, :endpoint, :extra_path, :query, :page, :headers) }
end
describe "Responds to" do
it { is_expected.to respond_to(:[]).with(0..1).arguments }
it { is_expected.to respond_to(:delete).with_keywords(:user, :account_id, :service, :endpoint, :sort, :extra_path, :range, :ids, :query, :headers, :count, :entries) }
it { is_expected.to respond_to(:get).with_keywords(:user, :account_id, :service, :endpoint, :sort, :extra_path, :range, :ids, :query, :headers, :count, :entries, :fields) }
it { is_expected.to respond_to(:post).with_keywords(:user, :account_id, :service, :endpoint, :extra_path, :query, :page, :headers) }
it { is_expected.to respond_to(:put).with_keywords(:user, :account_id, :service, :endpoint, :extra_path, :query, :page, :headers) }
end

describe "::[] (addressable method)" do
let(:service) { 'Media Data Service' }
describe "::[] (addressable method)" do
let(:service) { 'Media Data Service' }

context "when the key is not a string" do
it { expect { described_class[1] }.to raise_error(ArgumentError, /key must be a string/) }
end
context "when the key is not a string" do
it { expect { described_class[1] }.to raise_error(ArgumentError, /key must be a string/) }
end

context "when the key is not a service name" do
it { expect { described_class['1'] }.to raise_error(ArgumentError, /must be a service name/) }
end
context "when the key is not a service name" do
it { expect { described_class['1'] }.to raise_error(ArgumentError, /must be a service name/) }
end

context "when a key is not provided" do
it { expect(described_class[]).to be_instance_of Array }
end
context "when a key is not provided" do
it { expect(described_class[]).to be_instance_of Array }
end

it "is expected to search for services that are type: data only" do
expect(described_class[].count).to eq 29
end
it "is expected to search for services that are type: data only" do
expect(described_class[].count).to eq 29
end

it { expect(described_class[service]).to be_instance_of Driver::Service }
end
it { expect(described_class[service]).to be_instance_of Driver::Service }
end

describe '::delete', focus: true do
let(:call_method) { :delete }
describe '::delete' do
let(:call_method) { :delete }

include_examples 'call_constraints'
include_examples 'call_request', :delete
include_examples 'call_constraints'
include_examples 'call_request', :delete
end

describe "::get" do
let(:call_method) { :get }

context "when fields is provided" do
before { call_params[:fields] = 'id,guid' }

it "is expected to pass them to Request.call" do
allow(Driver::Request).to receive(:create).and_call_original
described_class.send call_method, call_params
expect(Driver::Request).to have_received(:create).with(a_hash_including(query: a_hash_including(fields: call_params[:fields])))
end
end

include_examples 'call_constraints'
include_examples 'call_request', :get
end

describe "::get" do
let(:call_method) { :get }
shared_context "with data call setup" do
let(:call_params) { { user: user, service: service, endpoint: endpoint, query: {} } }
let(:call_parent) { Cts::Mpx::Services::Data }
let(:create_with_params) { { method: described_class, url: "http://data.media.theplatform.com/media/data/Media/feed", query: { form: "cjson", schema: "1.7.0", token: "carpe diem" }, headers: {} } }

context "when fields is provided" do
before { call_params[:fields] = 'id,guid' }
before do
call_params[:page] = Driver::Page.create
create_with_params[:payload] = Oj.dump('xmlns' => {}, 'entries' => [])
end
end

it "is expected to pass them to Request.call" do
allow(Driver::Request).to receive(:create).and_call_original
described_class.send call_method, call_params
expect(Driver::Request).to have_received(:create).with(a_hash_including(query: a_hash_including(fields: call_params[:fields])))
end
end
[:put, :post].each do |verb|
describe verb, method: verb, focus: true, required_keywords: ['user', 'service', 'endpoint', 'page'], keyword_types: { user: User, query: Hash, page: Driver::Page } do
include_context "with data call setup", described_class
include_examples "when the user is not logged in", described_class
include_examples "when a required keyword isn't set", described_class
include_examples "when a keyword is not a type of", described_class

include_examples 'call_constraints'
include_examples 'call_request', :get
end
it { expect(call_parent.send described_class, (call_params)).to be_a_kind_of(Driver::Response) }

describe "::post" do
let(:call_method) { :post }
it "is expected to call Assemblers::host with user, service" do
allow(Driver::Assemblers).to receive(:host).and_call_original
call_parent.send described_class, call_params
expect(Driver::Assemblers).to have_received(:host).with service: service, user: user
end

before do
call_params[:page] = Driver::Page.create
with_params[:payload] = Oj.dump(xmlns: {}, entries: [])
end
it "is expected to call Assemblers::path with service, endpoint, and extra_path" do
allow(Driver::Assemblers).to receive(:path).and_call_original
call_parent.send described_class, call_params
expect(Driver::Assemblers).to have_received(:path).with service: service, endpoint: endpoint, extra_path: nil
end

include_examples 'call_constraints'
include_examples 'call_request', :post
it "is expected to call Assemblers::query with user, service, endpoint, query" do
allow(Driver::Assemblers).to receive(:query).and_call_original
call_parent.send described_class, call_params
expect(Driver::Assemblers).to have_received(:query).with account_id: nil, service: service, user: user, endpoint: endpoint, query: {}
end

describe "::put" do
let(:call_method) { :put }
it "is expected to call assembler_payload"

before do
call_params[:page] = Driver::Page.create
with_params[:payload] = Oj.dump(xmlns: {}, entries: [])
end
it "is expected to call Request.create with '...'" do
allow(Driver::Request).to receive(:create).and_call_original
call_parent.send described_class, call_params
expect(Driver::Request).to have_received(:create).with create_with_params
end

include_examples 'call_constraints', :put
include_examples 'call_request', :put
it "is expected to call request.call" do
call_parent.send described_class, call_params
expect(request).to have_received(:call).with(no_args)
end
end
end

describe "::post", method: :post, required_keywords: ['user', 'service', 'endpoint', 'page'], keyword_types: { user: User, query: Hash, page: Driver::Page } do
include_context "with data call setup", metadata[:method]
include_examples "when the user is not logged in"
include_examples "when a required keyword isn't set"
include_examples "when a keyword is not a type of"

it { expect(Data.post(call_params)).to be_a_kind_of(Driver::Response) }

it "is expected to call Assemblers::host with user, service" do
allow(Driver::Assemblers).to receive(:host).and_call_original
Data.post call_params
expect(Driver::Assemblers).to have_received(:host).with service: service, user: user
end

it "is expected to call Assemblers::path with service, endpoint, and extra_path" do
allow(Driver::Assemblers).to receive(:path).and_call_original
Data.post call_params
expect(Driver::Assemblers).to have_received(:path).with service: service, endpoint: endpoint, extra_path: nil
end

it "is expected to call Assemblers::query with user, service, endpoint, query" do
allow(Driver::Assemblers).to receive(:query).and_call_original
Data.post call_params
expect(Driver::Assemblers).to have_received(:query).with account_id: nil, service: service, user: user, endpoint: endpoint, query: {}
end

it "is expected to call assembler_payload"

it "is expected to call Request.create with '...'" do
allow(Driver::Request).to receive(:create).and_call_original
Data.post call_params
expect(Driver::Request).to have_received(:create).with create_with_params
end

it "is expected to call request.call" do
Data.post call_params
expect(request).to have_received(:call).with(no_args)
end
end

describe "::put" do
let(:call_method) { :put }

before do
call_params[:page] = Driver::Page.create
create_with_params[:payload] = Oj.dump(xmlns: {}, entries: [])
end

include_examples 'call_constraints'
include_examples 'call_request', :put
end
end
end
1 change: 0 additions & 1 deletion spec/cts/mpx/services/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ module Services
end
end


describe "::post" do
let(:call_method) { :post }
let(:request_params) do
Expand Down
2 changes: 1 addition & 1 deletion spec/matchers/raise_argument_exception.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec::Matchers.define :raise_argument_error do |variable, variable_type|
RSpec::Matchers.define :raise_argument_exception do |variable, variable_type|
match do
begin
actual.call
Expand Down
4 changes: 2 additions & 2 deletions spec/matchers/raise_error_without_user_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
begin
actual.call
rescue RuntimeError => e
user.token = token
user.instance_variable_set :@token, token
return e.message.match?(/#{user.username} is not signed in, \(token is set to nil\)\./)
end
user.token = token
user.instance_variable_set :@token, token
false
end

Expand Down
4 changes: 2 additions & 2 deletions spec/matchers/require_keyword_arguments.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
RSpec::Matchers.define :require_keyword_arguments do |method, params|
RSpec::Matchers.define :require_keyword_arguments do |call_class, method, params|
match do
params.each do |_arg|
dup_params = Oj.load Oj.dump params
begin
described_class.send(method, dup_params)
call_class.send(method, dup_params)
rescue ArgumentError => e
return e.message.match?(/is a required keyword./)
end
Expand Down
Loading

0 comments on commit 636249b

Please sign in to comment.