Commit 5efa4c1c055bd668afb836bfb3b80688073e6848

Authored by Rafael Martins
1 parent fc31ed1c

Updated unit tests

test/unit/article_test.rb
... ... @@ -412,8 +412,8 @@ class ArticleTest < ActiveSupport::TestCase
412 412 assert_includes c2.articles(true), art
413 413 assert_includes c1.articles(true), art
414 414  
415   - assert_includes art.categories_including_virtual(true), c2
416   - assert_includes art.categories_including_virtual(true), c1
  415 + assert_includes art.categories_including_virtual(true), c2
  416 + assert_includes art.categories_including_virtual(true), c1
417 417 end
418 418  
419 419 should 'redefine the entire category set at once' do
... ... @@ -429,8 +429,8 @@ class ArticleTest < ActiveSupport::TestCase
429 429 art.category_ids = [c2,c3].map(&:id)
430 430  
431 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)
  432 + assert_includes art.categories_including_virtual(true), c1
  433 + assert !art.categories_including_virtual(true).include?(c4)
434 434 end
435 435  
436 436 should 'be able to create an article already with categories' do
... ... @@ -442,7 +442,7 @@ class ArticleTest < ActiveSupport::TestCase
442 442 a = p.articles.create!(:name => 'test', :category_ids => [c1.id, c2.id])
443 443  
444 444 assert_equivalent [c1, c2], a.categories(true)
445   - assert_includes a.categories_including_virtual(true), parent1
  445 + assert_includes a.categories_including_virtual(true), parent1
446 446 end
447 447  
448 448 should 'not add a category twice to article' do
... ... @@ -1658,67 +1658,132 @@ class ArticleTest < ActiveSupport::TestCase
1658 1658 assert_equal [c1,c2,c5], Article.text_articles
1659 1659 end
1660 1660  
  1661 + should 'delegate region info to profile' do
  1662 + profile = fast_create(Profile)
  1663 + Profile.any_instance.expects(:region)
  1664 + Profile.any_instance.expects(:region_id)
  1665 + article = fast_create(Article, :profile_id => profile.id)
  1666 + article.region
  1667 + article.region_id
  1668 + end
  1669 +
  1670 + should 'delegate environment info to profile' do
  1671 + profile = fast_create(Profile)
  1672 + Profile.any_instance.expects(:environment)
  1673 + Profile.any_instance.expects(:environment_id)
  1674 + article = fast_create(Article, :profile_id => profile.id)
  1675 + article.environment
  1676 + article.environment_id
  1677 + end
  1678 +
1661 1679 should 'act as faceted' do
1662 1680 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)
  1681 + cat = Category.create!(:name => 'hardcore', :environment_id => Environment.default.id)
  1682 + a = Article.create!(:name => 'black flag review', :profile_id => person.id)
  1683 + a.add_category(cat, true)
  1684 + a.save!
  1685 + assert_equal Article.type_name, Article.facet_by_id(:f_type)[:proc].call(a.send(:f_type))
  1686 + assert_equal Person.type_name, Article.facet_by_id(:f_profile_type)[:proc].call(a.send(:f_profile_type))
  1687 + assert_equal a.published_at, a.send(:f_published_at)
  1688 + assert_equal ['hardcore'], a.send(:f_category)
  1689 + assert_equal "category_filter:\"#{cat.id}\"", Article.facet_category_query.call(cat)
