testscore 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/testscore.rb +62 -36
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d63551aaefa042f8d0a6013a3a06bdd987c36bb120a3017ff40a9349526063a
4
- data.tar.gz: 8b870d1ae8065a16db8e97c8a86d5fd1687b52280f44ab939f71a121d5821287
3
+ metadata.gz: 76bebdd70a20dcacf62e18deaf47d685e5babb2436631930612732247809d043
4
+ data.tar.gz: 1a86c18da613bf93eba915f8d93e086f22ca5c9279c6260947c050975315fd96
5
5
  SHA512:
6
- metadata.gz: 41319a64b799e0934ef33ee0b7cfd268cf829e04bc8f42572c7ec4d41c3d436841d21300438dc12d19a2b017ec0fb0a5f6a56a0d836cdf6578ea4c6b828a0826
7
- data.tar.gz: f043087d0f4751785372bad8aa436f689daa582ffb4756459cf8082a9376daa4dd2976c82e44e036a7c6e7a9b4a28219f0f7165d26621aaa0a281531617960d1
6
+ metadata.gz: 4fb1393c98e734ad8b3e73692c9c4e289afa7fc03e5bd6d6e671be2b9ba9ab4227386e5b5a94b8861d24cd4d087cf13db3444d0e97b8f152f7f13a809001c660
7
+ data.tar.gz: 705ed7b81f69ad5709d26fd2e4b85f9dba3122a725c7116b706788427482020e15759991c8a7435acb01318d3d6f961a47262a6472ce8ead153b21bcd40352fd
data/lib/testscore.rb CHANGED
@@ -2,53 +2,71 @@ require 'json'
2
2
  require 'zlib'
3
3
  require 'net/http'
4
4
 
5
- module ActiveRecord
6
- class QueryCounter
7
- class << self
8
- attr_accessor :queries
9
- end
5
+ if defined?(ActiveRecord)
6
+ module ActiveRecord
7
+ class QueryCounter
8
+ class << self
9
+ attr_accessor :queries
10
+ end
11
+
12
+ IGNORED_SQL = [
13
+ /^PRAGMA (?!(table_info))/,
14
+ /^SELECT currval/,
15
+ /^SELECT CAST/,
16
+ /^SELECT @@IDENTITY/,
17
+ /^SELECT @@ROWCOUNT/,
18
+ /^SAVEPOINT/,
19
+ /FROM pg_attribute/,
20
+ /JOIN pg_attribute/,
21
+ /JOIN pg_namespace/,
22
+ /SHOW search_path/,
23
+ /^BEGIN$/,
24
+ /^ROLLBACK$/,
25
+ /^ROLLBACK TO SAVEPOINT/,
26
+ /^RELEASE SAVEPOINT/,
27
+ /^SHOW max_identifier_length/
28
+ ]
10
29
 
11
- IGNORED_SQL = [
12
- /^PRAGMA (?!(table_info))/,
13
- /^SELECT currval/,
14
- /^SELECT CAST/,
15
- /^SELECT @@IDENTITY/,
16
- /^SELECT @@ROWCOUNT/,
17
- /^SAVEPOINT/,
18
- /FROM pg_attribute/,
19
- /JOIN pg_attribute/,
20
- /JOIN pg_namespace/,
21
- /SHOW search_path/,
22
- /^BEGIN$/,
23
- /^ROLLBACK$/,
24
- /^ROLLBACK TO SAVEPOINT/,
25
- /^RELEASE SAVEPOINT/,
26
- /^SHOW max_identifier_length/
27
- ]
28
-
29
- def call(name, start, finish, message_id, values)
30
- return if 'CACHE' == values[:name]
31
- return if IGNORED_SQL.any? { |r| values[:sql] =~ r }
32
-
33
- self.class.queries ||= []
34
- self.class.queries << {
35
- duration: finish - start,
36
- sql: values[:sql]
37
- }
30
+ def call(name, start, finish, message_id, values)
31
+ return if 'CACHE' == values[:name]
32
+ return if IGNORED_SQL.any? { |r| values[:sql] =~ r }
33
+
34
+ self.class.queries ||= []
35
+ self.class.queries << {
36
+ duration: finish - start,
37
+ sql: values[:sql]
38
+ }
39
+ end
38
40
  end
39
41
  end
40
42
  end
41
43
 
42
44
  module TestScore
45
+ def self.rails?
46
+ defined?(ActiveRecord) && defined?(ActiveSupport)
47
+ end
48
+
49
+ def self.queries
50
+ return [] unless rails?
51
+
52
+ ActiveRecord::QueryCounter.queries
53
+ end
54
+
43
55
  def self.log_suite_started
44
56
  @start_time = Time.now
45
- ActiveSupport::Notifications.subscribe('sql.active_record', ActiveRecord::QueryCounter.new)
57
+
58
+ if rails?
59
+ ActiveSupport::Notifications.subscribe('sql.active_record', ActiveRecord::QueryCounter.new)
60
+ end
46
61
  end
47
62
 
48
63
  def self.spec_started(example)
49
64
  @results ||= {}
50
65
  @results[example.id] = { started_at: Time.now }
51
- ActiveRecord::QueryCounter.queries = []
66
+
67
+ if rails?
68
+ ActiveRecord::QueryCounter.queries = []
69
+ end
52
70
  end
53
71
 
54
72
  def self.spec_ended(example)
@@ -56,7 +74,7 @@ module TestScore
56
74
  duration: Time.now - @results[example.id][:started_at],
57
75
  description: example.description,
58
76
  metadata: example.metadata.slice(:line_number, :file_path),
59
- queries: ActiveRecord::QueryCounter.queries
77
+ queries: queries
60
78
  }
61
79
  end
62
80
 
@@ -74,7 +92,15 @@ module TestScore
74
92
  req.body = gzip.close.string
75
93
  http = Net::HTTP.new(uri.host, uri.port)
76
94
  http.use_ssl = (uri.scheme == "https")
77
- http.request(req)
95
+ response = http.request(req).body
96
+
97
+ if response && response.length > 0
98
+ jsonResponse = JSON.parse(response)
99
+
100
+ if jsonResponse['error']
101
+ puts "Unable to report results to TestScore: #{jsonResponse['error']}"
102
+ end
103
+ end
78
104
  rescue => e
79
105
  puts "Unable to report results to TestScore: #{e.message}"
80
106
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: testscore
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - TestScore