简体   繁体   English

Rails:将cumming_sphinx与Cucumber和Webrat进行集成测试-如何索引事务性固定装置?

[英]Rails: Integration testing thinking_sphinx with cucumber and webrat - how do I index transactional fixtures?

I'd like to have some Cucumber/webrat integration tests of my search features that use thinking_sphinx & sphinx but the problem is that the data is loaded and then rolled back in a transaction during a typical cucumber test so there is no way for thinking_sphinx to index it. 我想对使用thinking_sphinx & sphinx Sphinx的搜索功能进行一些Cucumber / Webrat集成测试,但是问题是在典型的黄瓜测试过程中,先加载数据,然后在事务中回滚,所以无法使用thinking_sphinx进行测试。索引它。 Alternatively, is there a way to turn transactions off for just a subset of tests? 或者,是否有一种方法可以仅对部分测试关闭事务?

Have you solved this problem? 你解决了这个问题吗?

[edit - please don't suggesting mocking out the search results. [编辑-请不要建议模拟搜索结果。 I want the integration test to test the integration of all the features including thinking_sphinx]. 我希望集成测试可以测试所有功能(包括think_sphinx)的集成。

We were able to get our cucumber tests to work successfully with thinking sphinx by using a database cleaner plugin and modifying our features/support/env.rb as follows: 通过使用数据库清洁程序插件并按如下所示修改我们的功能/support/env.rb,我们能够使黄瓜测试成功地与思维狮身人面像配合使用:

# Sets up the Rails environment for Cucumber
ENV["RAILS_ENV"] ||= "cucumber"

...

# http://github.com/bmabey/database_cleaner
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
Before do
  DatabaseCleaner.clean
end

ThinkingSphinx::Configuration.instance.build
ThinkingSphinx::Configuration.instance.controller.start
at_exit do
  ThinkingSphinx::Configuration.instance.controller.stop
end
ThinkingSphinx.deltas_enabled = true
ThinkingSphinx.updates_enabled = true
ThinkingSphinx.suppress_delta_output = true

# Re-generate the index before each Scenario
Before do
  ThinkingSphinx::Configuration.instance.controller.index
end

As you can see, we also created a 'cucumber' environment and a separate database (in order to run cucumber and specs simultaneously without conflicts) - so you will need to add a 'cucumber:' entry to your database.yml and sphinx.yml if you would like to do that as well. 如您所见,我们还创建了一个“黄瓜”环境和一个单独的数据库(以便同时运行黄瓜和规格,而不会发生冲突)-因此您将需要在数据库.yml和狮身人面像中添加一个“黄瓜:”条目。 yml,如果您也想这样做。

I see the problem being in this statement: 我看到问题出在以下语句中:

the data is loaded and then rolled back in a transaction during a typical cucumber test so there is no way for thinking_sphinx to index it 在典型的黄瓜测试期间,数据已加载并在事务中回滚,因此没有办法使think_sphinx对其进行索引

It may not be fast to have thinking_sphinx index the results, but it's certainly possible within the transaction. 让think_sphinx索引结果可能不是很快 ,但是在事务中肯定是可能的。 Since it's a single integration test, and not done for each of your (many) unit tests, I'd take the speed hit. 由于这是一次集成测试,并且没有对您的每个(许多)单元测试都进行过测试,所以我考虑了速度。

So now you need to figure out how to trigger that re-index during a transaction. 因此,现在您需要弄清楚如何在事务处理期间触发该重新索引。

# somewhere in /features/support:
before('@reindexing') do
  require 'Rake'

  class MyModel
    after_save :force_reindex!

    def force_reindex!
      # in case multiple runs of this block cause the hook
      # to get added multiple times, let's check to make sure
      # we haven't already reindexed for this save
      return if @__reindexed
      Rake["thinking_sphinx:rebuild"].execute
      @__reindexed = true
    end
  end
end

after('@reindexing') do
  class MyModel
    def force_reindex!
      # do nothing; the hook still gets called, but that's ok
    end
  end
end

In /features/integration.feature (or whatever), you'd have /features/integration.feature (或其他任何内容)中,

@reindexing
Feature: The whole shebang, altogether

  Scenario: ...

The linked suggestion won't work, because the Rake task invokes the indexer in a seperate process, and so is outside of the test transaction. 链接的建议不起作用,因为Rake任务在单独的过程中调用索引器,因此在测试事务之外也是如此。

I don't know that there's any way around this, other than turning off test transactions so that the sphinx index process can see new and updated records. 除了关闭测试事务以使狮身人面像索引过程可以查看新记录和更新记录外,我不知道有什么方法可以解决此问题。 To do that, in your TestCase , just add the line 为此,在您的TestCase ,只需添加以下行

self.use_transactional_fixtures = false

Bear in mind that you'll need to manage clearing up any data at the end of your test. 请记住,您需要在测试结束时管理清除所有数据。

另一个使用标签来分隔黄瓜思维狮身人面像测试的示例, 此处使用了黄瓜标签。

I would not recommend you test a component that you don't own. 我不建议您测试不属于您的组件。 It will be a pretty brittle test since it would be dependent on the specific ranking algorithm in that specific version of sphinx that could change later and thus return unpredictable results in the future. 这将是一个相当脆弱的测试,因为它将取决于特定版本的狮身人面像中的特定排名算法,该算法稍后可能会更改,因此将来会返回不可预测的结果。

if you want to do this I would however suggest running a separate index for testing either using the test database via the mysql adapter or the xmlpipe2 option (xml2pipe obviously lets you change your data set without having to change the indexing options in your sphinx file). 如果要执行此操作,我建议您通过mysql适配器或xmlpipe2选项使用测试数据库运行单独的索引进行测试(显然,xml2pipe允许您更改数据集,而无需更改sphinx文件中的索引选项) 。 This will probably require that you set up your test data separately all at once, delete the indexes (using ruby shell commands) and then force the reindexing (waiting for the total number of indexed documents to be the same as the known amount of database records) and run your tests against the data. 这可能需要您一次单独设置测试数据,删除索引(使用ruby shell命令),然后强制重新索引(等待索引文档的总数与已知数据库记录的数量相同) ),然后对数据进行测试。

I would really caution against testing the index like I said you might just find yourself having to deal with a constantly breaking test or slow test runtime (depending on the amount of data you are indexing) 我真的要避免对索引进行测试,就像我说过的那样,您可能会发现自己不得不面对不断中断的测试或缓慢的测试运行时间(取决于要建立索引的数据量)

这可能比它的价值更麻烦,但是您是否尝试过打开增量索引?

I tried the suggested solution but sphinx times out with the following error. 我尝试了建议的解决方案,但狮身人面像超时并出现以下错误。 I don't think it is possible to re-index sphinx while transactional fixtures are enabled. 我认为启用交易固定装置后无法重新索引狮身人面像。

ERROR: index 'document_core': sql_query_pre[0]: Lock wait timeout exceeded; 错误:索引'document_core':sql_query_pre [0]:超出了锁定等待超时; try restarting transaction 尝试重新启动事务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM