<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Cucumber scenarios that depend on Sphinx Changes</title>
  <id>tag:opensoul.org,2009:/2009/6/1/cucumber-scenarios-that-depend-on-sphinx/changes</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  <link href="http://opensoul.org/2009/6/1/cucumber-scenarios-that-depend-on-sphinx/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2009/6/1/cucumber-scenarios-that-depend-on-sphinx" rel="alternate" type="text/html"/>
  <updated>2009-06-25T13:15:03Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2009-06-25:542:5</id>
    <published>2009-06-01T13:18:00Z</published>
    <updated>2009-06-25T13:15:03Z</updated>
    <link href="http://opensoul.org/2009/6/1/cucumber-scenarios-that-depend-on-sphinx" rel="alternate" type="text/html"/>
    <title>Cucumber scenarios that depend on Sphinx</title>
<content type="html">&lt;p&gt;I love writing apps that make heavy use of search indexes, but testing them can be a bit of a pain.  Here is how I got &lt;a href=&quot;http://freelancing-god.github.com/ts/en/&quot;&gt;ThinkingSphinx&lt;/a&gt; to play nice with &lt;a href=&quot;http://cukes.info&quot;&gt;Cucumber&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Here is the relevant part of what I put in &lt;code&gt;features/support/env.rb&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;# Cucumber::Rails.use_transactional_fixtures

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

ts = ThinkingSphinx::Configuration.instance
ts.build
FileUtils.mkdir_p ts.searchd_file_path
ts.controller.index
ts.controller.start
at_exit do
  ts.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
  ts.controller.index
end&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;What&#8217;s going on here?&lt;/h3&gt;


	&lt;p&gt;Start by commenting out the line about using transactional fixtures in &lt;code&gt;env.rb&lt;/code&gt;. Using transactional fixtures will run each scenario inside of a transaction and roll it back at the end of the scenario to revert the database state. Thinking Sphinx uses an &lt;code&gt;after_commit&lt;/code&gt; callback for kicking off the delta indexing, but the callback never gets run when transactional fixtures are enabled because the entire scenario is run inside of a big transaction.&lt;/p&gt;


	&lt;p&gt;Once we&#8217;ve disabled transactional fixtures, our test database will start to fill up, likely causing some problems. So we need to add a &lt;code&gt;Before&lt;/code&gt; block that clears out the database before each scenario. I&#8217;m using &lt;a href=&quot;http://github.com/bmabey/database_cleaner/tree/master&quot;&gt;database_cleaner&lt;/a&gt;, which gives you some different strategies for cleaning the database. Alternatively, the brute-force solution is just to reload the schema before each scenario, but this is slower than truncating the data.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Before do
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
  ActiveRecord::Schema.verbose = false
  load &amp;quot;#{RAILS_ROOT}/db/schema.rb&amp;quot;
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Next, we start Sphinx when &lt;code&gt;env.rb&lt;/code&gt; is loaded, and shut it down when the Ruby process exists.  We also enable deltas and updates, which are disabled by default in test mode.  Finally, we define a &lt;code&gt;Before&lt;/code&gt; block that updates the index before each scenario so we don&#8217;t end up with a stale index.&lt;/p&gt;


	&lt;h3&gt;Putting it all together&lt;/h3&gt;


	&lt;p&gt;I&#8217;m using Sphinx&#8217;s &lt;a href=&quot;http://opensoul.org/2009/4/30/keepin-sphinx-indexes-fresh-2&quot;&gt;delayed delta support&lt;/a&gt;, so whenever I update records, I need to have delayed_job process jobs. Instead of trying to get delayed_job to run in the background, I took the easy way out and defined a step: &#8220;When the system processes jobs&#8221;.&lt;/p&gt;


&lt;pre&gt;
Scenario: Posting a new listing
  Given I am logged in as &quot;MovinMan&quot; 
  When I create a new listing titled &quot;Lots of Boxes&quot; near &quot;49423&quot; 
  And the system processes jobs
  And I browse listings near &quot;49423&quot; 
  Then I can see a listing titled &quot;Lots of Boxes&quot; 
&lt;/pre&gt;

	&lt;p&gt;Which is just implemented as:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;When 'the system processes jobs' do
  Delayed::Job.work_off
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If you&#8217;re just using the default deltas, and not delayed deltas, then you can update the index like this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;When /^the system updates the index$/ do
  MyModel.sphinx_indexes.first.delta_object.index(MyModel)
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I hope that helps.  Post your suggestions in the comments for improving this.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2009-06-13:540:4</id>
    <published>2009-06-01T13:18:00Z</published>
    <updated>2009-06-13T02:03:55Z</updated>
    <link href="http://opensoul.org/2009/6/1/cucumber-scenarios-that-depend-on-sphinx" rel="alternate" type="text/html"/>
    <title>Cucumber scenarios that depend on Sphinx</title>
