api_mini_tester 0.1.4 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab778aea0e70140ad7987422ea3cc84423ca3c83
4
- data.tar.gz: 482db1276239fca74afb2b54a490e464f1529ccb
3
+ metadata.gz: 6217a0f3741f405f67be1d00fb16460ea10958b9
4
+ data.tar.gz: a6b132d8192eb00255ab4a019022b4e81cb12b77
5
5
  SHA512:
6
- metadata.gz: f75c1d2e8af06c2629da0c2e52f8b63f154871df0c3a0fb950e03dd2adf8f485f99161cbeb22f385d7869c5460341893a3f65ed366c9f217b1ecf4a8c3bf7bae
7
- data.tar.gz: d72335d77cab38d7f09a0171914b2a56323a59ae057c9ef2acac8a38ae9c5b6a1b176aa0de9889d675988eeceb4c9bbe099d2258b49a8cd2eaad04bf0dcbcb09
6
+ metadata.gz: 758c351cd3150f369bfd3452662d94bde7399fb24177b5766cd9dad9c334c7036cc71e199d46b9620e9cedfa1e033ffd4062095f9ee657daa44073b5daf44305
7
+ data.tar.gz: cf363712920a53022fc35b4f1a54b9ed67dd71a66c3e805a503ac5984dfd8dc7f28e82e4ecc050beb6f0954ad60c0905c2ae7e2bf62601c5ee32c065152644a2
@@ -0,0 +1,9 @@
1
+ require 'liquid'
2
+ require 'faker'
3
+
4
+ module TestFakerFilter
5
+ def fake(input)
6
+ # Not nice, but I don't know better way.
7
+ eval "Faker::#{input}" # rubocop:disable Security/Eval,Style/EvalWithLocation
8
+ end
9
+ end
@@ -1,52 +1,54 @@
1
1
  require 'builder'
2
2
  require 'socket'
3
3
 
4
- class TestFormatter
4
+ module ApiMiniTester
5
+ class TestFormatter
5
6
 
6
- RESULT_SECTIONS = %i[status headers body timing].freeze
7
- SECTION_TRANSLATE = {
8
- status: "Status",
9
- headers: "Headers",
10
- body: "Body",
11
- timing: "Timing"
12
- }.freeze
7
+ RESULT_SECTIONS = %i[status headers body timing].freeze
8
+ SECTION_TRANSLATE = {
9
+ status: "Status",
10
+ headers: "Headers",
11
+ body: "Body",
12
+ timing: "Timing"
13
+ }.freeze
13
14
 
14
- attr_accessor :results
15
+ attr_accessor :results
15
16
 
16
- def initialize(results)
17
- @results = results
18
- end
17
+ def initialize(results)
18
+ @results = results
19
+ end
19
20
 
20
- def to_json
21
- clean_up_infinity.to_json
22
- end
21
+ def to_json
22
+ clean_up_infinity.to_json
23
+ end
23
24
 
24
- def to_yaml
25
- clean_up_infinity.to_yaml
26
- end
25
+ def to_yaml
26
+ clean_up_infinity.to_yaml
27
+ end
27
28
 
28
- def to_junit_xml
29
- xml = Builder::XmlMarkup.new(indent: 2)
30
- xml.instruct! :xml, encoding: "UTF-8"
31
- xml.testsuites do
32
- results[:scenarios].each do |scenario|
33
- xml.testsuite name: scenario[:name],
34
- timestamp: timestamp,
35
- hostname: hostname,
36
- tests: tests_in_scenario(scenario),
37
- skipped: 0,
38
- failures: failed_in_scenario(scenario),
39
- errors: 0,
40
- time: time_in_scenario(scenario) do
41
- scenario[:steps].each do |step|
42
- classname = name_to_camelcase(step[:name])
43
- RESULT_SECTIONS.each do |section|
44
- step[section].each do |s|
45
- xml.testcase classname: classname,
46
- name: s[:name],
47
- time: step_timing(step),
48
- file: "./#{classname}.rb" do
49
- s[:result] ? nil : xml.failure(message: s[:desc])
29
+ def to_junit_xml
30
+ xml = Builder::XmlMarkup.new(indent: 2)
31
+ xml.instruct! :xml, encoding: "UTF-8"
32
+ xml.testsuites do
33
+ results[:scenarios].each do |scenario|
34
+ xml.testsuite name: scenario[:name],
35
+ timestamp: timestamp,
36
+ hostname: hostname,
37
+ tests: tests_in_scenario(scenario),
38
+ skipped: 0,
39
+ failures: failed_in_scenario(scenario),
40
+ errors: 0,
41
+ time: time_in_scenario(scenario) do
42
+ scenario[:steps].each do |step|
43
+ classname = name_to_camelcase(step[:name])
44
+ RESULT_SECTIONS.each do |section|
45
+ step[section].each do |s|
46
+ xml.testcase classname: classname,
47
+ name: s[:name],
48
+ time: step_timing(step),
49
+ file: "./#{classname}.rb" do
50
+ s[:result] ? nil : xml.failure(message: s[:desc])
51
+ end
50
52
  end
