Skip to content

Commit

Permalink
make use of assert_nothing_raised
Browse files Browse the repository at this point in the history
  • Loading branch information
stuzart committed Jan 31, 2025
1 parent 2d5c207 commit d051222
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 42 deletions.
14 changes: 7 additions & 7 deletions test/api_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def api_get_test(template, res)
get member_url(res), as: :json, headers: { 'Authorization' => read_access_auth }
assert_response :success

assert validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response")
assert_nothing_raised do
validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response")
end

expected = template
actual = JSON.parse(response.body)
Expand All @@ -48,15 +50,15 @@ def api_get_test(template, res)

def api_post_test(template)
expected = template
assert validate_json(template.to_json, "#/components/schemas/#{singular_name.camelize(:lower)}Post")
assert_nothing_raised { validate_json(template.to_json, "#/components/schemas/#{singular_name.camelize(:lower)}Post") }

# debug note: responds with redirect 302 if not really logged in.. could happen if database resets and has no users
assert_difference(-> { model.count }, 1) do
post collection_url, params: template, as: :json, headers: { 'Authorization' => write_access_auth }
assert_response :success
end

assert validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response")
assert_nothing_raised { validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response") }

actual = JSON.parse(response.body)

Expand All @@ -80,14 +82,14 @@ def api_patch_test(resource, template)
assert_response :success
expected = JSON.parse(response.body)

assert validate_json(template.to_json, "#/components/schemas/#{singular_name.camelize(:lower)}Patch")
assert_nothing_raised { validate_json(template.to_json, "#/components/schemas/#{singular_name.camelize(:lower)}Patch") }

assert_no_difference(-> { model.count }) do
patch member_url(resource), params: template, as: :json, headers: { 'Authorization' => write_access_auth }
assert_response :success
end

assert validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response")
assert_nothing_raised { validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response") }

actual = JSON.parse(response.body)

Expand Down Expand Up @@ -212,11 +214,9 @@ def validate_json(json, fragment = nil)
opts[:fragment] = fragment if fragment
errors = JSON::Validator.fully_validate_json(definitions_path, json, opts)
raise Minitest::Assertion, errors.join("\n") unless errors.empty?
return true
rescue JSON::Schema::SchemaError => e
if e.message.start_with?("Invalid fragment resolution for :fragment option")
warn "#{fragment} is missing from API spec, skipping validation"
return true
else
raise e
end
Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/assay_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setup
assert_no_difference('Assay.count') do
delete member_url(a), headers: { 'Authorization' => write_access_auth }
assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/integration/api/data_file_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def setup
}

assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
blob = df.content_blob.reload
assert_nil blob.md5sum
assert blob.no_content?
Expand All @@ -91,7 +91,7 @@ def setup
}

assert_response :bad_request
assert validate_json(response.body, '#/components/schemas/badRequestResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/badRequestResponse') }
blob = df.content_blob.reload
assert_equal original_md5, blob.md5sum
assert blob.file_size > 0
Expand All @@ -113,7 +113,7 @@ def setup
assert_no_difference(-> { model.count }) do
post collection_url, params: to_post, headers: { 'Authorization' => write_access_auth }
assert_response :unprocessable_entity
assert validate_json(response.body, '#/components/schemas/unprocessableEntityResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
end

h = JSON.parse(response.body)
Expand All @@ -137,7 +137,7 @@ def setup
assert_no_difference(-> { model.count }) do
post collection_url, params: to_post, as: :json, headers: { 'Authorization' => write_access_auth }
assert_response :unprocessable_entity
assert validate_json(response.body, '#/components/schemas/unprocessableEntityResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/investigation_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setup
assert_no_difference('Investigation.count') do
delete member_url(inv), headers: { 'Authorization' => write_access_auth }
assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/integration/api/model_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def setup
}

assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
blob = pdf_blob.reload
assert_nil blob.md5sum
assert blob.no_content?
Expand All @@ -97,7 +97,7 @@ def setup
}

assert_response :bad_request
assert validate_json(response.body, '#/components/schemas/badRequestResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/badRequestResponse') }
blob = pdf_blob.reload
assert_equal original_md5, blob.md5sum
assert blob.file_size > 0
Expand All @@ -122,7 +122,7 @@ def setup
assert_no_difference(-> { model.count }) do
post collection_url, params: to_post, headers: { 'Authorization' => write_access_auth }
assert_response :unprocessable_entity
assert validate_json(response.body, '#/components/schemas/unprocessableEntityResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
end

h = JSON.parse(response.body)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/api/presentation_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setup
}

assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
blob = pres.content_blob.reload
assert_nil blob.md5sum
assert blob.no_content?
Expand All @@ -70,7 +70,7 @@ def setup
}

assert_response :bad_request
assert validate_json(response.body, '#/components/schemas/badRequestResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/badRequestResponse') }
blob = pres.content_blob.reload
assert_equal original_md5, blob.md5sum
assert blob.file_size > 0
Expand Down
4 changes: 2 additions & 2 deletions test/integration/api/programme_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setup
assert_no_difference('Programme.count', -1) do
delete member_url(prog), headers: { 'Authorization' => write_access_auth }
assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end

#no projects ==> can delete
Expand All @@ -62,6 +62,6 @@ def setup

get member_url(prog), headers: { 'Authorization' => read_access_auth }
assert_response :not_found
assert validate_json(response.body, '#/components/schemas/notFoundResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/notFoundResponse') }
end
end
6 changes: 3 additions & 3 deletions test/integration/api/read_api_test_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def index_response_fragment
assert_response :not_implemented
else
perform_jsonapi_checks
assert validate_json(response.body, index_response_fragment)
assert_nothing_raised { validate_json(response.body, index_response_fragment) }
end
end

