require 'spec_helper' require 'flydata/plugin_support/context' require 'flydata/query_based_sync/client' require 'flydata/query_based_sync/resource_requester' require 'flydata/query_based_sync/response_handler' require 'flydata/query_based_sync/response' require 'flydata/fluent-plugins/flydata_plugin_ext/flydata_sync_context' module Flydata module QueryBasedSync class DummyResourceRequester < ResourceRequester end class DummyResponseHandler < ResponseHandler end class DummyResponse < Response end class DummyClient < Client RESOURCE_REQUESTER_CLASS = DummyResourceRequester RESPONSE_HANDLER_CLASS = DummyResponseHandler end class DummySourcePos include Comparable def initialize(*args) @snapshot = args.first @args = args end attr_reader :snapshot attr_reader :args def <=>(other) snapshot <=> other.snapshot end end shared_context 'query based sync context' do include_context 'flydata sync context' def create_response(context, table_name, records, query_cond = {}) DummyResponse.new(context, table_name, records, query_cond) end let(:resource_client) { double('client') } let(:records) { [ {'1'=>1, '2'=>'a'}, {'1'=>2, '2'=>'b'}, {'1'=>3, '2'=>'c'}, ] } let(:query_cond) { {from_sid: previous_snapshot, to_sid: current_snapshot} } let(:response) { create_response(context, 'table_1', records, query_cond) } let(:sync_fm) { double('sync_fm') } let(:table_meta) { double('table_meta') } let(:table_meta_1) { { table_name: 'table_1', table_schema: nil, pk_positions: [1], primary_keys: ['id'], columns: [ { column: 'id', type: 'int4' }, { column: 'value', type: 'varchar(24)' }, ] } } let(:cur_src_pos_file) { double('cur_src_pos_file') } let(:table_1_src_pos_file) { double('table_1_src_pos_file') } let(:table_2_src_pos_file) { double('table_2_src_pos_file') } let(:table_3_src_pos_file) { double('table_3_src_pos_file') } let(:context) { Flydata::PluginSupport::Context.new( cur_src_pos_file: cur_src_pos_file, tables: %w(table_1 table_2 table_3), tag: 'test_tag', sync_fm: sync_fm, omit_events: { 'table_3' => [:delete, :truncate] }, table_revs: { 'table_1' => 1, 'table_2' => 1, 'table_3' => 1 }, table_meta: table_meta, table_src_pos_files: { table_1: table_1_src_pos_file, table_2: table_2_src_pos_file, table_3: table_3_src_pos_file, }, params: { fetch_interval: 10, retry_interval: 5, } ) } let(:previous_snapshot) { double('previous_snapshot') } let(:current_snapshot) { double('current_snapshot') } def setup_dummy_objects allow_any_instance_of(DummyResourceRequester).to receive(:create_resource_client).and_return(resource_client) allow_any_instance_of(DummyResourceRequester).to receive(:close) allow(context).to receive(:source_pos_class).and_return(DummySourcePos) response.instance_variable_set(:@new_source_pos, DummySourcePos.new(current_snapshot)) end before do allow(table_meta).to receive(:[]).with(:table_1).and_return(table_meta_1) allow(table_meta).to receive(:current_snapshot).and_return(current_snapshot) allow(cur_src_pos_file).to receive(:save).with(current_snapshot) setup_dummy_objects end end end end