1667 1690 end
1668 1691  
1669 1692 should 'act as searchable' do
1670 1693 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
  1694 + :nickname => 'Protagonist')
  1695 + person2 = fast_create(Person, :name => "Raven")
  1696 + category = fast_create(Category, :name => "science fiction", :acronym => "sf", :abbreviation => "sci-fi")
  1697 + a = Article.create!(:name => 'a searchable article about bananas', :profile_id => person.id,
  1698 + :body => 'the body talks about mosquitos', :abstract => 'and the abstract is about beer',
  1699 + :filename => 'not_a_virus.exe')
  1700 + a.add_category(category)
  1701 + c = a.comments.build(:title => 'snow crash', :author => person2, :body => 'wanna try some?')
  1702 + c.save!
  1703 +
  1704 + # fields
  1705 + assert_includes Article.find_by_contents('bananas')[:results].docs, a
  1706 + assert_includes Article.find_by_contents('mosquitos')[:results].docs, a
  1707 + assert_includes Article.find_by_contents('beer')[:results].docs, a
  1708 + assert_includes Article.find_by_contents('not_a_virus.exe')[:results].docs, a
  1709 + # filters
  1710 + assert_includes Article.find_by_contents('bananas', {}, {:filter_queries => ["public:true"]})[:results].docs, a
  1711 + assert_not_includes Article.find_by_contents('bananas', {}, {:filter_queries => ["public:false"]})[:results].docs, a
  1712 + assert_includes Article.find_by_contents('bananas', {}, {:filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, a
  1713 + assert_includes Article.find_by_contents('bananas', {}, {:filter_queries => ["profile_id:\"#{person.id}\""]})[:results].docs, a
1695 1714 # 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
  1715 + assert_includes Article.find_by_contents('Hiro')[:results].docs, a
  1716 + assert_includes Article.find_by_contents("person-#{person.id}")[:results].docs, a
  1717 + assert_includes Article.find_by_contents("California")[:results].docs, a
  1718 + assert_includes Article.find_by_contents("Protagonist")[:results].docs, a
  1719 + assert_includes Article.find_by_contents("snow")[:results].docs, a
  1720 + assert_includes Article.find_by_contents("try some")[:results].docs, a
  1721 + assert_includes Article.find_by_contents("Raven")[:results].docs, a
  1722 + assert_includes Article.find_by_contents("science")[:results].docs, a
  1723 + assert_includes Article.find_by_contents(category.slug)[:results].docs, a
  1724 + assert_includes Article.find_by_contents("sf")[:results].docs, a
  1725 + assert_includes Article.find_by_contents("sci-fi")[:results].docs, a
1707 1726 end
1708 1727  
1709 1728 should 'boost name matches' do
1710 1729 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
  1730 + in_body = Article.create!(:name => 'something', :profile_id => person.id, :body => 'bananas in the body!')
  1731 + in_name = Article.create!(:name => 'bananas in the name!', :profile_id => person.id)
  1732 + assert_equal [in_name, in_body], Article.find_by_contents('bananas')[:results].docs
1714 1733 end
1715 1734  
1716 1735 should 'boost if profile is enabled' do
1717 1736 person2 = fast_create(Person, :enabled => false)
1718   - art_profile_disabled = Article.create!(:name => 'profile disabled', :profile_id => person2.id)
  1737 + art_profile_disabled = Article.create!(:name => 'profile disabled', :profile_id => person2.id)
1719 1738 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
  1739 + art_profile_enabled = Article.create!(:name => 'profile enabled', :profile_id => person1.id)
  1740 + assert_equal [art_profile_enabled, art_profile_disabled], Article.find_by_contents('profile')[:results].docs
  1741 + end
  1742 +
  1743 + should 'remove all categorizations when destroyed' do
  1744 + art = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id)
  1745 + cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id)
  1746 + art.add_category cat
  1747 + art.destroy
  1748 + assert cat.articles.reload.empty?
  1749 + end
  1750 +
  1751 + should 'show more popular articles' do
  1752 + art1 = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id)
  1753 + art2 = Article.create!(:name => 'article 2', :profile_id => fast_create(Person).id)
  1754 + art3 = Article.create!(:name => 'article 3', :profile_id => fast_create(Person).id)
  1755 +
  1756 + art1.hits = 56; art1.save!
  1757 + art3.hits = 92; art3.save!
  1758 + art2.hits = 3; art2.save!
  1759 +
  1760 + assert_equal [art3, art1, art2], Article.more_popular
  1761 + end
  1762 +
  1763 + should 'return more commented with pagination' do
  1764 + art1 = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id)
  1765 + art2 = Article.create!(:name => 'article 2', :profile_id => fast_create(Person).id)
  1766 + art3 = Article.create!(:name => 'article 3', :profile_id => fast_create(Person).id)
  1767 +
  1768 + art1.comments_count = 56; art1.save!
  1769 + art3.comments_count = 92; art3.save!
  1770 + art2.comments_count = 3; art2.save!
  1771 +
  1772 + assert_equal [art3, art1], Article.most_commented(2)
  1773 + end
  1774 +
  1775 + should 'show if article is public' do
  1776 + art1 = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id)
  1777 + art2 = Article.create!(:name => 'article 2', :profile_id => fast_create(Person).id)
  1778 + art2.advertise = false; art2.save!
  1779 + art3 = Article.create!(:name => 'article 3', :profile_id => fast_create(Person).id)
  1780 + art3.published = false; art3.save!
  1781 + art4 = Article.create!(:name => 'article 4', :profile_id => fast_create(Person).id)
  1782 + art4.profile.visible = false; art4.save!
  1783 + art5 = Article.create!(:name => 'article 5', :profile_id => fast_create(Person).id)
  1784 + art5.profile.public_profile = false; art5.save!
  1785 +
  1786 + assert_equal [art1], Article.public
  1787 + end
1723 1788  
1724 1789 end
... ...
test/unit/category_test.rb
... ... @@ -32,11 +32,6 @@ class CategoryTest < ActiveSupport::TestCase
32 32 assert_equal parent_category, c.parent
33 33 end
34 34  
35   - # def test_full_text_search
36   - # c = Category.create!(:name => 'product category for testing', :environment_id => @env.id)
37   - # assert @env.product_categories.full_text_search('product*').include?(c)
38   - # end
39   -
40 35 def test_category_full_name
41 36 cat = Category.new(:name => 'category_name')
42 37 assert_equal 'category_name', cat.full_name
... ... @@ -102,10 +97,10 @@ class CategoryTest < ActiveSupport::TestCase
102 97 sub_cat = fast_create(Category, :environment_id => @env.id, :parent_id => cat.id)
103 98  
104 99 roots = Category.top_level_for(@env)
105   -
  100 +
106 101 assert_equal 1, roots.size
107 102 end
108   -
  103 +
109 104 def test_slug
110 105 c = Category.new(:name => 'Category name')
111 106 assert_equal 'category-name', c.slug
... ... @@ -176,7 +171,7 @@ class CategoryTest < ActiveSupport::TestCase
176 171 c.display_color = 10
177 172 c.valid?
178 173 assert c.errors.invalid?(:display_color)
179   -
  174 +
180 175 valid = %w[ 1 2 3 4 ].map { |item| item.to_i }
181 176 valid.each do |item|
182 177 c.display_color = item
... ... @@ -197,7 +192,7 @@ class CategoryTest < ActiveSupport::TestCase
197 192 c.display_color = 2
198 193 c.valid?
199 194 assert !c.errors.invalid?(:display_color)
200   -
  195 +
201 196 end
202 197  
203 198 should 'be able to get top ancestor' do
... ... @@ -219,7 +214,7 @@ class CategoryTest < ActiveSupport::TestCase
219 214 ################################################################
220 215 # category filter stuff
221 216 ################################################################
222   -
  217 +
223 218 should 'should paginate recent-like methods' do
224 219 c = @env.categories.build(:name => 'my category'); c.save!
225 220 assert c.recent_people.respond_to? 'total_entries'
... ... @@ -492,30 +487,63 @@ class CategoryTest < ActiveSupport::TestCase
492 487 assert_not_includes Category.top_level_for(Environment.default).from_types(['ProductCategory']), toplevel_category
493 488 end
494 489  
  490 + should 'paginate upcoming events' do
  491 + category = Category.create!(:name => 'category1', :environment_id => Environment.default.id)
  492 + profile = fast_create(Profile)
  493 + event1 = category.events.build(:name => 'event1', :start_date => Time.now, :profile => profile)
  494 + event2 = category.events.build(:name => 'event2', :start_date => Time.now + 1.hour, :profile => profile)
  495 + event3 = category.events.build(:name => 'event3', :start_date => Time.now + 1.day, :profile => profile)
  496 + category.save!
  497 + assert_equal [event1, event2], category.upcoming_events(2)
  498 + end
  499 +
  500 + should 'remove all article categorizations when destroyed' do
  501 + cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id)
  502 + art = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id)
  503 + art.add_category cat
  504 + cat.destroy
  505 + assert art.categories.reload.empty?
  506 + end
  507 +
  508 + should 'remove all profile categorizations when destroyed' do
  509 + cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id)
  510 + p = create(Person, :user_id => fast_create(User).id)
  511 + p.add_category cat
  512 + cat.destroy
  513 + assert p.categories.reload.empty?
  514 + end
  515 +