51
53
  end
52
54
  end
@@ -54,156 +56,155 @@ class TestFormatter
54
56
  end
55
57
  end
56
58
  end
57
- end
58
59
 
59
- def timestamp
60
- Time.now.strftime("%FT%T%:z")
61
- end
60
+ def timestamp
61
+ Time.now.strftime("%FT%T%:z")
62
+ end
62
63
 
63
- def scenarios_count
64
- results[:scenarios].size
65
- end
64
+ def scenarios_count
65
+ results[:scenarios].size
66
+ end
66
67
 
67
- def steps_count(scenario)
68
- scenario[:steps].size
69
- end
68
+ def steps_count(scenario)
69
+ scenario[:steps].size
70
+ end
70
71
 
71
- def test_count(step)
72
- count = 0
73
- [:status, :headers, :body, :timing].each do |section|
74
- count += step[section].size
72
+ def test_count(step)
73
+ count = 0
74
+ [:status, :headers, :body, :timing].each do |section|
75
+ count += step[section].size
76
+ end
77
+ count
75
78
  end
76
- count
77
- end
78
79
 
79
- def tests_in_scenario(scenario)
80
- count = 0
81
- scenario[:steps].each do |step|
82
- count += test_count(step)
80
+ def tests_in_scenario(scenario)
81
+ count = 0
82
+ scenario[:steps].each do |step|
83
+ count += test_count(step)
84
+ end
85
+ count
83
86
  end
84
- count
85
- end
86
87
 
87
- def time_in_scenario(scenario)
88
- time = 0.0
89
- scenario[:steps].each do |step|
90
- time += step[:timing].first[:real]
88
+ def time_in_scenario(scenario)
89
+ time = 0.0
90
+ scenario[:steps].each do |step|
91
+ time += step[:timing].first[:real]
92
+ end
93
+ time
91
94
  end
92
- time
93
- end
94
95
 
95
- def failed_in_scenario(scenario)
96
- count = 0
97
- scenario[:steps].each do |step|
98
- [:status, :headers, :body, :timing].each do |section|
99
- count += step[section].map { |res| res[:result] }.select(&:!).size
96
+ def failed_in_scenario(scenario)
97
+ count = 0
98
+ scenario[:steps].each do |step|
99
+ [:status, :headers, :body, :timing].each do |section|
100
+ count += step[section].map { |res| res[:result] }.select(&:!).size
101
+ end
100
102
  end
103
+ count
101
104
  end
102
- count
103
- end
104
105
 
105
- def hostname
106
- Socket.gethostname
107
- end
106
+ def hostname
107
+ Socket.gethostname
108
+ end
108
109
 
109
- def name_to_camelcase(name)
110
- name.gsub(/[[:space:]]+/, '_').downcase.gsub(/(?:^|_)([a-z])/) { $1.upcase }
111
- end
110
+ def name_to_camelcase(name)
111
+ name.gsub(/[[:space:]]+/, '_').downcase.gsub(/(?:^|_)([a-z])/) { $1.upcase }
112
+ end
112
113
 
113
- def step_timing(step)
114
- step[:timing].first ? step[:timing].first[:real] : 0
115
- end
114
+ def step_timing(step)
115
+ step[:timing].first ? step[:timing].first[:real] : 0
116
+ end
116
117
 
117
- def clean_up_infinity
118
- res = results.dup
119
- res[:scenarios].each do |scenario|
120
- scenario[:steps].each do |step|
121
- step[:timing].each do |timing|
122
- timing[:exp] = "Not Specified" if timing.is_a?(Hash) && timing[:exp] && timing[:exp] == Float::INFINITY
118
+ def clean_up_infinity
119
+ res = results.dup
120
+ res[:scenarios].each do |scenario|
121
+ scenario[:steps].each do |step|
122
+ step[:timing].each do |timing|
123
+ timing[:exp] = "Not Specified" if timing.is_a?(Hash) && timing[:exp] && timing[:exp] == Float::INFINITY
124
+ end
123
125
  end
