client_response_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. require File.dirname(__FILE__) + '/../init'
  2. # To execute these tests you need to execute sphinx_test.sql and configure sphinx using sphinx.conf
  3. # (both files are placed under sphinx directory)
  4. context 'The SphinxApi connected to Sphinx' do
  5. setup do
  6. @sphinx = Sphinx::Client.new
  7. end
  8. specify 'should parse response in Query method' do
  9. result = @sphinx.Query('wifi', 'test1')
  10. validate_results_wifi(result)
  11. end
  12. specify 'should process 64-bit keys in Query method' do
  13. result = @sphinx.Query('wifi', 'test2')
  14. result['total_found'].should == 3
  15. result['matches'].length.should == 3
  16. result['matches'][0]['id'].should == 4294967298
  17. result['matches'][1]['id'].should == 4294967299
  18. result['matches'][2]['id'].should == 4294967297
  19. end
  20. specify 'should parse batch-query responce in RunQueries method' do
  21. @sphinx.AddQuery('wifi', 'test1')
  22. @sphinx.AddQuery('gprs', 'test1')
  23. results = @sphinx.RunQueries
  24. results.should be_an_instance_of(Array)
  25. results.length.should == 2
  26. validate_results_wifi(results[0])
  27. end
  28. specify 'should parse response in BuildExcerpts method' do
  29. result = @sphinx.BuildExcerpts(['what the world', 'London is the capital of Great Britain'], 'test1', 'the')
  30. result.should == ['what <b>the</b> world', 'London is <b>the</b> capital of Great Britain']
  31. end
  32. specify 'should parse response in BuildKeywords method' do
  33. result = @sphinx.BuildKeywords('wifi gprs', 'test1', true)
  34. result.should == [
  35. { 'normalized' => 'wifi', 'tokenized' => 'wifi', 'hits' => 6, 'docs' => 3 },
  36. { 'normalized' => 'gprs', 'tokenized' => 'gprs', 'hits' => 1, 'docs' => 1 }
  37. ]
  38. end
  39. specify 'should parse response in UpdateAttributes method' do
  40. @sphinx.UpdateAttributes('test1', ['group_id'], { 2 => [1] }).should == 1
  41. result = @sphinx.Query('wifi', 'test1')
  42. result['matches'][0]['attrs']['group_id'].should == 1
  43. @sphinx.UpdateAttributes('test1', ['group_id'], { 2 => [2] }).should == 1
  44. result = @sphinx.Query('wifi', 'test1')
  45. result['matches'][0]['attrs']['group_id'].should == 2
  46. end
  47. specify 'should parse response in UpdateAttributes method with MVA' do
  48. @sphinx.UpdateAttributes('test1', ['tags'], { 2 => [[1, 2, 3, 4, 5, 6, 7, 8, 9]] }, true).should == 1
  49. result = @sphinx.Query('wifi', 'test1')
  50. result['matches'][0]['attrs']['tags'].should == [1, 2, 3, 4, 5, 6, 7, 8, 9]
  51. @sphinx.UpdateAttributes('test1', ['tags'], { 2 => [[5, 6, 7, 8]] }, true).should == 1
  52. result = @sphinx.Query('wifi', 'test1')
  53. result['matches'][0]['attrs']['tags'].should == [5, 6, 7, 8]
  54. end
  55. specify 'should process errors in Query method' do
  56. @sphinx.Query('wifi', 'fakeindex').should be_false
  57. @sphinx.GetLastError.length.should_not == 0
  58. end
  59. specify 'should process errors in RunQueries method' do
  60. @sphinx.AddQuery('wifi', 'fakeindex')
  61. r = @sphinx.RunQueries
  62. r[0]['error'].length.should_not == 0
  63. end
  64. def validate_results_wifi(result)
  65. result['total_found'].should == 3
  66. result['matches'].length.should == 3
  67. result['time'].should_not be_nil
  68. result['attrs'].should == {
  69. 'group_id' => Sphinx::Client::SPH_ATTR_INTEGER,
  70. 'created_at' => Sphinx::Client::SPH_ATTR_TIMESTAMP,
  71. 'rating' => Sphinx::Client::SPH_ATTR_FLOAT,
  72. 'tags' => Sphinx::Client::SPH_ATTR_MULTI | Sphinx::Client::SPH_ATTR_INTEGER
  73. }
  74. result['fields'].should == [ 'name', 'description' ]
  75. result['total'].should == 3
  76. result['matches'].should be_an_instance_of(Array)
  77. result['matches'][0]['id'].should == 2
  78. result['matches'][0]['weight'].should == 2
  79. result['matches'][0]['attrs']['group_id'].should == 2
  80. result['matches'][0]['attrs']['created_at'].should == 1175658555
  81. result['matches'][0]['attrs']['tags'].should == [5, 6, 7, 8]
  82. ('%0.2f' % result['matches'][0]['attrs']['rating']).should == '54.85'
  83. result['matches'][1]['id'].should == 3
  84. result['matches'][1]['weight'].should == 2
  85. result['matches'][1]['attrs']['group_id'].should == 1
  86. result['matches'][1]['attrs']['created_at'].should == 1175658647
  87. result['matches'][1]['attrs']['tags'].should == [1, 7, 9, 10]
  88. ('%0.2f' % result['matches'][1]['attrs']['rating']).should == '16.25'
  89. result['matches'][2]['id'].should == 1
  90. result['matches'][2]['weight'].should == 1
  91. result['matches'][2]['attrs']['group_id'].should == 1
  92. result['matches'][2]['attrs']['created_at'].should == 1175658490
  93. result['matches'][2]['attrs']['tags'].should == [1, 2, 3, 4]
  94. ('%0.2f' % result['matches'][2]['attrs']['rating']).should == '13.32'
  95. result['words'].should == { 'wifi' => { 'hits' => 6, 'docs' => 3 } }
  96. end
  97. end