Synchronizations now use Carto::UserTable #11889

pull/11886/head
Juan Ignacio Sánchez Lara 8 years ago
parent 6be8982b53
commit 0c9229bd0f

@ -80,7 +80,7 @@ module CartoDB
end
def fix_oid(table_name)
user_table = user.tables.where(name: table_name).first
user_table = Carto::UserTable.find(user.tables.where(name: table_name).first.id)
user_table.sync_table_id
user_table.save
@ -114,7 +114,7 @@ module CartoDB
end
def setup_table(table_name, geo_type)
table = user.tables.where(name: table_name).first.service
table = Carto::UserTable.find(user.tables.where(name: table_name).first.id).service
table.force_schema = true

@ -0,0 +1,11 @@
Country,Amount
Spain,1
France,2
China,3
Japan,4
UK,5
Holland,8
Korea,6
The Netherlands,7
Portugal,9
Brazil,10
1 Country Amount
2 Spain 1
3 France 2
4 China 3
5 Japan 4
6 UK 5
7 Holland 8
8 Korea 6
9 The Netherlands 7
10 Portugal 9
11 Brazil 10

@ -382,37 +382,6 @@ describe Downloader do
end
end
def stub_download(url:, filepath:, headers: {}, content_disposition: true)
Typhoeus.stub(url).and_return(response_for(filepath, headers, content_disposition: content_disposition))
end
def stub_failed_download(options)
url = options.fetch(:url)
filepath = options.fetch(:filepath)
headers = options.fetch(:headers, {})
Typhoeus.stub(url).and_return(failed_response_for(filepath, headers))
end
def response_for(filepath, headers = {}, content_disposition: true)
response = Typhoeus::Response.new(
code: 200,
body: File.new(filepath).read.to_s,
headers: headers.merge(headers_for(filepath, content_disposition: content_disposition))
)
response
end
def failed_response_for(filepath, headers={})
Typhoeus::Response.new(code: 404, body: nil, headers: {})
end
def headers_for(filepath, content_disposition: true)
return {} unless content_disposition
filename = filepath.split('/').last
{ "Content-Disposition" => "attachment; filename=#{filename}" }
end
def path_to(filename)
File.join(File.dirname(__FILE__), '..', 'fixtures', filename)
end

@ -42,4 +42,35 @@ module FileServerHelper
raise "No ports available on machine."
end
def stub_download(url:, filepath:, headers: {}, content_disposition: true)
Typhoeus.stub(url).and_return(response_for(filepath, headers, content_disposition: content_disposition))
end
def stub_failed_download(options)
url = options.fetch(:url)
filepath = options.fetch(:filepath)
headers = options.fetch(:headers, {})
Typhoeus.stub(url).and_return(failed_response_for(filepath, headers))
end
def response_for(filepath, headers = {}, content_disposition: true)
response = Typhoeus::Response.new(
code: 200,
body: File.new(filepath).read.to_s,
headers: headers.merge(headers_for(filepath, content_disposition: content_disposition))
)
response
end
def failed_response_for(filepath, headers={})
Typhoeus::Response.new(code: 404, body: nil, headers: {})
end
def headers_for(filepath, content_disposition: true)
return {} unless content_disposition
filename = filepath.split('/').last
{ "Content-Disposition" => "attachment; filename=#{filename}" }
end
end

@ -6,6 +6,7 @@ require_relative '../../../services/data-repository/backend/sequel'
require_relative '../../../services/data-repository/repository'
require_relative '../../../app/models/synchronization/member'
require 'helpers/unique_names_helper'
require 'helpers/file_server_helper'
include UniqueNamesHelper
include CartoDB
@ -51,34 +52,67 @@ describe Synchronization::Member do
end
end
describe "External sources" do
describe "synchronizations" do
before(:all) do
@user_1 = create_user(sync_tables_enabled: true)
@user_2 = create_user(sync_tables_enabled: true)
end
before(:each) do
@user_1 = FactoryGirl.create(:valid_user)
@user_2 = FactoryGirl.create(:valid_user)
bypass_named_maps
Cartodb.config[:metrics] = {}
end
after(:each) do
after(:all) do
@user_1.destroy
@user_2.destroy
end
it "Authorizes to sync always if from an external source" do
member = Synchronization::Member.new(random_attributes({user_id: @user_1.id})).store
member.fetch
describe 'external sources' do
it "Authorizes to sync always if from an external source" do
member = Synchronization::Member.new(random_attributes({user_id: @user_1.id})).store
member.fetch
member.expects(:from_external_source?)
.returns(true)
@user_1.sync_tables_enabled = true
@user_2.sync_tables_enabled = true
member.authorize?(@user_1).should eq true
member.authorize?(@user_2).should eq false
@user_1.sync_tables_enabled = false
@user_2.sync_tables_enabled = false
member.authorize?(@user_1).should eq true
end
end
describe "synchronization" do
it 'syncs' do
# TODO: this is the minimum test valid to reproduce #11889, it's not a complete sync test
CartoDB::Logger.stubs(:error).never
member.expects(:from_external_source?)
.returns(true)
url = 'https://wadus.com/guess_country.csv'
@user_1.sync_tables_enabled = true
@user_2.sync_tables_enabled = true
path = fake_data_path('guess_country.csv')
stub_download(url: url, filepath: path, content_disposition: false)
member.authorize?(@user_1).should eq true
member.authorize?(@user_2).should eq false
attrs = random_attributes(user_id: @user_1.id).merge(service_item_id: url, url: url, name: 'guess_country')
member = Synchronization::Member.new(attrs).store
@user_1.sync_tables_enabled = false
@user_2.sync_tables_enabled = false
DataImport.create(
user_id: @user_1.id,
data_source: fake_data_path('guess_country.csv'),
synchronization_id: member.id,
service_name: 'public_url',
service_item_id: url,
updated_at: Time.now
).run_import!
member.authorize?(@user_1).should eq true
member.run
end
end
end
@ -87,7 +121,7 @@ describe Synchronization::Member do
def random_attributes(attributes={})
random = unique_integer
{
name: attributes.fetch(:name, "name #{random}"),
name: attributes.fetch(:name, "name#{random}"),
interval: attributes.fetch(:interval, 15 * 60 + random),
state: attributes.fetch(:state, 'enabled'),
user_id: attributes.fetch(:user_id, nil)

@ -0,0 +1,11 @@
Country,Amount
Spain,1
France,2
China,3
Japan,4
UK,5
Holland,8
Korea,6
The Netherlands,7
Portugal,9
Brazil,10
1 Country Amount
2 Spain 1
3 France 2
4 China 3
5 Japan 4
6 UK 5
7 Holland 8
8 Korea 6
9 The Netherlands 7
10 Portugal 9
11 Brazil 10

@ -1,6 +1 @@
# This makes factory_girl compatible with Sequel
class Sequel::Model
def save!
save(:validate=>false)
end
end

Loading…
Cancel
Save