495 516 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)
  517 + parent = fast_create(Category, :name => 'books')
  518 + c = Category.create!(:name => "science fiction", :acronym => "sf", :abbreviation => "sci-fi",
  519 + :environment_id => Environment.default.id, :parent_id => parent.id)
499 520  
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
  521 + # fields
  522 + assert_includes Category.find_by_contents('fiction')[:results].docs, c
  523 + assert_includes Category.find_by_contents('sf')[:results].docs, c
  524 + assert_includes Category.find_by_contents('sci-fi')[:results].docs, c
  525 + # filters
  526 + assert_includes Category.find_by_contents('science', {}, {
  527 + :filter_queries => ["parent_id:#{parent.id}"]})[:results].docs, c
507 528 end
508 529  
509 530 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
  531 + c_abbr = Category.create!(:name => "something else", :abbreviation => "science", :environment_id => Environment.default.id)
  532 + c_name = Category.create!(:name => "science fiction", :environment_id => Environment.default.id)
  533 + assert_equal [c_name, c_abbr], Category.find_by_contents("science")[:results].docs
513 534 end
514 535  
515   - should 'solr save' do
  536 + should 'solr save' do
516 537 c = @env.categories.build(:name => 'my category');
517   - c.expects(:solr_save)
518   - c.save!
519   - end
  538 + c.expects(:solr_save)
  539 + c.save!
  540 + end
520 541  
  542 + should 'reindex articles after saving' do
  543 + cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id)
  544 + art = Article.create!(:name => 'something', :profile_id => fast_create(Person).id)
  545 + Category.expects(:solr_batch_add).with(includes(art))
  546 + art.add_category cat
  547 + cat.save!
  548 + end
521 549 end
... ...
test/unit/certifier_test.rb
... ... @@ -58,4 +58,20 @@ class CertifierTest < ActiveSupport::TestCase
58 58 assert_equal [first, last], Certifier.all.sort
59 59 end
60 60  
  61 + should 'reindex products after saving' do
  62 + product = mock
  63 + Certifier.any_instance.stubs(:products).returns([product])
  64 + Certifier.expects(:solr_batch_add).with(includes(product))
  65 + cert = fast_create(Certifier)
  66 + cert.save!
  67 + end
  68 +
  69 + should 'set qualifier as self-certified when destroyed' do
  70 + pq = mock
  71 + Certifier.any_instance.stubs(:product_qualifiers).returns([pq])
  72 + pq.expects(:update_attributes!).with(:certifier => nil)
  73 + cert = fast_create(Certifier)
  74 + cert.destroy
  75 + end
  76 +
61 77 end
... ...
test/unit/enterprise_test.rb
... ... @@ -98,7 +98,7 @@ class EnterpriseTest < ActiveSupport::TestCase
98 98 assert_not_includes result, ent2
99 99 end
100 100  
101   - should 'be found in search for its product categories hierarchy' do
  101 + should 'be found in search for its product categories hierarchy' do
102 102 ent1 = fast_create(Enterprise, :name => 'test1', :identifier => 'test1')
103 103 prod_cat = fast_create(ProductCategory, :name => 'pctest', :environment_id => Environment.default.id)
104 104 prod_child = fast_create(ProductCategory, :name => 'pchild', :environment_id => Environment.default.id, :parent_id => prod_cat.id)
... ... @@ -201,9 +201,9 @@ class EnterpriseTest < ActiveSupport::TestCase
201 201 ent.reload
202 202 assert_equal 1, ent.boxes.size
203 203 assert_equal 1, ent.boxes[0].blocks.size
204   - end
  204 + end
205 205  
206   - should 'not replace template if environment doesnt allow' do
  206 + should 'not replace template if environment doesnt allow' do
207 207 inactive_template = fast_create(Enterprise, :name => 'inactive enteprise template', :identifier => 'inactive_enterprise_template')
208 208 inactive_template.boxes.destroy_all
209 209 inactive_template.boxes << Box.new
... ... @@ -408,8 +408,8 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
408 408 }
409 409 Product.create!(:name => "product 4", :enterprise_id => e1.id, :product_category_id => @product_category.id, :highlighted => true)
410 410 Product.create!(:name => "product 5", :enterprise_id => e1.id, :product_category_id => @product_category.id, :image_builder => {
411   - :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
412   - })
  411 + :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
  412 + })
413 413 assert_equal products, e1.highlighted_products_with_image
414 414 end
415 415  
... ... @@ -421,7 +421,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
421 421  
422 422 assert_equal product.inputs, enterprise.inputs
423 423 end
424   -
  424 +
425 425 should 'reindex when products are changed' do
426 426 enterprise = fast_create(Enterprise)
427 427 product = fast_create(Product, :enterprise_id => enterprise.id, :product_category_id => @product_category.id)
... ... @@ -458,4 +458,14 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
458 458 e = fast_create(Enterprise)
459 459 assert_respond_to e, :production_costs
460 460 end
  461 +
  462 + should 'reindex products with full category name after save' do
  463 + product = mock
  464 + product.expects(:category_full_name)
  465 + Enterprise.any_instance.stubs(:products).returns([product])
  466 + Enterprise.expects(:solr_batch_add).with(includes(product))
  467 + ent = fast_create(Enterprise)
  468 + ent.save!
  469 + end
  470 +
461 471 end
... ...
test/unit/location_block_test.rb
... ... @@ -21,9 +21,15 @@ class LocationBlockTest &lt; ActiveSupport::TestCase
21 21 should 'be editable' do
22 22 assert LocationBlock.new.editable?
23 23 end
24   -
  24 +
