testear el buscador

This commit is contained in:
f 2021-05-14 16:59:47 -03:00
parent 7413f6e2aa
commit 219a9985f5
5 changed files with 107 additions and 31 deletions

View file

@ -19,7 +19,7 @@ class IndexedPost < ApplicationRecord
query: query, query: query,
using: { using: {
tsearch: { tsearch: {
dictionary: dictionary, dictionary: IndexedPost.to_dictionary(locale: locale),
tsvector_column: 'indexed_content' tsvector_column: 'indexed_content'
}, },
trigram: { trigram: {
@ -30,9 +30,9 @@ class IndexedPost < ApplicationRecord
} }
# Trae los IndexedPost en el orden en que van a terminar en el sitio. # Trae los IndexedPost en el orden en que van a terminar en el sitio.
default_scope lambda { order(order: :desc, created_at: :desc) } default_scope -> { order(order: :desc, created_at: :desc) }
scope :in_category, lambda { |category| where("front_matter->'categories' ? :category", category: category.to_s) } scope :in_category, ->(category) { where("front_matter->'categories' ? :category", category: category.to_s) }
scope :by_usuarie, lambda { |usuarie| where("front_matter->'usuaries' @> :usuarie::jsonb", usuarie: usuarie.to_s) } scope :by_usuarie, ->(usuarie) { where("front_matter->'usuaries' @> :usuarie::jsonb", usuarie: usuarie.to_s) }
belongs_to :site belongs_to :site

View file

@ -47,15 +47,10 @@ class Post
# #
# @return [Hash] # @return [Hash]
def indexable_front_matter def indexable_front_matter
{}.tap do |indexable_front_matter| {}.tap do |ifm|
indexable_front_matter = { ifm[:usuaries] = usuaries.map(&:id)
usuaries: usuaries.map(&:id), ifm[:draft] = attribute?(:draft) ? draft.value : false
draft: attribute?(:draft) ? draft.value : false ifm[:categories] = categories.indexable_values if attribute? :categories
}
if attribute? :categories
indexable_front_matter[:categories] = categories.indexable_values
end
end end
end end

View file

@ -7,7 +7,6 @@ class Site < ApplicationRecord
include Site::Forms include Site::Forms
include Site::FindAndReplace include Site::FindAndReplace
include Site::Api include Site::Api
include Site::Index
include Tienda include Tienda
# Cifrar la llave privada que cifra y decifra campos ocultos. Sutty # Cifrar la llave privada que cifra y decifra campos ocultos. Sutty
@ -38,7 +37,6 @@ class Site < ApplicationRecord
belongs_to :design belongs_to :design
belongs_to :licencia belongs_to :licencia
has_many :indexed_posts, dependent: :destroy
has_many :log_entries, dependent: :destroy has_many :log_entries, dependent: :destroy
has_many :deploys, dependent: :destroy has_many :deploys, dependent: :destroy
has_many :build_stats, through: :deploys has_many :build_stats, through: :deploys
@ -70,6 +68,9 @@ class Site < ApplicationRecord
# El sitio en Jekyll # El sitio en Jekyll
attr_reader :jekyll attr_reader :jekyll
# XXX: Es importante incluir luego de los callbacks de :load_jekyll
include Site::Index
# No permitir HTML en estos atributos # No permitir HTML en estos atributos
def title=(title) def title=(title)
super(title.strip_tags) super(title.strip_tags)

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'test_helper'
class IndexedPostTest < ActiveSupport::TestCase
def site
@site ||= create :site
end
teardown do
@site&.destroy
end
test 'se pueden convertir los diccionarios' do
IndexedPost::DICTIONARIES.each do |locale, dict|
assert_equal dict, IndexedPost.to_dictionary(locale: locale)
end
end
test 'se pueden buscar por categoría' do
assert(post = site.posts.create(title: SecureRandom.hex, description: SecureRandom.hex,
categories: [SecureRandom.hex, SecureRandom.hex]))
assert_not_empty site.indexed_posts.in_category(post.categories.value.sample)
end
test 'se pueden encontrar por usuarie' do
usuarie = create :usuarie
assert(post = site.posts.create(title: SecureRandom.hex, description: SecureRandom.hex))
post.usuaries << usuarie
post.save
assert_not_empty site.indexed_posts.by_usuarie(usuarie.id)
end
end

View file

@ -0,0 +1,45 @@
# frozen_string_literal: true
require 'test_helper'
class Post::IndexableTest < ActiveSupport::TestCase
setup do
@site = create :site
end
teardown do
@site&.destroy
end
test 'los posts se indexan apenas se crean' do
post = @site.posts.create(title: SecureRandom.hex, description: SecureRandom.hex)
indexed_post = @site.indexed_posts.find_by_title post.title.value
assert indexed_post
assert_equal post.locale.value.to_s, indexed_post.locale
assert_equal post.order.value, indexed_post.order
assert_equal post.path.basename, indexed_post.path
assert_equal post.layout.name.to_s, indexed_post.layout
end
test 'se pueden encontrar posts' do
post = @site.posts.sample
assert @site.indexed_posts.where(locale: post.lang.value).search(post.lang.value, post.title.value)
assert @site.indexed_posts.where(locale: post.lang.value).search(post.lang.value, post.description.value)
end
test 'se pueden actualizar posts' do
post = @site.posts.sample
post.description.value = SecureRandom.hex
assert post.save
assert @site.indexed_posts.where(locale: post.lang.value).search(post.lang.value, post.description.value)
end
test 'al borrar el post se borra el indice' do
post = @site.posts.sample
assert post.destroy
assert_not @site.indexed_posts.find_by_id(post.uuid.value)
end
end