Skip to content

Commit

Permalink
Refactored specs so live sphinx index is not needed during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kpumuk committed Mar 14, 2013
1 parent 9e179a2 commit c382123
Show file tree
Hide file tree
Showing 154 changed files with 237 additions and 93 deletions.
7 changes: 5 additions & 2 deletions .gitignore
@@ -1,4 +1,7 @@
rdoc
doc
.yardoc
.DS_Store
.bundle
.rvmrc
Gemfile.lock
doc
pkg
25 changes: 25 additions & 0 deletions Rakefile
Expand Up @@ -19,3 +19,28 @@ end

require 'bundler'
Bundler::GemHelper.install_tasks

namespace :fixtures do
desc 'Update textures for sphinx requests'
task :requests do
Dir["#{File.dirname(__FILE__)}/spec/fixtures/requests/php/*.php"].each do |file|
puts name = File.basename(file, '.php')
File.open(File.join(File.dirname(file), '..', "#{name}.dat"), 'w') do |f|
f.write `env SPHINX_MOCK_REQUEST=1 php "#{file}"`
end
end
end

desc 'Update textures for sphinx responses'
task :responses do
Dir["#{File.dirname(__FILE__)}/spec/fixtures/responses/php/*.php"].each do |file|
puts name = File.basename(file, '.php')
File.open(File.join(File.dirname(file), '..', "#{name}.dat"), 'w') do |f|
f.write `env SPHINX_MOCK_RESPONSE=1 php "#{file}"`
end
end
end
end

desc 'Update binary fixtures'
task :fixtures => %w[ fixtures:requests fixtures:responses]
54 changes: 31 additions & 23 deletions spec/client_response_spec.rb
Expand Up @@ -6,14 +6,23 @@
before :each do
@sphinx = Sphinx::Client.new
end


def mock_sphinx_response(fixture)
response = sphinx_fixture(fixture, :response)
@sock = SphinxFakeSocket.new(response, 'rb')
servers = @sphinx.instance_variable_get(:@servers)
servers.first.stub(:get_socket => @sock, :free_socket => nil)
end

context 'in Query method' do
it 'should parse response' do
mock_sphinx_response('query')
result = @sphinx.Query('wifi', 'test1')
validate_results_wifi(result)
end

it 'should process 64-bit keys' do
mock_sphinx_response('query_id64')
result = @sphinx.Query('wifi', 'test2')
result['total_found'].should == 3
result['matches'].length.should == 3
Expand All @@ -23,13 +32,15 @@
end

it 'should process errors in Query method' do
mock_sphinx_response('query_error')
@sphinx.Query('wifi', 'fakeindex').should be_false
@sphinx.GetLastError.should_not be_empty
@sphinx.GetLastError.should =~ /unknown local index/
end
end

context 'in RunQueries method' do
it 'should parse batch-query responce' do
mock_sphinx_response('run_queries')
@sphinx.AddQuery('wifi', 'test1')
@sphinx.AddQuery('gprs', 'test1')
results = @sphinx.RunQueries
Expand All @@ -39,6 +50,7 @@
end

it 'should process errors in RunQueries method' do
mock_sphinx_response('run_queries_error')
@sphinx.AddQuery('wifi', 'fakeindex')
r = @sphinx.RunQueries
r[0]['error'].should_not be_empty
Expand All @@ -47,13 +59,15 @@

context 'in BuildExcerpts method' do
it 'should parse response' do
mock_sphinx_response('build_excerpts')
result = @sphinx.BuildExcerpts(['what the world', 'London is the capital of Great Britain'], 'test1', 'the')
result.should == ['what <b>the</b> world', 'London is <b>the</b> capital of Great Britain']
end
end

