fitting 2.5.0 → 2.6.0

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
  SHA1:
3
- metadata.gz: 873df0636bcad7162066d90caaeaebef52eb1ef6
4
- data.tar.gz: 3ec118c489b012fdcc62c6f3ff4600a5efe6131a
3
+ metadata.gz: 0f803f26bbe2cb7472a2ca8077aa7a00754ec755
4
+ data.tar.gz: 38f77aec3f07b3aed3f82ebcae34549a953d45f5
5
5
  SHA512:
6
- metadata.gz: 7072803275b24d10a44a58762f5c884c06310339da331c41514b24ccb40417681d3f51fc532196cdfa9aa27a08f70d4a4281a3dd254729c2b6c58f3878ed9f94
7
- data.tar.gz: cdd978371460f13dde294c13eae7b2c8ed049d1faaecbc236e4504abd56107ddc9868f9c2fb189e11b34f672cd633ddd0642df111687537c891ce032a5417cd2
6
+ metadata.gz: 3a3619063602e165398a68ea20e4630be7d3877119f11cbead7fdc05a38b4f436e2d06039b75c3d09a55b069498e0f2e043d7acb14151342a90c7282378cc580
7
+ data.tar.gz: 7c2ec1512bc655dbd816b3ba9665ec6af4bbf7e9c68deb8f3d1f608396ac1e24389bb5099621c732aeb0f895c019cd7e54725e825015bfe8d94829dda00586f1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Change log
2
2
 
3
+ ### 2.6.0 - 2018-08-28
4
+
5
+ * features
6
+ * Add two rake tasks fitting:documentation and fitting:tests
7
+
3
8
  ### 2.5.0 - 2018-07-05
4
9
 
5
10
  * features
data/lib/fitting.rb CHANGED
@@ -3,6 +3,7 @@ require 'fitting/configuration'
3
3
  require 'fitting/matchers/response_matcher'
4
4
  require 'fitting/documentation'
5
5
  require 'fitting/storage/responses'
6
+ require 'fitting/railtie' if defined?(Rails)
6
7
 
7
8
  module Fitting
8
9
  class << self
@@ -19,7 +20,7 @@ module Fitting
19
20
 
20
21
  RSpec.configure do |config|
21
22
  config.after(:each, type: :controller) do
22
- responses.add(response)
23
+ responses.add(response, self.inspect)
23
24
  end
24
25
 
25
26
  config.after(:suite) do
@@ -27,5 +28,36 @@ module Fitting
27
28
  end
28
29
  end
29
30
  end
31
+
32
+ def save_test_data
33
+ responses = Fitting::Storage::Responses.new
34
+
35
+ RSpec.configure do |config|
36
+ config.after(:each, type: :controller) do
37
+ responses.add(response, self.inspect)
38
+ end
39
+
40
+ config.after(:suite) do
41
+ responses.tests.save
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ def self.loaded_tasks=(val)
48
+ @loaded_tasks = val
49
+ end
50
+
51
+ def self.loaded_tasks
52
+ @loaded_tasks
53
+ end
54
+
55
+ def self.load_tasks
56
+ return if loaded_tasks
57
+ self.loaded_tasks = true
58
+
59
+ Dir[File.join(File.dirname(__FILE__), 'tasks', '**/*.rake')].each do |rake|
60
+ load rake
61
+ end
30
62
  end
