From bc432b4334ecc52b59aa9f47dc8ec3d50763e589 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 7 Jul 2015 16:16:25 +0200 Subject: [PATCH] Add new test file for AbstractTableGeocoder --- Makefile | 1 + .../spec/lib/abstract_table_geocoder_spec.rb | 84 +++++++++++++++++++ .../spec/table_geocoder_spec.rb | 4 +- 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 services/table-geocoder/spec/lib/abstract_table_geocoder_spec.rb diff --git a/Makefile b/Makefile index dbf18fba4a..ac91103854 100644 --- a/Makefile +++ b/Makefile @@ -179,6 +179,7 @@ WORKING_SPECS_10 = \ spec/models/carto/user_spec.rb \ spec/models/carto/user_creation_spec.rb \ spec/models/carto/organization_spec.rb \ + services/table-geocoder/spec/lib/abstract_table_geocoder_spec.rb \ $(NULL) CDB_PATH=lib/assets/javascripts/cdb diff --git a/services/table-geocoder/spec/lib/abstract_table_geocoder_spec.rb b/services/table-geocoder/spec/lib/abstract_table_geocoder_spec.rb new file mode 100644 index 0000000000..645d45b57c --- /dev/null +++ b/services/table-geocoder/spec/lib/abstract_table_geocoder_spec.rb @@ -0,0 +1,84 @@ +# encoding: utf-8 + +require_relative '../factories/pg_connection' +require_relative '../../lib/abstract_table_geocoder' +require_relative '../../../../spec/rspec_configuration.rb' + +describe CartoDB::AbstractTableGeocoder do + + before(:all) do + class ConcreteTableGeocoder < CartoDB::AbstractTableGeocoder; end + + conn = CartoDB::Importer2::Factories::PGConnection.new + @db = conn.connection + @pg_options = conn.pg_options + @table_name = "ne_10m_populated_places_simple" + load_csv path_to("populated_places_short.csv") + end + + after(:all) do + @db.drop_table @table_name + end + + + describe '#initialize' do + it 'sets the connection timeout to 5 hours' do + tg = ConcreteTableGeocoder.new({ + connection: @db, + table_name: @table_name, + qualified_table_name: @table_name + }) + timeout = @db.fetch("SHOW statement_timeout").first.fetch(:statement_timeout) + timeout.should == '5h' + end + end + + + describe '#add_georef_status_column' do + + before(:each) do + @tg = ConcreteTableGeocoder.new({ + connection: @db, + table_name: @table_name, + qualified_table_name: @table_name + }) + end + + it 'adds a georef_status_column if it does not exists' do + @tg.send(:add_georef_status_column) + assert_correctness_of_georef_status_column + end + + it 'does nothing if the column already exists' do + @tg.send(:add_georef_status_column) + @tg.send(:add_georef_status_column) + assert_correctness_of_georef_status_column + end + + it 'casts its type if the column exists and is not bool' do + @db.add_column @table_name, :georef_status_column, :text + @tg.send(:add_georef_status_column) + assert_correctness_of_georef_status_column + end + + end + + + def assert_correctness_of_georef_status_column + georef_status_column = @db.schema(@table_name, reload: true).select {|c| c[0] == :cartodb_georef_status}.first + georef_status_column.nil?.should == false + georef_status_column[1][:db_type].should == 'boolean' + end + + def load_csv(path) + @db.run("CREATE TABLE #{@table_name} (the_geom geometry, cartodb_id integer, name text, iso3 text)") + @db.run("COPY #{@table_name.lit}(cartodb_id, name, iso3) FROM '#{path}' DELIMITER ',' CSV") + end + + def path_to(filepath = '') + File.expand_path( + File.join(File.dirname(__FILE__), "../fixtures/#{filepath}") + ) + end + +end diff --git a/services/table-geocoder/spec/table_geocoder_spec.rb b/services/table-geocoder/spec/table_geocoder_spec.rb index 4e5a14c36d..5ccffbeec9 100644 --- a/services/table-geocoder/spec/table_geocoder_spec.rb +++ b/services/table-geocoder/spec/table_geocoder_spec.rb @@ -221,12 +221,12 @@ describe CartoDB::TableGeocoder do File.expand_path( File.join(File.dirname(__FILE__), "../spec/fixtures/#{filepath}") ) - end #path_to + end def load_csv(path) @db.run("CREATE TABLE #{@table_name} (the_geom geometry, cartodb_id integer, name text, iso3 text)") @db.run("COPY #{@table_name.lit}(cartodb_id, name, iso3) FROM '#{path}' DELIMITER ',' CSV") - end # create_table + end end