25 25 should 'default title be blank by default' do
26 26 assert_equal '', LocationBlock.new.title
27 27 end
28 28  
  29 + should 'use google maps api v3' do
  30 + @block.owner.lat = '-12.34'; @block.owner.save!
  31 + assert_match 'http://maps.google.com/maps/api/staticmap', @block.content
  32 + assert_no_match /key=/, @block.content
  33 + end
  34 +
29 35 end
... ...
test/unit/product_category_test.rb
... ... @@ -15,7 +15,7 @@ class ProductCategoryTest &lt; ActiveSupport::TestCase
15 15 p1 = Product.create(:name => 'product2', :product_category => c1, :enterprise_id => profile.id)
16 16 c0.reload; c1.reload
17 17 assert_equivalent [p0, p1], c0.all_products
18   - assert_equivalent [p1], c1.all_products
  18 + assert_equivalent [p1], c1.all_products
19 19 end
20 20  
21 21 should 'return top level product categories for environment when no parent product category specified' do
... ... @@ -35,4 +35,13 @@ class ProductCategoryTest &lt; ActiveSupport::TestCase
35 35  
36 36 assert_equal [c11], ProductCategory.menu_categories(c1, nil)
37 37 end
  38 +
  39 + should 'reindex products after save' do
  40 + product = mock
  41 + ProductCategory.any_instance.stubs(:products).returns([product])
  42 + ProductCategory.expects(:solr_batch_add).with(includes(product))
  43 + pc = fast_create(ProductCategory)
  44 + pc.save!
  45 + end
  46 +
38 47 end
... ...
test/unit/product_test.rb
... ... @@ -8,6 +8,13 @@ class ProductTest &lt; ActiveSupport::TestCase
8 8 @profile = fast_create(Enterprise)
9 9 end
10 10  
  11 + should 'validate the presence of enterprise' do
  12 + p = Product.new
  13 + assert_raise ActiveRecord::RecordInvalid do
  14 + p.save!
  15 + end
  16 + end
  17 +
11 18 should 'return associated enterprise region' do
12 19 @profile.region = fast_create Region, :name => 'Salvador'
13 20 @profile.save!
... ... @@ -82,7 +89,7 @@ class ProductTest &lt; ActiveSupport::TestCase
82 89 end
83 90 end
84 91  
85   - should 'calculate catagory full name' do
  92 + should 'calculate category full name' do
86 93 cat = mock
87 94 cat.expects(:full_name).returns('A/B/C')
88 95  
... ... @@ -221,20 +228,27 @@ class ProductTest &lt; ActiveSupport::TestCase
221 228 end
222 229  
223 230 should 'format values to float with 2 decimals' do
224   - ent = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1')
225   - product = fast_create(Product, :enterprise_id => ent.id, :price => 12.994, :discount => 1.994)
  231 + ent = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1')
  232 + product = fast_create(Product, :enterprise_id => ent.id, :price => 12.994, :discount => 1.994)
226 233  
227 234 assert_equal "12.99", product.formatted_value(:price)
228 235 assert_equal "1.99", product.formatted_value(:discount)
229 236 end
230 237  
231 238 should 'calculate price with discount' do
232   - ent = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1')
233   - product = fast_create(Product, :enterprise_id => ent.id, :price => 12.994, :discount => 1.994)
  239 + ent = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1')
  240 + product = fast_create(Product, :enterprise_id => ent.id, :price => 12.994, :discount => 1.994)
234 241  
235 242 assert_equal 11.00, product.price_with_discount
236 243 end
237 244  
  245 + should 'calculate price without discount' do
  246 + ent = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1')
  247 + product = fast_create(Product, :enterprise_id => ent.id, :price => 12.994, :discount => 0)
  248 +
  249 + assert_equal product.price, product.price_with_discount
  250 + end
  251 +
238 252 should 'have default image' do
239 253 product = Product.new
240 254 assert_equal '/images/icons-app/product-default-pic-thumb.png', product.default_image
... ... @@ -251,24 +265,24 @@ class ProductTest &lt; ActiveSupport::TestCase
251 265 end
252 266  
253 267 should 'return product inputs' do
254   - ent = fast_create(Enterprise)
255   - product = fast_create(Product, :enterprise_id => ent.id)
256   - input = fast_create(Input, :product_id => product.id, :product_category_id => @product_category.id)
  268 + ent = fast_create(Enterprise)
  269 + product = fast_create(Product, :enterprise_id => ent.id)
  270 + input = fast_create(Input, :product_id => product.id, :product_category_id => @product_category.id)
257 271  
258   - assert_equal [input], product.inputs
  272 + assert_equal [input], product.inputs
259 273 end
260 274  
261 275 should 'destroy inputs when product is removed' do
262   - ent = fast_create(Enterprise)
263   - product = fast_create(Product, :enterprise_id => ent.id)
264   - input = fast_create(Input, :product_id => product.id, :product_category_id => @product_category.id)
  276 + ent = fast_create(Enterprise)
  277 + product = fast_create(Product, :enterprise_id => ent.id)
  278 + input = fast_create(Input, :product_id => product.id, :product_category_id => @product_category.id)
265 279  
266   - services_category = fast_create(ProductCategory, :name => 'Services')
267   - input2 = fast_create(Input, :product_id => product.id, :product_category_id => services_category.id)
  280 + services_category = fast_create(ProductCategory, :name => 'Services')
  281 + input2 = fast_create(Input, :product_id => product.id, :product_category_id => services_category.id)
268 282  
269   - assert_difference Input, :count, -2 do
270   - product.destroy
271   - end
  283 + assert_difference Input, :count, -2 do
  284 + product.destroy
  285 + end