124
126
  end
127
+ res
125
128
  end
126
- res
127
- end
128
129
 
129
- def array_in(array)
130
- list = ''
131
- array.each do |item|
132
- if item.instance_of?(Array)
133
- list << array_in(item)
134
- elsif item.instance_of?(Hash)
135
- list << hash_in(item)
130
+ def array_in(array)
131
+ list = ''
132
+ array.each do |item|
133
+ if item.instance_of?(Array)
134
+ list << array_in(item)
135
+ elsif item.instance_of?(Hash)
136
+ list << hash_in(item)
137
+ end
136
138
  end
139
+ list
137
140
  end
138
- list
139
- end
140
141
 
141
- def hash_in(hash)
142
- list = ''
143
- hash.each do |k, v|
144
- if v.instance_of?(Hash)
145
- list << hash_in(v)
146
- elsif v.instance_of?(Array)
147
- list << array_in(v)
148
- elsif k == :result
149
- list << (v ? '.' : 'E')
142
+ def hash_in(hash)
143
+ list = ''
144
+ hash.each do |k, v|
145
+ if v.instance_of?(Hash)
146
+ list << hash_in(v)
147
+ elsif v.instance_of?(Array)
148
+ list << array_in(v)
149
+ elsif k == :result
150
+ list << (v ? '.' : 'E')
151
+ end
150
152
  end
153
+ list
151
154
  end
152
- list
153
- end
154
155
 
155
- def to_simple
156
- hash_in results
157
- end
156
+ def to_simple
157
+ hash_in results
158
+ end
158
159
 
159
- def md_section_header(header, desc, level)
160
- output = []
161
- output << "#{'#' * level} #{header}"
162
- output << ""
163
- if desc
164
- output << "Desc: #{desc}"
160
+ def md_section_header(header, desc, level)
161
+ output = []
162
+ output << "#{'#' * level} #{header}"
165
163
  output << ""
164
+ if desc
165
+ output << "Desc: #{desc}"
166
+ output << ""
167
+ end
168
+ output
166
169
  end
167
- output
168
- end
169
170
 
170
- def md_step_header(step)
171
- output = []
172
- output.push(*md_section_header(step[:name], step[:desc], 3))
173
- output.push(*md_section_header("Call", nil, 4))
174
- step[:url].each do |url|
175
- output << "* #{url[:desc]}"
176
- end
177
- step[:method].each do |method|
178
- output << "* #{method[:desc]}"
171
+ def md_step_header(step)
172
+ output = []
173
+ output.push(*md_section_header(step[:name], step[:desc], 3))
174
+ output.push(*md_section_header("Call", nil, 4))
175
+ step[:url].each do |url|
176
+ output << "* #{url[:desc]}"
177
+ end
178
+ step[:method].each do |method|
179
+ output << "* #{method[:desc]}"
180
+ end
181
+ output << ""
182
+ output
179
183
  end
180
- output << ""
181
- output
182
- end
183
184
 
184
- def md_section_content(step, section)
185
- output = []
186
- output << md_section_header(SECTION_TRANSLATE[section], nil, 4)
187
- step[section].each do |status|
188
- output << "* `#{status[:result]}`: #{status[:desc]}"
185
+ def md_section_content(step, section)
186
+ output = []
187
+ output << md_section_header(SECTION_TRANSLATE[section], nil, 4)
188
+ step[section].each do |status|
189
+ output << "* `#{status[:result]}`: #{status[:desc]}"
190
+ end
191
+ output << ""
192
+ output
189
193
  end
190
- output << ""
191
- output
192
- end
193
194
 
194
- def to_markdown
195
- output = []
196
- output << md_section_header(results[:name], results[:desc], 1)
197
- results[:scenarios].each do |scenario|
198
- output << md_section_header(scenario[:name], scenario[:desc], 2)
199
- scenario[:steps].each do |step|
200
- output.push(*md_step_header(step))
201
- RESULT_SECTIONS.each do |section|
202
- output.push(*md_section_content(step, section))
195
+ def to_markdown
196
+ output = []
197
+ output << md_section_header(results[:name], results[:desc], 1)
198
+ results[:scenarios].each do |scenario|
199
+ output << md_section_header(scenario[:name], scenario[:desc], 2)
200
+ scenario[:steps].each do |step|
201
+ output.push(*md_step_header(step))
202
+ RESULT_SECTIONS.each do |section|
203
+ output.push(*md_section_content(step, section))
204
+ end
203
205
  end
