File: query_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 (101 lines) | stat: -rw-r--r-- 3,333 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true

require 'spec_helper'

describe Riddle::Query, :live => true do
  describe '.connection' do
    let(:connection) { Riddle::Query.connection 'localhost', 9306 }

    it "returns a MySQL Client" do
      connection.should be_a(Mysql2::Client)
    end

    it "should handle search requests" do
      connection.query(Riddle::Query.tables).to_a.should match_array([
        {'Index' => 'people', 'Type' => 'local'},
        {'Index' => 'article_core', 'Type' => 'local'},
        {'Index' => 'article_delta', 'Type' => 'local'}
      ])
    end
  end
end unless RUBY_PLATFORM == 'java' || Riddle.loaded_version.to_i < 2

describe Riddle::Query do
  describe '.set' do
    it 'handles a single value' do
      Riddle::Query.set('foo', 'bar').should == 'SET GLOBAL foo = bar'
    end

    it 'handles multiple values' do
      Riddle::Query.set('foo', [1, 2, 3]).should == 'SET GLOBAL foo = (1, 2, 3)'
    end

    it 'handles non-global settings' do
      Riddle::Query.set('foo', 'bar', false).should == 'SET foo = bar'
    end
  end

  describe '.snippets' do
    it 'handles a basic request' do
      Riddle::Query.snippets('foo bar baz', 'foo_core', 'foo').
        should == "CALL SNIPPETS('foo bar baz', 'foo_core', 'foo')"
    end

    it 'handles a request with options' do
      Riddle::Query.snippets('foo bar baz', 'foo_core', 'foo', :around => 5).
        should == "CALL SNIPPETS('foo bar baz', 'foo_core', 'foo', 5 AS around)"
    end

    it 'handles string options' do
      Riddle::Query.snippets('foo bar baz', 'foo_core', 'foo',
        :before_match => '<strong>').should == "CALL SNIPPETS('foo bar baz', 'foo_core', 'foo', '<strong>' AS before_match)"
    end

    it "handles boolean options" do
      Riddle::Query.snippets('foo bar baz', 'foo_core', 'foo',
        :exact_phrase => true).should == "CALL SNIPPETS('foo bar baz', 'foo_core', 'foo', 1 AS exact_phrase)"
    end

    it "escapes quotes in the text data" do
      Riddle::Query.snippets("foo bar 'baz", 'foo_core', 'foo').
        should == "CALL SNIPPETS('foo bar \\'baz', 'foo_core', 'foo')"
    end

    it "escapes quotes in the query data" do
      Riddle::Query.snippets("foo bar baz", 'foo_core', "foo'").
        should == "CALL SNIPPETS('foo bar baz', 'foo_core', 'foo\\'')"
    end
  end

  describe '.create_function' do
    it 'handles a basic create request' do
      Riddle::Query.create_function('foo', :bigint, 'foo.sh').
        should == "CREATE FUNCTION foo RETURNS BIGINT SONAME 'foo.sh'"
    end
  end

  describe '.update' do
    it 'handles a basic update request' do
      Riddle::Query.update('foo_core', 5, :deleted => 1).
        should == 'UPDATE foo_core SET deleted = 1 WHERE id = 5'
    end
  end

  describe '.escape' do
    %w(( ) | - ! @ ~ / ^ $ " > < ?).each do |reserved|
      it "escapes #{reserved}" do
        Riddle::Query.escape(reserved).should == "\\#{reserved}"
      end
    end

    it "escapes word-operators correctly" do
      operators = ['MAYBE', 'NEAR', 'PARAGRAPH', 'SENTENCE', 'ZONE', 'ZONESPAN']
      operators.each do |operator|
        base = "string with #{operator} operator"
        Riddle::Query.escape(base).should == base.gsub(operator, "\\#{operator}")
      end

      Riddle::Query.escape("FIND THE ZONES").should == "FIND THE ZONES"
    end
  end
end