31
63
  end
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+
3
+ module Fitting
4
+ class MyRailtie < Rails::Railtie
5
+ rake_tasks do
6
+ load 'tasks/fitting.rake'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,54 @@
1
+ require 'fitting/records/test_unit/request'
2
+
3
+ module Fitting
4
+ class Records
5
+ class RealizedUnit
6
+ def initialize(realized_requests, documented_requests)
7
+ @realized_requests = realized_requests
8
+ @documented_requests = documented_requests
9
+ end
10
+
11
+ def fully_covered?
12
+ test_file_paths.each do |key, requests|
13
+ all_good = requests.all? { |request| request.documented? }
14
+ return false unless all_good
15
+ end
16
+ test_file_paths.each do |key, requests|
17
+ all_good = requests.all? { |request| request.response_documented? }
18
+ return false unless all_good
19
+ end
20
+ test_file_paths.each do |key, requests|
21
+ all_good = requests.all? { |request| request.response_json_schemas? }
22
+ return false unless all_good
23
+ end
24
+ test_file_paths.each do |key, requests|
25
+ all_good = requests.all? { |request| request.valid_json_schemas? }
26
+ return false unless all_good
27
+ end
28
+ true
29
+ end
30
+
31
+ def test_file_paths
32
+ return @test_file_paths if @test_file_paths
33
+ @test_file_paths = {}
34
+ white_unit.map do |request|
35
+ @test_file_paths[request.test_file_path] ||= []
36
+ @test_file_paths[request.test_file_path].push(request)
37
+ end
38
+ @test_file_paths
39
+ end
40
+
41
+ def all_documented_requests
42
+ @all_documented_requests ||= @documented_requests.to_a.inject([]) do |res, tomogram_request|
43
+ res.push(Fitting::Records::Documented::Request.new(tomogram_request, nil))
44
+ end
45
+ end
46
+
47
+ def white_unit
48
+ @white_unit_requests ||= @realized_requests.to_a.inject([]) do |res, tested_request|
49
+ res.push(Fitting::Records::TestUnit::Request.new(tested_request, all_documented_requests))
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,49 @@
1
+ require 'json'
2
+ require 'fitting/records/spherical/response'
3
+
4
+ module Fitting
5
+ class Records
6
+ class Spherical
7
+ class Request
8
+ attr_reader :method, :path, :body, :response, :title, :group
9
+
10
+ def initialize(method:, path:, body:, response:, title:, group:)
11
+ @method = method
12
+ @path = path
13
+ @body = body
14
+ @response = response
15
+ @title = title
16
+ @group = group
17
+ end
18
+
19
+ def to_hash
20
+ {
21
+ method: method,
22
+ path: path.to_s,
23
+ body: body,
24
+ response: response.to_hash,
25
+ title: title,
26
+ group: group
27
+ }
28
+ end
29
+
30
+ def to_json
31
+ JSON.dump(to_hash)
32
+ end
33
+
34
+ class << self
35
+ def load(hash)
36
+ new(
37
+ method: hash["method"],
38
+ path: hash["path"],
39
+ body: hash["body"],
40
+ response: Fitting::Records::Spherical::Response.load(hash["response"]),
41
+ title: hash["title"],
42
+ group: hash["group"]
43
+ )
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ require 'fitting/records/spherical/request'
2
+ require 'json'
3
+
4
+ module Fitting
5
+ class Records
6
+ class Spherical
7
+ class Requests
8
+ def to_a
9
+ return @to_a if @to_a
10
+
11
+ array = JSON.load(File.read('tests.json'))
12
+ @to_a = array.inject([]) do |res, tested_request|
13
+ res.push(Fitting::Records::Spherical::Request.load(tested_request))
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ require 'json'
2
+
3
+ module Fitting
4
+ class Records
5
+ class Spherical
6
+ class Response
7
+ attr_reader :status, :body
8
+
9
+ def initialize(status:, body:)
10
+ @status = status
11
+ @body = body
12
+ end
13
+
14
+ def to_hash
15
+ {
16
+ status: status,
17
+ body: JSON.load(body)
18
+ }
19
+ rescue JSON::ParserError
20
+ {
21
+ status: status,
22
+ body: {}
23
+ }
24
+ end
25
+
26
+ def to_json
27
+ JSON.dump(to_hash)
28
+ end
29
+
30
+ class << self
31
+ def load(hash)
32
+ new(status: hash["status"], body: hash["body"])
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,96 @@
1
+ require 'json-schema'
2
+
3
+ module Fitting
4
+ class Records
5
+ class TestUnit
6
+ class Request
7
+ def initialize(tested_request, all_documented_requests)
8
+ @tested_request = tested_request
9
+ @all_documented_requests = all_documented_requests
10
+ end
11
+
12
+ def method
13
+ @method ||= @tested_request.method
14
+ end
15
+
16
+ def path
17
+ @path ||= @tested_request.path
18
+ end
19
+
20
+ def body
21
+ @body ||= @tested_request.body
22
+ end
23
+
24
+ def response
25
+ @response ||= @tested_request.response
26
+ end
27
+
28
+ def test_path
29
+ @test_path ||= @tested_request.title
30
+ end
31
+
32
+ def test_file_path
33
+ @test_file_path ||= @tested_request.group
34
+ end
35
+
36
+ def documented_requests
37
+ @documented_requests ||= @all_documented_requests.inject([]) do |res, documented_request|
38
+ next res unless @tested_request.method == documented_request.method &&
39
+ documented_request.path.match(@tested_request.path.to_s)
40
+ res.push(documented_request)
41
+ end
42
+ end
43
+
44
+ def documented?
45
+ @documented ||= documented_requests.present?
46
+ end
47
+
48
+ def documented_responses
49
+ @documented_responses ||= documented_requests.inject([]) do |res, documented_request|
50
+ documented_request.responses.map do |documented_response|
51
+ next unless documented_response["status"] == response.status.to_s
52
+ res.push(documented_response)
53
+ end
54
+ end.flatten.compact
55
+ end
56
+
57
+ def response_documented?
58
+ @response_documented ||= documented_responses.present?
59
+ end
60
+
61
+ def response_json_schemas
62
+ @response_json_schemas ||= documented_responses.inject([]) do |res, documented_response|
63
+ res.push(documented_response["json_schemas"])
64
+ end.flatten
65
+ end
66
+
67
+ def response_json_schemas?
68
+ @response_json_schemas_present ||= response_json_schemas.present?
69
+ end
70
+
71
+ def valid_json_schemas
72
+ @valid_json_schemas ||= response_json_schemas.inject([]) do |res, json_schema|
73
+ next res unless JSON::Validator.validate(json_schema, response.body)
74
+ res.push(json_schema)
75
+ end.flatten
76
+ end
77
+
78
+ def invalid_json_schemas
79
+ @invalid_json_schemas ||= response_json_schemas.inject([]) do |res, json_schema|
80
+ next res if JSON::Validator.validate(json_schema, response.body)
81
+ res.push(
82
+ {
83
+ json_schema: json_schema,
84
+ fully_validate: JSON::Validator.fully_validate(json_schema, response.body)
85
+ }
86
+ )
87
+ end.flatten
88
+ end
89
+
90
+ def valid_json_schemas?
91
+ @valid_json_schemas_present ||= valid_json_schemas.present?
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -1,12 +1,14 @@
1
1
  require 'tomograph/path'
