Merge pull request #617 from CartoDB/CDB-1657

CDB-1657 Fixed georeferencer spec
pull/629/head
Jose A. Rilla 10 years ago
commit 0c84e32d7a

@ -2,35 +2,66 @@
require 'pg'
require 'sequel'
require 'json'
require 'yaml'
module CartoDB
module Importer2
module Factories
class PGConnection
def initialize
raise(
"Please configure your database settings " +
"in spec/factories/database.json"
) unless File.exists?(configuration_file)
@pg_options = ::JSON.parse(File.read(configuration_file))
def initialize(options = {})
@options = options.reverse_merge(read_config)
create_db if options[:create_db]
end #initialize
def connection
Sequel.postgres(pg_options)
Sequel.postgres(@options)
end #connection
def pg_options
Hash[@pg_options.map { |k, v| [k.to_sym, v] }]
end #pg_options
private
def read_config
begin
load_from_json
rescue
load_from_yml
end
end
def create_db
begin
connection.run("CREATE DATABASE \"#{ @options[:create_db] }\"
WITH TEMPLATE = template_postgis
OWNER = #{ @options[:user] }
ENCODING = 'UTF8'
CONNECTION LIMIT=-1")
rescue Sequel::DatabaseError => e
raise unless e.message =~ /database .* already exists/
end
begin
connection.run("CREATE EXTENSION postgis")
rescue Sequel::DatabaseError => e
raise unless e.message =~ /extension \"postgis\" already exists/
end
end
def configuration_file
File.join(File.dirname("#{__FILE__}"), 'database.json')
end #configuration_file
def load_from_json
json_config = File.join(File.dirname("#{__FILE__}"), 'database.json')
json_config = ::JSON.parse(File.read(json_config)).symbolize_keys
json_config
end
def load_from_yml
yml_config = "#{File.dirname(__FILE__)}/../../../../config/database.yml"
yml_config = YAML.load_file(yml_config)['test'].symbolize_keys
yml_config[:user] = yml_config.delete :username
yml_config
rescue
raise(
"Please configure your database settings " +
"in RAILS_ROOT/config/database.yml "+
"or spec/factories/database.json"
)
end
end # PGConnection
end # Factories
end # Importer2
end # CartoDB

@ -6,7 +6,9 @@ include CartoDB
describe Importer2::Georeferencer do
before(:all) do
@db = Importer2::Factories::PGConnection.new.connection
@db = Importer2::Factories::PGConnection.new(
:create_db => 'georeferencer_spec'
).connection
create_schema(@db, 'cdb_importer')
@ -16,6 +18,7 @@ describe Importer2::Georeferencer do
end
after(:all) do
puts "Dropping table..."
@db.drop_table? @table_name
#TODO Drop schema
@ -46,11 +49,11 @@ describe Importer2::Georeferencer do
georeferencer = Importer2::Georeferencer.new(@db, @table_name)
georeferencer.run
dataset.first.fetch(:the_geom).wont_be_nil
dataset.first.fetch(:the_geom).wont_be_empty
dataset.first.fetch(:the_geom).should_not be_nil
dataset.first.fetch(:the_geom).should_not be_empty
end
it 'returns self if no lat / lon columns in the tabl' do
it 'returns self if no lat / lon columns in the table' do
table_name = create_table(@db,
latitude_column: 'bogus_1',
longitude_column: 'bogus_2'
@ -93,30 +96,30 @@ describe Importer2::Georeferencer do
:"#{lon}" => rand(180)
)
dataset.first.fetch(:the_geom).must_be_nil
dataset.first.fetch(:the_geom).should be_nil
georeferencer.populate_the_geom_from_latlon(table_name, lat, lon)
dataset.first.fetch(:the_geom).wont_be_nil
dataset.first.fetch(:the_geom).should be_nil
end
end #georeference
describe '#create_the_geom_in' do
before do
@table_name = create_table(@db)
end
it 'adds a the_geom column to a table' do
georeferencer = Importer2::Georeferencer.new(@db, @table_name)
georeferencer.column_exists_in?(@table_name, 'the_geom')
.should eq false
georeferencer.column_exists_in?(@table_name, 'the_geom').should eq false
georeferencer.create_the_geom_in(@table_name)
georeferencer.column_exists_in?(@table_name, 'the_geom')
.should eq true
georeferencer.column_exists_in?(@table_name, 'the_geom').should eq true
end
it 'returns false if the_geom column already exists' do
georeferencer = Importer2::Georeferencer.new(@db, @table_name)
georeferencer.column_exists_in?(@table_name, 'the_geom')
.should eq false
georeferencer.column_exists_in?(@table_name, 'the_geom').should eq false
georeferencer.create_the_geom_in(@table_name)
georeferencer.create_the_geom_in(@table_name).should eq false
end
end #create_the_geom_in
@ -145,7 +148,7 @@ describe Importer2::Georeferencer do
it 'returns the name of a latitude column within a set of candidates, if
existing' do
georeferencer = Importer2::Georeferencer.new(@db, @table_name)
georeferencer.latitude_column_name_in(@table_name).should eq 'lat'
georeferencer.latitude_column_name_in.should eq 'lat'
end
end
@ -153,7 +156,7 @@ describe Importer2::Georeferencer do
it 'returns the name of a longitude column within a set of candidates, if
existing' do
georeferencer = Importer2::Georeferencer.new(@db, @table_name)
georeferencer.longitude_column_name_in(@table_name).should eq 'lon'
georeferencer.longitude_column_name_in.should eq 'lon'
end
end
@ -187,6 +190,7 @@ describe Importer2::Georeferencer do
String :description
String latitude_column.to_sym
String longitude_column.to_sym
String :ogc_fid
end
table_name
@ -197,8 +201,8 @@ describe Importer2::Georeferencer do
name: 'bogus',
description: 'bogus',
lat: rand(90),
lon: rand(180)
lon: rand(180),
ogc_fid: rand(100)
}
end #random_record
end # Importer2::Georeferencer

Loading…
Cancel
Save