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,7 +21,7 @@ module Noosfero | ||
21 | 21 | ||
22 | scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present? | 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 | query = params[:query] || "" | 26 | query = params[:query] || "" |
27 | order = "more_recent" | 27 | order = "more_recent" |
test/unit/api/articles_test.rb
@@ -555,6 +555,23 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -555,6 +555,23 @@ class ArticlesTest < ActiveSupport::TestCase | ||
555 | assert_equal e1.id, json['articles'][0]['id'] | 555 | assert_equal e1.id, json['articles'][0]['id'] |
556 | end | 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 | should 'list events of a community in a given 2 categories' do | 575 | should 'list events of a community in a given 2 categories' do |
559 | co = Community.create(identifier: 'my-community', name: 'name-my-community') | 576 | co = Community.create(identifier: 'my-community', name: 'name-my-community') |
560 | c1 = Category.create(environment: Environment.default, name: 'my-category') | 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,13 +2,14 @@ require File.dirname(__FILE__) + '/test_helper' | ||
2 | 2 | ||
3 | class SearchTest < ActiveSupport::TestCase | 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 | end | 7 | end |
8 | + attr_reader :person | ||
8 | 9 | ||
9 | should 'not list unpublished articles' do | 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 | assert !article.published? | 13 | assert !article.published? |
13 | get "/api/v1/search/article" | 14 | get "/api/v1/search/article" |
14 | json = JSON.parse(last_response.body) | 15 | json = JSON.parse(last_response.body) |
@@ -16,52 +17,48 @@ class SearchTest < ActiveSupport::TestCase | @@ -16,52 +17,48 @@ class SearchTest < ActiveSupport::TestCase | ||
16 | end | 17 | end |
17 | 18 | ||
18 | should 'list articles' do | 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 | get "/api/v1/search/article" | 21 | get "/api/v1/search/article" |
22 | json = JSON.parse(last_response.body) | 22 | json = JSON.parse(last_response.body) |
23 | assert_not_empty json['articles'] | 23 | assert_not_empty json['articles'] |
24 | end | 24 | end |
25 | 25 | ||
26 | should 'invalid search string articles' do | 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 | get "/api/v1/search/article?query=test" | 28 | get "/api/v1/search/article?query=test" |
30 | json = JSON.parse(last_response.body) | 29 | json = JSON.parse(last_response.body) |
31 | assert_empty json['articles'] | 30 | assert_empty json['articles'] |
32 | end | 31 | end |
33 | 32 | ||
34 | should 'do not list articles of wrong type' do | 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 | get "/api/v1/search/article?type=TinyMceArticle" | 35 | get "/api/v1/search/article?type=TinyMceArticle" |
38 | json = JSON.parse(last_response.body) | 36 | json = JSON.parse(last_response.body) |
39 | assert_empty json['articles'] | 37 | assert_empty json['articles'] |
40 | end | 38 | end |
41 | 39 | ||
42 | should 'list articles of one type' do | 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 | get "/api/v1/search/article?type=TinyMceArticle" | 44 | get "/api/v1/search/article?type=TinyMceArticle" |
47 | json = JSON.parse(last_response.body) | 45 | json = JSON.parse(last_response.body) |
48 | - assert_equal 1, json['articles'].count | 46 | + assert_equal article.id, json['articles'].first['id'] |
49 | end | 47 | end |
50 | 48 | ||
51 | should 'list articles of one type and query string' do | 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 | get "/api/v1/search/article?type=TinyMceArticle&query=thing" | 53 | get "/api/v1/search/article?type=TinyMceArticle&query=thing" |
57 | json = JSON.parse(last_response.body) | 54 | json = JSON.parse(last_response.body) |
58 | assert_equal 1, json['articles'].count | 55 | assert_equal 1, json['articles'].count |
56 | + assert_equal article.id, json['articles'].first['id'] | ||
59 | end | 57 | end |
60 | 58 | ||
61 | should 'not return more entries than page limit' do | 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 | end | 62 | end |
66 | 63 | ||
67 | get "/api/v1/search/article?query=Article&per_page=3" | 64 | get "/api/v1/search/article?query=Article&per_page=3" |
@@ -71,9 +68,8 @@ class SearchTest < ActiveSupport::TestCase | @@ -71,9 +68,8 @@ class SearchTest < ActiveSupport::TestCase | ||
71 | end | 68 | end |
72 | 69 | ||
73 | should 'return entries second page' do | 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 | end | 73 | end |
78 | 74 | ||
79 | get "/api/v1/search/article?query=Article&per_page=3&page=2" | 75 | get "/api/v1/search/article?query=Article&per_page=3&page=2" |
@@ -83,38 +79,44 @@ class SearchTest < ActiveSupport::TestCase | @@ -83,38 +79,44 @@ class SearchTest < ActiveSupport::TestCase | ||
83 | end | 79 | end |
84 | 80 | ||
85 | should 'search articles in profile' do | 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 | json = JSON.parse(last_response.body) | 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 | end | 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 | get "/api/v1/search/article?fields=title" | 94 | get "/api/v1/search/article?fields=title" |
103 | json = JSON.parse(last_response.body) | 95 | json = JSON.parse(last_response.body) |
104 | assert_not_empty json['articles'] | 96 | assert_not_empty json['articles'] |
105 | - assert_equal ['title'], json['articles'].first.keys | 97 | + assert_equal ['title'], json['articles'].first.keys |
106 | end | 98 | end |
107 | 99 | ||
108 | should 'search with parent' do | 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 | get "/api/v1/search/article?parent_id=#{parent.id}" | 104 | get "/api/v1/search/article?parent_id=#{parent.id}" |
114 | json = JSON.parse(last_response.body) | 105 | json = JSON.parse(last_response.body) |
115 | - assert_not_empty json['articles'] | ||
116 | - assert_equal art.id, json['articles'].first["id"] | ||
117 | assert_equal 1, json['articles'].count | 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 | end | 120 | end |
119 | 121 | ||
120 | end | 122 | end |