Expand All @@ -64,13 +64,13 @@ def index_response_fragment
user_login(FactoryBot.create(:person))
get member_url(res), headers: { 'Authorization' => read_access_auth }
assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end

test 'getting resource with non-existent ID should throw error' do
get member_url(MissingItem.new(model)), headers: { 'Authorization' => read_access_auth }
assert_response :not_found
assert validate_json(response.body, '#/components/schemas/notFoundResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/notFoundResponse') }
end

test 'write show example' do
Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/sop_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def setup

parsed_policy = JSON.parse(@response.body)['data']['attributes']['policy']

assert validate_json(parsed_policy.to_json, "#/components/schemas/policy")
assert_nothing_raised { validate_json(parsed_policy.to_json, "#/components/schemas/policy") }

to_patch = {
data: {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/study_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def setup
assert_no_difference('Study.count') do
delete member_url(study), headers: { 'Authorization' => write_access_auth }
assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/workflow_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setup
post '/workflows.json', params: template, as: :json, headers: { 'Authorization' => write_access_auth }
assert_response :success

assert validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response")
assert_nothing_raised { validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response") }
res = JSON.parse(response.body)
tools = res['data']['attributes']['tools']
assert_equal 3, tools.length
Expand Down
18 changes: 9 additions & 9 deletions test/integration/api/write_api_test_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def ignored_attributes
get member_url(res)

assert_response :not_found
assert validate_json(response.body, '#/components/schemas/notFoundResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/notFoundResponse') }
end

test 'unauthorized user cannot update resource' do
Expand All @@ -48,7 +48,7 @@ def ignored_attributes
patch member_url(res), params: body, headers: { 'Authorization' => write_access_auth }

assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end

test 'unauthorized user cannot delete resource' do
Expand All @@ -58,7 +58,7 @@ def ignored_attributes
delete member_url(res), headers: { 'Authorization' => write_access_auth }

assert_response :forbidden
assert validate_json(response.body, '#/components/schemas/forbiddenResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end
end

Expand All @@ -70,7 +70,7 @@ def ignored_attributes
post collection_url, params: body, headers: { 'Authorization' => write_access_auth }, as: :json

assert_response :unprocessable_entity
assert validate_json(response.body, '#/components/schemas/unprocessableEntityResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match 'A POST request is not allowed to specify an id', response.body
end
end
Expand All @@ -82,7 +82,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
post collection_url, params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
assert validate_json(response.body, '#/components/schemas/unprocessableEntityResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "The specified data:type does not match the URL's object (#{body['data']['type']} vs. #{plural_name})", response.body
end
end
Expand All @@ -94,7 +94,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
post collection_url, params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
assert validate_json(response.body, '#/components/schemas/unprocessableEntityResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "A POST/PUT request must specify a data:type", response.body
end
end
Expand All @@ -105,7 +105,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
put member_url(resource), params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
assert validate_json(response.body, '#/components/schemas/unprocessableEntityResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "id specified by the PUT request does not match object-id in the JSON input", response.body
end
end
Expand All @@ -117,7 +117,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
put member_url(resource), params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
assert validate_json(response.body, '#/components/schemas/unprocessableEntityResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "The specified data:type does not match the URL's object (#{body['data']['type']} vs. #{plural_name})", response.body
end
end
Expand All @@ -129,7 +129,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
put member_url(resource), params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
assert validate_json(response.body, '#/components/schemas/unprocessableEntityResponse')
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "A POST/PUT request must specify a data:type", response.body
end
end
Expand Down
4 changes: 3 additions & 1 deletion test/unit/api_examples_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class ApiExamplesTest < ActiveSupport::TestCase
fragment = fragment.sub('PatchResponse', 'Response')
fragment = fragment.sub(/[a-zA-Z]+sResponse|peopleResponse/, 'indexResponse') unless fragment.end_with?('sampleAttributeTypesResponse')
define_method("test_#{item.sub('.', '_')}") do
assert validate_json(example.to_json, fragment)
assert_nothing_raised do
validate_json(example.to_json, fragment)
end
end
end
end
8 changes: 6 additions & 2 deletions test/unit/exception_forwarder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ class ExceptionForwarderTest < ActiveSupport::TestCase
test 'send notification' do
with_config_value(:email_enabled, true) do
with_config_value(:exception_notification_enabled, true) do
Seek::Errors::ExceptionForwarder.send_notification(StandardError.new('test'),data:{})
assert true # no errors thrown. Not possible to test if emails sent, as they are disable except in production

# no errors thrown. Not possible to test if emails sent, as they are disable except in production
assert_nothing_raised do
Seek::Errors::ExceptionForwarder.send_notification(StandardError.new('test'),data:{})
end

end
end
end
Expand Down
5 changes: 3 additions & 2 deletions test/unit/jobs/reindex_all_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ class ReindexAllJobTest < ActiveSupport::TestCase
# simple sanity check, to catch interface or gem change bugs
test 'perform' do
FactoryBot.create(:person)
ReindexAllJob.new('Person').perform_now
assert true
assert_nothing_raised do
ReindexAllJob.new('Person').perform_now
end
end

end
3 changes: 1 addition & 2 deletions test/unit/ontologies/ontology_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ def default_parent_class_uri
reader = SubOntologyReader.instance
begin
reader.class_hierarchy
assert true
rescue NotImplementedError => error
assert !error.nil?
assert error.present?
end
end
end

0 comments on commit d051222

Please sign in to comment.