From 1fb84838d1ea57d5066441ea180fe07fc1b27395 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Wed, 30 Apr 2008 02:35:20 +0000 Subject: [PATCH] ActionItem280: PostgreSQL porting --- app/controllers/public/search_controller.rb | 2 +- app/models/article.rb | 2 +- app/models/category_finder.rb | 30 +++++------------------------- app/models/comment.rb | 2 +- app/models/environment_finder.rb | 26 +++----------------------- app/models/product.rb | 2 +- app/models/profile.rb | 2 +- test/functional/search_controller_test.rb | 4 ++-- test/unit/category_finder_test.rb | 43 +++++++++++++++++++++++++++---------------- test/unit/environment_finder_test.rb | 14 +++++++------- test/unit/sqlite_extension_test.rb | 34 ++++++++++++++++++++++------------ 11 files changed, 71 insertions(+), 90 deletions(-) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index a182441..e01b64e 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -67,7 +67,7 @@ class SearchController < ApplicationController @results = {} @names = {} SEARCH_IN.each do |key, description| - @results[key] = @finder.send(key, @filtered_query) if @searching[key] + @results[key] = @finder.find(key, @filtered_query) if @searching[key] @names[key] = gettext(description) end end diff --git a/app/models/article.rb b/app/models/article.rb index 6656860..423edc6 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -113,7 +113,7 @@ class Article < ActiveRecord::Base end def self.find_by_initial(initial) - self.find(:all, :order => 'articles.name', :conditions => [ 'articles.name like (?)', initial + '%']) + self.find(:all, :order => 'articles.name', :conditions => [ 'articles.name like (?) or articles.name like (?)', initial + '%', initial.upcase + '%']) end private diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index ec7da7d..11c6fe5 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -7,28 +7,8 @@ class CategoryFinder attr_reader :category_ids - def articles(query='*', options={}) - find_in_categorized(Article, query, options) - end - - def people(query='*', options={}) - find_in_categorized(Person, query, options) - end - - def communities(query='*', options={}) - find_in_categorized(Community, query, options) - end - - def enterprises(query='*', options={}) - find_in_categorized(Enterprise, query, options) - end - - def products(query='*', options={}) - find_in_categorized(Product, query, options) - end - - def comments(query='*', options={}) - find_in_categorized(Comment, query, options) + def find(asset, query) + find_in_categorized(asset.to_s.singularize.camelize.constantize, query) end def recent(asset, limit = 10) @@ -70,11 +50,11 @@ class CategoryFinder # FIXME copy/pasted from options_for_find above !!! case klass.name when 'Comment' - {:select => 'distinct comments.*', :joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id in (?) and comments.title like (?)', category_ids, initial + '%']} + {:select => 'distinct comments.*', :joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id in (?) and (comments.title like (?) or comments.title like (?))', category_ids, initial + '%', initial.upcase + '%']} when 'Product' - {:select => 'distinct products.*', :joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?) and products.name like (?)', category_ids, initial + '%']} + {:select => 'distinct products.*', :joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?) and (products.name like (?) or products.name like (?))', category_ids, initial + '%', initial.upcase + '%']} when 'Article', 'Person', 'Community', 'Enterprise' - {:include => 'categories', :conditions => ['categories.id IN (?) and %s.name like (?)' % klass.table_name, category_ids, initial + '%']} + {:include => 'categories', :conditions => ['categories.id IN (?) and (%s.name like (?) or %s.name like (?))' % [klass.table_name, klass.table_name], category_ids, initial + '%', initial.upcase + '%']} else raise "unreconized class #{klass.name}" end diff --git a/app/models/comment.rb b/app/models/comment.rb index 2824808..74276a5 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -42,7 +42,7 @@ class Comment < ActiveRecord::Base end def self.find_by_initial(initial) - self.find(:all, :order => 'comments.title', :conditions => ['comments.title like (?)', initial + '%']) + self.find(:all, :order => 'comments.title', :conditions => ['comments.title like (?) or comments.title like (?)', initial + '%', initial.upcase + '%']) end end diff --git a/app/models/environment_finder.rb b/app/models/environment_finder.rb index 3540dd4..1344c75 100644 --- a/app/models/environment_finder.rb +++ b/app/models/environment_finder.rb @@ -4,33 +4,13 @@ class EnvironmentFinder @environment = env end - def articles(query='*', options = {}) - @environment.articles.find_by_contents(query, {}, options) - end - - def people(query='*', options = {}) - @environment.people.find_by_contents(query, {}, options) - end - - def communities(query='*', options = {}) - @environment.communities.find_by_contents(query, {}, options) - end - - def products(query='*', options = {}) - @environment.products.find_by_contents(query, {}, options) - end - - def enterprises(query='*', options = {}) - @environment.enterprises.find_by_contents(query, {}, options) - end - - def comments(query='*', options = {}) - @environment.comments.find_by_contents(query, {}, options) + def find(asset, query) + @environment.send(asset).find_by_contents(query) end def recent(asset, limit = 10) with_options :limit => limit, :order => 'created_at desc, id desc' do |finder| - finder.send(asset, '*', {}) + @environment.send(asset).recent(limit) end end diff --git a/app/models/product.rb b/app/models/product.rb index c8eb26c..3fc13ff 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -39,7 +39,7 @@ class Product < ActiveRecord::Base end def self.find_by_initial(initial) - self.find(:all, :order => 'products.name', :conditions => [ 'products.name like (?)', initial + '%']) + self.find(:all, :order => 'products.name', :conditions => [ 'products.name like (?) or products.name like (?)', initial + '%', initial.upcase + '%']) end end diff --git a/app/models/profile.rb b/app/models/profile.rb index 95a0de0..0fd5104 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -290,7 +290,7 @@ class Profile < ActiveRecord::Base end def self.find_by_initial(initial) - self.find(:all, :order => 'profiles.name', :conditions => [ 'profiles.name like (?)', (initial + '%') ]) + self.find(:all, :order => 'profiles.name', :conditions => [ 'profiles.name like (?) or profiles.name like (?)', (initial + '%'), (initial.upcase + '%') ]) end end diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 3ee5770..5af467e 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -236,7 +236,7 @@ class SearchControllerTest < Test::Unit::TestCase get :assets, :asset => 'people' - assert_equal [p2,p1], assigns(:results)[:people].instance_variable_get('@results') + assert_equivalent [p2,p1], assigns(:results)[:people] end # 'assets' menu inside a category @@ -274,7 +274,7 @@ class SearchControllerTest < Test::Unit::TestCase c2 = Community.create!(:name => 'another beautiful community', :identifier => 'an_bea_comm', :environment => Environment.default) get :assets, :asset => 'communities' - assert_equal [c2, c1], assigns(:results)[:communities].instance_variable_get('@results') + assert_equivalent [c2, c1], assigns(:results)[:communities] end # 'assets' menu diff --git a/test/unit/category_finder_test.rb b/test/unit/category_finder_test.rb index 14f4795..f1e521c 100644 --- a/test/unit/category_finder_test.rb +++ b/test/unit/category_finder_test.rb @@ -19,8 +19,9 @@ class CategoryFinderTest < ActiveSupport::TestCase art2 = person.articles.build(:name => 'another article to be found') art2.save! - assert_includes @finder.articles, art1 - assert_not_includes @finder.articles, art2 + list = @finder.find(:articles, 'found') + assert_includes list, art1 + assert_not_includes list, art2 end should 'search for comments in a specific category' do @@ -37,35 +38,41 @@ class CategoryFinderTest < ActiveSupport::TestCase art2.save! comment2 = art2.comments.build(:title => 'comment to be found', :body => 'hfyfyh', :author => person); comment2.save! - assert_includes @finder.comments, comment1 - assert_not_includes @finder.comments, comment2 + list = @finder.find(:comments, 'found') + assert_includes list, comment1 + assert_not_includes list, comment2 end should 'search for enterprises in a specific category' do # in category - ent1 = Enterprise.create!(:name => 'testing enterprise 1', :identifier => 'test1', :categories => [@category]) + ent1 = Enterprise.create!(:name => 'beautiful enterprise 1', :identifier => 'test1', :categories => [@category]) # not in category - ent2 = Enterprise.create!(:name => 'testing enterprise 2', :identifier => 'test2') + ent2 = Enterprise.create!(:name => 'beautiful enterprise 2', :identifier => 'test2') - assert_includes @finder.enterprises, ent1 - assert_not_includes @finder.enterprises, ent2 + list = @finder.find(:enterprises, 'beautiful') + assert_includes list, ent1 + assert_not_includes list, ent2 end should 'search for people in a specific category' do p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.categories << @category; p1.save! p2 = create_user('people_2').person; p2.name = 'another beautiful person'; p2.save! - assert_includes @finder.people, p1 - assert_not_includes @finder.people, p2 + + list = @finder.find(:people, 'beautiful') + assert_includes list, p1 + assert_not_includes list, p2 end should 'search for communities in a specific category' do c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default) c2 = Community.create!(:name => 'another beautiful community', :identifier => 'an_bea_comm', :environment => Environment.default) c1.categories << @category; c1.save! - assert_includes @finder.communities, c1 - assert_not_includes @finder.communities, c2 + + list = @finder.find(:communities, 'beautiful') + assert_includes list, c1 + assert_not_includes list, c2 end should 'search for products in a specific category' do @@ -73,8 +80,10 @@ class CategoryFinderTest < ActiveSupport::TestCase ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') prod1 = ent1.products.create!(:name => 'a beautiful product') prod2 = ent2.products.create!(:name => 'another beautiful product') - assert_includes @finder.products, prod1 - assert_not_includes @finder.products, prod2 + + list = @finder.find(:products, 'beautiful') + assert_includes list, prod1 + assert_not_includes list, prod2 end should 'load ids for category full hierarchy' do @@ -93,7 +102,7 @@ class CategoryFinderTest < ActiveSupport::TestCase p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.categories << child; p1.save! f = CategoryFinder.new(parent) - assert_includes f.people, p1 + assert_includes f.find(:people, 'beautiful'), p1 end should 'list recent enterprises' do @@ -141,7 +150,9 @@ class CategoryFinderTest < ActiveSupport::TestCase p1.categories << parent; p1.save! f = CategoryFinder.new(parent) - result = f.people + result = f.find(:people, 'beautiful') + + assert_equivalent [p1], result assert_equal 1, result.size end diff --git a/test/unit/environment_finder_test.rb b/test/unit/environment_finder_test.rb index d79cafa..fc7bef5 100644 --- a/test/unit/environment_finder_test.rb +++ b/test/unit/environment_finder_test.rb @@ -8,39 +8,39 @@ class EnvironmentFinderTest < ActiveSupport::TestCase person = create_user('teste').person art = person.articles.build(:name => 'an article to be found'); art.save! finder = EnvironmentFinder.new(Environment.default) - assert_includes finder.articles, art + assert_includes finder.find(:articles, 'found'), art end should 'find people' do p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.save! finder = EnvironmentFinder.new(Environment.default) - assert_includes finder.people, p1 + assert_includes finder.find(:people, 'beautiful'), p1 end should 'find communities' do c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default) finder = EnvironmentFinder.new(Environment.default) - assert_includes finder.communities, c1 + assert_includes finder.find(:communities, 'beautiful'), c1 end should 'find comments' do person = create_user('teste').person art = person.articles.build(:name => 'an article to be found'); art.save! comment = art.comments.build(:title => 'comment to be found', :body => 'some sample text', :author => person); comment.save! - assert_includes EnvironmentFinder.new(Environment.default).comments('found'), comment + assert_includes EnvironmentFinder.new(Environment.default).find(:comments, 'found'), comment end should 'find products' do finder = EnvironmentFinder.new(Environment.default) ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') prod = ent.products.create!(:name => 'a beautiful product') - assert_includes finder.products, prod + assert_includes finder.find(:products, 'beautiful'), prod end should 'find enterprises' do finder = EnvironmentFinder.new(Environment.default) - ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') - assert_includes finder.enterprises, ent + ent = Enterprise.create!(:name => 'a beautiful enterprise', :identifier => 'teste') + assert_includes finder.find(:enterprises, 'beautiful'), ent end should 'list recent enterprises' do diff --git a/test/unit/sqlite_extension_test.rb b/test/unit/sqlite_extension_test.rb index 4ce10b0..85a1e29 100644 --- a/test/unit/sqlite_extension_test.rb +++ b/test/unit/sqlite_extension_test.rb @@ -4,21 +4,31 @@ 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 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 + if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i - 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 + 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 square root function' do - assert_in_delta 1.4142, ActiveRecord::Base.connection.execute('select sqrt(2) as sqrt').first['sqrt'], 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 + + should 'have square root function' do + assert_in_delta 1.4142, ActiveRecord::Base.connection.execute('select sqrt(2) as sqrt').first['sqrt'], 0.0001 + end + + should 'have a distance function' do + args = [32.918593, -96.958444, 32.951613, -96.958444].map{|l|l * Math::PI/180} + assert_in_delta 2.28402, ActiveRecord::Base.connection.execute("select spheric_distance(#{args.inspect[1..-2]}, 3963.19) as dist").first['dist'], 0.0001 + end + + else + + should 'just pass (not using SQLite)' do + assert true + end - should 'have a distance function' do - args = [32.918593, -96.958444, 32.951613, -96.958444].map{|l|l * Math::PI/180} - assert_in_delta 2.28402, ActiveRecord::Base.connection.execute("select spheric_distance(#{args.inspect[1..-2]}, 3963.19) as dist").first['dist'], 0.0001 end end -- libgit2 0.21.2