272 286 end
273 287  
274 288 should 'test if name is blank' do
... ... @@ -501,71 +515,242 @@ class ProductTest &lt; ActiveSupport::TestCase
501 515 assert_equal 0, product.price_description_percentage
502 516 end
503 517  
  518 + should 'return solidarity percentage from inputs' do
  519 + prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :enterprise_id => @profile.id)
  520 + puts "$$$$ #{prod.percentage_from_solidarity_economy}"
  521 + assert_equal 0, prod.percentage_from_solidarity_economy.first
  522 +
  523 + Input.create!(:product_id => prod.id, :product_category_id => @product_category.id,
  524 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false)
  525 + assert_equal 0, prod.percentage_from_solidarity_economy.first
  526 +
  527 + Input.create!(:product_id => prod.id, :product_category_id => @product_category.id,
  528 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
  529 + assert_equal 50, prod.percentage_from_solidarity_economy.first
  530 +
  531 + Input.create!(:product_id => prod.id, :product_category_id => @product_category.id,
  532 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false)
  533 + assert_equal 25, prod.percentage_from_solidarity_economy.first
  534 +
  535 + prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :enterprise_id => @profile.id)
  536 + Input.create!(:product_id => prod.id, :product_category_id => @product_category.id,
  537 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
  538 + Input.create!(:product_id => prod.id, :product_category_id => @product_category.id,
  539 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
  540 + Input.create!(:product_id => prod.id, :product_category_id => @product_category.id,
  541 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
  542 + Input.create!(:product_id => prod.id, :product_category_id => @product_category.id,
  543 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false)
  544 + assert_equal 75, prod.percentage_from_solidarity_economy.first
  545 +
  546 + prod = fast_create(Product, :name => 'test product', :product_category_id => @product_category.id, :enterprise_id => @profile.id)
  547 + Input.create!(:product_id => prod.id, :product_category_id => @product_category.id,
  548 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
  549 + assert_equal 100, prod.percentage_from_solidarity_economy.first
  550 + end
  551 +
  552 + should 'delegate region info to enterprise' do
  553 + enterprise = fast_create(Enterprise)
  554 + Enterprise.any_instance.expects(:region)
  555 + Enterprise.any_instance.expects(:region_id)
  556 + product = fast_create(Product, :enterprise_id => enterprise.id)
  557 + product.region
  558 + product.region_id
  559 + end
  560 +
  561 + should 'delegate environment info to enterprise' do
  562 + enterprise = fast_create(Enterprise)
  563 + Enterprise.any_instance.expects(:environment)
  564 + Enterprise.any_instance.expects(:environment_id)
  565 + product = fast_create(Product, :enterprise_id => enterprise.id)
  566 + product.environment
  567 + product.environment_id
  568 + end
  569 +
504 570 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)
  571 + s = fast_create(State, :acronym => 'XZ')
  572 + c = fast_create(City, :name => 'Tabajara', :parent_id => s.id)
  573 + ent = fast_create(Enterprise, :region_id => c.id)
  574 + cat = fast_create(ProductCategory, :name => 'hardcore')
  575 + p = Product.create!(:name => 'black flag', :enterprise_id => ent.id, :product_category_id => cat.id)
509 576 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)
  577 + :certifier => fast_create(Certifier, :name => 'certifier'))
  578 + assert_equal 'Related products', Product.facet_by_id(:f_category)[:label]
  579 + assert_equal ['Tabajara', ', XZ'], Product.facet_by_id(:f_region)[:proc].call(p.send(:f_region))
  580 + assert_equal ['qualifier', ' cert. certifier'], Product.facet_by_id(:f_qualifier)[:proc].call(p.send(:f_qualifier).last)
  581 + assert_equal 'hardcore', p.send(:f_category)
  582 + assert_equal "category_filter:#{cat.id}", Product.facet_category_query.call(cat)
514 583 end
515 584  
516 585 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)
  586 + s = fast_create(State, :acronym => 'XZ')
  587 + c = fast_create(City, :name => 'Tabajara', :parent_id => s.id)
  588 + ent = fast_create(Enterprise, :region_id => c.id, :name => "Black Sun")
  589 + category = fast_create(ProductCategory, :name => "homemade", :acronym => "hm", :abbreviation => "homey")
  590 + p = Product.create!(:name => 'bananas syrup', :description => 'surrounded by mosquitos', :enterprise_id => ent.id,
  591 + :product_category_id => category.id)
  592 + qual = Qualifier.create!(:name => 'qualificador', :environment_id => Environment.default.id)
  593 + cert = Certifier.create!(:name => 'certificador', :environment_id => Environment.default.id)
