rspec-sql 0.0.0 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b432f8fb44ff4ca401c638737b32836ffe8d6e1f4af9cfe11ad7ee5c6131c557
4
- data.tar.gz: '090f57ea9432f75110f666e57e8df787b203c53d2793c6b7ca24af05a4ed49a8'
3
+ metadata.gz: 5bcabc5d90f3ad9fbdecc557c3604203ae859cf4725664a18819d1290ced63e2
4
+ data.tar.gz: '09e1ba8a94a2e92355414104384bc7de76e7aa42bdf1c2f0bc5311c29ecc66a8'
5
5
  SHA512:
6
- metadata.gz: ff5a40e0967222a5163d273ff77ef94f2bbf58318d6189e38d063dcded3b070ac29c93fc2c4923c27b824ce6f4d686ed3eaebe893d615397c355f9ab66e3adf3
7
- data.tar.gz: 199a52a03863bb1a699835cc4de38fa91b0a12c77d0df111ff57dcf0ac1b1bda00f62f97bf11ef9b3e70b5e6bd8d607ee659977327b97a346da10696271f6643
6
+ metadata.gz: ff5a4cdf9a0c27921a32ca65da0c682b6412652cf78b6d155309e6e97b7cb13702086f183d1d6ed88f2083e57fc99e6b5e4c799867b5e5a1f659363ecbda1df5
7
+ data.tar.gz: 3af870e827e52cfc8e1a548d9ff456c26521291de6bd4fb093172537f4f46ca551b84bdd2dc3306b0b665eecb1f838ff9687b2f699d9acf12b9fbe42352cfa63
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module Sql
5
+ # Converts a list of queries into a summary hash.
6
+ class QuerySummary
7
+ QUERY_TYPES = %i[delete insert select update].freeze
8
+
9
+ attr_reader :summary
10
+
11
+ def initialize(queries)
12
+ @summary = {}
13
+ queries.each do |payload|
14
+ type = get_type(payload[:sql])
15
+ next if QUERY_TYPES.exclude?(type) || pg_query?(payload[:sql])
16
+
17
+ table = get_table(payload[:sql])
18
+ @summary[type] ||= {}
19
+ @summary[type][table] ||= 0
20
+ @summary[type][table] += 1
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def get_table(sql)
27
+ sql_parts = sql.split
28
+ case get_type(sql)
29
+ when :insert
30
+ sql_parts[2]
31
+ when :update
32
+ sql_parts[1]
33
+ else
34
+ table_index = sql_parts.index("FROM")
35
+ sql_parts[table_index + 1]
36
+ end.gsub(/(\\|")/, "").to_sym
37
+ end
38
+
39
+ def get_type(sql)
40
+ sql.split[0].downcase.to_sym
41
+ end
42
+
43
+ def pg_query?(sql)
44
+ sql.include?("SELECT a.attname") || sql.include?("pg_attribute")
45
+ end
46
+ end
47
+ end
48
+ end
data/lib/rspec/sql.rb CHANGED
@@ -3,10 +3,15 @@
3
3
  require "active_support"
4
4
  require "rspec"
5
5
 
6
+ require_relative "sql/query_summary"
7
+
8
+ # We are building within the RSpec namespace for consistency and convenience.
9
+ # We are not part of the RSpec team though.
6
10
  module RSpec
11
+ # RSpec::Sql contains our code.
7
12
  module Sql; end
8
13
 
9
- RSpec::Matchers.define :query_database do |expected = nil|
14
+ Matchers.define :query_database do |expected = nil|
10
15
  match do |block|
11
16
  @queries = scribe_queries(&block)
12
17
 
@@ -18,12 +23,18 @@ module RSpec
18
23
  @queries.size == expected.size
19
24
  elsif expected.is_a?(Array)
20
25
  query_names == expected
26
+ elsif expected.is_a?(Hash)
27
+ query_summary == expected
21
28
  else
22
29
  raise "What are you expecting?"
23
30
  end
24
31
  end
25
32
 
26
33
  failure_message do |_block|
34
+ if expected.is_a?(Enumerator) && expected.inspect.match?(/:times>$/)
35
+ expected = expected.size
36
+ end
37
+
27
38
  <<~MESSAGE
28
39
  Expected database queries: #{expected}
29
40
  Actual database queries: #{query_names}
@@ -37,7 +48,11 @@ module RSpec
37
48
  end
38
49
 
39
50
  failure_message_when_negated do |_block|
40
- "Expected no database queries but observed:\n\n#{query_descriptions.join("\n")}"
51
+ <<~TXT
52
+ Expected no database queries but observed:
53
+
54
+ #{query_descriptions.join("\n")}
55
+ TXT
41
56
  end
42
57
 
43
58
  def supports_block_expectations?
@@ -52,12 +67,16 @@ module RSpec
52
67
  @queries.map { |q| "#{q[:name]} #{q[:sql]}" }
53
68
  end
54
69
 
70
+ def query_summary
71
+ Sql::QuerySummary.new(@queries).summary
72
+ end
73
+
55
74
  def scribe_queries(&)
56
75
  queries = []
57
76
 
58
- logger = ->(_name, _started, _finished, _unique_id, payload) {
77
+ logger = lambda do |_name, _started, _finished, _unique_id, payload|
59
78
  queries << payload unless %w[CACHE SCHEMA].include?(payload[:name])
60
- }
79
+ end
61
80
 
62
81
  ActiveSupport::Notifications.subscribed(logger, "sql.active_record", &)
63
82
 
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maikel Linke
8
- autorequire:
8
+ - Open Food Network contributors
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2024-02-29 00:00:00.000000000 Z
12
+ date: 2024-04-16 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: activesupport
@@ -45,11 +46,15 @@ extensions: []
45
46
  extra_rdoc_files: []
46
47
  files:
47
48
  - lib/rspec/sql.rb
49
+ - lib/rspec/sql/query_summary.rb
48
50
  homepage: https://github.com/openfoodfoundation/rspec-sql
49
51
  licenses:
50
52
  - AGPL-3.0-or-later
51
- metadata: {}
52
- post_install_message:
53
+ metadata:
54
+ changelog_uri: https://github.com/datafoodconsortium/connector-ruby/blob/main/CHANGELOG.md
55
+ source_code_uri: https://github.com/datafoodconsortium/connector-ruby/
56
+ rubygems_mfa_required: 'true'
57
+ post_install_message:
53
58
  rdoc_options: []
54
59
  require_paths:
55
60
  - lib
@@ -68,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
73
  version: '0'
69
74
  requirements: []
70
75
  rubygems_version: 3.3.26
71
- signing_key:
76
+ signing_key:
72
77
  specification_version: 4
73
78
  summary: RSpec::Sql matcher
74
79
  test_files: []