File: caller_logging_spec.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (52 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download | duplicates (3)
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
require_relative "spec_helper"
require 'logger'

describe "caller_logging extension" do
  before do
    @db = Sequel.mock(:extensions=>[:caller_logging])
    @log_stream = StringIO.new
    @db.loggers << Logger.new(@log_stream)
    @ds = @db[:items]
  end

  exec_sql_line = __LINE__ + 2
  def exec_sql(sql)
    @db[sql].all
  end

  it "should log caller information, skipping internal Sequel code" do
    exec_sql("SELECT * FROM items")
    @log_stream.rewind
    lines = @log_stream.read.split("\n")
    lines.length.must_equal 1
    lines[0].must_match(/ \(source: #{__FILE__}:#{exec_sql_line}:in `exec_sql'\) SELECT \* FROM items\z/)
  end

  it "should allow formatting of caller information" do
    @db.caller_logging_formatter = lambda{|line| line.sub(/\A.+(caller_logging_spec\.rb:\d+).+\z/, '\1:')}
    exec_sql("SELECT * FROM items")
    @log_stream.rewind
    lines = @log_stream.read.split("\n")
    lines.length.must_equal 1
    lines[0].must_match(/ caller_logging_spec\.rb:#{exec_sql_line}: SELECT \* FROM items\z/)
  end

  it "should allow ignoring additional caller lines in application" do
    @db.caller_logging_ignore = /exec_sql/
    exec_sql("SELECT * FROM items"); line = __LINE__
    @log_stream.rewind
    lines = @log_stream.read.split("\n")
    lines.length.must_equal 1
    lines[0].must_match(/ \(source: #{__FILE__}:#{line}:in `block.+\) SELECT \* FROM items\z/)
  end

  it "should not log caller information if all callers lines are filtered" do
    @db.caller_logging_ignore = /./
    exec_sql("SELECT * FROM items")
    @log_stream.rewind
    lines = @log_stream.read.split("\n")
    lines.length.must_equal 1
    lines[0].must_match(/ SELECT \* FROM items\z/)
    lines[0].wont_match(/ source: #{__FILE__}/)
  end
end