Skip to content

Commit

Permalink
refactor test to be faster by doing the indexation only if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed May 6, 2024
1 parent f7eaf98 commit b5360d2
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 36 deletions.
17 changes: 11 additions & 6 deletions test/controllers/test_annotator_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ def self.before_suite
end

LinkedData::SampleData::Ontology.delete_ontologies_and_submissions
@@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies
@@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies(process_submission: true,
process_options: {
process_rdf: true,
extract_metadata: false,
index_search: true
})
annotator = Annotator::Models::NcboAnnotator.new
annotator.init_redis_for_tests()
annotator.create_term_cache_from_ontologies(@@ontologies, false)
Expand Down Expand Up @@ -260,16 +265,16 @@ def test_default_properties_output
assert last_response.ok?
annotations = MultiJson.load(last_response.body)
assert_equal 9, annotations.length
annotations.sort! { |a,b| a["annotatedClass"]["prefLabel"].downcase <=> b["annotatedClass"]["prefLabel"].downcase }
annotations.sort! { |a,b| a["annotatedClass"]["prefLabel"].first.downcase <=> b["annotatedClass"]["prefLabel"].first.downcase }
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Aggregate_Human_Data", annotations.first["annotatedClass"]["@id"]
assert_equal "Aggregate Human Data", annotations.first["annotatedClass"]["prefLabel"]
assert_equal "Aggregate Human Data", Array(annotations.first["annotatedClass"]["prefLabel"]).first

params = {text: text, include: "prefLabel,definition"}
get "/annotator", params
assert last_response.ok?
annotations = MultiJson.load(last_response.body)
assert_equal 9, annotations.length
annotations.sort! { |a,b| a["annotatedClass"]["prefLabel"].downcase <=> b["annotatedClass"]["prefLabel"].downcase }
annotations.sort! { |a,b| Array(a["annotatedClass"]["prefLabel"]).first.downcase <=> Array(b["annotatedClass"]["prefLabel"]).first.downcase }
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Aggregate_Human_Data", annotations.first["annotatedClass"]["@id"]
assert_equal ["A resource that provides data from clinical care that comprises combined data from multiple individual human subjects."], annotations.first["annotatedClass"]["definition"]
end
Expand Down Expand Up @@ -348,15 +353,15 @@ def self.mapping_test_set
classes = []
class_id = terms_a[i]
ont_acr = onts_a[i]
sub = LinkedData::Models::Ontology.find(ont_acr).first.latest_submission
sub = LinkedData::Models::Ontology.find(ont_acr).first.latest_submission(status: :any)
sub.bring(ontology: [:acronym])
c = LinkedData::Models::Class.find(RDF::URI.new(class_id))
.in(sub)
.first
classes << c
class_id = terms_b[i]
ont_acr = onts_b[i]
sub = LinkedData::Models::Ontology.find(ont_acr).first.latest_submission
sub = LinkedData::Models::Ontology.find(ont_acr).first.latest_submission(status: :any)
sub.bring(ontology: [:acronym])
c = LinkedData::Models::Class.find(RDF::URI.new(class_id))
.in(sub)
Expand Down
3 changes: 1 addition & 2 deletions test/controllers/test_batch_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

class TestBatchController < TestCase
def self.before_suite
LinkedData::SampleData::Ontology.delete_ontologies_and_submissions
@@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies
@@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies(process_submission: true)
end

def test_class_batch_one_ontology
Expand Down
4 changes: 3 additions & 1 deletion test/controllers/test_classes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ def self.before_suite
submission_count: 3,
submissions_to_process: [1, 2],
process_submission: true,
random_submission_count: false}
random_submission_count: false,
process_options: {process_rdf: true, extract_metadata: false}
}
return LinkedData::SampleData::Ontology.create_ontologies_and_submissions(options)
end

Expand Down
23 changes: 12 additions & 11 deletions test/controllers/test_metrics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ def self.before_suite
"individuals"=>124,
"properties"=>63,
"maxDepth"=> 7 }
@@options = {ont_count: 2,
submission_count: 3,
submissions_to_process: [1, 2],
process_submission: true,
random_submission_count: false}
@@options = { ont_count: 2,
submission_count: 3,
submissions_to_process: [1, 2],
process_submission: true,
process_options: { process_rdf: true, extract_metadata: false, run_metrics: true, index_properties: true },
random_submission_count: false }
LinkedData::SampleData::Ontology.create_ontologies_and_submissions(@@options)
end

Expand Down Expand Up @@ -78,18 +79,18 @@ def test_metrics_missing
get '/metrics/missing'
assert last_response.ok?
ontologies = MultiJson.load(last_response.body)
assert_equal(0, ontologies.length, msg='Failure to detect 0 ontologies with missing metrics.')
assert_equal(0, ontologies.length, msg = 'Failure to detect 0 ontologies with missing metrics.')
# create ontologies with latest submissions that have no metrics
delete_ontologies_and_submissions
options = {ont_count: 2,
submission_count: 1,
process_submission: false,
random_submission_count: false}
options = { ont_count: 2,
submission_count: 1,
process_submission: false,
random_submission_count: false }
create_ontologies_and_submissions(options)
get '/metrics/missing'
assert last_response.ok?
ontologies = MultiJson.load(last_response.body)
assert_equal(2, ontologies.length, msg='Failure to detect 2 ontologies with missing metrics.')
assert_equal(2, ontologies.length, msg = 'Failure to detect 2 ontologies with missing metrics.')
# recreate the before_suite data (this test might not be the last one to run in the suite)
delete_ontologies_and_submissions
create_ontologies_and_submissions(@@options)
Expand Down
34 changes: 31 additions & 3 deletions test/controllers/test_ontologies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ def test_download_ontology
end

def test_download_ontology_csv
num_onts_created, created_ont_acronyms, onts = create_ontologies_and_submissions(ont_count: 1, submission_count: 1, process_submission: true)
num_onts_created, created_ont_acronyms, onts = create_ontologies_and_submissions(ont_count: 1, submission_count: 1,
process_submission: true,
process_options:{process_rdf: true, extract_metadata: true, index_search: true})
ont = onts.first
acronym = created_ont_acronyms.first

Expand Down Expand Up @@ -220,13 +222,13 @@ def test_download_acl_only
begin
allowed_user = User.new({
username: "allowed",
email: "test@example.org",
email: "test1@example.org",
password: "12345"
})
allowed_user.save
blocked_user = User.new({
username: "blocked",
email: "test@example.org",
email: "test2@example.org",
password: "12345"
})
blocked_user.save
Expand Down Expand Up @@ -296,6 +298,32 @@ def test_on_demand_ontology_pull
end
end

def test_detach_a_view
view = Ontology.find(@@view_acronym).include(:viewOf).first
ont = view.viewOf
refute_nil view
refute_nil ont

remove_view_of = {viewOf: ''}
patch "/ontologies/#{@@view_acronym}", MultiJson.dump(remove_view_of), "CONTENT_TYPE" => "application/json"

assert last_response.status == 204

get "/ontologies/#{@@view_acronym}"
onto = MultiJson.load(last_response.body)
assert_nil onto["viewOf"]


add_view_of = {viewOf: @@acronym}
patch "/ontologies/#{@@view_acronym}", MultiJson.dump(add_view_of), "CONTENT_TYPE" => "application/json"

assert last_response.status == 204

get "/ontologies/#{@@view_acronym}?include=all"
onto = MultiJson.load(last_response.body)
assert_equal onto["viewOf"], ont.id.to_s
end

private

def start_server
Expand Down
16 changes: 12 additions & 4 deletions test/controllers/test_ontology_submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def self._set_vars
administeredBy: "tim",
"file" => Rack::Test::UploadedFile.new(@@test_file, ""),
released: DateTime.now.to_s,
contact: [{name: "test_name", email: "[email protected]"}]
contact: [{name: "test_name", email: "[email protected]"}],
URI: 'https://test.com/test',
status: 'production',
description: 'ontology description'
}
@@status_uploaded = "UPLOADED"
@@status_rdf = "RDF"
Expand All @@ -36,6 +39,12 @@ def self._create_onts
ont.save
end

def setup
delete_ontologies_and_submissions
ont = Ontology.new(acronym: @@acronym, name: @@name, administeredBy: [@@user])
ont.save
end

