File: provenance_spec.rb

package info (click to toggle)
ruby-sequel 5.97.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,188 kB
  • sloc: ruby: 123,115; makefile: 3
file content (140 lines) | stat: -rw-r--r-- 4,644 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require_relative "spec_helper"

describe "provenance extension" do
  before do
    @ds = Sequel.mock.dataset.extension(:provenance)
  end

  line = __LINE__ + 1
  def ds
    ds = @ds.from(:t)
    ds = ds.select(:a)
    ds = ds.where(:c)
    ds
  end

  line1 = line+1
  line2 = line+2
  line3 = line+3

  it "should not include provenance comment if there is no comment" do
    @ds.sql.must_equal 'SELECT *'
  end

  it "should include provenance comment in select SQL" do
    ds.sql.must_match %r/
      \ASELECT\ a\ FROM\ t\ WHERE\ c\ --\ 
      \n\ --\ Dataset\ Provenance
      \n\ --\ Keys:\[:from\]\ Source:#{__FILE__}:#{line1}:in\ .+
      \n\ --\ Keys:\[:select\]\ Source:#{__FILE__}:#{line2}:in\ .+
      \n\ --\ Keys:\[:where\]\ Source:#{__FILE__}:#{line3}:in\ .+
      \n\z
    /x
  end

  it "should include provenance comment in insert SQL" do
    ds.insert_sql.must_match %r/
      \AINSERT\ INTO\ t\ DEFAULT\ VALUES\ --\ 
      \n\ --\ Dataset\ Provenance
      \n\ --\ Keys:\[:from\]\ Source:#{__FILE__}:#{line1}:in\ .+
      \n\ --\ Keys:\[:select\]\ Source:#{__FILE__}:#{line2}:in\ .+
      \n\ --\ Keys:\[:where\]\ Source:#{__FILE__}:#{line3}:in\ .+
      \n\z
    /x
  end

  it "should include provenance comment in delete SQL" do
    ds.delete_sql.must_match %r/
      \ADELETE\ FROM\ t\ WHERE\ c\ --\ 
      \n\ --\ Dataset\ Provenance
      \n\ --\ Keys:\[:from\]\ Source:#{__FILE__}:#{line1}:in\ .+
      \n\ --\ Keys:\[:select\]\ Source:#{__FILE__}:#{line2}:in\ .+
      \n\ --\ Keys:\[:where\]\ Source:#{__FILE__}:#{line3}:in\ .+
      \n\z
    /x
  end

  it "should include provenance comment in insert SQL" do
    ds.update_sql(:d=>1).must_match %r/
      \AUPDATE\ t\ SET\ d\ =\ 1\ WHERE\ c\ --\ 
      \n\ --\ Dataset\ Provenance
      \n\ --\ Keys:\[:from\]\ Source:#{__FILE__}:#{line1}:in\ .+
      \n\ --\ Keys:\[:select\]\ Source:#{__FILE__}:#{line2}:in\ .+
      \n\ --\ Keys:\[:where\]\ Source:#{__FILE__}:#{line3}:in\ .+
      \n\z
    /x
  end

  it "should include provenance comment for subqueries" do
    line4 = __LINE__+1
    ds.db.from(ds).sql.must_match %r/
      \ASELECT\ \*\ FROM\ \(SELECT\ a\ FROM\ t\ WHERE\ c\ --\ 
      \n\ --\ Dataset\ Provenance
      \n\ --\ Keys:\[:from\]\ Source:#{__FILE__}:#{line1}:in\ .+
      \n\ --\ Keys:\[:select\]\ Source:#{__FILE__}:#{line2}:in\ .+
      \n\ --\ Keys:\[:where\]\ Source:#{__FILE__}:#{line3}:in\ .+
      \n\ --\ Keys:\[:append_sql\]\ Source:#{__FILE__}:#{line4}:in\ .+
      \n\)\ AS\ t1\z
    /x
  end

  it "should handle frozen SQL strings" do
    @ds = @ds.db.dataset.with_extend{def select_sql; super.freeze; end}.extension(:provenance)
    ds.sql.must_match %r/
      \ASELECT\ a\ FROM\ t\ WHERE\ c\ --\ 
      \n\ --\ Dataset\ Provenance
      \n\ --\ Keys:\[:from\]\ Source:#{__FILE__}:#{line1}:in\ .+
      \n\ --\ Keys:\[:select\]\ Source:#{__FILE__}:#{line2}:in\ .+
      \n\ --\ Keys:\[:where\]\ Source:#{__FILE__}:#{line3}:in\ .+
      \n\z
    /x
  end

  it "should handle use with placeholder literalizers" do
    ds = self.ds

    2.times do
      line4 = __LINE__+1
      ds.first(1)
      sql = ds.db.sqls.last
      sql.must_match %r/
        \ASELECT\ a\ FROM\ t\ WHERE\ c\ LIMIT\ 1\ --\ 
        \n\ --\ Dataset\ Provenance
        \n\ --\ Keys:\[:from\]\ Source:#{__FILE__}:#{line1}:in\ .+
        \n\ --\ Keys:\[:select\]\ Source:#{__FILE__}:#{line2}:in\ .+
        \n\ --\ Keys:\[:where\]\ Source:#{__FILE__}:#{line3}:in\ .+
        \n\ --\ Keys:\[:limit\]\ Source:#{__FILE__}:#{line4}:in\ .+
        \n\z
      /x
    end

    2.times do
      line4 = __LINE__+1
      ds.first(1)
      sql = ds.db.sqls.last
      sql.must_match %r/
        \ASELECT\ a\ FROM\ t\ WHERE\ c\ LIMIT\ 1\ --\ 
        \n\ --\ Dataset\ Provenance
        \n\ --\ Keys:\[:from\]\ Source:#{__FILE__}:#{line1}:in\ .+
        \n\ --\ Keys:\[:select\]\ Source:#{__FILE__}:#{line2}:in\ .+
        \n\ --\ Keys:\[:where\]\ Source:#{__FILE__}:#{line3}:in\ .+
        \n\ --\ Keys:\[:limit\]\ Source:#{__FILE__}:#{line4}:in\ .+
        \n\ --\ Keys:\[:placeholder_literalizer\]\ Source:#{__FILE__}:#{line4}:in\ .+
        \n\z
      /x
    end
  end

  it "should respect :provenance_caller_ignore Database option" do
    ds.db.opts[:provenance_caller_ignore] = /:(#{line1}|#{line2}|#{line3}):/
    line4 = __LINE__+1
    ds.sql.must_match %r/
      \ASELECT\ a\ FROM\ t\ WHERE\ c\ --\ 
      \n\ --\ Dataset\ Provenance
      \n\ --\ Keys:\[:from\]\ Source:#{__FILE__}:#{line4}:in\ .+
      \n\ --\ Keys:\[:select\]\ Source:#{__FILE__}:#{line4}:in\ .+
      \n\ --\ Keys:\[:where\]\ Source:#{__FILE__}:#{line4}:in\ .+
      \n\z
    /x
  end
end