525 594 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
  595 + p.qualifiers.reload
  596 + p.certifiers.reload
  597 + p.save!
  598 + # fields
  599 + assert_includes Product.find_by_contents('bananas')[:results].docs, p
  600 + assert_includes Product.find_by_contents('mosquitos')[:results].docs, p
  601 + assert_includes Product.find_by_contents('homemade')[:results].docs, p
  602 + # filters
  603 + assert_includes Product.find_by_contents('bananas', {}, { :filter_queries => ["public:true"]})[:results].docs, p
  604 + assert_not_includes Product.find_by_contents('bananas', {}, { :filter_queries => ["public:false"]})[:results].docs, p
  605 + assert_includes Product.find_by_contents('bananas', {}, { :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, p
537 606 # 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
  607 + assert_includes Product.find_by_contents("homemade")[:results].docs, p
  608 + assert_includes Product.find_by_contents(category.slug)[:results].docs, p
  609 + assert_includes Product.find_by_contents("hm")[:results].docs, p
  610 + assert_includes Product.find_by_contents("homey")[:results].docs, p
  611 + assert_includes Product.find_by_contents("Tabajara")[:results].docs, p
  612 + assert_includes Product.find_by_contents("Black Sun")[:results].docs, p
  613 + assert_includes Product.find_by_contents("qualificador")[:results].docs, p
  614 + assert_includes Product.find_by_contents("certificador")[:results].docs, p
544 615 end
545 616  
546 617 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
  618 + ent = fast_create(Enterprise)
  619 + cat = fast_create(ProductCategory)
  620 + in_desc = Product.create!(:name => 'something', :enterprise_id => ent.id, :description => 'bananas in the description!',
  621 + :product_category_id => cat.id)
  622 + in_name = Product.create!(:name => 'bananas in the name!', :enterprise_id => ent.id, :product_category_id => cat.id)
  623 + assert_equal [in_name, in_desc], Product.find_by_contents('bananas')[:results].docs
  624 + end
  625 +
  626 + should 'reindex enterprise after saving' do
  627 + ent = fast_create(Enterprise)
  628 + cat = fast_create(ProductCategory)
  629 + prod = Product.create!(:name => 'something', :enterprise_id => ent.id, :product_category_id => cat.id)
  630 + Product.expects(:solr_batch_add).with([ent])
  631 + prod.save!
  632 + end
  633 +
  634 + should 'boost search results that include an image' do
  635 + product_without_image = Product.create!(:name => 'product without image', :product_category => @product_category,
  636 + :enterprise_id => @profile.id)
  637 + product_with_image = Product.create!(:name => 'product with image', :product_category => @product_category,
  638 + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')},
  639 + :enterprise_id => @profile.id)
  640 + assert_equal [product_with_image, product_without_image], Product.find_by_contents('product image')[:results].docs
  641 + end
  642 +
  643 + should 'boost search results that include qualifier' do
  644 + product_without_q = Product.create!(:name => 'product without qualifier', :product_category => @product_category,
  645 + :enterprise_id => @profile.id)
  646 + product_with_q = Product.create!(:name => 'product with qualifier', :product_category => @product_category,
  647 + :enterprise_id => @profile.id)
  648 + product_with_q.product_qualifiers.create(:qualifier => fast_create(Qualifier), :certifier => nil)
  649 + product_with_q.save!
  650 +
  651 + assert_equal [product_with_q, product_without_q], Product.find_by_contents('product qualifier')[:results].docs
  652 + end
  653 +
  654 + should 'boost search results with open price' do
  655 + product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id)
  656 + product.price = 100
  657 + product.save!
  658 + open_price = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id)
  659 + open_price.price = 100
  660 + Input.create!(:product_id => open_price.id, :product_category_id => @product_category.id,
  661 + :amount_used => 10, :price_per_unit => 10)
  662 + open_price.update_price_details []
  663 + open_price.save!
  664 +
  665 + #pp Product::Boosts[2][2].call(product)
  666 + #pp Product::Boosts[2][2].call(open_price)
  667 + #pp Product.find_by_contents('product')[:results].docs.first.solr_score
  668 + #pp Product.find_by_contents('product')[:results].docs.last.solr_score
  669 + assert_equal [open_price, product], Product.find_by_contents('product')[:results].docs
  670 + end
  671 +
  672 + should 'boost search results with solidarity inputs' do
  673 + product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id)
  674 + perc_50 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id)
  675 + Input.create!(:product_id => perc_50.id, :product_category_id => @product_category.id,
  676 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
  677 + Input.create!(:product_id => perc_50.id, :product_category_id => @product_category.id,
  678 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false)
  679 + perc_50.save!
  680 + perc_75 = Product.create!(:name => 'product 3', :product_category => @product_category, :enterprise_id => @profile.id)
  681 + Input.create!(:product_id => perc_75.id, :product_category_id => @product_category.id,
  682 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false)
  683 + Input.create!(:product_id => perc_75.id, :product_category_id => @product_category.id,
  684 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
  685 + Input.create!(:product_id => perc_75.id, :product_category_id => @product_category.id,
  686 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
  687 + Input.create!(:product_id => perc_75.id, :product_category_id => @product_category.id,
  688 + :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true)
  689 + perc_75.save!
  690 +
  691 + assert_equal [perc_75, perc_50, product], Product.find_by_contents('product')[:results].docs
  692 + end
  693 +
  694 + should 'boost available search results' do
  695 + product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id)
  696 + product.available = false
  697 + product.save!
  698 + product2 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id)
  699 + product2.available = true
  700 + product2.save!
  701 +
  702 + assert_equal [product2, product], Product.find_by_contents('product')[:results].docs
  703 + end
  704 +
  705 + should 'boost search results created updated recently' do
  706 + product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id)
  707 + product.update_attribute :created_at, Time.now - 10.day
  708 + product2 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id)
  709 +
  710 + assert_equal [product2, product], Product.find_by_contents('product')[:results].docs
  711 + end
  712 +
  713 + should 'boost search results with description' do
  714 + product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => @profile.id,
  715 + :description => '')
  716 + product2 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id,
  717 + :description => 'a small description')
  718 +
  719 + assert_equal [product2, product], Product.find_by_contents('product')[:results].docs
  720 + end
  721 +
  722 + should 'boost if enterprise is enabled' do
  723 + ent = Enterprise.create!(:name => 'ent', :identifier => 'ent', :enabled => false)
  724 + product = Product.create!(:name => 'product 1', :product_category => @product_category, :enterprise_id => ent.id)
  725 + product2 = Product.create!(:name => 'product 2', :product_category => @product_category, :enterprise_id => @profile.id)
  726 +
  727 + assert_equal [product2, product], Product.find_by_contents('product')[:results].docs
  728 + end
  729 +
  730 + should 'combine different boost types' do
  731 + product = Product.create!(:name => 'product', :product_category => @product_category, :enterprise_id => @profile.id)
  732 + image_only = Product.create!(:name => 'product with image', :product_category => @product_category,
  733 + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')},
  734 + :enterprise_id => @profile.id)
  735 + qual_only = Product.create!(:name => 'product with qualifier', :product_category => @product_category,
  736 + :enterprise_id => @profile.id)
  737 + img_and_qual = Product.create!(:name => 'product with image and qualifier', :product_category => @product_category,
  738 + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')},
  739 + :enterprise_id => @profile.id)
  740 + qual_only.product_qualifiers.create(:qualifier => fast_create(Qualifier), :certifier => nil)
  741 + img_and_qual.product_qualifiers.create(:qualifier => fast_create(Qualifier), :certifier => nil)
  742 + qual_only.save!
  743 + img_and_qual.save!
  744 +
  745 + assert_equal [img_and_qual, image_only, qual_only, product], Product.find_by_contents('product')[:results].docs
  746 + end
  747 +
  748 + should 'return more recent products' do
  749 + prod1 = Product.create!(:name => 'Damaged LP', :enterprise_id => @profile.id, :product_category_id => @product_category.id)
  750 + prod2 = Product.create!(:name => 'Damaged CD', :enterprise_id => @profile.id, :product_category_id => @product_category.id)
  751 + prod3 = Product.create!(:name => 'Damaged DVD', :enterprise_id => @profile.id, :product_category_id => @product_category.id)
  752 +
  753 + assert_equal [prod3, prod2, prod1], Product.more_recent
  754 + end
