rspec-sql 0.0.2 → 0.0.3

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: 5bcabc5d90f3ad9fbdecc557c3604203ae859cf4725664a18819d1290ced63e2
4
- data.tar.gz: '09e1ba8a94a2e92355414104384bc7de76e7aa42bdf1c2f0bc5311c29ecc66a8'
3
+ metadata.gz: 9cfbb9dfa6686fc934767248d98c65a3ffa0647f9674df74792ab344109b2899
4
+ data.tar.gz: 576dce207bae8bdb03585dc0234a898bf12d4b8e9e27f22f55817ce03babb4d8
5
5
  SHA512:
6
- metadata.gz: ff5a4cdf9a0c27921a32ca65da0c682b6412652cf78b6d155309e6e97b7cb13702086f183d1d6ed88f2083e57fc99e6b5e4c799867b5e5a1f659363ecbda1df5
7
- data.tar.gz: 3af870e827e52cfc8e1a548d9ff456c26521291de6bd4fb093172537f4f46ca551b84bdd2dc3306b0b665eecb1f838ff9687b2f699d9acf12b9fbe42352cfa63
6
+ metadata.gz: 8d50829389362c678403a0ddaf982e44232aa2b884159f8928f07943b351ca00b015d48854e953f977ac6f5f3eefd6e47b168e2c732311e7187f677e97753ed5
7
+ data.tar.gz: aff99ef38190a45774eb0ada50daaba8f9182d2f89f3ebcdfb35ed3781210b4e19941b3bedcc5ef736c186474c6ccec41bd0c9149918e38c1c773087a3b9673d
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "query_summary"
4
+
5
+ module RSpec
6
+ module Sql
7
+ # Compares expectations with actual queries.
8
+ class QueryMatcher
9
+ attr_reader :expected
10
+
11
+ def initialize(queries, expected)
12
+ @queries = queries
13
+ @expected = sanitize_number(expected)
14
+ end
15
+
16
+ def matches?
17
+ # Simple presence validation.
18
+ return !@queries.empty? if expected.nil?
19
+
20
+ actual == expected
21
+ end
22
+
23
+ def actual
24
+ @actual ||= actual_compared_to_expected
25
+ end
26
+
27
+ private
28
+
29
+ # Support writing: `is_expected.to query_database 5.times`
30
+ def sanitize_number(expected)
31
+ if expected.is_a?(Enumerator) && expected.inspect.match?(/:times>$/)
32
+ expected.size
33
+ else
34
+ expected
35
+ end
36
+ end
37
+
38
+ def actual_compared_to_expected
39
+ case expected
40
+ when Integer
41
+ @queries.size
42
+ when Array
43
+ query_names
44
+ when Hash
45
+ query_summary
46
+ else
47
+ raise "What are you expecting?"
48
+ end
49
+ end
50
+
51
+ def query_names
52
+ @queries.map { |q| q[:name] || q[:sql].split.take(2).join(" ") }
53
+ end
54
+
55
+ def query_summary
56
+ QuerySummary.new(@queries).summary
57
+ end
58
+ end
59
+ end
60
+ end
data/lib/rspec/sql.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require "active_support"
4
4
  require "rspec"
5
5
 
6
- require_relative "sql/query_summary"
6
+ require_relative "sql/query_matcher"
7
7
 
8
8
  # We are building within the RSpec namespace for consistency and convenience.
9
9
  # We are not part of the RSpec team though.
@@ -14,32 +14,22 @@ module RSpec
14
14
  Matchers.define :query_database do |expected = nil|
15
15
  match do |block|
16
16
  @queries = scribe_queries(&block)
17
+ @matcher = Sql::QueryMatcher.new(@queries, expected)
18
+ expected = matcher.expected
17
19
 
18
- if expected.nil?
19
- !@queries.empty?
20
- elsif expected.is_a?(Integer)
21
- @queries.size == expected
22
- elsif expected.is_a?(Enumerator) && expected.inspect.match?(/:times>$/)
23
- @queries.size == expected.size
24
- elsif expected.is_a?(Array)
25
- query_names == expected
26
- elsif expected.is_a?(Hash)
27
- query_summary == expected
28
- else
29
- raise "What are you expecting?"
30
- end
20
+ matcher.matches?
31
21
  end
32
22
 
33
23
  failure_message do |_block|
34
- if expected.is_a?(Enumerator) && expected.inspect.match?(/:times>$/)
35
- expected = expected.size
24
+ if expected.nil?
25
+ return "Expected at least one database query but observed none."
36
26
  end
37
27
 
38
28
  <<~MESSAGE
39
29
  Expected database queries: #{expected}
40
- Actual database queries: #{query_names}
30
+ Actual database queries: #{matcher.actual}
41
31
 
42
- Diff: #{Expectations.differ.diff_as_object(query_names, expected)}
32
+ Diff: #{diff(matcher.actual, expected)}
43
33
 
44
34
  Full query log:
45
35
 
@@ -59,16 +49,21 @@ module RSpec
59
49
  true
60
50
  end
61
51
 
62
- def query_names
63
- @queries.map { |q| q[:name] || q[:sql].split.take(2).join(" ") }
64
- end
65
-
66
52
  def query_descriptions
67
53
  @queries.map { |q| "#{q[:name]} #{q[:sql]}" }
68
54
  end
69
55
 
70
- def query_summary
71
- Sql::QuerySummary.new(@queries).summary
56
+ def matcher
57
+ @matcher
58
+ end
59
+
60
+ def diff(actual, expected)
61
+ if expected.is_a?(Numeric)
62
+ change = actual - expected
63
+ format("%+d", change)
64
+ else
65
+ Expectations.differ.diff_as_object(actual, expected)
66
+ end
72
67
  end
73
68
 
74
69
  def scribe_queries(&)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maikel Linke
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-04-16 00:00:00.000000000 Z
12
+ date: 2024-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -46,13 +46,14 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - lib/rspec/sql.rb
49
+ - lib/rspec/sql/query_matcher.rb
49
50
  - lib/rspec/sql/query_summary.rb
50
51
  homepage: https://github.com/openfoodfoundation/rspec-sql
51
52
  licenses:
52
53
  - AGPL-3.0-or-later
53
54
  metadata:
54
- changelog_uri: https://github.com/datafoodconsortium/connector-ruby/blob/main/CHANGELOG.md
55
- source_code_uri: https://github.com/datafoodconsortium/connector-ruby/
55
+ changelog_uri: https://github.com/openfoodfoundation/rspec-sql/releases
56
+ source_code_uri: https://github.com/openfoodfoundation/rspec-sql/
56
57
  rubygems_mfa_required: 'true'
57
58
  post_install_message:
58
59
  rdoc_options: []