<content type="html">&lt;p&gt;I love writing apps that make heavy use of search indexes, but testing them can be a bit of a pain.  Here is how I got &lt;a href=&quot;http://freelancing-god.github.com/ts/en/&quot;&gt;ThinkingSphinx&lt;/a&gt; to play nice with &lt;a href=&quot;http://cukes.info&quot;&gt;Cucumber&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Here is the relevant part of what I put in &lt;code&gt;features/support/env.rb&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;# Cucumber::Rails.use_transactional_fixtures

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

# Run sphinx during the tests
ts = ThinkingSphinx::Configuration.instance
ts.build
FileUtils.mkdir_p ts.searchd_file_path
ts.controller.index
ts.controller.start
at_exit do
  ts.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
  ts.controller.index
end&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;What&#8217;s going on here?&lt;/h3&gt;


	&lt;p&gt;Start by commenting out the line about using transactional fixtures in &lt;code&gt;env.rb&lt;/code&gt;. Using transactional fixtures will run each scenario inside of a transaction and roll it back at the end of the scenario to revert the database state. Thinking Sphinx uses an &lt;code&gt;after_commit&lt;/code&gt; callback for kicking off the delta indexing, but the callback never gets run when transactional fixtures are enabled because the entire scenario is run inside of a big transaction.&lt;/p&gt;


	&lt;p&gt;Once we&#8217;ve disabled transactional fixtures, our test database will start to fill up, likely causing some problems. So we need to add a &lt;code&gt;Before&lt;/code&gt; block that clears out the database before each scenario. I&#8217;m using &lt;a href=&quot;http://github.com/bmabey/database_cleaner/tree/master&quot;&gt;database_cleaner&lt;/a&gt;, which gives you some different strategies for cleaning the database. Alternatively, the brute-force solution is just to reload the schema before each scenario, but this is slower than truncating the data.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Before do
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
  ActiveRecord::Schema.verbose = false
  load &amp;quot;#{RAILS_ROOT}/db/schema.rb&amp;quot;
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Next, we start Sphinx when &lt;code&gt;env.rb&lt;/code&gt; is loaded, and shut it down when the Ruby process exists.  We also enable deltas and updates, which are disabled by default in test mode.  Finally, we define a &lt;code&gt;Before&lt;/code&gt; block that updates the index before each scenario so we don&#8217;t end up with a stale index.&lt;/p&gt;


	&lt;h3&gt;Putting it all together&lt;/h3&gt;


	&lt;p&gt;I&#8217;m using Sphinx&#8217;s &lt;a href=&quot;http://opensoul.org/2009/4/30/keepin-sphinx-indexes-fresh-2&quot;&gt;delayed delta support&lt;/a&gt;, so whenever I update records, I need to have delayed_job process jobs. Instead of trying to get delayed_job to run in the background, I took the easy way out and defined a step: &#8220;When the system processes jobs&#8221;.&lt;/p&gt;


&lt;pre&gt;
Scenario: Posting a new listing
  Given I am logged in as &quot;MovinMan&quot; 
  When I create a new listing titled &quot;Lots of Boxes&quot; near &quot;49423&quot; 
  And the system processes jobs
  And I browse listings near &quot;49423&quot; 
  Then I can see a listing titled &quot;Lots of Boxes&quot; 
&lt;/pre&gt;

	&lt;p&gt;Which is just implemented as:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;When 'the system processes jobs' do
  Delayed::Job.work_off
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If you&#8217;re just using the default deltas, and not delayed deltas, then you can update the index like this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;When /^the system updates the index$/ do
  MyModel.sphinx_indexes.first.delta_object.delayed_index(MyModel)
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I hope that helps.  Post your suggestions in the comments for improving this.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2009-06-05:539:3</id>
    <published>2009-06-01T13:18:00Z</published>
    <updated>2009-06-05T13:17:00Z</updated>
    <link href="http://opensoul.org/2009/6/1/cucumber-scenarios-that-depend-on-sphinx" rel="alternate" type="text/html"/>
    <title>Cucumber scenarios that depend on Sphinx</title>
