From 0700b8027ee11c7d4e64a8a75fd4d3b3594d67e9 Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Mon, 28 Sep 2015 15:11:09 -0300 Subject: [PATCH] refactoring search api unit tests --- lib/noosfero/api/v1/search.rb | 2 +- test/unit/api/articles_test.rb | 17 +++++++++++++++++ test/unit/api/search_test.rb | 88 +++++++++++++++++++++++++++++++++++++++++++++------------------------------------------- 3 files changed, 63 insertions(+), 44 deletions(-) diff --git a/lib/noosfero/api/v1/search.rb b/lib/noosfero/api/v1/search.rb index 3fed86a..00dec52 100644 --- a/lib/noosfero/api/v1/search.rb +++ b/lib/noosfero/api/v1/search.rb @@ -21,7 +21,7 @@ module Noosfero scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present? - category = params[:category] || "" + category = environment.categories.find(params[:category]) unless params[:category].nil? query = params[:query] || "" order = "more_recent" diff --git a/test/unit/api/articles_test.rb b/test/unit/api/articles_test.rb index 547e276..a0a568f 100644 --- a/test/unit/api/articles_test.rb +++ b/test/unit/api/articles_test.rb @@ -555,6 +555,23 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal e1.id, json['articles'][0]['id'] end + should 'not list uncategorized event of a community if a category is given' do + co = Community.create(identifier: 'my-community', name: 'name-my-community') + c1 = Category.create(environment: Environment.default, name: 'my-category') + c2 = Category.create(environment: Environment.default, name: 'dont-show-me-this-category') + e1 = fast_create(Event, :profile_id => co.id) + e2 = fast_create(Event, :profile_id => co.id) + e3 = fast_create(Event, :profile_id => co.id) + e1.categories << c1 + e2.categories << c2 + params['category_ids[]']=c1.id + params['content_type']='Event' + get "api/v1/communities/#{co.id}/articles?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 1, json['articles'].count + assert_equal e1.id, json['articles'][0]['id'] + end + should 'list events of a community in a given 2 categories' do co = Community.create(identifier: 'my-community', name: 'name-my-community') c1 = Category.create(environment: Environment.default, name: 'my-category') diff --git a/test/unit/api/search_test.rb b/test/unit/api/search_test.rb index 8f2cfef..aa47c5f 100644 --- a/test/unit/api/search_test.rb +++ b/test/unit/api/search_test.rb @@ -2,13 +2,14 @@ require File.dirname(__FILE__) + '/test_helper' class SearchTest < ActiveSupport::TestCase - def create_article_with_optional_category(name, profile, category = nil, parent = nil) - fast_create(Article, {:name => name, :profile_id => profile.id }, :search => true, :category => category, :title => name, :parent => parent) + def setup + @person = create_user('testing').person end + attr_reader :person should 'not list unpublished articles' do - person = fast_create(Person) - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) + Article.delete_all + article = fast_create(Article, :profile_id => person.id, :published => false) assert !article.published? get "/api/v1/search/article" json = JSON.parse(last_response.body) @@ -16,52 +17,48 @@ class SearchTest < ActiveSupport::TestCase end should 'list articles' do - person = fast_create(Person) - art = create_article_with_optional_category('an article to be found', person) + fast_create(Article, :profile_id => person.id) get "/api/v1/search/article" json = JSON.parse(last_response.body) assert_not_empty json['articles'] end should 'invalid search string articles' do - person = fast_create(Person) - art = create_article_with_optional_category('an article to be found', person) + fast_create(Article, :profile_id => person.id, :name => 'some article') get "/api/v1/search/article?query=test" json = JSON.parse(last_response.body) assert_empty json['articles'] end should 'do not list articles of wrong type' do - person = fast_create(Person) - art = create_article_with_optional_category('an article to be found', person) + fast_create(Article, :profile_id => person.id) get "/api/v1/search/article?type=TinyMceArticle" json = JSON.parse(last_response.body) assert_empty json['articles'] end should 'list articles of one type' do - person = fast_create(Person) - art = create_article_with_optional_category('an article to be found', person) - article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) + fast_create(Article, :profile_id => person.id) + article = fast_create(TinyMceArticle, :profile_id => person.id) + get "/api/v1/search/article?type=TinyMceArticle" json = JSON.parse(last_response.body) - assert_equal 1, json['articles'].count + assert_equal article.id, json['articles'].first['id'] end should 'list articles of one type and query string' do - person = fast_create(Person) - art = create_article_with_optional_category('an article to be found', person) - art1 = create_article_with_optional_category('article for query', person) - article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) + fast_create(Article, :profile_id => person.id, :name => 'some article') + fast_create(Article, :profile_id => person.id, :name => 'Some thing') + article = fast_create(TinyMceArticle, :profile_id => person.id, :name => 'Some thing') get "/api/v1/search/article?type=TinyMceArticle&query=thing" json = JSON.parse(last_response.body) assert_equal 1, json['articles'].count + assert_equal article.id, json['articles'].first['id'] end should 'not return more entries than page limit' do - person = fast_create(Person) - ('1'..'5').each do |n| - art = create_article_with_optional_category("Article #{n}", person) + 1.upto(5).each do |n| + fast_create(Article, :profile_id => person.id, :name => "Article #{n}") end get "/api/v1/search/article?query=Article&per_page=3" @@ -71,9 +68,8 @@ class SearchTest < ActiveSupport::TestCase end should 'return entries second page' do - person = fast_create(Person) - ('1'..'5').each do |n| - art = create_article_with_optional_category("Article #{n}", person) + 1.upto(5).each do |n| + fast_create(Article, :profile_id => person.id, :name => "Article #{n}") end get "/api/v1/search/article?query=Article&per_page=3&page=2" @@ -83,38 +79,44 @@ class SearchTest < ActiveSupport::TestCase end should 'search articles in profile' do - person1 = fast_create(Person) - person2 = fast_create(Person) - - art1 = create_article_with_optional_category("Article 1 for profile #{person1.id}", person1) - art2 = create_article_with_optional_category("Article for profile #{person2.id}", person2) - art3 = create_article_with_optional_category("Article 2 for profile #{person1.id}", person1) + person2 = fast_create(Person) + fast_create(Article, :profile_id => person.id) + fast_create(Article, :profile_id => person.id) + article = fast_create(Article, :profile_id => person2.id) - get "/api/v1/search/article?query=Article&profile_id=#{person1.id}" + get "/api/v1/search/article?query=Article&profile_id=#{person2.id}" json = JSON.parse(last_response.body) - # Only for person1 - assert_equal 2, json['articles'].count + assert_equal article.id, json['articles'].first['id'] end - should 'search with fields' do - person = fast_create(Person) - art = create_article_with_optional_category('an article to be found', person) + should 'search and return values specified in fields parameter' do + fast_create(Article, :profile_id => person.id) get "/api/v1/search/article?fields=title" json = JSON.parse(last_response.body) assert_not_empty json['articles'] - assert_equal ['title'], json['articles'].first.keys + assert_equal ['title'], json['articles'].first.keys end should 'search with parent' do - person = fast_create(Person) - parent = fast_create(Folder, :profile_id => person.id, :name => "parent", :published => true) - art = fast_create(Article, :profile_id => person.id, :name => "child", :parent_id => parent.id) - art2 = fast_create(Article, :profile_id => person.id, :name => "child2") + parent = fast_create(Folder, :profile_id => person.id) + fast_create(Article, :profile_id => person.id) + article = fast_create(Article, :profile_id => person.id, :parent_id => parent.id) get "/api/v1/search/article?parent_id=#{parent.id}" json = JSON.parse(last_response.body) - assert_not_empty json['articles'] - assert_equal art.id, json['articles'].first["id"] assert_equal 1, json['articles'].count + assert_equal article.id, json['articles'].first["id"] + end + + should 'search filter by category' do + Article.delete_all + fast_create(Article, :profile_id => person.id) + article = fast_create(Article, :profile_id => person.id) + category = fast_create(Category) + article.categories<< category + get "/api/v1/search/article?category=#{category.id}" + json = JSON.parse(last_response.body) + assert_equal 1, json['articles'].count + assert_equal article.id, json['articles'].first["id"] end end -- libgit2 0.21.2