570 755  
571 756 end
... ...
test/unit/profile_test.rb
... ... @@ -192,7 +192,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
192 192 assert_not_equal list.object_id, other_list.object_id
193 193 end
194 194  
195   -# This problem should be solved; talk to Bráulio if it fails
  195 + # This problem should be solved; talk to Bráulio if it fails
196 196 should 'be able to find profiles by their names' do
197 197 small = create(Profile, :name => 'A small profile for testing')
198 198 big = create(Profile, :name => 'A big profile for testing')
... ... @@ -388,12 +388,19 @@ class ProfileTest &lt; ActiveSupport::TestCase
388 388 assert_respond_to c, :categories
389 389 end
390 390  
  391 + should 'responds to categories including virtual' do
  392 + c = fast_create(Profile)
  393 + assert_respond_to c, :categories_including_virtual
  394 + end
  395 +
391 396 should 'have categories' do
392 397 c = fast_create(Profile)
393   - cat = Environment.default.categories.build(:name => 'a category'); cat.save!
  398 + pcat = Environment.default.categories.build(:name => 'a category'); pcat.save!
  399 + cat = Environment.default.categories.build(:name => 'a category', :parent_id => pcat.id); cat.save!
394 400 c.add_category cat
395 401 c.save!
396 402 assert_includes c.categories, cat
  403 + assert_includes c.categories_including_virtual, pcat
397 404 end
398 405  
399 406 should 'be able to list recent profiles' do
... ... @@ -543,13 +550,15 @@ class ProfileTest &lt; ActiveSupport::TestCase
543 550 profile = create_user('testuser').person
544 551 profile.add_category(c3)
545 552  
546   -
547 553 assert_equal [c3], profile.categories(true)
548 554 assert_equal [profile], c2.people(true)
549 555  
550 556 assert_includes c3.people(true), profile
551 557 assert_includes c2.people(true), profile
552 558 assert_includes c1.people(true), profile
  559 +
  560 + assert_includes profile.categories_including_virtual, c2
  561 + assert_includes profile.categories_including_virtual, c1
553 562 end
554 563  
555 564 should 'redefine the entire category set at once' do
... ... @@ -564,15 +573,18 @@ class ProfileTest &lt; ActiveSupport::TestCase
564 573 profile.category_ids = [c2,c3].map(&:id)
565 574  
566 575 assert_equivalent [c2, c3], profile.categories(true)
  576 + assert_equivalent [c2, c1, c3], profile.categories_including_virtual(true)
567 577 end
568 578  
569 579 should 'be able to create a profile with categories' do
570   - c1 = create(Category)
  580 + pcat = create(Category)
  581 + c1 = create(Category, :parent_id => pcat)
571 582 c2 = create(Category)
572 583  
573 584 profile = create(Profile, :category_ids => [c1.id, c2.id])
574 585  
575 586 assert_equivalent [c1, c2], profile.categories(true)
  587 + assert_equivalent [c1, pcat, c2], profile.categories_including_virtual(true)
576 588 end
577 589  
578 590 should 'be associated with a region' do
... ... @@ -625,32 +637,38 @@ class ProfileTest &lt; ActiveSupport::TestCase
625 637  
626 638 should 'be able to create with categories and region at the same time' do
627 639 region = fast_create(Region)
628   - category = fast_create(Category)
  640 + pcat = fast_create(Category)
  641 + category = fast_create(Category, :parent_id => pcat.id)
629 642 profile = create(Profile, :region => region, :category_ids => [category.id])
630 643  
631 644 assert_equivalent [region, category], profile.categories(true)
  645 + assert_equivalent [region, category, pcat], profile.categories_including_virtual(true)
632 646 end
633 647  
634 648 should 'be able to update categories and not get regions removed' do
635 649 region = fast_create(Region)
636 650 category = fast_create(Category)
637   - category2 = fast_create(Category)
  651 + pcat = fast_create(Category)
  652 + category2 = fast_create(Category, :parent_id => pcat.id)
638 653 profile = create(Profile, :region => region, :category_ids => [category.id])
639 654  
640 655 profile.update_attributes!(:category_ids => [category2.id])
641 656  
642 657 assert_includes profile.categories(true), region
  658 + assert_includes profile.categories_including_virtual(true), pcat
643 659 end
644 660  
645 661 should 'be able to update region and not get categories removed' do
646 662 region = fast_create(Region)
647 663 region2 = fast_create(Region)
648   - category = fast_create(Category)
  664 + pcat = fast_create(Category)
  665 + category = fast_create(Category, :parent_id => pcat.id)
649 666 profile = create(Profile, :region => region, :category_ids => [category.id])
650 667  
651 668 profile.update_attributes!(:region => region2)
652 669  
653 670 assert_includes profile.categories(true), category
  671 + assert_includes profile.categories_including_virtual(true), pcat
654 672 end
655 673  
656 674 should 'not accept product category as category' do
... ... @@ -757,6 +775,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
757 775 profile = fast_create(Profile)
758 776 profile.category_ids = [c2,c3,c3].map(&:id)
759 777 assert_equal [c2, c3], profile.categories(true)
  778 + assert_equal [c2, c1, c3], profile.categories_including_virtual(true)
760 779 end
761 780  
762 781 should 'not return nil members when a member is removed from system' do
... ... @@ -965,7 +984,7 @@ class ProfileTest &lt; ActiveSupport::TestCase
965 984  
966 985 assert !a_copy.advertise
967 986 end
968   -
  987 +
969 988 should 'copy set of boxes from profile template' do
970 989 template = fast_create(Profile)
971 990 template.boxes.destroy_all
... ... @@ -1403,10 +1422,12 @@ class ProfileTest &lt; ActiveSupport::TestCase
1403 1422  
1404 1423 should 'ignore category with id zero' do
1405 1424 profile = fast_create(Profile)
1406   - c = fast_create(Category)
  1425 + pcat = fast_create(Category, :id => 0)
  1426 + c = fast_create(Category, :parent_id => pcat.id)