<content type="html">&lt;p&gt;I love writing apps that make heavy use of search indexes, but testing them can be a bit of a pain.  Here is how I got &lt;a href=&quot;http://freelancing-god.github.com/ts/en/&quot;&gt;ThinkingSphinx&lt;/a&gt; to play nice with &lt;a href=&quot;http://cukes.info&quot;&gt;Cucumber&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Here is the relevant part of what I put in &lt;code&gt;features/support/env.rb&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;# Cucumber::Rails.use_transactional_fixtures

# 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&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;What&#8217;s going on here?&lt;/h3&gt;


	&lt;p&gt;Start by commenting out the line about using transactional fixtures in &lt;code&gt;env.rb&lt;/code&gt;. Using transactional fixtures will run each scenario inside of a transaction and roll it back at the end of the scenario to revert the database state. Thinking Sphinx uses an &lt;code&gt;after_commit&lt;/code&gt; callback for kicking off the delta indexing, but the callback never gets run when transactional fixtures are enabled because the entire scenario is run inside of a big transaction.&lt;/p&gt;


	&lt;p&gt;Once we&#8217;ve disabled transactional fixtures, our test database will start to fill up, likely causing some problems. So we need to add a &lt;code&gt;Before&lt;/code&gt; block that clears out the database before each scenario. I&#8217;m using &lt;a href=&quot;http://github.com/bmabey/database_cleaner/tree/master&quot;&gt;database_cleaner&lt;/a&gt;, which gives you some different strategies for cleaning the database. Alternatively, the brute-force solution is just to reload the schema before each scenario, but this is slower than truncating the data.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Before do
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
  ActiveRecord::Schema.verbose = false
  load &amp;quot;#{RAILS_ROOT}/db/schema.rb&amp;quot;
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Next, we start Sphinx when &lt;code&gt;env.rb&lt;/code&gt; is loaded, and shut it down when the Ruby process exists.  We also enable deltas and updates, which are disabled by default in test mode.  Finally, we define a &lt;code&gt;Before&lt;/code&gt; block that updates the index before each scenario so we don&#8217;t end up with a stale index.&lt;/p&gt;


	&lt;h3&gt;Putting it all together&lt;/h3&gt;


	&lt;p&gt;I&#8217;m using Sphinx&#8217;s &lt;a href=&quot;http://opensoul.org/2009/4/30/keepin-sphinx-indexes-fresh-2&quot;&gt;delayed delta support&lt;/a&gt;, so whenever I update records, I need to have delayed_job process jobs. Instead of trying to get delayed_job to run in the background, I took the easy way out and defined a step: &#8220;When the system processes jobs&#8221;.&lt;/p&gt;


&lt;pre&gt;
Scenario: Posting a new listing
  Given I am logged in as &quot;MovinMan&quot; 
  When I create a new listing titled &quot;Lots of Boxes&quot; near &quot;49423&quot; 
  And the system processes jobs
  And I browse listings near &quot;49423&quot; 
  Then I can see a listing titled &quot;Lots of Boxes&quot; 
&lt;/pre&gt;

	&lt;p&gt;Which is just implemented as:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;When 'the system processes jobs' do
  Delayed::Job.work_off
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If you&#8217;re just using the default deltas, and not delayed deltas, then you can update the index like this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;When /^the system updates the index$/ do
  MyModel.sphinx_indexes.first.delta_object.delayed_index(MyModel)
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I hope that helps.  Post your suggestions in the comments for improving this.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2009-06-01:538:2</id>
    <published>2009-06-01T13:18:00Z</published>
    <updated>2009-06-01T13:19:31Z</updated>
    <link href="http://opensoul.org/2009/6/1/cucumber-scenarios-that-depend-on-sphinx" rel="alternate" type="text/html"/>
    <title>Cucumber scenarios that depend on Sphinx</title>
