File: nulldb_rspec.rb

package info (click to toggle)
ruby-activerecord-nulldb-adapter 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 248 kB
  • sloc: ruby: 833; makefile: 3
file content (104 lines) | stat: -rw-r--r-- 2,404 bytes parent folder | download | duplicates (2)
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
require 'active_record/connection_adapters/nulldb_adapter'

module NullDB
  module RSpec
  end
end

module NullDB::RSpec::NullifiedDatabase
  NullDBAdapter = ActiveRecord::ConnectionAdapters::NullDBAdapter

  class HaveExecuted

    def initialize(entry_point)
      @entry_point = entry_point
    end

    def matches?(connection)
      log = connection.execution_log_since_checkpoint
      if @entry_point == :anything
        not log.empty?
      else
        log.include?(NullDBAdapter::Statement.new(@entry_point))
      end
    end

    def description
      "connection should execute #{@entry_point} statement"
    end

    def failure_message
      " did not execute #{@entry_point} statement when it should have"
    end

    def negative_failure_message
      " executed #{@entry_point} statement when it should not have"
    end
  end

  def self.globally_nullify_database
    block = lambda { |config| nullify_database(config) }
    if defined?(RSpec)
      RSpec.configure(&block)
    else
      Spec::Runner.configure(&block)
    end
  end

  def self.contextually_nullify_database(context)
    nullify_database(context)
  end

  # A matcher for asserting that database statements have (or have not) been
  # executed.  Usage:
  #
  #   ActiveRecord::Base.connection.should have_executed(:insert)
  #
  # The types of statement that can be matched mostly mirror the public
  # operations available in
  # ActiveRecord::ConnectionAdapters::DatabaseStatements:
  # - :select_one
  # - :select_all
  # - :select_value
  # - :insert
  # - :update
  # - :delete
  # - :execute
  #
  # There is also a special :anything symbol that will match any operation.
  def have_executed(entry_point)
    HaveExecuted.new(entry_point)
  end

  private

  def self.included(other)
    if nullify_contextually?(other)
      contextually_nullify_database(other)
    else
      globally_nullify_database
    end
  end

  def self.nullify_contextually?(other)
    if defined?(RSpec)
      other < RSpec::Core::ExampleGroup
    else
      other.is_a? Spec::ExampleGroup
    end
  end

  def self.nullify_database(receiver)
    receiver.before :all do
      ActiveRecord::Base.establish_connection(:adapter => :nulldb)
    end

    receiver.before :each do
      ActiveRecord::Base.connection.checkpoint!
    end

    receiver.after :all do
      ActiveRecord::Base.establish_connection(:test)
    end
  end
end