gpt4 book ai didi

ruby-on-rails - 为什么 thinking-sphinx 在开发中有效,但在我的规范中却无效?

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:05 25 4
gpt4 key购买 nike

这对我来说似乎很有趣:在开发模式下 TS 可以轻松处理查询,但在测试中我会出错。

这是我的测试:

describe 'GET #index' do
subject { get :index }

let!(:products) { Fabricate.times(10, :product) }

before(:all) do
ThinkingSphinx::Test.init
ThinkingSphinx::Test.index
# sleeping for some time
ThinkingSphinx::Test.start
end

after(:all) do
ThinkingSphinx::Test.stop
end

it { expect(subject).to render_template(:index) }
it { expect{subject}.to change{ assigns(:search).try(:products) }.to match_array(products) }
end

这是我的 Controller :

def index
@search = Query::Products.new(params.merge({
kind_cd: Product.product,
contractor: current_contractor
}))
end

这是我的服务对象:

class Query::Products

def initialize(params)
@params = params
process_products
process_categories # error in this method
process_properties
process_brands
process_prices
end

private

def process_products
# ... other code

@product_facets = Product.facets(query_string, common_search_options)
end

def common_search_options
{
order: ordering,
ranker: :bm25,
with: {
price_type_for_search => min_price_conditions..max_price_conditions,
kind_cd: @params[:kind_cd],
brand_id: [*@params[:brand_ids]],
category_ids: categories_for_search,
property_value_ids: [*@params[:property_value_ids]],
}
}
end

def query_string
@params[:query].present? ? Riddle.escape(@params[:query] + '*') : ''
end

def price_type_for_contractor
@price_type_for_contractor ||= PriceType.for(@params[:contractor])
end

def price_type_for_search
@price_type_for_search ||= "price_type_#{price_type_for_contractor.id}".to_sym
end

def min_price_conditions
@min_price_conditions ||= @params[:min_price_limit].try(:to_f) || Price.where(price_type: price_type_for_contractor).minimum(:value)
end

def max_price_conditions
@max_price_conditions ||= @params[:max_price_limit].try(:to_f) || Price.where(price_type: price_type_for_contractor).maximum(:value)
end

def ordering
(@params[:order_by] == 'new') ? :created_at : price_type_for_search
end

def categories_for_search
@params[:category_ids] || @params[:category_id] || []
end

def process_categories
category_ids = if @params[:available_category_ids]
Category.find(@params[:available_category_ids])
elsif @params[:category_id]
@category = Category.find(@params[:category_id])
@category.children.ids
else
@product_facets[:direct_category_id].keys # errors here, see @product_facets definition
end

@categories = Category.where(id: category_ids)
end

我得到了错误

 Failure/Error: subject { get :index }
ThinkingSphinx::SyntaxError:
sphinxql: syntax error, unexpected AND, expecting CONST_INT or CONST_FLOAT or '-' near 'AND AND `kind_cd` = 0 AND `sphinx_deleted` = 0 GROUP BY `sphinx_internal_class` ORDER BY `price_type_3` ASC LIMIT 0, 1000 OPTION ranker=bm25, max_matches=1000; SHOW META; SELECT *, @groupby, @count FROM `product_core` WHERE `price_type_3` BETWEEN AND AND `kind_cd` = 0 AND `sphinx_deleted` = 0 GROUP BY `direct_category_id` ORDER BY `price_type_3` ASC LIMIT 0, 1000 OPTION ranker=bm25, max_matches=1000; SHOW META; SELEC
./app/services/query/products.rb:76:in `process_categories'

在开发模式下我没有收到错误。为什么我有这个麻烦?

最佳答案

我找到了我的麻烦!

错误发生在事务性固定装置 上。禁用它们解决了我的问题。

这里有更详细的:How to test ThinkingSphinx using RSpec

关于ruby-on-rails - 为什么 thinking-sphinx 在开发中有效,但在我的规范中却无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24504240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com