File: search_controller_test.dsl.rb

package info (click to toggle)
ruby-elasticsearch-rails 7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,420 kB
  • sloc: ruby: 1,661; makefile: 4
file content (148 lines) | stat: -rw-r--r-- 5,904 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

require 'test_helper'
require 'sidekiq/api'

class SearchControllerTest < ActionController::TestCase
  setup do
    travel_to Time.new(2015, 03, 16, 10, 00, 00, 0)

    Article.delete_all

    articles = [
      { title: 'Article One', abstract: 'One', content: 'One', published_on: 1.day.ago, category_title: 'One', author_first_name: 'John', author_last_name: 'Smith' },
      { title: 'Article One Another', abstract: '', content: '', published_on: 2.days.ago, category_title: 'One', author_first_name: 'John', author_last_name: 'Smith' },
      { title: 'Article One Two', abstract: '', content: '', published_on: 10.days.ago, category_title: 'Two', author_first_name: 'Mary', author_last_name: 'Smith' },
      { title: 'Article Two', abstract: '', content: '', published_on: 12.days.ago, category_title: 'Two', author_first_name: 'Mary', author_last_name: 'Smith' },
      { title: 'Article Three', abstract: '', content: '', published_on: 12.days.ago, category_title: 'Three', author_first_name: 'Alice', author_last_name: 'Smith' }
    ]

    articles.each do |a|
      article = Article.create! \
        title:    a[:title],
        abstract: a[:abstract],
        content:  a[:content],
        published_on: a[:published_on]

      article.categories << Category.find_or_create_by!(title: a[:category_title])

      article.authors << Author.find_or_create_by!(first_name: a[:author_first_name], last_name: a[:author_last_name])

      article.save!
    end

    Article.find_by_title('Article Three').comments.create body: 'One'

    Sidekiq::Queue.new("elasticsearch").clear

    Article.__elasticsearch__.import force: true
    Article.__elasticsearch__.refresh_index!
  end

  test "should return search results" do
    get :index, params: { q: 'one' }
    assert_response :success
    assert_equal 3, assigns(:articles).size
  end

  test "should return search results in comments" do
    get :index, params: { q: 'one', comments: 'y' }
    assert_response :success
    assert_equal 4, assigns(:articles).size
  end

  test "should return highlighted snippets" do
    get :index, params: { q: 'one' }
    assert_response :success
    assert_match %r{<em class="label label-highlight">One</em>}, assigns(:articles).first.highlight.title.first
  end

  test "should return suggestions" do
    get :index, params: { q: 'one' }
    assert_response :success

    suggestions = assigns(:articles).response.suggestions

    assert_equal 'one', suggestions['suggest_title'][0]['text']
  end

  test "should return aggregations" do
    get :index, params: { q: 'one' }
    assert_response :success

    aggregations = assigns(:articles).response.response['aggregations']

    assert_equal 2, aggregations['categories']['categories']['buckets'].size
    assert_equal 2, aggregations['authors']['authors']['buckets'].size
    assert_equal 2, aggregations['published']['published']['buckets'].size

    assert_equal 'John Smith', aggregations['authors']['authors']['buckets'][0]['key']
    assert_equal 'One', aggregations['categories']['categories']['buckets'][0]['key']
    assert_equal '2015-03-02T00:00:00.000Z', aggregations['published']['published']['buckets'][0]['key_as_string']
  end

  test "should sort on the published date" do
    get :index, params: { q: 'one', s: 'published_on' }
    assert_response :success

    assert_equal 3, assigns(:articles).size
    assert_equal '2015-03-15', assigns(:articles)[0].published_on
    assert_equal '2015-03-14', assigns(:articles)[1].published_on
    assert_equal '2015-03-06', assigns(:articles)[2].published_on
  end

  test "should sort on the published date when no query is provided" do
    get :index, params: { q: '' }
    assert_response :success

    assert_equal 5, assigns(:articles).size
    assert_equal '2015-03-15', assigns(:articles)[0].published_on
    assert_equal '2015-03-14', assigns(:articles)[1].published_on
    assert_equal '2015-03-06', assigns(:articles)[2].published_on
  end

  test "should filter search results and the author and published date facets when user selects a category" do
    get :index, params: { q: 'one', c: 'One' }
    assert_response :success

    assert_equal 2, assigns(:articles).size

    aggregations = assigns(:articles).response.response['aggregations']

    assert_equal 1, aggregations['authors']['authors']['buckets'].size
    assert_equal 1, aggregations['published']['published']['buckets'].size

    # Do NOT filter the category facet
    assert_equal 2, aggregations['categories']['categories']['buckets'].size
  end

  test "should filter search results and the category and published date facets when user selects a category" do
    get :index, params: { q: 'one', a: 'Mary Smith' }
    assert_response :success

    assert_equal 1, assigns(:articles).size

    aggregations = assigns(:articles).response.response['aggregations']

    assert_equal 1, aggregations['categories']['categories']['buckets'].size
    assert_equal 1, aggregations['published']['published']['buckets'].size

    # Do NOT filter the authors facet
    assert_equal 2, aggregations['authors']['authors']['buckets'].size
  end
end