1407 1427 profile.category_ids = ['0', c.id, nil]
1408 1428  
1409 1429 assert_equal [c], profile.categories
  1430 + assert_not_includes profile.categories_including_virtual, [pcat]
1410 1431 end
1411 1432  
1412 1433 should 'get first blog when has multiple blogs' do
... ... @@ -1772,48 +1793,51 @@ class ProfileTest &lt; ActiveSupport::TestCase
1772 1793 end
1773 1794  
1774 1795 should 'act as faceted' do
1775   - st = fast_create(State, :acronym => 'XZ')
1776   - city = fast_create(City, :name => 'Tabajara', :parent_id => st.id)
  1796 + st = fast_create(State, :acronym => 'XZ')
  1797 + city = fast_create(City, :name => 'Tabajara', :parent_id => st.id)
  1798 + cat = fast_create(Category)
1777 1799 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))
  1800 + prof.add_category(cat, true)
  1801 + assert_equal ['Tabajara', ', XZ'], Profile.facet_by_id(:f_region)[:proc].call(prof.send(:f_region))
  1802 + assert_equal "category_filter:#{cat.id}", Person.facet_category_query.call(cat)
1779 1803 end
1780 1804  
1781 1805 should 'act as searchable' do
1782   - st = fast_create(State, :acronym => 'CA')
1783   - city = fast_create(City, :name => 'Inglewood', :parent_id => st.id)
  1806 + st = create(State, :name => 'California', :acronym => 'CA', :environment_id => Environment.default.id)
  1807 + city = create(City, :name => 'Inglewood', :parent_id => st.id, :environment_id => Environment.default.id)
1784 1808 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
  1809 + :user_id => fast_create(User).id, :region_id => city.id)
  1810 + cat = create(Category, :name => "Science Fiction", :acronym => "sf", :abbreviation => "sci-fi")
  1811 + p.add_category cat
  1812 + cat.profiles.reload
  1813 + cat.save!
  1814 + p.save!
  1815 +
  1816 + # fields
  1817 + assert_includes Profile.find_by_contents('Hiro')[:results].docs, p
  1818 + assert_includes Profile.find_by_contents('Stor')[:results].docs, p
  1819 + assert_includes Profile.find_by_contents('Protagonist')[:results].docs, p
  1820 + # filters
  1821 + assert_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["public:true"]})[:results].docs, p
  1822 + assert_not_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["public:false"]})[:results].docs, p
  1823 + assert_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, p
1802 1824 # includes
1803   - assert_includes Profile.find_by_contents("Inglewood")[:results].docs, p
  1825 + assert_includes Profile.find_by_contents("Inglewood")[:results].docs, p
  1826 + assert_includes Profile.find_by_contents("California")[:results].docs, p
  1827 + assert_includes Profile.find_by_contents("Science")[:results].docs, p
1804 1828 end
1805 1829  
1806 1830 should 'boost name matches' do
1807   - in_addr = create(Person, :name => 'something', :address => 'bananas in the address!', :user_id => fast_create(User).id)
  1831 + in_addr = create(Person, :name => 'something', :address => 'bananas in the address!', :user_id => fast_create(User).id)
1808 1832 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
  1833 + assert_equal [in_name, in_addr], Person.find_by_contents('bananas')[:results].docs
1810 1834 end
1811 1835  
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
  1836 + should 'reindex articles after saving' do
  1837 + profile = create(Person, :name => 'something', :user_id => fast_create(User).id)
  1838 + art = profile.articles.build(:name => 'something')
  1839 + Profile.expects(:solr_batch_add).with(includes(art))
  1840 + profile.save!
  1841 + end
1818 1842  
1819 1843 end
... ...
test/unit/qualifier_test.rb
... ... @@ -61,4 +61,11 @@ class QualifierTest &lt; ActiveSupport::TestCase
61 61 assert_equal [], product1.product_qualifiers(true)
62 62 end
63 63  
  64 + should 'reindex products after saving' do
  65 + product = mock
  66 + Qualifier.any_instance.stubs(:products).returns([product])
  67 + Qualifier.expects(:solr_batch_add).with(includes(product))
  68 + qual = fast_create(Qualifier)
  69 + qual.save!
  70 + end
64 71 end
... ...
test/unit/set_profile_region_from_city_state_test.rb
... ... @@ -0,0 +1,33 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class SetProfileRegionFromCityStateTest < ActiveSupport::TestCase
  4 +
  5 + should 'set city and state from names' do
  6 + s = State.create!(:name => 'Sao Paulo', :acronym => 'SP', :environment_id => Environment.default.id)
  7 + c = City.create!(:name => 'Pindamonhangaba', :parent_id => s.id, :environment_id => Environment.default.id)
  8 + p = fast_create(Person, :user_id => fast_create(User).id)
  9 + p.state_with_region = 'SP'
  10 + p.city_with_region = 'Pindamonhangaba'
  11 + p.save!
  12 + assert p.region == c
  13 + end
  14 +
  15 + should 'set region to null if city not found' do
  16 + s = State.create!(:name => 'Sao Paulo', :acronym => 'SP', :environment_id => Environment.default.id)
  17 + p = fast_create(Person, :user_id => fast_create(User).id)
  18 + p.state_with_region = 'SP'
  19 + p.city_with_region = 'Pindamonhangaba'
  20 + p.save!
  21 + assert p.region.nil?
  22 + end
  23 +
  24 + should 'set region to null if state not found' do
  25 + s = State.create!(:name => 'Sao Paulo', :acronym => 'SP', :environment_id => Environment.default.id)
  26 + c = City.create!(:name => 'Pindamonhangaba', :parent_id => s.id, :environment_id => Environment.default.id)
  27 + p = fast_create(Person, :user_id => fast_create(User).id)
  28 + p.state_with_region = 'RJ'
  29 + p.city_with_region = 'Pindamonhangaba'
  30 + p.save!
  31 + assert p.region.nil?
  32 + end
  33 +end
... ...