File: escaping_spec.rb

package info (click to toggle)
ruby-riddle 2.3.1-2~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,752 kB
  • sloc: sql: 25,022; php: 5,992; ruby: 4,757; sh: 59; makefile: 5
file content (51 lines) | stat: -rw-r--r-- 1,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

require 'spec_helper'

describe 'SphinxQL escaping', :live => true do
  let(:connection) { Mysql2::Client.new :host => '127.0.0.1', :port => 9306 }

  def sphinxql_matching(string)
    select = Riddle::Query::Select.new
    select.from 'people'
    select.matching string
    select.to_sql
  end

  ['@', "'", '"', '\\"', "\\'", "?"].each do |string|
    it "escapes #{string}" do
      lambda {
        connection.query sphinxql_matching(Riddle::Query.escape(string))
      }.should_not raise_error
    end
  end

  context 'on snippets' do
    def snippets_for(text, words = '', options = nil)
      snippets_query = Riddle::Query.snippets(text, 'people', words, options)
      connection.query(snippets_query).first['snippet']
    end

    it 'preserves original text with special SphinxQL escape characters' do
      text = 'email: john@example.com (yay!)'
      snippets_for(text).should == text
    end

    it 'preserves original text with special MySQL escape characters' do
      text = "'Dear' Susie\nAlways use {\\LaTeX}"
      snippets_for(text).should == text
    end

    it 'escapes match delimiters with special SphinxQL escape characters' do
      snippets = snippets_for('hello world', 'world',
        :before_match => '()|-!', :after_match => '@~"/^$')
      snippets.should == 'hello ()|-!world@~"/^$'
    end

    it 'escapes match delimiters with special MySQL escape characters' do
      snippets = snippets_for('hello world', 'world',
        :before_match => "'\"", :after_match => "\n\t\\")
      snippets.should == "hello '\"world\n\t\\"
    end
  end
end unless RUBY_PLATFORM == 'java' || Riddle.loaded_version.to_i < 2