From 256170c308d2ceb5f37f27e23298267adbb2e8e0 Mon Sep 17 00:00:00 2001 From: Dmitry Sinina Date: Sat, 7 Jul 2018 21:50:34 +0300 Subject: [PATCH] initial routing SP test --- lib/call_sql/base.rb | 55 +++++++++++++++++++++ lib/call_sql/yeti.rb | 20 ++++++++ spec/routing/route.rb | 110 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 lib/call_sql/base.rb create mode 100644 lib/call_sql/yeti.rb create mode 100644 spec/routing/route.rb diff --git a/lib/call_sql/base.rb b/lib/call_sql/base.rb new file mode 100644 index 000000000..254615ea3 --- /dev/null +++ b/lib/call_sql/base.rb @@ -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 diff --git a/lib/call_sql/yeti.rb b/lib/call_sql/yeti.rb new file mode 100644 index 000000000..1e9f7b173 --- /dev/null +++ b/lib/call_sql/yeti.rb @@ -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 diff --git a/spec/routing/route.rb b/spec/routing/route.rb new file mode 100644 index 000000000..fdb1119fc --- /dev/null +++ b/spec/routing/route.rb @@ -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