204
206
  end
207
+ output.join("\n")
205
208
  end
206
- output.join("\n")
207
209
  end
208
-
209
210
  end
@@ -3,7 +3,7 @@ require_relative 'test_step'
3
3
  module ApiMiniTester
4
4
  class TestScenario
5
5
 
6
- attr_accessor :base_uri, :scenario, :data, :results, :name
6
+ attr_reader :base_uri, :scenario, :data, :results, :name
7
7
 
8
8
  def initialize(base_uri, scenario, data)
9
9
  @base_uri = base_uri
@@ -2,8 +2,9 @@ require 'httparty'
2
2
  require 'liquid'
3
3
  require 'hash_parser'
4
4
  require 'uri'
5
- require 'faker'
6
5
  require 'json'
6
+ require_relative 'test_faker_filter'
7
+
7
8
 
8
9
  module ApiMiniTester
9
10
  class TestStep
@@ -16,17 +17,23 @@ module ApiMiniTester
16
17
  attr_reader :results
17
18
 
18
19
  def initialize(base_uri, step, context = nil, data = nil)
20
+ Liquid::Template.register_filter(::TestFakerFilter)
19
21
  @context = context
20
22
  uri_template = Liquid::Template.parse([base_uri, step['uri']].join("/"), error_mode: :strict)
21
23
  @name = step['name']
22
- @uri = uri_template.render({'context' => context, 'data' => data}, { strict_variables: true })
24
+
25
+ @uri = uri_template.render(
26
+ {'context' => context, 'data' => data},
27
+ { strict_variables: true })
23
28
  @method = step['method'].downcase.to_sym
24
29
 
25
30
  input_template = Liquid::Template.parse(step['input'].to_yaml.to_s, error_mode: :strict)
26
- @input = YAML.load(input_template.render({'context' => context, 'data' => data}, { strict_variables: true }))
31
+ @input = YAML.load(
32
+ input_template.render({'context' => context, 'data' => data}, { strict_variables: true }))
27
33
 
28
34
  output_template = Liquid::Template.parse(step['output'].to_yaml.to_s, error_mode: :strict)
29
- @output = YAML.load(output_template.render({'context' => context, 'data' => data}, { strict_variables: true }))
35
+ @output = YAML.load(
36
+ output_template.render({'context' => context, 'data' => data}, { strict_variables: true }))
30
37
 
31
38
  @results = { name: step['name'], desc: step['desc'], status: [], headers: [], body: [], url: [], method: [], timing: [] }
32
39
  end
@@ -154,7 +161,11 @@ module ApiMiniTester
154
161
 
155
162
  def array_diff(a, b, path = nil, section = :body)
156
163
  a.each do |a_item|
157
- if a_item.instance_of?(Hash)
164
+ if b.nil?
165
+ add_result section, { result: false,
166
+ name: "Response boby value: #{[path].join(".")}",
167
+ desc: "Assert #{[path].join(".")} is empty" }
168
+ elsif a_item.instance_of?(Hash)
158
169
  found = false
159
170
  b.each do |b_item|
160
171
  matching = true
@@ -5,7 +5,7 @@ require_relative 'test_formatter'
5
5
  module ApiMiniTester
6
6
  class TestSuite
7
7
 
8
- attr_accessor :base_uri, :scenarios, :data, :results
8
+ attr_reader :base_uri, :scenarios, :data, :results
9
9
 
10
10
  def initialize(suite_def)
11
11
  if suite_def.is_a?(String)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_mini_tester
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jindrich Skupa (@eMan)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-02 00:00:00.000000000 Z
11
+ date: 2018-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: builder
@@ -230,13 +230,14 @@ dependencies:
230
230
  - - ">="
231
231
  - !ruby/object:Gem::Version
232
232
  version: 3.4.2
233
- description: Runs automated REST API based on YAML definiti
233
+ description: Runs automated REST API based on YAML definition
234
234
  email: jindrich.skupa@gmail.com
235
235
  executables: []
236
236
  extensions: []
237
237
  extra_rdoc_files: []
238
238
  files:
239
239
  - lib/api_mini_tester.rb
240
+ - lib/api_mini_tester/test_faker_filter.rb
240
241
  - lib/api_mini_tester/test_formatter.rb
241
242
  - lib/api_mini_tester/test_scenario.rb
242
243
  - lib/api_mini_tester/test_step.rb