2
2
  require 'fitting/records/tested/response'
3
+ require 'fitting/records/spherical/request'
3
4
 
4
5
  module Fitting
5
6
  class Records
6
7
  class Tested
7
8
  class Request
8
- def initialize(env_response)
9
+ def initialize(env_response, test_title)
9
10
  @env_response = env_response
11
+ @test_title = test_title
10
12
  end
11
13
 
12
14
  def method
@@ -24,6 +26,25 @@ module Fitting
24
26
  def response
25
27
  @response ||= Fitting::Records::Tested::Response.new(@env_response)
26
28
  end
29
+
30
+ def test_path
31
+ @test_path ||= @test_title[/#{'\(\.'}(.*?)#{'\)'}/m, 1] || @test_title[/#{'\.'}(.*?)#{'\"'}/m, 1]
32
+ end
33
+
34
+ def test_file_path
35
+ @test_file_path ||= test_path.split(':').first
36
+ end
37
+
38
+ def to_spherical
39
+ Fitting::Records::Spherical::Request.new(
40
+ method: method,
41
+ path: path,
42
+ body: body,
43
+ response: response.to_spherical,
44
+ title: test_path,
45
+ group: test_file_path
46
+ )
47
+ end
27
48
  end
28
49
  end
29
50
  end
@@ -1,3 +1,5 @@
1
+ require 'fitting/records/spherical/response'
2
+
1
3
  module Fitting
2
4
  class Records
3
5
  class Tested
@@ -13,6 +15,13 @@ module Fitting
13
15
  def body
14
16
  @body ||= @env_response.body
15
17
  end
18
+
19
+ def to_spherical
20
+ Fitting::Records::Spherical::Response.new(
21
+ status: status,
22
+ body: body
23
+ )
24
+ end
16
25
  end
17
26
  end
18
27
  end
@@ -24,7 +24,7 @@ module Fitting
24
24
  end
25
25
 
26
26
  def tested_responses
27
- @tested_responses ||= @tested_requests.inject([]) do |res, tested_request|
27
+ @tested_responses ||= @tested_requests.to_a.inject([]) do |res, tested_request|
28
28
  next res unless @documented_request.method == tested_request.method &&
29
29
  @documented_request.path.match(tested_request.path.to_s)
30
30
  res.push(tested_request.response)
@@ -1,4 +1,5 @@
1
1
  require 'fitting/statistics'
2
+ require 'fitting/tests'
2
3
  require 'fitting/records/tested/request'
3
4
 
4
5
  module Fitting
@@ -8,13 +9,17 @@ module Fitting
8
9
  @tested_requests = []
9
10
  end
10
11
 
11
- def add(env_response)
12
- @tested_requests.push(Fitting::Records::Tested::Request.new(env_response))
12
+ def add(env_response, test_title = '')
13
+ @tested_requests.push(Fitting::Records::Tested::Request.new(env_response, test_title))
13
14
  end
14
15
 
15
16
  def statistics
16
17
  Fitting::Statistics.new(@tested_requests)
17
18
  end
19
+
20
+ def tests
21
+ Fitting::Tests.new(@tested_requests)
22
+ end
18
23
  end
19
24
  end
20
25
  end
@@ -0,0 +1,49 @@
1
+ module Fitting
2
+ class Templates
3
+ class RealizedTemplate
4
+ def initialize(realized_unit)
5
+ @realized_unit = realized_unit
6
+ end
7
+
8
+ def to_s
9
+ res = ''
10
+ res += "1. Find request method and path:\n"
11
+ @realized_unit.test_file_paths.each do |key, requests|
12
+ all_good = requests.all? { |request| request.documented? }
13
+ res += "file: #{key} #{all_good ? '✔' : '✖'}\n"
14
+ end
15
+ res += "\n2. Find response status code:\n"
16
+ @realized_unit.test_file_paths.each do |key, requests|
17
+ all_good = requests.all? { |request| request.response_documented? }
18
+ res += "file: #{key} #{all_good ? '✔' : '✖'}\n"
19
+ end
20
+ res += "\n3. Find response json-schemas:\n"
21
+ @realized_unit.test_file_paths.each do |key, requests|
22
+ all_good = requests.all? { |request| request.response_json_schemas? }
23
+ res += "file: #{key} #{all_good ? '✔' : '✖'}\n"
24
+ end
25
+ res += "\n4. Check valid json-schemas:\n"
26
+ @realized_unit.test_file_paths.each do |key, requests|
27
+ all_good = requests.all? { |request| request.valid_json_schemas? }
28
+ res += "path: #{key} #{all_good ? '✔' : '✖'}\n"
29
+ unless all_good
30
+ requests.map do |request|
31
+ unless request.valid_json_schemas?
32
+ res += " full path: #{request.test_path} ✖\n"
33
+ res += " request.method #{request.method}\n"
34
+ res += " request.path #{request.path}\n"
35
+ res += " request.response.status #{request.response.status}\n"
36
+ res += " request.response.body #{request.response.body}\n\n"
37
+ request.invalid_json_schemas.map do |schema|
38
+ res += " json_schemas: #{schema[:json_schema]}\n"
39
+ res += " fully_validate: #{schema[:fully_validate]}\n\n"
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ res
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ require 'fitting/statistics/template'
2
+
3
+ module Fitting
4
+ class Tests
5
+ def initialize(tested_requests)
6
+ @tested_requests = tested_requests
7
+ end
8
+
9
+ def save
10
+ array = @tested_requests.inject([]) do |res, request|
11
+ next res unless request.path.to_s.start_with?(Fitting.configuration.prefix)
12
+ res.push(request.to_spherical.to_hash)
13
+ end
14
+ json = JSON.dump(array)
15
+ File.open("tests.json", 'w') { |file| file.write(json) }
16
+ end
17
+
18
+ def make_dir(dir_name)
19
+ FileUtils.mkdir_p(dir_name)
20
+ end
21
+ end
22
+ end
23
+
@@ -1,3 +1,3 @@
1
1
  module Fitting
2
- VERSION = '2.5.0'.freeze
2
+ VERSION = '2.6.0'.freeze
3
3
  end
@@ -0,0 +1,34 @@
1
+ require 'fitting/records/spherical/requests'
2
+ require 'fitting/configuration'
3
+ require 'fitting/records/realized_unit'
4
+ require 'fitting/templates/realized_template'
5
+
6
+ namespace :fitting do
7
+ desc 'Fitting documentation'
8
+ task :documentation do
9
+ documented_unit = Fitting::Statistics::Template.new(
10
+ Fitting::Records::Spherical::Requests.new,
11
+ Fitting.configuration
12
+ )
13
+ puts documented_unit.stats
14
+
15
+ unless documented_unit.not_covered == "\n"
16
+ puts 'Not all responses from the whitelist are covered!'
17
+ exit 1
18
+ end
19
+ end
20
+
21
+ desc 'Fitting tests'
22
+ task :tests do
23
+ realized_unit = Fitting::Records::RealizedUnit.new(
24
+ Fitting::Records::Spherical::Requests.new,
25
+ Fitting.configuration.tomogram
26
+ )
27
+ puts Fitting::Templates::RealizedTemplate.new(realized_unit).to_s
28
+
29
+ unless realized_unit.fully_covered?
30
+ puts 'Not all responses from the whitelist are covered!'
31
+ exit 1
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fitting
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - d.efimov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-06 00:00:00.000000000 Z
11
+ date: 2018-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -197,7 +197,13 @@ files:
197
197
  - lib/fitting/configuration/yaml.rb
198
198
  - lib/fitting/documentation.rb
199
199
  - lib/fitting/matchers/response_matcher.rb
200
+ - lib/fitting/railtie.rb
200
201
  - lib/fitting/records/documented/request.rb
202
+ - lib/fitting/records/realized_unit.rb
203
+ - lib/fitting/records/spherical/request.rb
204
+ - lib/fitting/records/spherical/requests.rb
205
+ - lib/fitting/records/spherical/response.rb
206
+ - lib/fitting/records/test_unit/request.rb
201
207
  - lib/fitting/records/tested/request.rb
202
208
  - lib/fitting/records/tested/response.rb
203
209
  - lib/fitting/records/unit/json_schema.rb
@@ -219,7 +225,10 @@ files:
219
225
  - lib/fitting/statistics/template.rb
220
226
  - lib/fitting/storage/responses.rb
221
227
  - lib/fitting/storage/white_list.rb
228
+ - lib/fitting/templates/realized_template.rb
229
+ - lib/fitting/tests.rb
222
230
  - lib/fitting/version.rb
231
+ - lib/tasks/fitting.rake
223
232
  homepage: https://github.com/funbox/fitting
224
233
  licenses:
225
234
  - MIT