<content type="html">&lt;p&gt;I love writing apps that make heavy use of search indexes, but testing them can be a bit of a pain.  Here is how I got &lt;a href=&quot;http://freelancing-god.github.com/ts/en/&quot;&gt;ThinkingSphinx&lt;/a&gt; to play nice with &lt;a href=&quot;http://cukes.info&quot;&gt;Cucumber&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Here is the relevant part of what I put in &lt;code&gt;features/support/env.rb&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;# Cucumber::Rails.use_transactional_fixtures

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

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&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;What&#8217;s going on here?&lt;/h3&gt;


	&lt;p&gt;Start by commenting out the line about using transactional fixtures in &lt;code&gt;env.rb&lt;/code&gt;. Using transactional fixtures will run each scenario inside of a transaction and roll it back at the end of the scenario to revert the database state. Thinking Sphinx uses an &lt;code&gt;after_commit&lt;/code&gt; callback for kicking off the delta indexing, but the callback never gets run when transactional fixtures are enabled because the entire scenario is run inside of a big transaction.&lt;/p&gt;


	&lt;p&gt;Once we&#8217;ve disabled transactional fixtures, our test database will start to fill up, likely causing some problems. So we need to add a &lt;code&gt;Before&lt;/code&gt; block that clears out the database before each scenario. I&#8217;m using &lt;a href=&quot;github.com/bmabey/database_cleaner/tree/master&quot;&gt;database_cleaner&lt;/a&gt;, which gives you some different strategies for cleaning the database. Alternatively, the brute-force solution is just to reload the schema before each scenario, but this is slower than truncating the data.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Before do
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
  ActiveRecord::Schema.verbose = false
  load &amp;quot;#{RAILS_ROOT}/db/schema.rb&amp;quot;
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Next, we start Sphinx when &lt;code&gt;env.rb&lt;/code&gt; is loaded, and shut it down when the Ruby process exists.  We also enable deltas and updates, which are disabled by default in test mode.  Finally, we define a &lt;code&gt;Before&lt;/code&gt; block that updates the index before each scenario so we don&#8217;t end up with a stale index.&lt;/p&gt;


	&lt;h3&gt;Putting it all together&lt;/h3&gt;


	&lt;p&gt;I&#8217;m using Sphinx&#8217;s &lt;a href=&quot;opensoul.org/2009/4/30/keepin-sphinx-indexes-fresh-2&quot;&gt;delayed delta support&lt;/a&gt;, so whenever I update records, I need to have delayed_job process jobs. Instead of trying to get delayed_job to run in the background, I took the easy way out and defined a step: &#8220;When the system processes jobs&#8221;.&lt;/p&gt;


&lt;pre&gt;
Scenario: Posting a new listing
  Given I am logged in as &quot;MovinMan&quot; 
  When I create a new listing titled &quot;Lots of Boxes&quot; near &quot;49423&quot; 
  And the system processes jobs
  And I browse listings near &quot;49423&quot; 
  Then I can see a listing titled &quot;Lots of Boxes&quot; 
&lt;/pre&gt;

	&lt;p&gt;Which is just implemented as:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;When 'the system processes jobs' do
  Delayed::Job.work_off
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If you&#8217;re just using the default deltas, and not delayed deltas, then you can update the index like this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;When /^the system updates the index$/ do
  MyModel.sphinx_indexes.first.delta_object.delayed_index(MyModel)
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I hope that helps.  Post your suggestions in the comments for improving this.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2009-06-01:537:1</id>
    <updated>2009-06-01T12:07:05Z</updated>
    <link href="http://opensoul.org/2009/9/29/cucumber-scenarios-that-depend-on-sphinx" rel="alternate" type="text/html"/>
    <title>Cucumber scenarios that depend on sphinx</title>
<content type="html">&lt;p&gt;In which I show how to run Sphinx in Cucumber scenarios&lt;/p&gt;


	&lt;p&gt;Running the sphinx daemon while cucumber runs is pretty simple, just throw this in &lt;code&gt;features/support/env.rb&lt;/code&gt;:&lt;/p&gt;



# Run sphinx during the tests
ThinkingSphinx::Configuration.instance.controller.start
at_exit do
  ThinkingSphinx::Configuration.instance.controller.stop
end
# Tell sphinx to quiet down
ThinkingSphinx.suppress_delta_output = true


	&lt;h3&gt;Disable transactional fixtures&lt;/h3&gt;


	&lt;p&gt;Cucumber::Rails.use_transactional_fixtures&lt;/p&gt;


	&lt;p&gt;Thinking Sphinx uses an &lt;code&gt;after_commit&lt;/code&gt; callback for updating the index, which&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;Can&#8217;t use transactional fixtures because Sphinx doesn&#8217;t update the index&lt;/li&gt;
		&lt;li&gt;until after commit
require &#8216;database_cleaner&#8217;
DatabaseCleaner.strategy = :truncation
Before do
  DatabaseCleaner.clean
end&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Scenario: Posting a new listing
  Given I am logged in as &#8220;TreeHuggr&#8221; 
  When I create a new listing titled &#8220;Lots of Boxes&#8221; near &#8220;49423&#8221; 
  And the system processes jobs
  And I browse listings near &#8220;49423&#8221; 
  Then I can see a listing titled &#8220;Lots of Boxes&#8221;&lt;/p&gt;


	&lt;p&gt;When &#8216;the system processes jobs&#8217; do
  Delayed::Job.work_off
end&lt;/p&gt;</content>  </entry>
</feed>
