rspec-sql 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rspec/sql.rb +67 -0
  3. metadata +74 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b432f8fb44ff4ca401c638737b32836ffe8d6e1f4af9cfe11ad7ee5c6131c557
4
+ data.tar.gz: '090f57ea9432f75110f666e57e8df787b203c53d2793c6b7ca24af05a4ed49a8'
5
+ SHA512:
6
+ metadata.gz: ff5a40e0967222a5163d273ff77ef94f2bbf58318d6189e38d063dcded3b070ac29c93fc2c4923c27b824ce6f4d686ed3eaebe893d615397c355f9ab66e3adf3
7
+ data.tar.gz: 199a52a03863bb1a699835cc4de38fa91b0a12c77d0df111ff57dcf0ac1b1bda00f62f97bf11ef9b3e70b5e6bd8d607ee659977327b97a346da10696271f6643
data/lib/rspec/sql.rb ADDED
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "rspec"
5
+
6
+ module RSpec
7
+ module Sql; end
8
+
9
+ RSpec::Matchers.define :query_database do |expected = nil|
10
+ match do |block|
11
+ @queries = scribe_queries(&block)
12
+
13
+ if expected.nil?
14
+ !@queries.empty?
15
+ elsif expected.is_a?(Integer)
16
+ @queries.size == expected
17
+ elsif expected.is_a?(Enumerator) && expected.inspect.match?(/:times>$/)
18
+ @queries.size == expected.size
19
+ elsif expected.is_a?(Array)
20
+ query_names == expected
21
+ else
22
+ raise "What are you expecting?"
23
+ end
24
+ end
25
+
26
+ failure_message do |_block|
27
+ <<~MESSAGE
28
+ Expected database queries: #{expected}
29
+ Actual database queries: #{query_names}
30
+
31
+ Diff: #{Expectations.differ.diff_as_object(query_names, expected)}
32
+
33
+ Full query log:
34
+
35
+ #{query_descriptions.join("\n")}
36
+ MESSAGE
37
+ end
38
+
39
+ failure_message_when_negated do |_block|
40
+ "Expected no database queries but observed:\n\n#{query_descriptions.join("\n")}"
41
+ end
42
+
43
+ def supports_block_expectations?
44
+ true
45
+ end
46
+
47
+ def query_names
48
+ @queries.map { |q| q[:name] || q[:sql].split.take(2).join(" ") }
49
+ end
50
+
51
+ def query_descriptions
52
+ @queries.map { |q| "#{q[:name]} #{q[:sql]}" }
53
+ end
54
+
55
+ def scribe_queries(&)
56
+ queries = []
57
+
58
+ logger = ->(_name, _started, _finished, _unique_id, payload) {
59
+ queries << payload unless %w[CACHE SCHEMA].include?(payload[:name])
60
+ }
61
+
62
+ ActiveSupport::Notifications.subscribed(logger, "sql.active_record", &)
63
+
64
+ queries
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-sql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Maikel Linke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: RSpec matcher for database queries.
42
+ email: maikel@openfoodnetwork.org.au
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/rspec/sql.rb
48
+ homepage: https://github.com/openfoodfoundation/rspec-sql
49
+ licenses:
50
+ - AGPL-3.0-or-later
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '3.1'
61
+ - - "<"
62
+ - !ruby/object:Gem::Version
63
+ version: '4'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.3.26
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: RSpec::Sql matcher
74
+ test_files: []