Skip to content

Commit

Permalink
Merge pull request #332 from dmitry-sinina/routing_tests
Browse files Browse the repository at this point in the history
initial routing SP test
  • Loading branch information
dmitry-sinina authored Jul 7, 2018
2 parents b264fc3 + 256170c commit a9a1340
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/call_sql/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module CallSql
class Base
include Singleton
extend SingleForwardable

AVAILABLE_METHODS = [:select_value, :select_values, :execute, :select_all, :select_rows].freeze

def self._define_sql_method(name)
define_method(name) do |sql, *args|
perform_sp(name, sql, *args)
end
end

def self._delegate_to_instance(*names)
def_delegators :instance, *names
end

AVAILABLE_METHODS.each do |name|
_define_sql_method(name)
end

def select_row(sql, *bindings)
select_rows(sql, *bindings)[0]
end

def select_all_serialized(sql, *bindings)
result = select_all(sql, *bindings)
result.map { |row| row.map { |k, v| [k.to_sym, result.column_types[k].deserialize(v)] }.to_h }
end

_delegate_to_instance(:select_row, :select_all_serialized, *AVAILABLE_METHODS)

private

def model_klass
raise NotImplementedError, 'method #model_klass must be defined'
end

def connection
@connection ||= model_klass.connection
end

def sanitize_sql_array(sql, *bindings)
model_klass.send :sanitize_sql_array, bindings.unshift(sql)
end

def perform_sp(method, sql, *bindings)
if bindings.any?
sql = sanitize_sql_array(sql, *bindings)
end
connection.send(method, sql)
end

end
end
20 changes: 20 additions & 0 deletions lib/call_sql/yeti.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module CallSql
class Yeti < Base
_delegate_to_instance :table_full_size, :table_data_size

def table_full_size(table_name)
select_value('SELECT pg_total_relation_size(?)', table_name)
end

def table_data_size(table_name)
select_value('SELECT pg_relation_size(?)', table_name)
end

private

def model_klass
::Yeti::ActiveRecord
end

end
end
110 changes: 110 additions & 0 deletions spec/routing/route.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require 'spec_helper'

RSpec.describe '#routing logic' do
subject do
CallSql::Yeti.select_all_serialized(
'
SELECT * from switch16.route(
?::integer,
?::integer,
?::smallint,
?::inet,
?::integer,
?::inet,
?::integer,
?::varchar,
?::varchar,
?::varchar,
?::integer,
?::varchar,
?::varchar,
?::integer,
?::varchar,
?::varchar,
?::integer,
?::varchar,
?::varchar,
?::integer,
?::varchar,
?::varchar,
?::inet,
?::integer,
?::smallint,
?::varchar,
?::varchar,
?::varchar,
?::varchar,
?::varchar
)',
node_id,
pop_id,
protocol_id,
remote_ip,
remote_port,
local_ip,
local_port,
from_dsp,
from_name,
from_domain,
from_port,
to_name,
to_domain,
to_port,
contact_name,
contact_domain,
contact_port,
uri_name,
uri_domain,
auth_id,
x_yeti_auth,
diversion,
x_orig_ip,
x_orig_port,
x_orig_protocol_id,
pai,
ppi,
privacy,
rpid,
rpid_privacy
)
end

before {FactoryGirl.create(:customers_auth)}

let(:node_id) {1}
let(:pop_id) {12}
let(:protocol_id) {1}
let(:remote_ip) {'1.1.1.1'}
let(:remote_port) {5060}
let(:local_ip) {'2.2.2.2'}
let(:local_port) {5060}
let(:from_dsp) {'from display name'}
let(:from_name) {'from_username'}
let(:from_domain) {'from-domain'}
let(:from_port) {5060}
let(:to_name) {'to_username'}
let(:to_domain) {'to-domain'}
let(:to_port) {5060}
let(:contact_name) {'contact-username'}
let(:contact_domain) {'contact-domain'}
let(:contact_port) {5060}
let(:uri_name) {'uri-name'}
let(:uri_domain) {'uri-domain'}
let(:auth_id) {nil}
let(:x_yeti_auth) {nil}
let(:diversion) {'test'}
let(:x_orig_ip) {'3.3.3.3'}
let(:x_orig_port) {6050}
let(:x_orig_protocol_id) {2}
let(:pai) {'pAI'}
let(:ppi) {'ppi'}
let(:privacy) {'privacy'}
let(:rpid) {'rpid'}
let(:rpid_privacy) {'rpid-privacy'}

it ' return 404 ' do
response=subject
end

end

0 comments on commit a9a1340

Please sign in to comment.