Commit 6ea5773976698078222a147db5350957d5871465
1 parent
0860e7b8
Exists in
master
and in
29 other branches
ActionItem280: fixed some bugs on the tests
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1660 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
10 changed files
with
216 additions
and
78 deletions
Show diff stats
app/views/content_viewer/view_page.rhtml
config/environment.rb
... | ... | @@ -91,9 +91,6 @@ Tag.hierarchical = true |
91 | 91 | # several local libraries |
92 | 92 | require 'noosfero' |
93 | 93 | |
94 | -# sqlite extensions to able to test/develop georeference with sqlite | |
95 | -require 'sqlite_extension' | |
96 | - | |
97 | 94 | # locally-developed modules |
98 | 95 | require 'acts_as_filesystem' |
99 | 96 | require 'acts_as_searchable' | ... | ... |
lib/noosfero/core_ext/string.rb
1 | -require 'noosfero/transliterations' | |
2 | - | |
3 | 1 | class String |
2 | + | |
3 | + TRANSLITERATIONS = { | |
4 | + [ 'Á', 'À', 'À', 'Â', 'Ã', 'Ä' ] => 'A', | |
5 | + [ 'á', 'à', 'à', 'â', 'ã', 'ä', 'ª' ] => 'a', | |
6 | + [ 'É', 'È', 'Ê', 'Ë' ] => 'E', | |
7 | + [ 'é', 'è', 'ê', 'ë' ] => 'e', | |
8 | + [ 'Í', 'Ì', 'Î', 'Ï' ] => 'I', | |
9 | + [ 'í', 'ì', 'î', 'ï' ] => 'i', | |
10 | + [ 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'º' ] => 'O', | |
11 | + [ 'ó', 'ò', 'ô', 'ö', 'õ', 'º' ] => 'o', | |
12 | + [ 'Ú', 'Ù', 'Û', 'Ü' ] => 'U', | |
13 | + [ 'ú', 'ù', 'û', 'ü' ] => 'u', | |
14 | + [ 'Ç' ] => 'C', | |
15 | + [ 'ç' ] => 'c', | |
16 | + [ 'Ñ' ] => 'N', | |
17 | + [ 'ñ' ] => 'n', | |
18 | + [ 'Ÿ' ] => 'Y', | |
19 | + [ 'ÿ' ] => 'y', | |
20 | + } | |
21 | + | |
22 | + # transliterate a string (assumed to contain UTF-8 data) | |
23 | + # into ASCII by replacing non-ascii characters to their | |
24 | + # ASCII. | |
25 | + # | |
26 | + # The transliteration is, of course, lossy, and its performance is poor. | |
27 | + # Don't abuse this method. | |
28 | + def transliterate | |
29 | + | |
30 | + new = self.clone | |
31 | + TRANSLITERATIONS.each { |from,to| | |
32 | + from.each { |seq| | |
33 | + new.gsub!(seq, to) | |
34 | + } | |
35 | + } | |
36 | + new | |
37 | + end | |
38 | + | |
4 | 39 | def to_slug |
5 | - transliterate.downcase.gsub( /[^-a-z0-9~\s\.:;+=_]/, '').gsub(/[\s:;=_+]+/, '-').gsub(/[\-]{2,}/, '-').to_s | |
40 | + transliterate.downcase.gsub( /[^-a-z0-9~\s\.:;+=_]/, '').gsub(/[\s:;=_+-]+/, '-').gsub(/-$/, '').to_s | |
6 | 41 | end |
7 | 42 | end | ... | ... |
lib/noosfero/transliterations.rb
1 | -module Noosfero::Transliterations | |
2 | - | |
3 | - TRANSLATION = { | |
4 | - [ 'Á', 'À', 'À', 'Â', 'Ã', 'Ä' ] => 'A', | |
5 | - [ 'á', 'à', 'à', 'â', 'ã', 'ä', 'ª' ] => 'a', | |
6 | - [ 'É', 'È', 'Ê', 'Ë' ] => 'E', | |
7 | - [ 'é', 'è', 'ê', 'ë' ] => 'e', | |
8 | - [ 'Í', 'Ì', 'Î', 'Ï' ] => 'I', | |
9 | - [ 'í', 'ì', 'î', 'ï' ] => 'i', | |
10 | - [ 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'º' ] => 'O', | |
11 | - [ 'ó', 'ò', 'ô', 'ö', 'õ', 'º' ] => 'o', | |
12 | - [ 'Ú', 'Ù', 'Û', 'Ü' ] => 'U', | |
13 | - [ 'ú', 'ù', 'û', 'ü' ] => 'u', | |
14 | - [ 'Ç' ] => 'C', | |
15 | - [ 'ç' ] => 'c', | |
16 | - [ 'Ñ' ] => 'N', | |
17 | - [ 'ñ' ] => 'n', | |
18 | - [ 'Ÿ' ] => 'Y', | |
19 | - [ 'ÿ' ] => 'y', | |
20 | - } | |
21 | - | |
22 | - # transliterate a string (assumed to contain UTF-8 data) | |
23 | - # into ASCII by replacing non-ascii characters to their | |
24 | - # ASCII. | |
25 | - # | |
26 | - # The transliteration is, of course, lossy, and its performance is poor. | |
27 | - # Don't abuse this method. | |
28 | - def transliterate | |
29 | - | |
30 | - new = self.clone | |
31 | - Noosfero::Transliterations::TRANSLATION.each { |from,to| | |
32 | - from.each { |seq| | |
33 | - new.gsub!(seq, to) | |
34 | - } | |
35 | - } | |
36 | - new | |
37 | - end | |
38 | -end | |
39 | - | |
40 | -String.send(:include, Noosfero::Transliterations) | |
1 | +# nothing | ... | ... |
script/extract_sies_data.rb
... | ... | @@ -5,20 +5,48 @@ $LOAD_PATH.unshift('/usr/share/rails/activesupport/lib') |
5 | 5 | |
6 | 6 | require 'activerecord' |
7 | 7 | require 'active_support' |
8 | +require File.dirname(__FILE__) + "/../" + 'lib/noosfero/core_ext/string.rb' | |
8 | 9 | |
9 | -LIMIT = 5 | |
10 | +LIMIT = (ENV['DUMP_ALL'] ? nil : 5) | |
11 | +DUMP_ALL = LIMIT.nil? | |
10 | 12 | |
11 | 13 | # To connect with the database that contains the data to be extracted cofigure it in the 'database_farejador.yml' with the name 'farejador' |
12 | -ActiveRecord::Base.establish_connection(YAML::load(IO.read('database_farejador.yml'))['farejador']) | |
14 | +ActiveRecord::Base.establish_connection(YAML::load(IO.read(File.dirname(__FILE__) + '/database_farejador.yml'))['farejador']) | |
13 | 15 | |
14 | 16 | class Enterprise < ActiveRecord::Base |
15 | 17 | set_table_name 'cons_dadosbasicos' |
18 | + set_primary_key :id_sies | |
19 | + has_many :products, :foreign_key => 'V00', :conditions => "tipo = 'produto'" | |
20 | + has_many :input_products, :class_name => 'Product', :foreign_key => 'V00', :conditions => "tipo = 'insumo'" | |
21 | +end | |
22 | + | |
23 | +class Product < ActiveRecord::Base | |
24 | + set_table_name 'mapa_produtos' | |
25 | + belongs_to :category, :foreign_key => 'id_prod' | |
16 | 26 | end |
17 | 27 | |
18 | 28 | class Category < ActiveRecord::Base |
19 | 29 | set_table_name 'lista_produtos' |
20 | 30 | end |
21 | 31 | |
32 | +class Macroregion < ActiveRecord::Base | |
33 | + set_table_name 'macrorregioes' | |
34 | +end | |
35 | + | |
36 | +class State < ActiveRecord::Base | |
37 | + set_table_name 'estados' | |
38 | + set_primary_key :id_UF | |
39 | + has_one :macroregion, :foreign_key => 'UF' | |
40 | + | |
41 | + def cities | |
42 | + City.find(:all, :conditions => [ "id < 6000000 and id like '?%'", id_UF]) | |
43 | + end | |
44 | +end | |
45 | + | |
46 | +class City < ActiveRecord::Base | |
47 | + set_table_name 'cidades_ibge' | |
48 | +end | |
49 | + | |
22 | 50 | class Dumper |
23 | 51 | def initialize |
24 | 52 | @seq = 0 |
... | ... | @@ -33,23 +61,130 @@ class Dumper |
33 | 61 | end |
34 | 62 | end |
35 | 63 | |
36 | - def dump(cat, parent = nil) | |
37 | - | |
64 | + def dump_category(cat, parent = nil) | |
65 | + | |
38 | 66 | @seqs[cat] = @seq |
39 | - puts "cat#{@seq} = Category.create!(:name => #{pretty(cat.nome, cat.nome_alt).inspect}, :parent => #{parent ? 'cat' + @seqs[parent].to_s : 'nil' })" | |
40 | - @seq = @seq + 1 | |
67 | + puts <<-EOF | |
68 | +cat#{@seq} = ProductCategory.create!(:name => #{pretty(cat.nome, cat.nome_alt).inspect}, :parent => #{parent ? 'cat' + @seqs[parent].to_s : 'nil' }) | |
69 | +categories[#{cat.id}] = cat#{@seq}.id | |
70 | + EOF | |
71 | + @seq += 1 | |
41 | 72 | |
42 | 73 | Category.find(:all, :conditions => { :id_mae => cat.id }).each do |child| |
43 | - dump(child, cat) | |
74 | + dump_category(child, cat) if (DUMP_ALL || (@seq <= LIMIT)) | |
75 | + end | |
76 | + | |
77 | + end | |
78 | + | |
79 | + def dump_enterprise(ent) | |
80 | + email = nil | |
81 | + contato = nil | |
82 | + if (ent.corel =~ /@/) | |
83 | + email = ent.corel | |
84 | + else | |
85 | + contato = ent.corel | |
86 | + end | |
87 | + | |
88 | + endereco = ent.end | |
89 | + if ent.cep | |
90 | + endereco << " CEP: " << ent.cep | |
91 | + end | |
92 | + | |
93 | + puts <<-EOF | |
94 | +enterprise = Enterprise.create!( | |
95 | + :name => #{ent.nome.inspect}, | |
96 | + :identifier => #{ent.nome.to_slug.inspect}, | |
97 | + :contact_phone => #{ent.tel.inspect}, | |
98 | + :address => #{endereco.inspect}, | |
99 | + :lat => #{ent.lat.inspect}, | |
100 | + :lng => #{ent.long.inspect}, | |
101 | + :geocode_precision => #{ent.geomodificou.inspect}, | |
102 | + :data => { | |
103 | + :id_sies => #{ent.id_sies.inspect} | |
104 | + }, | |
105 | + :organization_info => OrganizationInfo.new( | |
106 | + :contact_email => #{email.inspect} | |
107 | + ) | |
108 | +) | |
109 | + EOF | |
110 | + | |
111 | + ent.products.each do |p| | |
112 | + cat = p.category | |
113 | + puts <<-EOF | |
114 | +enterprise.products.create!( | |
115 | + :name => #{cat.nome.inspect}, | |
116 | + :product_category_id => categories[#{cat.id}] | |
117 | +) | |
118 | + EOF | |
119 | + end | |
120 | + | |
121 | + ent.input_products.each do |i| | |
122 | + cat = i.category | |
123 | + puts <<-EOF | |
124 | +enterprise.consumptions.create!( | |
125 | + :product_category_id => categories[#{cat.id}] | |
126 | +) | |
127 | + EOF | |
44 | 128 | end |
45 | 129 | |
46 | 130 | end |
47 | 131 | |
132 | + def dump_city(city) | |
133 | + puts <<-EOF | |
134 | +Region.create!( | |
135 | + :name => #{city.cidade.inspect}, | |
136 | + :parent => STATES[#{city.id.to_s[0..1]}], | |
137 | + :lat => #{city.latitude}, | |
138 | + :lng => #{city.longitude} | |
139 | +) | |
140 | + EOF | |
141 | + end | |
142 | + | |
48 | 143 | end |
49 | 144 | |
50 | 145 | dumper = Dumper.new |
51 | -Category.find(:all, :conditions => 'id_mae is null or id_mae = -1').each do |cat| | |
52 | - dumper.dump(cat, nil) | |
146 | + | |
147 | +puts "categories = {}" | |
148 | +Category.find(:all, :conditions => 'id_mae is null or id_mae = -1', :limit => LIMIT).each do |cat| | |
149 | + dumper.dump_category(cat, nil) | |
53 | 150 | end |
54 | 151 | |
55 | -# puts Enterprise.find(:all, :limit => LIMIT).to_xml | |
152 | +Enterprise.find(:all, :limit => LIMIT).each do |ent| | |
153 | + dumper.dump_enterprise(ent) | |
154 | +end | |
155 | + | |
156 | +puts <<-EOF | |
157 | +STATES = { | |
158 | + 12 => Region.find_by_name('Acre'), | |
159 | + 27 => Region.find_by_name('Alagoas'), | |
160 | + 13 => Region.find_by_name('Amazonas'), | |
161 | + 16 => Region.find_by_name('Amapá'), | |
162 | + 29 => Region.find_by_name('Bahia'), | |
163 | + 23 => Region.find_by_name('Ceará'), | |
164 | + 53 => Region.find_by_name('Distrito Federal'), | |
165 | + 32 => Region.find_by_name('Espírito Santo'), | |
166 | + 52 => Region.find_by_name('Goiás'), | |
167 | + 21 => Region.find_by_name('Maranhão'), | |
168 | + 31 => Region.find_by_name('Minas Gerais'), | |
169 | + 50 => Region.find_by_name('Mato Grosso do Sul'), | |
170 | + 51 => Region.find_by_name('Mato Grosso'), | |
171 | + 15 => Region.find_by_name('Pará'), | |
172 | + 25 => Region.find_by_name('Paraíba'), | |
173 | + 26 => Region.find_by_name('Pernambuco'), | |
174 | + 22 => Region.find_by_name('Piauí'), | |
175 | + 41 => Region.find_by_name('Paraná'), | |
176 | + 33 => Region.find_by_name('Rio de Janeiro'), | |
177 | + 24 => Region.find_by_name('Rio Grande do Norte'), | |
178 | + 11 => Region.find_by_name('Rondônia'), | |
179 | + 14 => Region.find_by_name('Roraima'), | |
180 | + 43 => Region.find_by_name('Rio Grande do Sul'), | |
181 | + 42 => Region.find_by_name('Santa Catarina'), | |
182 | + 28 => Region.find_by_name('Sergipe'), | |
183 | + 35 => Region.find_by_name('São Paulo'), | |
184 | + 17 => Region.find_by_name('Tocantins'), | |
185 | +} | |
186 | +EOF | |
187 | + | |
188 | +City.find(:all, :limit => LIMIT).each do |city| | |
189 | + dumper.dump_city(city) | |
190 | +end | ... | ... |
test/functional/account_controller_test.rb
... | ... | @@ -18,13 +18,13 @@ class AccountControllerTest < Test::Unit::TestCase |
18 | 18 | end |
19 | 19 | |
20 | 20 | def test_should_login_and_redirect |
21 | - post :login, :login => 'johndoe', :password => 'test' | |
21 | + post :login, :user => {:login => 'johndoe', :password => 'test'} | |
22 | 22 | assert session[:user] |
23 | 23 | assert_response :redirect |
24 | 24 | end |
25 | 25 | |
26 | 26 | def test_should_fail_login_and_not_redirect |
27 | - post :login, :login => 'johndoe', :password => 'bad password' | |
27 | + post :login, :user => {:login => 'johndoe', :password => 'bad password'} | |
28 | 28 | assert_nil session[:user] |
29 | 29 | assert_response :success |
30 | 30 | end |
... | ... | @@ -92,12 +92,12 @@ class AccountControllerTest < Test::Unit::TestCase |
92 | 92 | end |
93 | 93 | |
94 | 94 | def test_should_remember_me |
95 | - post :login, :login => 'johndoe', :password => 'test', :remember_me => "1" | |
95 | + post :login, :user => {:login => 'johndoe', :password => 'test'}, :remember_me => "1" | |
96 | 96 | assert_not_nil @response.cookies["auth_token"] |
97 | 97 | end |
98 | 98 | |
99 | 99 | def test_should_not_remember_me |
100 | - post :login, :login => 'johndoe', :password => 'test', :remember_me => "0" | |
100 | + post :login, :user => {:login => 'johndoe', :password => 'test'}, :remember_me => "0" | |
101 | 101 | assert_nil @response.cookies["auth_token"] |
102 | 102 | end |
103 | 103 | ... | ... |
test/functional/cms_controller_test.rb
... | ... | @@ -166,6 +166,7 @@ class CmsControllerTest < Test::Unit::TestCase |
166 | 166 | assert_difference UploadedFile, :count do |
167 | 167 | post :new, :type => UploadedFile.name, :profile => profile.identifier, :article => { :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')} |
168 | 168 | end |
169 | + assert_not_nil profile.articles.find_by_path('test.txt') | |
169 | 170 | end |
170 | 171 | |
171 | 172 | should 'be able to update an uploaded file' do | ... | ... |
... | ... | @@ -0,0 +1,26 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +# tests for String#to_slug core extension. See lib/noosfero/core_ext/string.rb | |
4 | +class SlugTest < Test::Unit::TestCase | |
5 | + | |
6 | + should 'keep only alphanum' do | |
7 | + assert_equal 'abc', 'abc!)@(*#&@!*#*)'.to_slug | |
8 | + end | |
9 | + | |
10 | + should 'turn punctuation into dashes' do | |
11 | + assert_equal 'a-b-c-d-e-f', 'a:b;c+d=e_f'.to_slug | |
12 | + end | |
13 | + | |
14 | + should 'truncate dashes' do | |
15 | + assert_equal 'a-b-c', 'a---b: c ;;;'.to_slug | |
16 | + end | |
17 | + | |
18 | + should 'turn spaces into dashes' do | |
19 | + assert_equal 'a-b', 'a b'.to_slug | |
20 | + end | |
21 | + | |
22 | + should 'not remove dots' do | |
23 | + assert_equal 'a.b', 'a.b'.to_slug | |
24 | + end | |
25 | + | |
26 | +end | ... | ... |
test/unit/sqlite_extension_test.rb
... | ... | @@ -4,26 +4,10 @@ require File.dirname(__FILE__) + '/../test_helper' |
4 | 4 | # will just pass. The idea is to test our local extensions to SQLite. |
5 | 5 | class SQliteExtensionTest < Test::Unit::TestCase |
6 | 6 | |
7 | - should 'have sine function' do | |
8 | - assert_in_delta 0.0, ActiveRecord::Base.connection.execute('select sin(3.14159265358979) as sin').first['sin'], 0.0001 | |
9 | - end | |
10 | - | |
11 | - should 'have cosine function' do | |
12 | - assert_in_delta -1.0, ActiveRecord::Base.connection.execute('select cos(3.14159265358979) as cos').first['cos'], 0.0001 | |
13 | - end | |
14 | - | |
15 | 7 | should 'have power function' do |
16 | 8 | assert_in_delta 8.0, ActiveRecord::Base.connection.execute('select pow(2.0, 3.0) as result').first['result'], 0.0001 |
17 | 9 | end |
18 | 10 | |
19 | - should 'have arcsine function' do | |
20 | - assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select asin(1) as asin').first['asin'], 0.0001 | |
21 | - end | |
22 | - | |
23 | - should 'have arccosine function' do | |
24 | - assert_in_delta Math::PI, ActiveRecord::Base.connection.execute('select acos(-1.0) as acos').first['acos'], 0.0001 | |
25 | - end | |
26 | - | |
27 | 11 | should 'have radians function' do |
28 | 12 | assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select radians(90) as rad').first['rad'], 0.0001 |
29 | 13 | end | ... | ... |