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 'pg'
require 'sequel' require 'sequel'
require 'json' require 'json'
require 'yaml'
module CartoDB module CartoDB
module Importer2 module Importer2
module Factories module Factories
class PGConnection class PGConnection
def initialize def initialize(options = {})
raise( @options = options.reverse_merge(read_config)
"Please configure your database settings " + create_db if options[:create_db]
"in spec/factories/database.json"
) unless File.exists?(configuration_file)
@pg_options = ::JSON.parse(File.read(configuration_file))
end #initialize end #initialize
def connection def connection
Sequel.postgres(pg_options) Sequel.postgres(@options)
end #connection end #connection
def pg_options
Hash[@pg_options.map { |k, v| [k.to_sym, v] }]
end #pg_options
private 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 def load_from_json
File.join(File.dirname("#{__FILE__}"), 'database.json') json_config = File.join(File.dirname("#{__FILE__}"), 'database.json')
end #configuration_file 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 # PGConnection
end # Factories end # Factories
end # Importer2 end # Importer2
end # CartoDB end # CartoDB

@ -6,16 +6,19 @@ include CartoDB
describe Importer2::Georeferencer do describe Importer2::Georeferencer do
before(:all) 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') create_schema(@db, 'cdb_importer')
@db.execute('SET search_path TO cdb_importer,public') @db.execute('SET search_path TO cdb_importer,public')
@table_name = create_table(@db) @table_name = create_table(@db)
end end
after(:all) do after(:all) do
puts "Dropping table..."
@db.drop_table? @table_name @db.drop_table? @table_name
#TODO Drop schema #TODO Drop schema
@ -46,11 +49,11 @@ describe Importer2::Georeferencer do
georeferencer = Importer2::Georeferencer.new(@db, @table_name) georeferencer = Importer2::Georeferencer.new(@db, @table_name)
georeferencer.run georeferencer.run
dataset.first.fetch(:the_geom).wont_be_nil dataset.first.fetch(:the_geom).should_not be_nil
dataset.first.fetch(:the_geom).wont_be_empty dataset.first.fetch(:the_geom).should_not be_empty
end 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, table_name = create_table(@db,
latitude_column: 'bogus_1', latitude_column: 'bogus_1',
longitude_column: 'bogus_2' longitude_column: 'bogus_2'
@ -93,30 +96,30 @@ describe Importer2::Georeferencer do
:"#{lon}" => rand(180) :"#{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) 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
end #georeference end #georeference
describe '#create_the_geom_in' do describe '#create_the_geom_in' do
before do
@table_name = create_table(@db)
end
it 'adds a the_geom column to a table' do it 'adds a the_geom column to a table' do
georeferencer = Importer2::Georeferencer.new(@db, @table_name) georeferencer = Importer2::Georeferencer.new(@db, @table_name)
georeferencer.column_exists_in?(@table_name, 'the_geom') georeferencer.column_exists_in?(@table_name, 'the_geom').should eq false
.should eq false
georeferencer.create_the_geom_in(@table_name) georeferencer.create_the_geom_in(@table_name)
georeferencer.column_exists_in?(@table_name, 'the_geom') georeferencer.column_exists_in?(@table_name, 'the_geom').should eq true
.should eq true
end end
it 'returns false if the_geom column already exists' do it 'returns false if the_geom column already exists' do
georeferencer = Importer2::Georeferencer.new(@db, @table_name) georeferencer = Importer2::Georeferencer.new(@db, @table_name)
georeferencer.column_exists_in?(@table_name, 'the_geom') georeferencer.column_exists_in?(@table_name, 'the_geom').should eq false
.should eq false
georeferencer.create_the_geom_in(@table_name) georeferencer.create_the_geom_in(@table_name)
georeferencer.create_the_geom_in(@table_name).should eq false georeferencer.create_the_geom_in(@table_name).should eq false
end end
end #create_the_geom_in 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 it 'returns the name of a latitude column within a set of candidates, if
existing' do existing' do
georeferencer = Importer2::Georeferencer.new(@db, @table_name) 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
end end
@ -153,7 +156,7 @@ describe Importer2::Georeferencer do
it 'returns the name of a longitude column within a set of candidates, if it 'returns the name of a longitude column within a set of candidates, if
existing' do existing' do
georeferencer = Importer2::Georeferencer.new(@db, @table_name) 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
end end
@ -187,6 +190,7 @@ describe Importer2::Georeferencer do
String :description String :description
String latitude_column.to_sym String latitude_column.to_sym
String longitude_column.to_sym String longitude_column.to_sym
String :ogc_fid
end end
table_name table_name
@ -197,8 +201,8 @@ describe Importer2::Georeferencer do
name: 'bogus', name: 'bogus',
description: 'bogus', description: 'bogus',
lat: rand(90), lat: rand(90),
lon: rand(180) lon: rand(180),
ogc_fid: rand(100)
} }
end #random_record end #random_record
end # Importer2::Georeferencer end # Importer2::Georeferencer

Loading…
Cancel
Save