heroics 0.0.17 → 0.0.18

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.
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'helper'
3
-
4
- class RubyNameTest < MiniTest::Unit::TestCase
5
- # ruby_name is a no-op when an empty string is provided.
6
- def test_ruby_name_with_empty_name
7
- assert_equal('', Heroics.ruby_name(''))
8
- end
9
-
10
- # ruby_name converts capitals in a name to lowercase.
11
- def test_ruby_name_with_capitals
12
- assert_equal('capitalizedname', Heroics.ruby_name('CapitalizedName'))
13
- end
14
-
15
- # ruby_name converts dashes in a name to underscores.
16
- def test_ruby_name_with_dashes
17
- assert_equal('dashed_name', Heroics.ruby_name('dashed-name'))
18
- end
19
-
20
- # ruby_name converts spaces in a name to underscores.
21
- def test_ruby_name_with_spaces
22
- assert_equal('spaced_name', Heroics.ruby_name('spaced name'))
23
- end
24
- end
25
-
26
- class PrettyNameTest < MiniTest::Unit::TestCase
27
- # pretty_name is a no-op when an empty string is provided.
28
- def test_pretty_name_with_empty_name
29
- assert_equal('', Heroics.pretty_name(''))
30
- end
31
-
32
- # pretty_name converts capitals in a name to lowercase.
33
- def test_pretty_name_with_capitals
34
- assert_equal('capitalizedname', Heroics.pretty_name('CapitalizedName'))
35
- end
36
-
37
- # pretty_name converts underscores in a name to dashes.
38
- def test_pretty_name_with_underscores
39
- assert_equal('dashed-name', Heroics.pretty_name('dashed_name'))
40
- end
41
-
42
- # pretty_name converts spaces in a name to underscores.
43
- def test_pretty_name_with_spaces
44
- assert_equal('spaced-name', Heroics.pretty_name('spaced name'))
45
- end
46
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'helper'
3
-
4
- class ResourceTest < MiniTest::Unit::TestCase
5
- include ExconHelper
6
-
7
- # Resource.<link> raises a NoMethodError when a method is invoked without a
8
- # matching link.
9
- def test_invalid_link
10
- resource = Heroics::Resource.new({})
11
- error = assert_raises NoMethodError do
12
- resource.unknown
13
- end
14
- assert_match(
15
- /undefined method `unknown' for #<Heroics::Resource:0x.*>/,
16
- error.message)
17
- end
18
-
19
- # Resource.<link> finds the appropriate link and invokes it.
20
- def test_link
21
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
22
- link = Heroics::Link.new('https://username:secret@example.com',
23
- schema.resource('resource').link('list'))
24
- resource = Heroics::Resource.new({'link' => link})
25
- Excon.stub(method: :get) do |request|
26
- assert_equal('Basic dXNlcm5hbWU6c2VjcmV0',
27
- request[:headers]['Authorization'])
28
- assert_equal('example.com', request[:host])
29
- assert_equal(443, request[:port])
30
- assert_equal('/resource', request[:path])
31
- Excon.stubs.pop
32
- {status: 200, body: 'Hello, world!'}
33
- end
34
- assert_equal('Hello, world!', resource.link)
35
- end
36
- end
@@ -1,274 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'helper'
3
-
4
- class SchemaTest < MiniTest::Unit::TestCase
5
- # Schema.to_s returns a simple human-readable description of the schema
6
- # instance with the description embedded in it.
7
- def test_to_s
8
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
9
- assert_equal(
10
- '#<Heroics::Schema description="Sample schema for use in tests.">',
11
- schema.to_s)
12
- end
13
-
14
- # Schema.resource returns a ResourceSchema for the named resource.
15
- def test_resource
16
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
17
- assert_equal('resource', schema.resource('resource').name)
18
- end
19
-
20
- # Schema.resource raises a SchemaError is an unknown resource is requested.
21
- def test_resource_with_unknown_name
22
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
23
- error = assert_raises Heroics::SchemaError do
24
- schema.resource('unknown-resource')
25
- end
26
- assert_equal("Unknown resource 'unknown-resource'.", error.message)
27
- end
28
-
29
- # Schema.resources returns a sequence of ResourceSchema children.
30
- def test_resources
31
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
32
- assert_equal(['resource', 'another-resource', 'underscored_resource'],
33
- schema.resources.map(&:name))
34
- end
35
- end
36
-
37
- class ResourceSchemaTest < MiniTest::Unit::TestCase
38
- # ResourceSchema.link returns a LinkSchema for the named link.
39
- def test_link
40
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
41
- link = schema.resource('resource').link('list')
42
- assert_equal('list', link.name)
43
- end
44
-
45
- # ResourceSchema.link raises a SchemaError is an unknown link is requested.
46
- def test_link_with_unknown_name
47
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
48
- error = assert_raises Heroics::SchemaError do
49
- schema.resource('resource').link('unknown-link')
50
- end
51
- assert_equal("Unknown link 'unknown-link'.", error.message)
52
- end
53
-
54
- # ResourceSchema.links returns an array of LinkSchema children.
55
- def test_links
56
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
57
- assert_equal(
58
- ['list', 'info', 'identify_resource', 'create', 'submit', 'update', 'delete'],
59
- schema.resource('resource').links.map { |link| link.name })
60
- end
61
- end
62
-
63
- class LinkSchemaTest < MiniTest::Unit::TestCase
64
- # LinkSchema.name returns the sanitized link name.
65
- def test_name
66
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
67
- assert_equal('list', schema.resource('resource').link('list').name)
68
- end
69
-
70
- # LinkSchema.resource_name returns the parent resource name.
71
- def test_resource_name
72
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
73
- assert_equal('resource',
74
- schema.resource('resource').link('list').resource_name)
75
- end
76
-
77
- # LinkSchema.description returns the link description.
78
- def test_description
79
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
80
- assert_equal('Show all sample resources',
81
- schema.resource('resource').link('list').description)
82
- end
83
-
84
- # LinkSchema.parameters returns an empty array if the link doesn't require
85
- # parameters.
86
- def test_parameters_without_parameters
87
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
88
- link = schema.resource('resource').link('list')
89
- assert_equal([], link.parameters)
90
- end
91
-
92
- # LinkSchema.parameters returns an array of named parameter required to
93
- # invoke the link correctly.
94
- def test_parameters
95
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
96
- link = schema.resource('resource').link('info')
97
- assert_equal(['uuid_field'], link.parameters)
98
- end
99
-
100
- # LinkSchema.parameters returns a parameter name for multiple parameters
101
- # when the parameter contains a 'oneOf' element that references more than
102
- # one parameter.
103
- def test_parameters_with_one_of_field
104
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
105
- link = schema.resource('resource').link('identify_resource')
106
- assert_equal(['uuid_field|email_field'], link.parameters)
107
- end
108
-
109
- # LinkSchema.parameter_details returns an empty array if the link doesn't
110
- # require parameters.
111
- def test_parameter_details_without_parameters
112
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
113
- link = schema.resource('resource').link('list')
114
- assert_equal([], link.parameter_details)
115
- end
116
-
117
- # LinkSchema.parameter_details returns an array of Parameter with information
118
- # about the parameters accepted by the link.
119
- def test_parameter_details
120
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
121
- link = schema.resource('resource').link('info')
122
- parameters = link.parameter_details
123
- assert_equal(1, parameters.length)
124
- parameter = parameters[0]
125
- assert_equal('resource_uuid_field', parameter.name)
126
- assert_equal('A sample UUID field', parameter.description)
127
- end
128
-
129
- # LinkSchema.parameter_details returns an array of ParameterChoices, when
130
- # more than one value may be used with a property, with information about
131
- # the parameters accepted by the link.
132
- def test_parameter_details_with_one_of_field
133
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
134
- link = schema.resource('resource').link('identify_resource')
135
- parameters = link.parameter_details
136
- assert_equal(1, parameters.length)
137
- parameter = parameters[0]
138
- assert_equal('resource_uuid_field_or_resource_email_field', parameter.name)
139
- assert_equal('A sample UUID field or A sample email address field',
140
- parameter.description)
141
- end
142
-
143
- # LinkSchema.body returns nil if the link doesn't accept a request body.
144
- def test_example_body_without_body
145
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
146
- link = schema.resource('resource').link('info')
147
- assert_equal(nil, link.example_body)
148
- end
149
-
150
- # LinkSchema.body returns a sample body generated from the properties and
151
- # embedded examples.
152
- def test_example_body
153
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
154
- link = schema.resource('resource').link('create')
155
- assert_equal({'date_field' => '2013-10-19 22:10:29Z',
156
- 'string_field' => 'Sample text.',
157
- 'boolean_field' => true,
158
- 'uuid_field' => '44724831-bf66-4bc2-865f-e2c4c2b14c78',
159
- 'email_field' => 'username@example.com'},
160
- link.example_body)
161
- end
162
-
163
- # LinkSchema.format_path converts an array of parameters into a path.
164
- def test_format_path
165
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
166
- link = schema.resource('resource').link('info')
167
- assert_equal(['/resource/44724831-bf66-4bc2-865f-e2c4c2b14c78', nil],
168
- link.format_path(['44724831-bf66-4bc2-865f-e2c4c2b14c78']))
169
- end
170
-
171
- # LinkSchema.format_path escapes special URL characters in parameters.
172
- def test_format_path_with_illegal_literals
173
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
174
- link = schema.resource('resource').link('info')
175
- assert_equal(['/resource/foobar%25', nil],
176
- link.format_path(['foobar%']))
177
- end
178
-
179
- # LinkSchema.format_path correctly returns a parameter as a body if a path
180
- # doesn't have any parameters.
181
- def test_format_path_with_body
182
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
183
- link = schema.resource('resource').link('create')
184
- assert_equal(['/resource', {'new' => 'resource'}],
185
- link.format_path([{'new' => 'resource'}]))
186
- end
187
-
188
- # LinkSchema.format_path correctly returns a parameter as a body if a path
189
- # doesn't have any parameters.
190
- def test_format_path_with_path_and_body
191
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
192
- link = schema.resource('resource').link('update')
193
- assert_equal(['/resource/44724831-bf66-4bc2-865f-e2c4c2b14c78',
194
- {'new' => 'resource'}],
195
- link.format_path(['44724831-bf66-4bc2-865f-e2c4c2b14c78',
196
- {'new' => 'resource'}]))
197
- end
198
-
199
- # LinkSchema.format_path raises an ArgumentError if too few parameters are
200
- # provided
201
- def test_format_path_with_too_few_parameters
202
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
203
- link = schema.resource('resource').link('info')
204
- error = assert_raises ArgumentError do
205
- link.format_path([])
206
- end
207
- assert_equal('wrong number of arguments (0 for 1)', error.message)
208
- end
209
-
210
- # LinkSchema.format_path raises an ArgumentError if too many parameters are
211
- # provided
212
- def test_format_path_with_too_many_parameters
213
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
214
- link = schema.resource('resource').link('info')
215
- error = assert_raises ArgumentError do
216
- link.format_path(['too', 'many', 'parameters'])
217
- end
218
- assert_equal('wrong number of arguments (3 for 1)', error.message)
219
- end
220
-
221
- # LinkSchema.pretty_resource_name returns the resource name in a pretty
222
- # form, with underscores converted to dashes.
223
- def test_pretty_resource_name
224
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
225
- link = schema.resource('another-resource').link('list')
226
- assert_equal('another-resource', link.pretty_resource_name)
227
- end
228
-
229
- # LinkSchema.pretty_name returns the link name in a pretty form, with
230
- # underscores converted to dashes.
231
- def test_pretty_resource_name
232
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
233
- link = schema.resource('resource').link('identify_resource')
234
- assert_equal('identify-resource', link.pretty_name)
235
- end
236
-
237
- # LinkSchema.content_type returns the media type associated with this
238
- # resource.
239
- def test_content_type
240
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
241
- link = schema.resource('resource').link('submit')
242
- assert_equal('application/x-www-form-urlencoded', link.content_type)
243
- end
244
-
245
- # The content type should default to application/json
246
- def test_default_content_type
247
- schema = Heroics::Schema.new(SAMPLE_SCHEMA)
248
- link = schema.resource('resource').link('identify_resource')
249
- assert_equal('application/json', link.content_type)
250
- end
251
- end
252
-
253
- class DownloadSchemaTest < MiniTest::Unit::TestCase
254
- include ExconHelper
255
-
256
- # download_schema makes a request to fetch the schema, decodes the
257
- # downloaded JSON and returns a Ruby hash.
258
- def test_download_schema
259
- Excon.stub(method: :get) do |request|
260
- assert_equal('example.com', request[:host])
261
- assert_equal('/schema', request[:path])
262
- assert_equal('application/vnd.heroku+json; version=3',
263
- request[:headers]['Accept'])
264
- Excon.stubs.pop
265
- {status: 200, headers: {'Content-Type' => 'application/json'},
266
- body: MultiJson.dump(SAMPLE_SCHEMA)}
267
- end
268
-
269
- schema = Heroics::download_schema(
270
- 'https://username:token@example.com/schema',
271
- default_headers: {'Accept' => 'application/vnd.heroku+json; version=3'})
272
- assert_equal(SAMPLE_SCHEMA, schema.schema)
273
- end
274
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'helper'
3
-
4
- class VersionTest < MiniTest::Unit::TestCase
5
- # Heroics::VERSION defines the version for the project in MAJOR.MINOR.PATCH
6
- # format.
7
- def test_version
8
- assert_match(/\d+.\d+.\d+/, Heroics::VERSION)
9
- end
10
- end