Commit 0700b8027ee11c7d4e64a8a75fd4d3b3594d67e9
1 parent
3a02b482
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
refactoring search api unit tests
Showing
3 changed files
with
63 additions
and
44 deletions
Show diff stats
lib/noosfero/api/v1/search.rb
| ... | ... | @@ -21,7 +21,7 @@ module Noosfero |
| 21 | 21 | |
| 22 | 22 | scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present? |
| 23 | 23 | |
| 24 | - category = params[:category] || "" | |
| 24 | + category = environment.categories.find(params[:category]) unless params[:category].nil? | |
| 25 | 25 | |
| 26 | 26 | query = params[:query] || "" |
| 27 | 27 | order = "more_recent" | ... | ... |
test/unit/api/articles_test.rb
| ... | ... | @@ -555,6 +555,23 @@ class ArticlesTest < ActiveSupport::TestCase |
| 555 | 555 | assert_equal e1.id, json['articles'][0]['id'] |
| 556 | 556 | end |
| 557 | 557 | |
| 558 | + should 'not list uncategorized event of a community if a category is given' do | |
| 559 | + co = Community.create(identifier: 'my-community', name: 'name-my-community') | |
| 560 | + c1 = Category.create(environment: Environment.default, name: 'my-category') | |
| 561 | + c2 = Category.create(environment: Environment.default, name: 'dont-show-me-this-category') | |
| 562 | + e1 = fast_create(Event, :profile_id => co.id) | |
| 563 | + e2 = fast_create(Event, :profile_id => co.id) | |
| 564 | + e3 = fast_create(Event, :profile_id => co.id) | |
| 565 | + e1.categories << c1 | |
| 566 | + e2.categories << c2 | |
| 567 | + params['category_ids[]']=c1.id | |
| 568 | + params['content_type']='Event' | |
| 569 | + get "api/v1/communities/#{co.id}/articles?#{params.to_query}" | |
| 570 | + json = JSON.parse(last_response.body) | |
| 571 | + assert_equal 1, json['articles'].count | |
| 572 | + assert_equal e1.id, json['articles'][0]['id'] | |
| 573 | + end | |
| 574 | + | |
| 558 | 575 | should 'list events of a community in a given 2 categories' do |
| 559 | 576 | co = Community.create(identifier: 'my-community', name: 'name-my-community') |
| 560 | 577 | c1 = Category.create(environment: Environment.default, name: 'my-category') | ... | ... |
test/unit/api/search_test.rb
| ... | ... | @@ -2,13 +2,14 @@ require File.dirname(__FILE__) + '/test_helper' |
| 2 | 2 | |
| 3 | 3 | class SearchTest < ActiveSupport::TestCase |
| 4 | 4 | |
| 5 | - def create_article_with_optional_category(name, profile, category = nil, parent = nil) | |
| 6 | - fast_create(Article, {:name => name, :profile_id => profile.id }, :search => true, :category => category, :title => name, :parent => parent) | |
| 5 | + def setup | |
| 6 | + @person = create_user('testing').person | |
| 7 | 7 | end |
| 8 | + attr_reader :person | |
| 8 | 9 | |
| 9 | 10 | should 'not list unpublished articles' do |
| 10 | - person = fast_create(Person) | |
| 11 | - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) | |
| 11 | + Article.delete_all | |
| 12 | + article = fast_create(Article, :profile_id => person.id, :published => false) | |
| 12 | 13 | assert !article.published? |
| 13 | 14 | get "/api/v1/search/article" |
| 14 | 15 | json = JSON.parse(last_response.body) |
| ... | ... | @@ -16,52 +17,48 @@ class SearchTest < ActiveSupport::TestCase |
| 16 | 17 | end |
| 17 | 18 | |
| 18 | 19 | should 'list articles' do |
| 19 | - person = fast_create(Person) | |
| 20 | - art = create_article_with_optional_category('an article to be found', person) | |
| 20 | + fast_create(Article, :profile_id => person.id) | |
| 21 | 21 | get "/api/v1/search/article" |
| 22 | 22 | json = JSON.parse(last_response.body) |
| 23 | 23 | assert_not_empty json['articles'] |
| 24 | 24 | end |
| 25 | 25 | |
| 26 | 26 | should 'invalid search string articles' do |
| 27 | - person = fast_create(Person) | |
| 28 | - art = create_article_with_optional_category('an article to be found', person) | |
| 27 | + fast_create(Article, :profile_id => person.id, :name => 'some article') | |
| 29 | 28 | get "/api/v1/search/article?query=test" |
| 30 | 29 | json = JSON.parse(last_response.body) |
| 31 | 30 | assert_empty json['articles'] |
| 32 | 31 | end |
| 33 | 32 | |
| 34 | 33 | should 'do not list articles of wrong type' do |
| 35 | - person = fast_create(Person) | |
| 36 | - art = create_article_with_optional_category('an article to be found', person) | |
| 34 | + fast_create(Article, :profile_id => person.id) | |
| 37 | 35 | get "/api/v1/search/article?type=TinyMceArticle" |
| 38 | 36 | json = JSON.parse(last_response.body) |
| 39 | 37 | assert_empty json['articles'] |
| 40 | 38 | end |
| 41 | 39 | |
| 42 | 40 | should 'list articles of one type' do |
| 43 | - person = fast_create(Person) | |
| 44 | - art = create_article_with_optional_category('an article to be found', person) | |
| 45 | - article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) | |
| 41 | + fast_create(Article, :profile_id => person.id) | |
| 42 | + article = fast_create(TinyMceArticle, :profile_id => person.id) | |
| 43 | + | |
| 46 | 44 | get "/api/v1/search/article?type=TinyMceArticle" |
| 47 | 45 | json = JSON.parse(last_response.body) |
| 48 | - assert_equal 1, json['articles'].count | |
| 46 | + assert_equal article.id, json['articles'].first['id'] | |
| 49 | 47 | end |
| 50 | 48 | |
| 51 | 49 | should 'list articles of one type and query string' do |
| 52 | - person = fast_create(Person) | |
| 53 | - art = create_article_with_optional_category('an article to be found', person) | |
| 54 | - art1 = create_article_with_optional_category('article for query', person) | |
| 55 | - article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) | |
| 50 | + fast_create(Article, :profile_id => person.id, :name => 'some article') | |
| 51 | + fast_create(Article, :profile_id => person.id, :name => 'Some thing') | |
| 52 | + article = fast_create(TinyMceArticle, :profile_id => person.id, :name => 'Some thing') | |
| 56 | 53 | get "/api/v1/search/article?type=TinyMceArticle&query=thing" |
| 57 | 54 | json = JSON.parse(last_response.body) |
| 58 | 55 | assert_equal 1, json['articles'].count |
| 56 | + assert_equal article.id, json['articles'].first['id'] | |
| 59 | 57 | end |
| 60 | 58 | |
| 61 | 59 | should 'not return more entries than page limit' do |
| 62 | - person = fast_create(Person) | |
| 63 | - ('1'..'5').each do |n| | |
| 64 | - art = create_article_with_optional_category("Article #{n}", person) | |
| 60 | + 1.upto(5).each do |n| | |
| 61 | + fast_create(Article, :profile_id => person.id, :name => "Article #{n}") | |
| 65 | 62 | end |
| 66 | 63 | |
| 67 | 64 | get "/api/v1/search/article?query=Article&per_page=3" |
| ... | ... | @@ -71,9 +68,8 @@ class SearchTest < ActiveSupport::TestCase |
| 71 | 68 | end |
| 72 | 69 | |
| 73 | 70 | should 'return entries second page' do |
| 74 | - person = fast_create(Person) | |
| 75 | - ('1'..'5').each do |n| | |
| 76 | - art = create_article_with_optional_category("Article #{n}", person) | |
| 71 | + 1.upto(5).each do |n| | |
| 72 | + fast_create(Article, :profile_id => person.id, :name => "Article #{n}") | |
| 77 | 73 | end |
| 78 | 74 | |
| 79 | 75 | get "/api/v1/search/article?query=Article&per_page=3&page=2" |
| ... | ... | @@ -83,38 +79,44 @@ class SearchTest < ActiveSupport::TestCase |
| 83 | 79 | end |
| 84 | 80 | |
| 85 | 81 | should 'search articles in profile' do |
| 86 | - person1 = fast_create(Person) | |
| 87 | - person2 = fast_create(Person) | |
| 88 | - | |
| 89 | - art1 = create_article_with_optional_category("Article 1 for profile #{person1.id}", person1) | |
| 90 | - art2 = create_article_with_optional_category("Article for profile #{person2.id}", person2) | |
| 91 | - art3 = create_article_with_optional_category("Article 2 for profile #{person1.id}", person1) | |
| 82 | + person2 = fast_create(Person) | |
| 83 | + fast_create(Article, :profile_id => person.id) | |
| 84 | + fast_create(Article, :profile_id => person.id) | |
| 85 | + article = fast_create(Article, :profile_id => person2.id) | |
| 92 | 86 | |
| 93 | - get "/api/v1/search/article?query=Article&profile_id=#{person1.id}" | |
| 87 | + get "/api/v1/search/article?query=Article&profile_id=#{person2.id}" | |
| 94 | 88 | json = JSON.parse(last_response.body) |
| 95 | - # Only for person1 | |
| 96 | - assert_equal 2, json['articles'].count | |
| 89 | + assert_equal article.id, json['articles'].first['id'] | |
| 97 | 90 | end |
| 98 | 91 | |
| 99 | - should 'search with fields' do | |
| 100 | - person = fast_create(Person) | |
| 101 | - art = create_article_with_optional_category('an article to be found', person) | |
| 92 | + should 'search and return values specified in fields parameter' do | |
| 93 | + fast_create(Article, :profile_id => person.id) | |
| 102 | 94 | get "/api/v1/search/article?fields=title" |
| 103 | 95 | json = JSON.parse(last_response.body) |
| 104 | 96 | assert_not_empty json['articles'] |
| 105 | - assert_equal ['title'], json['articles'].first.keys | |
| 97 | + assert_equal ['title'], json['articles'].first.keys | |
| 106 | 98 | end |
| 107 | 99 | |
| 108 | 100 | should 'search with parent' do |
| 109 | - person = fast_create(Person) | |
| 110 | - parent = fast_create(Folder, :profile_id => person.id, :name => "parent", :published => true) | |
| 111 | - art = fast_create(Article, :profile_id => person.id, :name => "child", :parent_id => parent.id) | |
| 112 | - art2 = fast_create(Article, :profile_id => person.id, :name => "child2") | |
| 101 | + parent = fast_create(Folder, :profile_id => person.id) | |
| 102 | + fast_create(Article, :profile_id => person.id) | |
| 103 | + article = fast_create(Article, :profile_id => person.id, :parent_id => parent.id) | |
| 113 | 104 | get "/api/v1/search/article?parent_id=#{parent.id}" |
| 114 | 105 | json = JSON.parse(last_response.body) |
| 115 | - assert_not_empty json['articles'] | |
| 116 | - assert_equal art.id, json['articles'].first["id"] | |
| 117 | 106 | assert_equal 1, json['articles'].count |
| 107 | + assert_equal article.id, json['articles'].first["id"] | |
| 108 | + end | |
| 109 | + | |
| 110 | + should 'search filter by category' do | |
| 111 | + Article.delete_all | |
| 112 | + fast_create(Article, :profile_id => person.id) | |
| 113 | + article = fast_create(Article, :profile_id => person.id) | |
| 114 | + category = fast_create(Category) | |
| 115 | + article.categories<< category | |
| 116 | + get "/api/v1/search/article?category=#{category.id}" | |
| 117 | + json = JSON.parse(last_response.body) | |
| 118 | + assert_equal 1, json['articles'].count | |
| 119 | + assert_equal article.id, json['articles'].first["id"] | |
| 118 | 120 | end |
| 119 | 121 | |
| 120 | 122 | end | ... | ... |