Commit 6d432a84fa350e5d9f1b4e0cc7c885aeba6fd713
1 parent
ee116023
Exists in
master
and in
29 other branches
Added small fixes and unit tests for all searchable models
Showing
11 changed files
with
254 additions
and
30 deletions
Show diff stats
app/models/article.rb
@@ -55,7 +55,7 @@ class Article < ActiveRecord::Base | @@ -55,7 +55,7 @@ class Article < ActiveRecord::Base | ||
55 | xss_terminate :only => [ :name ], :on => 'validation', :with => 'white_list' | 55 | xss_terminate :only => [ :name ], :on => 'validation', :with => 'white_list' |
56 | 56 | ||
57 | named_scope :in_category, lambda { |category| | 57 | named_scope :in_category, lambda { |category| |
58 | - {:include => 'categories', :conditions => { 'categories.id' => category.id }} | 58 | + {:include => 'categories_including_virtual', :conditions => { 'categories.id' => category.id }} |
59 | } | 59 | } |
60 | 60 | ||
61 | named_scope :by_range, lambda { |range| { | 61 | named_scope :by_range, lambda { |range| { |
@@ -599,6 +599,7 @@ class Article < ActiveRecord::Base | @@ -599,6 +599,7 @@ class Article < ActiveRecord::Base | ||
599 | def self.f_type_proc(klass) | 599 | def self.f_type_proc(klass) |
600 | klass.constantize.type_name | 600 | klass.constantize.type_name |
601 | end | 601 | end |
602 | + | ||
602 | def self.f_profile_type_proc(klass) | 603 | def self.f_profile_type_proc(klass) |
603 | klass.constantize.type_name | 604 | klass.constantize.type_name |
604 | end | 605 | end |
@@ -612,12 +613,15 @@ class Article < ActiveRecord::Base | @@ -612,12 +613,15 @@ class Article < ActiveRecord::Base | ||
612 | self.class.name | 613 | self.class.name |
613 | end | 614 | end |
614 | end | 615 | end |
616 | + | ||
615 | def f_profile_type | 617 | def f_profile_type |
616 | self.profile.class.name | 618 | self.profile.class.name |
617 | end | 619 | end |
620 | + | ||
618 | def f_published_at | 621 | def f_published_at |
619 | self.published_at | 622 | self.published_at |
620 | end | 623 | end |
624 | + | ||
621 | def f_category | 625 | def f_category |
622 | self.categories.collect(&:name) | 626 | self.categories.collect(&:name) |
623 | end | 627 | end |
@@ -626,12 +630,15 @@ class Article < ActiveRecord::Base | @@ -626,12 +630,15 @@ class Article < ActiveRecord::Base | ||
626 | def name_sortable # give a different name for solr | 630 | def name_sortable # give a different name for solr |
627 | name | 631 | name |
628 | end | 632 | end |
633 | + | ||
629 | def public | 634 | def public |
630 | self.public? | 635 | self.public? |
631 | end | 636 | end |
637 | + | ||
632 | def category_filter | 638 | def category_filter |
633 | categories_including_virtual_ids | 639 | categories_including_virtual_ids |
634 | end | 640 | end |
641 | + | ||
635 | public | 642 | public |
636 | 643 | ||
637 | acts_as_faceted :fields => { | 644 | acts_as_faceted :fields => { |
app/models/product.rb
@@ -225,7 +225,7 @@ class Product < ActiveRecord::Base | @@ -225,7 +225,7 @@ class Product < ActiveRecord::Base | ||
225 | end | 225 | end |
226 | end | 226 | end |
227 | def self.f_qualifier_proc(ids) | 227 | def self.f_qualifier_proc(ids) |
228 | - array = ids.split ' ' | 228 | + array = ids.split |
229 | qualifier = Qualifier.find_by_id array[0] | 229 | qualifier = Qualifier.find_by_id array[0] |
230 | certifier = Certifier.find_by_id array[1] | 230 | certifier = Certifier.find_by_id array[1] |
231 | certifier ? [qualifier.name, _(' cert. ') + certifier.name] : qualifier.name | 231 | certifier ? [qualifier.name, _(' cert. ') + certifier.name] : qualifier.name |
app/models/textile_article.rb
app/models/tiny_mce_article.rb
test/unit/article_test.rb
@@ -157,7 +157,7 @@ class ArticleTest < ActiveSupport::TestCase | @@ -157,7 +157,7 @@ class ArticleTest < ActiveSupport::TestCase | ||
157 | fifth = fast_create(TextArticle, :profile_id => profile.id, :name => 'fifth') | 157 | fifth = fast_create(TextArticle, :profile_id => profile.id, :name => 'fifth') |
158 | 158 | ||
159 | other_first = other_profile.articles.build(:name => 'first'); other_first.save! | 159 | other_first = other_profile.articles.build(:name => 'first'); other_first.save! |
160 | - | 160 | + |
161 | assert_equal [other_first, fifth, fourth], Article.recent(3) | 161 | assert_equal [other_first, fifth, fourth], Article.recent(3) |
162 | assert_equal [other_first, fifth, fourth, third, second, first], Article.recent(6) | 162 | assert_equal [other_first, fifth, fourth, third, second, first], Article.recent(6) |
163 | end | 163 | end |
@@ -290,7 +290,9 @@ class ArticleTest < ActiveSupport::TestCase | @@ -290,7 +290,9 @@ class ArticleTest < ActiveSupport::TestCase | ||
290 | 290 | ||
291 | should 'associate with categories' do | 291 | should 'associate with categories' do |
292 | env = Environment.default | 292 | env = Environment.default |
293 | - c1 = env.categories.build(:name => "test category 1"); c1.save! | 293 | + parent_cat = env.categories.build(:name => "parent category") |
294 | + parent_cat.save! | ||
295 | + c1 = env.categories.build(:name => "test category 1", :parent_id => parent_cat.id); c1.save! | ||
294 | c2 = env.categories.build(:name => "test category 2"); c2.save! | 296 | c2 = env.categories.build(:name => "test category 2"); c2.save! |
295 | 297 | ||
296 | article = profile.articles.build(:name => 'withcategories') | 298 | article = profile.articles.build(:name => 'withcategories') |
@@ -300,6 +302,7 @@ class ArticleTest < ActiveSupport::TestCase | @@ -300,6 +302,7 @@ class ArticleTest < ActiveSupport::TestCase | ||
300 | article.add_category c2 | 302 | article.add_category c2 |
301 | 303 | ||
302 | assert_equivalent [c1,c2], article.categories(true) | 304 | assert_equivalent [c1,c2], article.categories(true) |
305 | + assert_equivalent [c1, parent_cat, c2], article.categories_including_virtual(true) | ||
303 | end | 306 | end |
304 | 307 | ||
305 | should 'remove comments when removing article' do | 308 | should 'remove comments when removing article' do |
@@ -408,6 +411,9 @@ class ArticleTest < ActiveSupport::TestCase | @@ -408,6 +411,9 @@ class ArticleTest < ActiveSupport::TestCase | ||
408 | assert_includes c3.articles(true), art | 411 | assert_includes c3.articles(true), art |
409 | assert_includes c2.articles(true), art | 412 | assert_includes c2.articles(true), art |
410 | assert_includes c1.articles(true), art | 413 | assert_includes c1.articles(true), art |
414 | + | ||
415 | + assert_includes art.categories_including_virtual(true), c2 | ||
416 | + assert_includes art.categories_including_virtual(true), c1 | ||
411 | end | 417 | end |
412 | 418 | ||
413 | should 'redefine the entire category set at once' do | 419 | should 'redefine the entire category set at once' do |
@@ -423,26 +429,31 @@ class ArticleTest < ActiveSupport::TestCase | @@ -423,26 +429,31 @@ class ArticleTest < ActiveSupport::TestCase | ||
423 | art.category_ids = [c2,c3].map(&:id) | 429 | art.category_ids = [c2,c3].map(&:id) |
424 | 430 | ||
425 | assert_equivalent [c2, c3], art.categories(true) | 431 | assert_equivalent [c2, c3], art.categories(true) |
432 | + assert_includes art.categories_including_virtual(true), c1 | ||
433 | + assert !art.categories_including_virtual(true).include?(c4) | ||
426 | end | 434 | end |
427 | 435 | ||
428 | should 'be able to create an article already with categories' do | 436 | should 'be able to create an article already with categories' do |
429 | - c1 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c1') | 437 | + parent1 = fast_create(Category, :environment_id => Environment.default.id, :name => 'parent1') |
438 | + c1 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c1', :parent_id => parent1.id) | ||
430 | c2 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c2') | 439 | c2 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c2') |
431 | 440 | ||
432 | p = create_user('testinguser').person | 441 | p = create_user('testinguser').person |
433 | a = p.articles.create!(:name => 'test', :category_ids => [c1.id, c2.id]) | 442 | a = p.articles.create!(:name => 'test', :category_ids => [c1.id, c2.id]) |
434 | 443 | ||
435 | assert_equivalent [c1, c2], a.categories(true) | 444 | assert_equivalent [c1, c2], a.categories(true) |
445 | + assert_includes a.categories_including_virtual(true), parent1 | ||
436 | end | 446 | end |
437 | 447 | ||
438 | should 'not add a category twice to article' do | 448 | should 'not add a category twice to article' do |
439 | c1 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c1') | 449 | c1 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c1') |
440 | - c2 = c1.children.create!(:environment => Environment.default, :name => 'c2') | ||
441 | - c3 = c1.children.create!(:environment => Environment.default, :name => 'c3') | 450 | + c2 = c1.children.create!(:environment => Environment.default, :name => 'c2', :parent_id => c1.id) |
451 | + c3 = c1.children.create!(:environment => Environment.default, :name => 'c3', :parent_id => c1.id) | ||
442 | owner = create_user('testuser').person | 452 | owner = create_user('testuser').person |
443 | art = owner.articles.create!(:name => 'ytest') | 453 | art = owner.articles.create!(:name => 'ytest') |
444 | art.category_ids = [c2,c3,c3].map(&:id) | 454 | art.category_ids = [c2,c3,c3].map(&:id) |
445 | assert_equal [c2, c3], art.categories(true) | 455 | assert_equal [c2, c3], art.categories(true) |
456 | + assert_equal [c2, c1, c3], art.categories_including_virtual(true) | ||
446 | end | 457 | end |
447 | 458 | ||
448 | should 'not accept Product category as category' do | 459 | should 'not accept Product category as category' do |
@@ -459,8 +470,8 @@ class ArticleTest < ActiveSupport::TestCase | @@ -459,8 +470,8 @@ class ArticleTest < ActiveSupport::TestCase | ||
459 | article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false) | 470 | article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false) |
460 | 471 | ||
461 | assert !article.display_to?(nil) | 472 | assert !article.display_to?(nil) |
462 | - end | ||
463 | - | 473 | + end |
474 | + | ||
464 | should 'say that not member of profile cannot see private article' do | 475 | should 'say that not member of profile cannot see private article' do |
465 | profile = fast_create(Profile, :name => 'test profile', :identifier => 'test_profile') | 476 | profile = fast_create(Profile, :name => 'test profile', :identifier => 'test_profile') |
466 | article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false) | 477 | article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false) |
@@ -468,7 +479,7 @@ class ArticleTest < ActiveSupport::TestCase | @@ -468,7 +479,7 @@ class ArticleTest < ActiveSupport::TestCase | ||
468 | 479 | ||
469 | assert !article.display_to?(person) | 480 | assert !article.display_to?(person) |
470 | end | 481 | end |
471 | - | 482 | + |
472 | should 'say that member user can not see private article' do | 483 | should 'say that member user can not see private article' do |
473 | profile = fast_create(Profile, :name => 'test profile', :identifier => 'test_profile') | 484 | profile = fast_create(Profile, :name => 'test profile', :identifier => 'test_profile') |
474 | article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false) | 485 | article = fast_create(Article, :name => 'test article', :profile_id => profile.id, :published => false) |
@@ -553,7 +564,7 @@ class ArticleTest < ActiveSupport::TestCase | @@ -553,7 +564,7 @@ class ArticleTest < ActiveSupport::TestCase | ||
553 | person = create_user('test_user').person | 564 | person = create_user('test_user').person |
554 | a = person.articles.create!(:name => 'test article', :body => 'some text') | 565 | a = person.articles.create!(:name => 'test article', :body => 'some text') |
555 | b = a.copy(:parent => a, :profile => a.profile) | 566 | b = a.copy(:parent => a, :profile => a.profile) |
556 | - | 567 | + |
557 | assert_includes a.children, b | 568 | assert_includes a.children, b |
558 | assert_equal 'some text', b.body | 569 | assert_equal 'some text', b.body |
559 | end | 570 | end |
@@ -752,10 +763,12 @@ class ArticleTest < ActiveSupport::TestCase | @@ -752,10 +763,12 @@ class ArticleTest < ActiveSupport::TestCase | ||
752 | 763 | ||
753 | should 'ignore category with zero as id' do | 764 | should 'ignore category with zero as id' do |
754 | a = profile.articles.create!(:name => 'a test article') | 765 | a = profile.articles.create!(:name => 'a test article') |
755 | - c = fast_create(Category, :name => 'test category', :environment_id => profile.environment.id) | 766 | + c = fast_create(Category, :name => 'test category', :environment_id => profile.environment.id, :parent_id => 0) |
756 | a.category_ids = ['0', c.id, nil] | 767 | a.category_ids = ['0', c.id, nil] |
757 | assert a.save | 768 | assert a.save |
758 | assert_equal [c], a.categories | 769 | assert_equal [c], a.categories |
770 | + # also ignore parent with id = 0 | ||
771 | + assert_equal [c], a.categories_including_virtual | ||
759 | 772 | ||
760 | a = profile.articles.find_by_name 'a test article' | 773 | a = profile.articles.find_by_name 'a test article' |
761 | assert_equal [c], a.categories | 774 | assert_equal [c], a.categories |
@@ -810,7 +823,8 @@ class ArticleTest < ActiveSupport::TestCase | @@ -810,7 +823,8 @@ class ArticleTest < ActiveSupport::TestCase | ||
810 | 823 | ||
811 | should 'find articles in a specific category' do | 824 | should 'find articles in a specific category' do |
812 | env = Environment.default | 825 | env = Environment.default |
813 | - category_with_articles = env.categories.create!(:name => "Category with articles") | 826 | + parent_category = env.categories.create!(:name => "parent category") |
827 | + category_with_articles = env.categories.create!(:name => "Category with articles", :parent_id => parent_category.id) | ||
814 | category_without_articles = env.categories.create!(:name => "Category without articles") | 828 | category_without_articles = env.categories.create!(:name => "Category without articles") |
815 | 829 | ||
816 | article_in_category = profile.articles.create!(:name => 'Article in category') | 830 | article_in_category = profile.articles.create!(:name => 'Article in category') |
@@ -818,6 +832,7 @@ class ArticleTest < ActiveSupport::TestCase | @@ -818,6 +832,7 @@ class ArticleTest < ActiveSupport::TestCase | ||
818 | article_in_category.add_category(category_with_articles) | 832 | article_in_category.add_category(category_with_articles) |
819 | 833 | ||
820 | assert_includes profile.articles.in_category(category_with_articles), article_in_category | 834 | assert_includes profile.articles.in_category(category_with_articles), article_in_category |
835 | + assert_includes profile.articles.in_category(parent_category), article_in_category | ||
821 | assert_not_includes profile.articles.in_category(category_without_articles), article_in_category | 836 | assert_not_includes profile.articles.in_category(category_without_articles), article_in_category |
822 | end | 837 | end |
823 | 838 | ||
@@ -954,7 +969,7 @@ class ArticleTest < ActiveSupport::TestCase | @@ -954,7 +969,7 @@ class ArticleTest < ActiveSupport::TestCase | ||
954 | 969 | ||
955 | should 'track action when a published article is created in a community' do | 970 | should 'track action when a published article is created in a community' do |
956 | community = fast_create(Community) | 971 | community = fast_create(Community) |
957 | - p1 = ActionTracker::Record.current_user_from_model | 972 | + p1 = ActionTracker::Record.current_user_from_model |
958 | p2 = fast_create(Person) | 973 | p2 = fast_create(Person) |
959 | p3 = fast_create(Person) | 974 | p3 = fast_create(Person) |
960 | community.add_member(p1) | 975 | community.add_member(p1) |
@@ -1064,7 +1079,7 @@ class ArticleTest < ActiveSupport::TestCase | @@ -1064,7 +1079,7 @@ class ArticleTest < ActiveSupport::TestCase | ||
1064 | assert_equal false, a.notifiable? | 1079 | assert_equal false, a.notifiable? |
1065 | assert_equal true, a.advertise? | 1080 | assert_equal true, a.advertise? |
1066 | assert_equal false, a.is_trackable? | 1081 | assert_equal false, a.is_trackable? |
1067 | - | 1082 | + |
1068 | a.published=false | 1083 | a.published=false |
1069 | assert_equal false, a.published? | 1084 | assert_equal false, a.published? |
1070 | assert_equal false, a.is_trackable? | 1085 | assert_equal false, a.is_trackable? |
@@ -1643,4 +1658,67 @@ class ArticleTest < ActiveSupport::TestCase | @@ -1643,4 +1658,67 @@ class ArticleTest < ActiveSupport::TestCase | ||
1643 | assert_equal [c1,c2,c5], Article.text_articles | 1658 | assert_equal [c1,c2,c5], Article.text_articles |
1644 | end | 1659 | end |
1645 | 1660 | ||
1661 | + should 'act as faceted' do | ||
1662 | + person = fast_create(Person) | ||
1663 | + a = Article.new(:profile_id => person.id) | ||
1664 | + assert_equal Article.type_name, Article.facet_by_id(:f_type)[:proc].call(a.send(:f_type)) | ||
1665 | + assert_equal Person.type_name, Article.facet_by_id(:f_profile_type)[:proc].call(a.send(:f_profile_type)) | ||
1666 | + assert_equal a.published_at, a.send(:f_published_at) | ||
1667 | + end | ||
1668 | + | ||
1669 | + should 'act as searchable' do | ||
1670 | + person = fast_create(Person, :name => "Hiro", :address => 'U-Stor-It @ Inglewood, California', | ||
1671 | + :nickname => 'Protagonist') | ||
1672 | + person2 = fast_create(Person, :name => "Raven") | ||
1673 | + category = fast_create(Category, :name => "science fiction", :acronym => "sf", :abbreviation => "sci-fi") | ||
1674 | + a = Article.create!(:name => 'a searchable article about bananas', :profile_id => person.id, | ||
1675 | + :body => 'the body talks about mosquitos', :abstract => 'and the abstract is about beer', | ||
1676 | + :filename => 'not_a_virus.exe') | ||
1677 | + a.add_category(category) | ||
1678 | + c = a.comments.build(:title => 'snow crash', :author => person2, :body => 'wanna try some?') | ||
1679 | + c.save! | ||
1680 | + | ||
1681 | + # fields | ||
1682 | + assert_includes Article.find_by_contents('bananas')[:results].docs, a | ||
1683 | + assert_includes Article.find_by_contents('mosquitos')[:results].docs, a | ||
1684 | + assert_includes Article.find_by_contents('beer')[:results].docs, a | ||
1685 | + assert_includes Article.find_by_contents('not_a_virus.exe')[:results].docs, a | ||
1686 | + # filters | ||
1687 | + assert_includes Article.find_by_contents('bananas', {}, { | ||
1688 | + :filter_queries => ["public:true"]})[:results].docs, a | ||
1689 | + assert_not_includes Article.find_by_contents('bananas', {}, { | ||
1690 | + :filter_queries => ["public:false"]})[:results].docs, a | ||
1691 | + assert_includes Article.find_by_contents('bananas', {}, { | ||
1692 | + :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, a | ||
1693 | + assert_includes Article.find_by_contents('bananas', {}, { | ||
1694 | + :filter_queries => ["profile_id:\"#{person.id}\""]})[:results].docs, a | ||
1695 | + # includes | ||
1696 | + assert_includes Article.find_by_contents('Hiro')[:results].docs, a | ||
1697 | + assert_includes Article.find_by_contents("person-#{person.id}")[:results].docs, a | ||
1698 | + assert_includes Article.find_by_contents("California")[:results].docs, a | ||
1699 | + assert_includes Article.find_by_contents("Protagonist")[:results].docs, a | ||
1700 | + assert_includes Article.find_by_contents("snow")[:results].docs, a | ||
1701 | + assert_includes Article.find_by_contents("try some")[:results].docs, a | ||
1702 | + assert_includes Article.find_by_contents("Raven")[:results].docs, a | ||
1703 | + assert_includes Article.find_by_contents("science")[:results].docs, a | ||
1704 | + assert_includes Article.find_by_contents(category.slug)[:results].docs, a | ||
1705 | + assert_includes Article.find_by_contents("sf")[:results].docs, a | ||
1706 | + assert_includes Article.find_by_contents("sci-fi")[:results].docs, a | ||
1707 | + end | ||
1708 | + | ||
1709 | + should 'boost name matches' do | ||
1710 | + person = fast_create(Person) | ||
1711 | + in_body = Article.create!(:name => 'something', :profile_id => person.id, :body => 'bananas in the body!') | ||
1712 | + in_name = Article.create!(:name => 'bananas in the name!', :profile_id => person.id) | ||
1713 | + assert_equal [in_name, in_body], Article.find_by_contents('bananas')[:results].docs | ||
1714 | + end | ||
1715 | + | ||
1716 | + should 'boost if profile is enabled' do | ||
1717 | + person2 = fast_create(Person, :enabled => false) | ||
1718 | + art_profile_disabled = Article.create!(:name => 'profile disabled', :profile_id => person2.id) | ||
1719 | + person1 = fast_create(Person, :enabled => true) | ||
1720 | + art_profile_enabled = Article.create!(:name => 'profile enabled', :profile_id => person1.id) | ||
1721 | + assert_equal [art_profile_enabled, art_profile_disabled], Article.find_by_contents('profile')[:results].docs | ||
1722 | + end | ||
1723 | + | ||
1646 | end | 1724 | end |
test/unit/category_test.rb
@@ -492,4 +492,30 @@ class CategoryTest < ActiveSupport::TestCase | @@ -492,4 +492,30 @@ class CategoryTest < ActiveSupport::TestCase | ||
492 | assert_not_includes Category.top_level_for(Environment.default).from_types(['ProductCategory']), toplevel_category | 492 | assert_not_includes Category.top_level_for(Environment.default).from_types(['ProductCategory']), toplevel_category |
493 | end | 493 | end |
494 | 494 | ||
495 | + should 'act as searchable' do | ||
496 | + parent = fast_create(Category, :name => 'books') | ||
497 | + c = Category.create!(:name => "science fiction", :acronym => "sf", :abbreviation => "sci-fi", | ||
498 | + :environment_id => Environment.default.id, :parent_id => parent.id) | ||
499 | + | ||
500 | + # fields | ||
501 | + assert_includes Category.find_by_contents('fiction')[:results].docs, c | ||
502 | + assert_includes Category.find_by_contents('sf')[:results].docs, c | ||
503 | + assert_includes Category.find_by_contents('sci-fi')[:results].docs, c | ||
504 | + # filters | ||
505 | + assert_includes Category.find_by_contents('science', {}, { | ||
506 | + :filter_queries => ["parent_id:#{parent.id}"]})[:results].docs, c | ||
507 | + end | ||
508 | + | ||
509 | + should 'boost name matches' do | ||
510 | + c_abbr = Category.create!(:name => "something else", :abbreviation => "science", :environment_id => Environment.default.id) | ||
511 | + c_name = Category.create!(:name => "science fiction", :environment_id => Environment.default.id) | ||
512 | + assert_equal [c_name, c_abbr], Category.find_by_contents("science")[:results].docs | ||
513 | + end | ||
514 | + | ||
515 | + should 'solr save' do | ||
516 | + c = @env.categories.build(:name => 'my category'); | ||
517 | + c.expects(:solr_save) | ||
518 | + c.save! | ||
519 | + end | ||
520 | + | ||
495 | end | 521 | end |
test/unit/product_test.rb
@@ -20,14 +20,14 @@ class ProductTest < ActiveSupport::TestCase | @@ -20,14 +20,14 @@ class ProductTest < ActiveSupport::TestCase | ||
20 | assert_difference Product, :count do | 20 | assert_difference Product, :count do |
21 | p = Product.new(:name => 'test product1', :product_category => @product_category, :enterprise_id => @profile.id) | 21 | p = Product.new(:name => 'test product1', :product_category => @product_category, :enterprise_id => @profile.id) |
22 | assert p.save | 22 | assert p.save |
23 | - end | 23 | + end |
24 | end | 24 | end |
25 | 25 | ||
26 | should 'destroy product' do | 26 | should 'destroy product' do |
27 | p = fast_create(Product, :name => 'test product2', :product_category_id => @product_category.id) | 27 | p = fast_create(Product, :name => 'test product2', :product_category_id => @product_category.id) |
28 | assert_difference Product, :count, -1 do | 28 | assert_difference Product, :count, -1 do |
29 | p.destroy | 29 | p.destroy |
30 | - end | 30 | + end |
31 | end | 31 | end |
32 | 32 | ||
33 | should 'display category name if name is nil' do | 33 | should 'display category name if name is nil' do |
@@ -69,7 +69,7 @@ class ProductTest < ActiveSupport::TestCase | @@ -69,7 +69,7 @@ class ProductTest < ActiveSupport::TestCase | ||
69 | p1 = enterprise.products.create!(:name => 'product 1', :product_category => @product_category) | 69 | p1 = enterprise.products.create!(:name => 'product 1', :product_category => @product_category) |
70 | p2 = enterprise.products.create!(:name => 'product 2', :product_category => @product_category) | 70 | p2 = enterprise.products.create!(:name => 'product 2', :product_category => @product_category) |
71 | p3 = enterprise.products.create!(:name => 'product 3', :product_category => @product_category) | 71 | p3 = enterprise.products.create!(:name => 'product 3', :product_category => @product_category) |
72 | - | 72 | + |
73 | assert_equal [p3, p2], Product.recent(2) | 73 | assert_equal [p3, p2], Product.recent(2) |
74 | end | 74 | end |
75 | 75 | ||
@@ -79,7 +79,7 @@ class ProductTest < ActiveSupport::TestCase | @@ -79,7 +79,7 @@ class ProductTest < ActiveSupport::TestCase | ||
79 | :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') | 79 | :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') |
80 | }, :enterprise_id => @profile.id) | 80 | }, :enterprise_id => @profile.id) |
81 | assert_equal p.image(true).filename, 'rails.png' | 81 | assert_equal p.image(true).filename, 'rails.png' |
82 | - end | 82 | + end |
83 | end | 83 | end |
84 | 84 | ||
85 | should 'calculate catagory full name' do | 85 | should 'calculate catagory full name' do |
@@ -125,7 +125,7 @@ class ProductTest < ActiveSupport::TestCase | @@ -125,7 +125,7 @@ class ProductTest < ActiveSupport::TestCase | ||
125 | ent.lat = 45.0; ent.lng = 45.0; ent.save! | 125 | ent.lat = 45.0; ent.lng = 45.0; ent.save! |
126 | process_delayed_job_queue | 126 | process_delayed_job_queue |
127 | prod.reload | 127 | prod.reload |
128 | - | 128 | + |
129 | assert_in_delta 45.0, prod.lat, 0.0001 | 129 | assert_in_delta 45.0, prod.lat, 0.0001 |
130 | assert_in_delta 45.0, prod.lng, 0.0001 | 130 | assert_in_delta 45.0, prod.lng, 0.0001 |
131 | end | 131 | end |
@@ -501,4 +501,71 @@ class ProductTest < ActiveSupport::TestCase | @@ -501,4 +501,71 @@ class ProductTest < ActiveSupport::TestCase | ||
501 | assert_equal 0, product.price_description_percentage | 501 | assert_equal 0, product.price_description_percentage |
502 | end | 502 | end |
503 | 503 | ||
504 | + should 'act as faceted' do | ||
505 | + s = fast_create(State, :acronym => 'XZ') | ||
506 | + c = fast_create(City, :name => 'Tabajara', :parent_id => s.id) | ||
507 | + ent = fast_create(Enterprise, :region_id => c.id) | ||
508 | + p = fast_create(Product, :enterprise_id => ent.id) | ||
509 | + pq = p.product_qualifiers.create!(:qualifier => fast_create(Qualifier, :name => 'qualifier'), | ||
510 | + :certifier => fast_create(Certifier, :name => 'certifier')) | ||
511 | + assert_equal 'Related products', Product.facet_by_id(:f_category)[:label] | ||
512 | + assert_equal ['Tabajara', ', XZ'], Product.facet_by_id(:f_region)[:proc].call(p.send(:f_region)) | ||
513 | + assert_equal ['qualifier', ' cert. certifier'], Product.facet_by_id(:f_qualifier)[:proc].call(p.send(:f_qualifier).last) | ||
514 | + end | ||
515 | + | ||
516 | + should 'act as searchable' do | ||
517 | + s = fast_create(State, :acronym => 'XZ') | ||
518 | + c = fast_create(City, :name => 'Tabajara', :parent_id => s.id) | ||
519 | + ent = fast_create(Enterprise, :region_id => c.id, :name => "Black Sun") | ||
520 | + category = fast_create(ProductCategory, :name => "homemade", :acronym => "hm", :abbreviation => "homey") | ||
521 | + p = Product.create!(:name => 'bananas syrup', :description => 'surrounded by mosquitos', :enterprise_id => ent.id, | ||
522 | + :product_category_id => category.id) | ||
523 | + qual = Qualifier.create!(:name => 'qualificador', :environment_id => Environment.default.id) | ||
524 | + cert = Certifier.create!(:name => 'certificador', :environment_id => Environment.default.id) | ||
525 | + pq = p.product_qualifiers.create!(:qualifier => qual, :certifier => cert) | ||
526 | + # fields | ||
527 | + assert_includes Product.find_by_contents('bananas')[:results].docs, p | ||
528 | + assert_includes Product.find_by_contents('mosquitos')[:results].docs, p | ||
529 | + assert_includes Product.find_by_contents('homemade')[:results].docs, p | ||
530 | + # filters | ||
531 | + assert_includes Product.find_by_contents('bananas', {}, { | ||
532 | + :filter_queries => ["public:true"]})[:results].docs, p | ||
533 | + assert_not_includes Product.find_by_contents('bananas', {}, { | ||
534 | + :filter_queries => ["public:false"]})[:results].docs, p | ||
535 | + assert_includes Product.find_by_contents('bananas', {}, { | ||
536 | + :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, p | ||
537 | + # includes | ||
538 | + assert_includes Product.find_by_contents("homemade")[:results].docs, p | ||
539 | + assert_includes Product.find_by_contents(category.slug)[:results].docs, p | ||
540 | + assert_includes Product.find_by_contents("hm")[:results].docs, p | ||
541 | + assert_includes Product.find_by_contents("homey")[:results].docs, p | ||
542 | + assert_includes Product.find_by_contents("Tabajara")[:results].docs, p | ||
543 | + assert_includes Product.find_by_contents("Black Sun")[:results].docs, p | ||
544 | + end | ||
545 | + | ||
546 | + should 'boost name matches' do | ||
547 | + ent = fast_create(Enterprise) | ||
548 | + cat = fast_create(ProductCategory) | ||
549 | + in_desc = Product.create!(:name => 'something', :enterprise_id => ent.id, :description => 'bananas in the description!', | ||
550 | + :product_category_id => cat.id) | ||
551 | + in_name = Product.create!(:name => 'bananas in the name!', :enterprise_id => ent.id, :product_category_id => cat.id) | ||
552 | + assert_equal [in_name, in_desc], Product.find_by_contents('bananas')[:results].docs | ||
553 | + end | ||
554 | + | ||
555 | + should 'boost if profile is enabled' do | ||
556 | + person2 = fast_create(Person, :enabled => false) | ||
557 | + art_profile_disabled = Article.create!(:name => 'profile disabled', :profile_id => person2.id) | ||
558 | + person1 = fast_create(Person, :enabled => true) | ||
559 | + art_profile_enabled = Article.create!(:name => 'profile enabled', :profile_id => person1.id) | ||
560 | + assert_equal [art_profile_enabled, art_profile_disabled], Article.find_by_contents('profile')[:results].docs | ||
561 | + end | ||
562 | + | ||
563 | + should 'reindex enterprise after saving' do | ||
564 | + ent = fast_create(Enterprise) | ||
565 | + cat = fast_create(ProductCategory) | ||
566 | + prod = Product.create!(:name => 'something', :enterprise_id => ent.id, :product_category_id => cat.id) | ||
567 | + Product.expects(:solr_batch_add).with([ent]) | ||
568 | + prod.save! | ||
569 | + end | ||
570 | + | ||
504 | end | 571 | end |
test/unit/profile_test.rb
@@ -1770,4 +1770,50 @@ class ProfileTest < ActiveSupport::TestCase | @@ -1770,4 +1770,50 @@ class ProfileTest < ActiveSupport::TestCase | ||
1770 | assert !profile.valid? | 1770 | assert !profile.valid? |
1771 | assert profile.errors.invalid?(:identifier) | 1771 | assert profile.errors.invalid?(:identifier) |
1772 | end | 1772 | end |
1773 | + | ||
1774 | + should 'act as faceted' do | ||
1775 | + st = fast_create(State, :acronym => 'XZ') | ||
1776 | + city = fast_create(City, :name => 'Tabajara', :parent_id => st.id) | ||
1777 | + prof = fast_create(Person, :region_id => city.id) | ||
1778 | + assert_equal ['Tabajara', ', XZ'], Profile.facet_by_id(:f_region)[:proc].call(prof.send(:f_region)) | ||
1779 | + end | ||
1780 | + | ||
1781 | + should 'act as searchable' do | ||
1782 | + st = fast_create(State, :acronym => 'CA') | ||
1783 | + city = fast_create(City, :name => 'Inglewood', :parent_id => st.id) | ||
1784 | + p = create(Person, :name => "Hiro", :address => 'U-Stor-It', :nickname => 'Protagonist', | ||
1785 | + :user_id => fast_create(User).id, :region_id => city.id) | ||
1786 | + cat = fast_create(Category, :name => "Science Fiction", :acronym => "sf", :abbreviation => "sci-fi") | ||
1787 | + p.add_category cat | ||
1788 | + cat.save! | ||
1789 | + p.save! | ||
1790 | + | ||
1791 | + # fields | ||
1792 | + assert_includes Profile.find_by_contents('Hiro')[:results].docs, p | ||
1793 | + assert_includes Profile.find_by_contents('Stor')[:results].docs, p | ||
1794 | + assert_includes Profile.find_by_contents('Protagonist')[:results].docs, p | ||
1795 | + # filters | ||
1796 | + assert_includes Profile.find_by_contents('Hiro', {}, { | ||
1797 | + :filter_queries => ["public:true"]})[:results].docs, p | ||
1798 | + assert_not_includes Profile.find_by_contents('Hiro', {}, { | ||
1799 | + :filter_queries => ["public:false"]})[:results].docs, p | ||
1800 | + assert_includes Profile.find_by_contents('Hiro', {}, { | ||
1801 | + :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, p | ||
1802 | + # includes | ||
1803 | + assert_includes Profile.find_by_contents("Inglewood")[:results].docs, p | ||
1804 | + end | ||
1805 | + | ||
1806 | + should 'boost name matches' do | ||
1807 | + in_addr = create(Person, :name => 'something', :address => 'bananas in the address!', :user_id => fast_create(User).id) | ||
1808 | + in_name = create(Person, :name => 'bananas in the name!', :user_id => fast_create(User).id) | ||
1809 | + assert_equal [in_name, in_addr], Person.find_by_contents('bananas')[:results].docs | ||
1810 | + end | ||
1811 | + | ||
1812 | + should 'reindex articles after saving' do | ||
1813 | + profile = create(Person, :name => 'something', :user_id => fast_create(User).id) | ||
1814 | + art = profile.articles.build(:name => 'something') | ||
1815 | + Profile.expects(:solr_batch_add).with(includes(art)) | ||
1816 | + profile.save! | ||
1817 | + end | ||
1818 | + | ||
1773 | end | 1819 | end |
test/unit/search_helper_test.rb
@@ -4,8 +4,6 @@ class SearchHelperTest < ActiveSupport::TestCase | @@ -4,8 +4,6 @@ class SearchHelperTest < ActiveSupport::TestCase | ||
4 | 4 | ||
5 | include SearchHelper | 5 | include SearchHelper |
6 | 6 | ||
7 | - | ||
8 | - | ||
9 | should 'display search page title' do | 7 | should 'display search page title' do |
10 | title = 'page_title' | 8 | title = 'page_title' |
11 | assert_equal search_page_title(title), '<h1>page_title</h1>' | 9 | assert_equal search_page_title(title), '<h1>page_title</h1>' |
test/unit/textile_article_test.rb
@@ -190,4 +190,9 @@ class TextileArticleTest < ActiveSupport::TestCase | @@ -190,4 +190,9 @@ class TextileArticleTest < ActiveSupport::TestCase | ||
190 | article | 190 | article |
191 | end | 191 | end |
192 | 192 | ||
193 | + should 'define type facet' do | ||
194 | + a = TextileArticle.new | ||
195 | + assert_equal TextArticle.type_name, TextileArticle.send(:f_type_proc, a.send(:f_type)) | ||
196 | + end | ||
197 | + | ||
193 | end | 198 | end |
test/unit/tiny_mce_article_test.rb
@@ -247,4 +247,9 @@ class TinyMceArticleTest < ActiveSupport::TestCase | @@ -247,4 +247,9 @@ class TinyMceArticleTest < ActiveSupport::TestCase | ||
247 | assert TinyMceArticle.new.tiny_mce? | 247 | assert TinyMceArticle.new.tiny_mce? |
248 | end | 248 | end |
249 | 249 | ||
250 | + should 'define type facet' do | ||
251 | + a = TinyMceArticle.new | ||
252 | + assert_equal TextArticle.type_name, TinyMceArticle.send(:f_type_proc, a.send(:f_type)) | ||
253 | + end | ||
254 | + | ||
250 | end | 255 | end |