def test_submissions_for_given_ontology
num_onts_created, created_ont_acronyms = create_ontologies_and_submissions(ont_count: 1)
ontology = created_ont_acronyms.first
Expand Down Expand Up @@ -156,13 +165,13 @@ def test_download_acl_only
begin
allowed_user = User.new({
username: "allowed",
email: "test@example.org",
email: "test4@example.org",
password: "12345"
})
allowed_user.save
blocked_user = User.new({
username: "blocked",
email: "test@example.org",
email: "test5@example.org",
password: "12345"
})
blocked_user.save
Expand Down Expand Up @@ -235,5 +244,4 @@ def test_ontology_submissions_access_controller
del.delete if del
end
end

end
2 changes: 2 additions & 0 deletions test/controllers/test_properties_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class TestPropertiesController < TestCase
def self.before_suite
count, acronyms, bro = LinkedData::SampleData::Ontology.create_ontologies_and_submissions({
process_submission: true,
process_options:{process_rdf: true, extract_metadata: false},
acronym: "BROSEARCHTEST",
name: "BRO Search Test",
file_path: "./test/data/ontology_files/BRO_v3.2.owl",
Expand All @@ -15,6 +16,7 @@ def self.before_suite

count, acronyms, mccl = LinkedData::SampleData::Ontology.create_ontologies_and_submissions({
process_submission: true,
process_options:{process_rdf: true, extract_metadata: true},
acronym: "MCCLSEARCHTEST",
name: "MCCL Search Test",
file_path: "./test/data/ontology_files/CellLine_OWL_BioPortal_v1.0.owl",
Expand Down
2 changes: 2 additions & 0 deletions test/controllers/test_properties_search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class TestPropertiesSearchController < TestCase
def self.before_suite
count, acronyms, bro = LinkedData::SampleData::Ontology.create_ontologies_and_submissions({
process_submission: true,
process_options:{process_rdf: true, extract_metadata: false, index_properties: true},
acronym: "BROSEARCHTEST",
name: "BRO Search Test",
file_path: "./test/data/ontology_files/BRO_v3.2.owl",
Expand All @@ -15,6 +16,7 @@ def self.before_suite

count, acronyms, mccl = LinkedData::SampleData::Ontology.create_ontologies_and_submissions({
process_submission: true,
process_options:{process_rdf: true, extract_metadata: false, index_properties: true},
acronym: "MCCLSEARCHTEST",
name: "MCCL Search Test",
file_path: "./test/data/ontology_files/CellLine_OWL_BioPortal_v1.0.owl",
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/test_recommender_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.before_suite
@@redis.del(mappings)
end
LinkedData::SampleData::Ontology.delete_ontologies_and_submissions
@@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies
@@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies(process_submission: true)
annotator = Annotator::Models::NcboAnnotator.new
annotator.init_redis_for_tests()
annotator.create_term_cache_from_ontologies(@@ontologies, false)
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/test_recommender_v1_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require_relative '../test_case'

class TestRecommenderController < TestCase
class TestRecommenderV1Controller < TestCase

def self.before_suite
LinkedData::SampleData::Ontology.delete_ontologies_and_submissions
@@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies
@@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies(process_submission: true)
@@text = <<eos
Ginsenosides chemistry, biosynthesis, analysis, and potential health effects in software concept or data." "Ginsenosides are a special group of triterpenoid saponins that can be classified into two groups by the skeleton of their aglycones, namely dammarane- and oleanane-type. Ginsenosides are found nearly exclusively in Panax species (ginseng) and up to now more than 150 naturally occurring ginsenosides have been isolated from roots, leaves/stems, fruits, and/or flower heads of ginseng. The same concept indicates Ginsenosides have been the target of a lot of research as they are believed to be the main active principles behind the claims of ginsengs efficacy. The potential health effects of ginsenosides that are discussed in this chapter include anticarcinogenic, immunomodulatory, anti-inflammatory, antiallergic, antiatherosclerotic, antihypertensive, and antidiabetic effects as well as antistress activity and effects on the central nervous system. Ginsensoides can be metabolized in the stomach (acid hydrolysis) and in the gastrointestinal tract (bacterial hydrolysis) or transformed to other ginsenosides by drying and steaming of ginseng to more bioavailable and bioactive ginsenosides. The metabolization and transformation of intact ginsenosides, which seems to play an important role for their potential health effects, are discussed. Qualitative and quantitative analytical techniques for the analysis of ginsenosides are important in relation to quality control of ginseng products and plant material and for the determination of the effects of processing of plant material as well as for the determination of the metabolism and bioavailability of ginsenosides. Analytical techniques for the analysis of ginsenosides that are described in this chapter are thin-layer chromatography (TLC), high-performance liquid chromatography (HPLC) combined with various detectors, gas chromatography (GC), colorimetry, enzyme immunoassays (EIA), capillary electrophoresis (CE), nuclear magnetic resonance (NMR) spectroscopy, and spectrophotometric methods.
eos
Expand Down
12 changes: 6 additions & 6 deletions test/middleware/test_rack_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def self.before_suite
LinkedData::OntologiesAPI.settings.req_per_second_per_ip = 1
LinkedData::OntologiesAPI.settings.safe_ips = Set.new(["1.2.3.4", "1.2.3.5"])

@@user = LinkedData::Models::User.new({username: "user", password: "test_password", email: "test_email@example.org"})
@@user = LinkedData::Models::User.new({username: "user", password: "test_password", email: "test_email1@example.org"})
@@user.save

@@bp_user = LinkedData::Models::User.new({username: "ncbobioportal", password: "test_password", email: "test_email@example.org"})
@@bp_user = LinkedData::Models::User.new({username: "ncbobioportal", password: "test_password", email: "test_email2@example.org"})
@@bp_user.save

admin_role = LinkedData::Models::Users::Role.find("ADMINISTRATOR").first
@@admin = LinkedData::Models::User.new({username: "admin", password: "test_password", email: "test_email@example.org", role: [admin_role]})
@@admin = LinkedData::Models::User.new({username: "admin", password: "test_password", email: "test_email3@example.org", role: [admin_role]})
@@admin.save

# Redirect output or we get a bunch of noise from Rack (gets reset in the after_suite method).
Expand All @@ -34,8 +34,8 @@ def self.before_suite
$stdout = File.open("/dev/null", "w")
$stderr = File.open("/dev/null", "w")

# http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic.2C_private_or_ephemeral_ports
@@port1 = Random.rand(55000..65535)

@@port1 = self.new('').unused_port

# Fork the process to create two servers. This isolates the Rack::Attack configuration, which makes other tests fail if included.
@@pid1 = fork do
Expand All @@ -47,7 +47,7 @@ def self.before_suite
Signal.trap("HUP") { Process.exit! }
end

@@port2 = Random.rand(55000..65535) # http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic.2C_private_or_ephemeral_ports
@@port2 = self.new('').unused_port
@@pid2 = fork do
require_relative '../../config/rack_attack'
Rack::Server.start(
Expand Down
25 changes: 25 additions & 0 deletions test/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
require_relative 'test_log_file'
require_relative '../app'
require 'minitest/unit'
require 'webmock/minitest'
MiniTest::Unit.autorun
WebMock.allow_net_connect!
require 'rack/test'
require 'multi_json'
require 'oj'
Expand Down Expand Up @@ -144,6 +146,9 @@ def app
# @option options [TrueClass, FalseClass] :random_submission_count Use a random number of submissions between 1 and :submission_count
# @option options [TrueClass, FalseClass] :process_submission Parse the test ontology file
def create_ontologies_and_submissions(options = {})
if options[:process_submission] && options[:process_options].nil?
options[:process_options] = { process_rdf: true, extract_metadata: false, generate_missing_labels: false }
end
LinkedData::SampleData::Ontology.create_ontologies_and_submissions(options)
end

Expand Down Expand Up @@ -214,4 +219,24 @@ def self.reset_to_not_admin(user)
user.save
end

def unused_port
max_retries = 5
retries = 0
server_port = Random.rand(55000..65535)
while port_in_use?(server_port)
retries += 1
break if retries >= max_retries
server_port = Random.rand(55000..65535)
end
server_port
end
private
def port_in_use?(port)
server = TCPServer.new(port)
server.close
false
rescue Errno::EADDRINUSE
true
end

end

0 comments on commit b5360d2

Please sign in to comment.