-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #8016 - api connection moved to context
- connection moved to context - connection is now instance, not class - dependency injection for connection and connectors
- Loading branch information
Tomas Strachota
committed
Nov 25, 2016
1 parent
37cfb78
commit e91c5ab
Showing
9 changed files
with
139 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
require File.join(File.dirname(__FILE__), './apipie/option_builder') | ||
require File.join(File.dirname(__FILE__), './apipie/api_connection') | ||
require File.join(File.dirname(__FILE__), './apipie/command') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'apipie_bindings' | ||
module HammerCLI::Apipie | ||
class ApiConnection < HammerCLI::AbstractConnector | ||
attr_reader :api | ||
|
||
def initialize(params, options = {}) | ||
@logger = options[:logger] | ||
@api = ApipieBindings::API.new(params) | ||
if options[:reload_cache] | ||
@api.clean_cache | ||
@logger.debug 'Apipie cache was cleared' unless @logger.nil? | ||
end | ||
end | ||
|
||
def resources | ||
@api.resources | ||
end | ||
|
||
def resource(resource_name) | ||
@api.resource(resource_name) | ||
end | ||
|
||
def has_resource?(resource_name) | ||
@api.has_resource?(resource_name) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require File.join(File.dirname(__FILE__), '../../test_helper') | ||
|
||
describe HammerCLI::Apipie::ApiConnection do | ||
let(:connection) { HammerCLI::Connection.new } | ||
|
||
describe '#initialize' do | ||
|
||
let(:empty_params) {{}} | ||
|
||
def api_stub(params = {}) | ||
api_stub = stub() | ||
ApipieBindings::API.expects(:new).with(params).returns(api_stub) | ||
api_stub | ||
end | ||
|
||
it "passes attributes to apipie bindings" do | ||
params = { :apidoc_cache_name => 'test.example.com' } | ||
|
||
api_stub(params) | ||
HammerCLI::Apipie::ApiConnection.new(params) | ||
end | ||
|
||
context "with :clear_cache => true" do | ||
it "clears cache" do | ||
api_stub.expects(:clean_cache) | ||
HammerCLI::Apipie::ApiConnection.new(empty_params, :reload_cache => true) | ||
end | ||
|
||
it "logs message when logger is available" do | ||
logger = stub() | ||
logger.expects(:debug).with('Apipie cache was cleared') | ||
|
||
api_stub.expects(:clean_cache) | ||
HammerCLI::Apipie::ApiConnection.new(empty_params, :reload_cache => true, :logger => logger) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,59 @@ | ||
require File.join(File.dirname(__FILE__), 'test_helper') | ||
|
||
describe HammerCLI::Connection do | ||
|
||
before :each do | ||
# clean up global settings | ||
HammerCLI::Connection.drop_all | ||
HammerCLI::Settings.load({:_params => {:interactive => false}}) | ||
end | ||
|
||
let(:connection) { HammerCLI::Connection } | ||
|
||
class Connector < HammerCLI::AbstractConnector | ||
|
||
attr_reader :url | ||
|
||
def initialize(params) | ||
@url = params[:url] | ||
super | ||
let(:connections) { HammerCLI::Connection.new } | ||
|
||
describe '#create' do | ||
it "creates new connection" do | ||
connections.create(:test) do | ||
:conn1 | ||
end | ||
assert_equal :conn1, connections.get(:test) | ||
end | ||
|
||
end | ||
|
||
it "should return the conection" do | ||
conn = connection.create(:test, {}) | ||
conn.must_be_kind_of HammerCLI::AbstractConnector | ||
end | ||
|
||
it "should create the connection only once" do | ||
conn1 = connection.create(:test, {}) | ||
conn2 = connection.create(:test, {}) | ||
conn1.must_equal conn2 | ||
end | ||
it "doesn't overwrite the connection when called multiple times" do | ||
connections.create(:test) do | ||
:conn1 | ||
end | ||
connections.create(:test) do | ||
:conn2 | ||
end | ||
assert_equal :conn1, connections.get(:test) | ||
end | ||
|
||
it "should test the connection" do | ||
connection.exist?(:test).must_equal false | ||
conn1 = connection.create(:test, {}) | ||
connection.exist?(:test).must_equal true | ||
end | ||
it 'writes message to log' do | ||
logger = stub() | ||
logger.expects(:debug).with('Registered: test_connection') | ||
|
||
it "should get the connection" do | ||
conn1 = connection.create(:test, {}) | ||
conn2 = connection.get(:test) | ||
conn1.must_equal conn2 | ||
connections = HammerCLI::Connection.new(logger) | ||
connections.create(:test_connection) do | ||
:test_connection | ||
end | ||
end | ||
end | ||
|
||
|
||
it "should be able to drop all" do | ||
conn1 = connection.create(:test, {}) | ||
connection.drop_all | ||
conn2 = connection.create(:test, {}) | ||
conn1.wont_equal conn2 # TODO | ||
describe '#drop' do | ||
it 'drops the connection' do | ||
connections.create(:test) do | ||
:conn1 | ||
end | ||
connections.drop(:test) | ||
assert_nil connections.get(:test) | ||
end | ||
end | ||
|
||
it "should drop the connection" do | ||
conn1 = connection.create(:test, {}) | ||
connection.drop(:test) | ||
conn2 = connection.create(:test, {}) | ||
conn1.wont_equal conn2 | ||
end | ||
describe '#drop_all' do | ||
it 'drops all connections' do | ||
connections.create(:test1) do | ||
:conn1 | ||
end | ||
connections.create(:test2) do | ||
:conn3 | ||
end | ||
connections.drop_all | ||
|
||
it "should accept custom connector" do | ||
conn = connection.create(:test, {:url => 'URL'}, :connector => Connector) | ||
conn.must_be_kind_of Connector | ||
conn.url.must_equal 'URL' | ||
assert_nil connections.get(:test1) | ||
assert_nil connections.get(:test2) | ||
end | ||
end | ||
|
||
end |