context 'in BuildKeywords method' do
it 'should parse response' do
mock_sphinx_response('build_keywords')
result = @sphinx.BuildKeywords('wifi gprs', 'test1', true)
result.should == [
{ 'normalized' => 'wifi', 'tokenized' => 'wifi', 'hits' => 6, 'docs' => 3 },
Expand All @@ -64,21 +78,13 @@

context 'in UpdateAttributes method' do
it 'should parse response' do
mock_sphinx_response('update_attributes')
@sphinx.UpdateAttributes('test1', ['group_id'], { 2 => [1] }).should == 1
result = @sphinx.Query('wifi', 'test1')
result['matches'][0]['attrs']['group_id'].should == 1
@sphinx.UpdateAttributes('test1', ['group_id'], { 2 => [2] }).should == 1
result = @sphinx.Query('wifi', 'test1')
result['matches'][0]['attrs']['group_id'].should == 2
end

it 'should parse response with MVA' do
mock_sphinx_response('update_attributes_mva')
@sphinx.UpdateAttributes('test1', ['tags'], { 2 => [[1, 2, 3, 4, 5, 6, 7, 8, 9]] }, true).should == 1
result = @sphinx.Query('wifi', 'test1')
result['matches'][0]['attrs']['tags'].should == [1, 2, 3, 4, 5, 6, 7, 8, 9]
@sphinx.UpdateAttributes('test1', ['tags'], { 2 => [[5, 6, 7, 8]] }, true).should == 1
result = @sphinx.Query('wifi', 'test1')
result['matches'][0]['attrs']['tags'].should == [5, 6, 7, 8]
end
end

Expand All @@ -90,30 +96,30 @@
socket.should be_kind_of(Sphinx::BufferedIO)
socket.close
end

it 'should produce an error when opened twice' do
@sphinx.Open.should be_true
@sphinx.Open.should be_false
@sphinx.GetLastError.should == 'already connected'

socket = @sphinx.servers.first.instance_variable_get(:@socket)
socket.should be_kind_of(Sphinx::BufferedIO)
socket.close
end
end

context 'in Close method' do
it 'should open socket' do
@sphinx.Open.should be_true
@sphinx.Close.should be_true
@sphinx.servers.first.instance_variable_get(:@socket).should be_nil
end

it 'should produce socket is closed' do
@sphinx.Close.should be_false
@sphinx.GetLastError.should == 'not connected'
@sphinx.servers.first.instance_variable_get(:@socket).should be_nil

@sphinx.Open.should be_true
@sphinx.Close.should be_true
@sphinx.Close.should be_false
Expand All @@ -124,16 +130,18 @@

context 'in Status method' do
it 'should parse response' do
mock_sphinx_response('status')
response = @sphinx.Status
response.should be_an(Array)
response.size.should be > 10
end
end

context 'in FlushAttrs method' do
context 'in FlushAttributes method' do
it 'should not raise an error' do
mock_sphinx_response('flush_attributes')
expect {
@sphinx.FlushAttrs
@sphinx.FlushAttributes
}.to_not raise_error
end
end
Expand All @@ -155,21 +163,21 @@ def validate_results_wifi(result)
result['matches'][0]['id'].should == 2
result['matches'][0]['weight'].should == 2
result['matches'][0]['attrs']['group_id'].should == 2
result['matches'][0]['attrs']['created_at'].should == 1175658555
result['matches'][0]['attrs']['created_at'].should == 1175683755
result['matches'][0]['attrs']['tags'].should == [5, 6, 7, 8]
('%0.2f' % result['matches'][0]['attrs']['rating']).should == '54.85'

result['matches'][1]['id'].should == 3
result['matches'][1]['weight'].should == 2
result['matches'][1]['attrs']['group_id'].should == 1
result['matches'][1]['attrs']['created_at'].should == 1175658647
result['matches'][1]['attrs']['created_at'].should == 1175683847
result['matches'][1]['attrs']['tags'].should == [1, 7, 9, 10]
('%0.2f' % result['matches'][1]['attrs']['rating']).should == '16.25'

result['matches'][2]['id'].should == 1
result['matches'][2]['weight'].should == 1
result['matches'][2]['attrs']['group_id'].should == 1
result['matches'][2]['attrs']['created_at'].should == 1175658490
result['matches'][2]['attrs']['created_at'].should == 1175683690
result['matches'][2]['attrs']['tags'].should == [1, 2, 3, 4]
('%0.2f' % result['matches'][2]['attrs']['rating']).should == '13.32'

Expand Down
4 changes: 2 additions & 2 deletions spec/client_spec.rb
Expand Up @@ -130,10 +130,10 @@
cnt = 0
expect {
@sphinx.send(:with_socket, @server) { cnt += 1; sleep 2 }
}.to raise_error(Sphinx::SphinxResponseError, 'failed to read searchd response (msg=time\'s up!)')
}.to raise_error(Sphinx::SphinxResponseError, 'failed to read searchd response (msg=execution expired)')
cnt.should == 1

@sphinx.GetLastError.should == 'failed to read searchd response (msg=time\'s up!)'
@sphinx.GetLastError.should == 'failed to read searchd response (msg=execution expired)'
@sphinx.IsConnectError.should be_false
end

