From 6ea5773976698078222a147db5350957d5871465 Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Mon, 14 Apr 2008 21:07:21 +0000 Subject: [PATCH] ActionItem280: fixed some bugs on the tests --- app/views/content_viewer/view_page.rhtml | 2 +- config/environment.rb | 3 --- lib/noosfero/core_ext/string.rb | 41 ++++++++++++++++++++++++++++++++++++++--- lib/noosfero/transliterations.rb | 41 +---------------------------------------- script/extract_sies_data.rb | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- test/functional/account_controller_test.rb | 8 ++++---- test/functional/cms_controller_test.rb | 1 + test/unit/slug_test.rb | 26 ++++++++++++++++++++++++++ test/unit/sqlite_extension_test.rb | 16 ---------------- test/unit/transliterations_test.rb | 1 - 10 files changed, 216 insertions(+), 78 deletions(-) create mode 100644 test/unit/slug_test.rb diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index 833f8f0..d8f69d2 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -2,7 +2,7 @@ <% # AddThis Button - if block_given? + if block_given? and File.exists?( RAILS_ROOT + '/config/web2.0.yml') opts = YAML.load_file( RAILS_ROOT + '/config/web2.0.yml' ) if opts['addthis'] opts = opts['addthis'] diff --git a/config/environment.rb b/config/environment.rb index a17dcfd..a1ac61d 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -91,9 +91,6 @@ Tag.hierarchical = true # several local libraries require 'noosfero' -# sqlite extensions to able to test/develop georeference with sqlite -require 'sqlite_extension' - # locally-developed modules require 'acts_as_filesystem' require 'acts_as_searchable' diff --git a/lib/noosfero/core_ext/string.rb b/lib/noosfero/core_ext/string.rb index eb54a28..246ffd6 100644 --- a/lib/noosfero/core_ext/string.rb +++ b/lib/noosfero/core_ext/string.rb @@ -1,7 +1,42 @@ -require 'noosfero/transliterations' - class String + + TRANSLITERATIONS = { + [ 'Á', 'À', 'À', 'Â', 'Ã', 'Ä' ] => 'A', + [ 'á', 'à', 'à', 'â', 'ã', 'ä', 'ª' ] => 'a', + [ 'É', 'È', 'Ê', 'Ë' ] => 'E', + [ 'é', 'è', 'ê', 'ë' ] => 'e', + [ 'Í', 'Ì', 'Î', 'Ï' ] => 'I', + [ 'í', 'ì', 'î', 'ï' ] => 'i', + [ 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'º' ] => 'O', + [ 'ó', 'ò', 'ô', 'ö', 'õ', 'º' ] => 'o', + [ 'Ú', 'Ù', 'Û', 'Ü' ] => 'U', + [ 'ú', 'ù', 'û', 'ü' ] => 'u', + [ 'Ç' ] => 'C', + [ 'ç' ] => 'c', + [ 'Ñ' ] => 'N', + [ 'ñ' ] => 'n', + [ 'Ÿ' ] => 'Y', + [ 'ÿ' ] => 'y', + } + + # transliterate a string (assumed to contain UTF-8 data) + # into ASCII by replacing non-ascii characters to their + # ASCII. + # + # The transliteration is, of course, lossy, and its performance is poor. + # Don't abuse this method. + def transliterate + + new = self.clone + TRANSLITERATIONS.each { |from,to| + from.each { |seq| + new.gsub!(seq, to) + } + } + new + end + def to_slug - transliterate.downcase.gsub( /[^-a-z0-9~\s\.:;+=_]/, '').gsub(/[\s:;=_+]+/, '-').gsub(/[\-]{2,}/, '-').to_s + transliterate.downcase.gsub( /[^-a-z0-9~\s\.:;+=_]/, '').gsub(/[\s:;=_+-]+/, '-').gsub(/-$/, '').to_s end end diff --git a/lib/noosfero/transliterations.rb b/lib/noosfero/transliterations.rb index 24ef6f4..690ee71 100644 --- a/lib/noosfero/transliterations.rb +++ b/lib/noosfero/transliterations.rb @@ -1,40 +1 @@ -module Noosfero::Transliterations - - TRANSLATION = { - [ 'Á', 'À', 'À', 'Â', 'Ã', 'Ä' ] => 'A', - [ 'á', 'à', 'à', 'â', 'ã', 'ä', 'ª' ] => 'a', - [ 'É', 'È', 'Ê', 'Ë' ] => 'E', - [ 'é', 'è', 'ê', 'ë' ] => 'e', - [ 'Í', 'Ì', 'Î', 'Ï' ] => 'I', - [ 'í', 'ì', 'î', 'ï' ] => 'i', - [ 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'º' ] => 'O', - [ 'ó', 'ò', 'ô', 'ö', 'õ', 'º' ] => 'o', - [ 'Ú', 'Ù', 'Û', 'Ü' ] => 'U', - [ 'ú', 'ù', 'û', 'ü' ] => 'u', - [ 'Ç' ] => 'C', - [ 'ç' ] => 'c', - [ 'Ñ' ] => 'N', - [ 'ñ' ] => 'n', - [ 'Ÿ' ] => 'Y', - [ 'ÿ' ] => 'y', - } - - # transliterate a string (assumed to contain UTF-8 data) - # into ASCII by replacing non-ascii characters to their - # ASCII. - # - # The transliteration is, of course, lossy, and its performance is poor. - # Don't abuse this method. - def transliterate - - new = self.clone - Noosfero::Transliterations::TRANSLATION.each { |from,to| - from.each { |seq| - new.gsub!(seq, to) - } - } - new - end -end - -String.send(:include, Noosfero::Transliterations) +# nothing diff --git a/script/extract_sies_data.rb b/script/extract_sies_data.rb index a74c46b..a4320d2 100644 --- a/script/extract_sies_data.rb +++ b/script/extract_sies_data.rb @@ -5,20 +5,48 @@ $LOAD_PATH.unshift('/usr/share/rails/activesupport/lib') require 'activerecord' require 'active_support' +require File.dirname(__FILE__) + "/../" + 'lib/noosfero/core_ext/string.rb' -LIMIT = 5 +LIMIT = (ENV['DUMP_ALL'] ? nil : 5) +DUMP_ALL = LIMIT.nil? # To connect with the database that contains the data to be extracted cofigure it in the 'database_farejador.yml' with the name 'farejador' -ActiveRecord::Base.establish_connection(YAML::load(IO.read('database_farejador.yml'))['farejador']) +ActiveRecord::Base.establish_connection(YAML::load(IO.read(File.dirname(__FILE__) + '/database_farejador.yml'))['farejador']) class Enterprise < ActiveRecord::Base set_table_name 'cons_dadosbasicos' + set_primary_key :id_sies + has_many :products, :foreign_key => 'V00', :conditions => "tipo = 'produto'" + has_many :input_products, :class_name => 'Product', :foreign_key => 'V00', :conditions => "tipo = 'insumo'" +end + +class Product < ActiveRecord::Base + set_table_name 'mapa_produtos' + belongs_to :category, :foreign_key => 'id_prod' end class Category < ActiveRecord::Base set_table_name 'lista_produtos' end +class Macroregion < ActiveRecord::Base + set_table_name 'macrorregioes' +end + +class State < ActiveRecord::Base + set_table_name 'estados' + set_primary_key :id_UF + has_one :macroregion, :foreign_key => 'UF' + + def cities + City.find(:all, :conditions => [ "id < 6000000 and id like '?%'", id_UF]) + end +end + +class City < ActiveRecord::Base + set_table_name 'cidades_ibge' +end + class Dumper def initialize @seq = 0 @@ -33,23 +61,130 @@ class Dumper end end - def dump(cat, parent = nil) - + def dump_category(cat, parent = nil) + @seqs[cat] = @seq - puts "cat#{@seq} = Category.create!(:name => #{pretty(cat.nome, cat.nome_alt).inspect}, :parent => #{parent ? 'cat' + @seqs[parent].to_s : 'nil' })" - @seq = @seq + 1 + puts <<-EOF +cat#{@seq} = ProductCategory.create!(:name => #{pretty(cat.nome, cat.nome_alt).inspect}, :parent => #{parent ? 'cat' + @seqs[parent].to_s : 'nil' }) +categories[#{cat.id}] = cat#{@seq}.id + EOF + @seq += 1 Category.find(:all, :conditions => { :id_mae => cat.id }).each do |child| - dump(child, cat) + dump_category(child, cat) if (DUMP_ALL || (@seq <= LIMIT)) + end + + end + + def dump_enterprise(ent) + email = nil + contato = nil + if (ent.corel =~ /@/) + email = ent.corel + else + contato = ent.corel + end + + endereco = ent.end + if ent.cep + endereco << " CEP: " << ent.cep + end + + puts <<-EOF +enterprise = Enterprise.create!( + :name => #{ent.nome.inspect}, + :identifier => #{ent.nome.to_slug.inspect}, + :contact_phone => #{ent.tel.inspect}, + :address => #{endereco.inspect}, + :lat => #{ent.lat.inspect}, + :lng => #{ent.long.inspect}, + :geocode_precision => #{ent.geomodificou.inspect}, + :data => { + :id_sies => #{ent.id_sies.inspect} + }, + :organization_info => OrganizationInfo.new( + :contact_email => #{email.inspect} + ) +) + EOF + + ent.products.each do |p| + cat = p.category + puts <<-EOF +enterprise.products.create!( + :name => #{cat.nome.inspect}, + :product_category_id => categories[#{cat.id}] +) + EOF + end + + ent.input_products.each do |i| + cat = i.category + puts <<-EOF +enterprise.consumptions.create!( + :product_category_id => categories[#{cat.id}] +) + EOF end end + def dump_city(city) + puts <<-EOF +Region.create!( + :name => #{city.cidade.inspect}, + :parent => STATES[#{city.id.to_s[0..1]}], + :lat => #{city.latitude}, + :lng => #{city.longitude} +) + EOF + end + end dumper = Dumper.new -Category.find(:all, :conditions => 'id_mae is null or id_mae = -1').each do |cat| - dumper.dump(cat, nil) + +puts "categories = {}" +Category.find(:all, :conditions => 'id_mae is null or id_mae = -1', :limit => LIMIT).each do |cat| + dumper.dump_category(cat, nil) end -# puts Enterprise.find(:all, :limit => LIMIT).to_xml +Enterprise.find(:all, :limit => LIMIT).each do |ent| + dumper.dump_enterprise(ent) +end + +puts <<-EOF +STATES = { + 12 => Region.find_by_name('Acre'), + 27 => Region.find_by_name('Alagoas'), + 13 => Region.find_by_name('Amazonas'), + 16 => Region.find_by_name('Amapá'), + 29 => Region.find_by_name('Bahia'), + 23 => Region.find_by_name('Ceará'), + 53 => Region.find_by_name('Distrito Federal'), + 32 => Region.find_by_name('Espírito Santo'), + 52 => Region.find_by_name('Goiás'), + 21 => Region.find_by_name('Maranhão'), + 31 => Region.find_by_name('Minas Gerais'), + 50 => Region.find_by_name('Mato Grosso do Sul'), + 51 => Region.find_by_name('Mato Grosso'), + 15 => Region.find_by_name('Pará'), + 25 => Region.find_by_name('Paraíba'), + 26 => Region.find_by_name('Pernambuco'), + 22 => Region.find_by_name('Piauí'), + 41 => Region.find_by_name('Paraná'), + 33 => Region.find_by_name('Rio de Janeiro'), + 24 => Region.find_by_name('Rio Grande do Norte'), + 11 => Region.find_by_name('Rondônia'), + 14 => Region.find_by_name('Roraima'), + 43 => Region.find_by_name('Rio Grande do Sul'), + 42 => Region.find_by_name('Santa Catarina'), + 28 => Region.find_by_name('Sergipe'), + 35 => Region.find_by_name('São Paulo'), + 17 => Region.find_by_name('Tocantins'), +} +EOF + +City.find(:all, :limit => LIMIT).each do |city| + dumper.dump_city(city) +end diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 7f56572..beb51bc 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -18,13 +18,13 @@ class AccountControllerTest < Test::Unit::TestCase end def test_should_login_and_redirect - post :login, :login => 'johndoe', :password => 'test' + post :login, :user => {:login => 'johndoe', :password => 'test'} assert session[:user] assert_response :redirect end def test_should_fail_login_and_not_redirect - post :login, :login => 'johndoe', :password => 'bad password' + post :login, :user => {:login => 'johndoe', :password => 'bad password'} assert_nil session[:user] assert_response :success end @@ -92,12 +92,12 @@ class AccountControllerTest < Test::Unit::TestCase end def test_should_remember_me - post :login, :login => 'johndoe', :password => 'test', :remember_me => "1" + post :login, :user => {:login => 'johndoe', :password => 'test'}, :remember_me => "1" assert_not_nil @response.cookies["auth_token"] end def test_should_not_remember_me - post :login, :login => 'johndoe', :password => 'test', :remember_me => "0" + post :login, :user => {:login => 'johndoe', :password => 'test'}, :remember_me => "0" assert_nil @response.cookies["auth_token"] end diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 8aa2afe..b3d51df 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -166,6 +166,7 @@ class CmsControllerTest < Test::Unit::TestCase assert_difference UploadedFile, :count do post :new, :type => UploadedFile.name, :profile => profile.identifier, :article => { :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')} end + assert_not_nil profile.articles.find_by_path('test.txt') end should 'be able to update an uploaded file' do diff --git a/test/unit/slug_test.rb b/test/unit/slug_test.rb new file mode 100644 index 0000000..85197ef --- /dev/null +++ b/test/unit/slug_test.rb @@ -0,0 +1,26 @@ +require File.dirname(__FILE__) + '/../test_helper' + +# tests for String#to_slug core extension. See lib/noosfero/core_ext/string.rb +class SlugTest < Test::Unit::TestCase + + should 'keep only alphanum' do + assert_equal 'abc', 'abc!)@(*#&@!*#*)'.to_slug + end + + should 'turn punctuation into dashes' do + assert_equal 'a-b-c-d-e-f', 'a:b;c+d=e_f'.to_slug + end + + should 'truncate dashes' do + assert_equal 'a-b-c', 'a---b: c ;;;'.to_slug + end + + should 'turn spaces into dashes' do + assert_equal 'a-b', 'a b'.to_slug + end + + should 'not remove dots' do + assert_equal 'a.b', 'a.b'.to_slug + end + +end diff --git a/test/unit/sqlite_extension_test.rb b/test/unit/sqlite_extension_test.rb index 9dc3363..4ce10b0 100644 --- a/test/unit/sqlite_extension_test.rb +++ b/test/unit/sqlite_extension_test.rb @@ -4,26 +4,10 @@ require File.dirname(__FILE__) + '/../test_helper' # will just pass. The idea is to test our local extensions to SQLite. class SQliteExtensionTest < Test::Unit::TestCase - should 'have sine function' do - assert_in_delta 0.0, ActiveRecord::Base.connection.execute('select sin(3.14159265358979) as sin').first['sin'], 0.0001 - end - - should 'have cosine function' do - assert_in_delta -1.0, ActiveRecord::Base.connection.execute('select cos(3.14159265358979) as cos').first['cos'], 0.0001 - end - should 'have power function' do assert_in_delta 8.0, ActiveRecord::Base.connection.execute('select pow(2.0, 3.0) as result').first['result'], 0.0001 end - should 'have arcsine function' do - assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select asin(1) as asin').first['asin'], 0.0001 - end - - should 'have arccosine function' do - assert_in_delta Math::PI, ActiveRecord::Base.connection.execute('select acos(-1.0) as acos').first['acos'], 0.0001 - end - should 'have radians function' do assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select radians(90) as rad').first['rad'], 0.0001 end diff --git a/test/unit/transliterations_test.rb b/test/unit/transliterations_test.rb index c4e3eea..808ee1c 100644 --- a/test/unit/transliterations_test.rb +++ b/test/unit/transliterations_test.rb @@ -1,5 +1,4 @@ require File.dirname(__FILE__) + '/../test_helper' -require 'noosfero/transliterations' class TransliterationsTest < Test::Unit::TestCase -- libgit2 0.21.2