Expand Down
Binary file added spec/fixtures/requests/default_search.dat
Binary file not shown.
Binary file added spec/fixtures/requests/default_search_index.dat
Binary file not shown.
Binary file added spec/fixtures/requests/excerpt_custom.dat
Binary file not shown.
Binary file added spec/fixtures/requests/excerpt_default.dat
Binary file not shown.
Binary file added spec/fixtures/requests/excerpt_flags.dat
Binary file not shown.
Binary file added spec/fixtures/requests/field_weights.dat
Binary file not shown.
Binary file added spec/fixtures/requests/filter.dat
Binary file not shown.
Binary file added spec/fixtures/requests/filter_exclude.dat
Binary file not shown.
Binary file added spec/fixtures/requests/filter_float_range.dat
Binary file not shown.
Binary file not shown.
Binary file added spec/fixtures/requests/filter_range.dat
Binary file not shown.
Binary file added spec/fixtures/requests/filter_range_exclude.dat
Binary file not shown.
Binary file added spec/fixtures/requests/filter_range_int64.dat
Binary file not shown.
Binary file added spec/fixtures/requests/filter_ranges.dat
Binary file not shown.
Binary file added spec/fixtures/requests/filters.dat
Binary file not shown.
Binary file added spec/fixtures/requests/filters_different.dat
Binary file not shown.
Binary file added spec/fixtures/requests/geo_anchor.dat
Binary file not shown.
Binary file added spec/fixtures/requests/group_by_attr.dat
Binary file not shown.
Binary file added spec/fixtures/requests/group_by_attrpair.dat
Binary file not shown.
Binary file added spec/fixtures/requests/group_by_day.dat
Binary file not shown.
Binary file added spec/fixtures/requests/group_by_day_sort.dat
Binary file not shown.
Binary file added spec/fixtures/requests/group_by_month.dat
Binary file not shown.
Binary file added spec/fixtures/requests/group_by_week.dat
Binary file not shown.
Binary file added spec/fixtures/requests/group_by_year.dat
Binary file not shown.
Binary file added spec/fixtures/requests/group_distinct.dat
Binary file not shown.
Binary file added spec/fixtures/requests/id_range.dat
Binary file not shown.
Binary file added spec/fixtures/requests/id_range64.dat
Binary file not shown.
Binary file added spec/fixtures/requests/index_weights.dat
Binary file not shown.
Binary file added spec/fixtures/requests/keywords.dat
Binary file not shown.
Binary file added spec/fixtures/requests/limits.dat
Binary file not shown.
Binary file added spec/fixtures/requests/limits_cutoff.dat
Binary file not shown.
Binary file added spec/fixtures/requests/limits_max.dat
Binary file not shown.
Binary file added spec/fixtures/requests/limits_max_cutoff.dat
Binary file not shown.
Binary file added spec/fixtures/requests/match_all.dat
Binary file not shown.
Binary file added spec/fixtures/requests/match_any.dat
Binary file not shown.
Binary file added spec/fixtures/requests/match_boolean.dat
Binary file not shown.
Binary file added spec/fixtures/requests/match_extended.dat
Binary file not shown.
Binary file added spec/fixtures/requests/match_extended2.dat
Binary file not shown.
Binary file added spec/fixtures/requests/match_fullscan.dat
Binary file not shown.
Binary file added spec/fixtures/requests/match_phrase.dat
Binary file not shown.
Binary file added spec/fixtures/requests/max_query_time.dat
Binary file not shown.
Binary file added spec/fixtures/requests/miltiple_queries.dat
Binary file not shown.
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->Query('query');
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->Query('query', 'index');
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->BuildExcerpts(array('10', '20'), 'index', 'word1 word2', array('before_match' => 'before',
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->BuildExcerpts(array('10', '20'), 'index', 'word1 word2');
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->BuildExcerpts(array('10', '20'), 'index', 'word1 word2', array('exact_phrase' => true,
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFieldWeights(array('field1' => 10, 'field2' => 20));
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilter('attr', array(10, 20, 30));
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilter('attr', array(10, 20, 30), true);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilterFloatRange('attr', 10.5, 20.3);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilterFloatRange('attr', 10.5, 20.3, true);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilterRange('attr', 10, 20);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilterRange('attr', 10, 20, true);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilterRange('attr1', -10, 20);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilterRange('attr2', 30, 40);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilter('attr2', array(40, 50));
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetFilterRange('attr1', 10, 20, true);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetGeoAnchor('attrlat', 'attrlong', 20.3, 40.7);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetGroupBy('attr', SPH_GROUPBY_ATTR);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetGroupBy('attr', SPH_GROUPBY_ATTRPAIR);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetGroupBy('attr', SPH_GROUPBY_DAY);
Expand Down
@@ -1,6 +1,6 @@
<?php

require ("sphinxapi.php");
require ("spec/fixtures/sphinxapi.php");

$cl = new SphinxClient();
$cl->SetGroupBy('attr', SPH_GROUPBY_DAY, 'somesort');
Expand Down

0 comments on commit c382123

Please sign in to comment.