diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index e28dcd0..569b9bd 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -19,7 +19,7 @@ jobs: strategy: fail-fast: false matrix: - ruby-version: ['2.7', '3.0'] + ruby-version: ['3.0', '3.1'] steps: - uses: actions/checkout@v3 - name: Set up Ruby @@ -34,4 +34,4 @@ jobs: - name: Run tests env: UT_APIKEY: ${{ secrets.UT_APIKEY }} - run: bundle exec rake test + run: bundle exec rake test TESTOPTS="-v" diff --git a/Gemfile b/Gemfile index 1d035b1..cd1e8ba 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,9 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gemspec +gem 'pry' gem 'rake' gem 'rubocop', '~> 1.43' -gem 'pry' -gem 'test-unit' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 9cf0f9d..4cb70a6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,8 +1,8 @@ PATH remote: . specs: - ontologies_api_client (2.2.2) - activesupport (= 6.1.7.3) + ontologies_api_client (2.4.0) + activesupport (= 7.0.8) addressable (~> 2.8) excon faraday @@ -11,74 +11,83 @@ PATH lz4-ruby multi_json oj - spawnling (= 2.1.5) GEM remote: https://rubygems.org/ specs: - activesupport (6.1.7.3) + activesupport (7.0.8) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) - zeitwerk (~> 2.3) - addressable (2.8.4) - public_suffix (>= 2.0.2, < 6.0) + addressable (2.8.7) + public_suffix (>= 2.0.2, < 7.0) ast (2.4.2) + bigdecimal (3.1.8) coderay (1.1.3) - concurrent-ruby (1.2.2) - excon (0.100.0) - faraday (2.7.6) - faraday-net_http (>= 2.0, < 3.1) - ruby2_keywords (>= 0.0.4) + concurrent-ruby (1.3.3) + excon (0.111.0) + faraday (2.10.0) + faraday-net_http (>= 2.0, < 3.2) + logger faraday-excon (2.1.0) excon (>= 0.27.4) faraday (~> 2.0) + faraday-follow_redirects (0.3.0) + faraday (>= 1, < 3) faraday-multipart (1.0.4) multipart-post (~> 2) - faraday-net_http (3.0.2) - i18n (1.14.1) + faraday-net_http (3.1.0) + net-http + i18n (1.14.5) concurrent-ruby (~> 1.0) - json (2.6.3) + json (2.7.2) + language_server-protocol (3.17.0.3) + logger (1.6.0) lz4-ruby (0.3.3) - method_source (1.0.0) - minitest (5.18.1) + method_source (1.1.0) + minitest (5.25.1) + minitest-hooks (1.5.1) + minitest (> 5.3) multi_json (1.15.0) - multipart-post (2.3.0) - oj (3.15.0) - parallel (1.22.1) - parser (3.2.0.0) + multipart-post (2.4.1) + net-http (0.4.1) + uri + oj (3.16.4) + bigdecimal (>= 3.0) + parallel (1.25.1) + parser (3.3.4.0) ast (~> 2.4.1) - power_assert (2.0.3) + racc pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) - public_suffix (5.0.1) + public_suffix (6.0.0) + racc (1.8.0) rainbow (3.1.1) - rake (13.0.6) - regexp_parser (2.6.2) - rexml (3.2.5) - rubocop (1.44.1) + rake (13.2.1) + regexp_parser (2.9.2) + rexml (3.3.2) + strscan + rubocop (1.65.0) json (~> 2.3) + language_server-protocol (>= 3.17.0) parallel (~> 1.10) - parser (>= 3.2.0.0) + parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 1.8, < 3.0) + regexp_parser (>= 2.4, < 3.0) rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.24.1, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.24.1) - parser (>= 3.1.1.0) - ruby-progressbar (1.11.0) - ruby2_keywords (0.0.5) - spawnling (2.1.5) - test-unit (3.5.7) - power_assert + rubocop-ast (1.31.3) + parser (>= 3.3.1.0) + ruby-progressbar (1.13.0) + strscan (3.1.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) - unicode-display_width (2.4.2) - zeitwerk (2.6.8) + unicode-display_width (2.5.0) + uri (0.13.0) PLATFORMS ruby @@ -87,11 +96,13 @@ PLATFORMS x86_64-linux DEPENDENCIES + faraday-follow_redirects (~> 0.3) + minitest (~> 5.25) + minitest-hooks (~> 1.5) ontologies_api_client! pry rake rubocop (~> 1.43) - test-unit BUNDLED WITH - 2.3.22 + 2.4.18 diff --git a/README.md b/README.md index 6c8ce8d..9bc1f70 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ # ontologies_api_client -Models and serializers for ontologies and related artifacts backed by a [4store](https://github.com/4store/4store) -RDF database. This library can be used for interacting with a 4store instance that stores -[BioPortal](https://bioportal.bioontology.org/) ontology data. Models in the library are based on -[Graph Oriented Objects for Ruby (Goo)](https://github.com/ncbo/goo). Serializers support RDF serialization as -Rack Middleware and automatic generation of hypermedia links. +Models and serializers for ontologies and related artifacts backed by an +[AllegroGraph](https://allegrograph.com/products/allegrograph/) or a [4store](https://github.com/4store/4store) +database. This library can be used for interacting with an AllegroGraph or a 4store instance that +stores [BioPortal](https://bioportal.bioontology.org/) ontology data. Models in the library are based on +[Graph Oriented Objects for Ruby (Goo)](https://github.com/ncbo/goo). Serializers support RDF serialization as Rack Middleware and automatic +generation of hypermedia links. ## Install @@ -152,7 +153,7 @@ For questions please email [support@bioontology.org](support@bioontology.org.) ## License -Copyright (c) 2021, The Board of Trustees of Leland Stanford Junior University All rights reserved. +Copyright (c) 2024, The Board of Trustees of Leland Stanford Junior University All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/config/config.test.rb b/config/config.test.rb index af6837b..1a9395f 100644 --- a/config/config.test.rb +++ b/config/config.test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # config.rb is required for testing # unit test makes calls to bioportal api so it needs a valid API key which can # be set via ENV variable UT_APIKEY @@ -5,9 +7,11 @@ abort('UT_APIKEY env variable is set to an empty value. Canceling tests') unless ENV['UT_APIKEY'].size > 5 LinkedData::Client.config do |config| - config.rest_url = 'https://data.bioontology.org' - config.apikey = ENV['UT_APIKEY'] -# config.apikey = 'xxxxx-xxxxx-xxxxxxxxxx' - config.links_attr = 'links' - config.cache = false + config.rest_url = 'https://data.bioontology.org' + config.apikey = ENV['UT_APIKEY'] + # config.apikey = 'xxxxx-xxxxx-xxxxxxxxxx' + config.links_attr = 'links' + config.purl_host = 'purl.bioontology.org' + config.purl_prefix = 'https://purl.bioontology.org/ontology' + config.cache = false end diff --git a/lib/ontologies_api_client.rb b/lib/ontologies_api_client.rb index 739639a..a08dbb1 100644 --- a/lib/ontologies_api_client.rb +++ b/lib/ontologies_api_client.rb @@ -1,6 +1,5 @@ require 'oj' require 'multi_json' -require 'spawnling' require_relative 'ontologies_api_client/config' require_relative 'ontologies_api_client/http' @@ -9,6 +8,7 @@ require_relative 'ontologies_api_client/collection' require_relative 'ontologies_api_client/read_write' require_relative 'ontologies_api_client/analytics' +require_relative 'ontologies_api_client/version' # Models curr_dir = File.expand_path("../ontologies_api_client", __FILE__) diff --git a/lib/ontologies_api_client/base.rb b/lib/ontologies_api_client/base.rb index d764d7e..ff4aba6 100644 --- a/lib/ontologies_api_client/base.rb +++ b/lib/ontologies_api_client/base.rb @@ -6,7 +6,15 @@ class Base attr_accessor :context, :links class << self - attr_accessor :media_type, :act_as_media_type, :include_attrs, :include_attrs_full, :attrs_always_present + attr_accessor :act_as_media_type, :include_attrs, :include_attrs_full, :attrs_always_present + def media_types + Array(@media_type) + end + + def media_type + media_types.first + end + end ## @@ -31,8 +39,11 @@ def self.map_classes map = {} classes = LinkedData::Client::Models.constants.map {|c| LinkedData::Client::Models.const_get(c)} classes.each do |media_type_cls| - next if map[media_type_cls] || !media_type_cls.respond_to?(:media_type) || !media_type_cls.ancestors.include?(LinkedData::Client::Base) - map[media_type_cls.media_type] = media_type_cls + next if map[media_type_cls] || !media_type_cls.respond_to?(:media_types) || !media_type_cls.ancestors.include?(LinkedData::Client::Base) + media_type_cls.media_types.each do |type| + next if map[type] + map[type] = media_type_cls + end media_type_cls.act_as_media_type.each {|mt| map[mt] = media_type_cls} if media_type_cls.act_as_media_type end return map diff --git a/lib/ontologies_api_client/collection.rb b/lib/ontologies_api_client/collection.rb index 97ccc0f..f0bb0f2 100644 --- a/lib/ontologies_api_client/collection.rb +++ b/lib/ontologies_api_client/collection.rb @@ -25,7 +25,7 @@ def method_missing(meth, *args, &block) ## # Get all top-level links for the API def top_level_links - HTTP.get(LinkedData::Client.settings.rest_url) + @top_level_links ||= HTTP.get(LinkedData::Client.settings.rest_url) end ## @@ -73,18 +73,18 @@ def where(params = {}, &block) end end - ## # Find a resource by id + # + # @deprecated Use {#get} instead def find(id, params = {}) - found = where do |obj| - obj.id.eql?(id) - end - found.first + get(id, params) end ## # Get a resource by id (this will retrieve it from the REST service) def get(id, params = {}) + path = collection_path + id = "#{path}/#{id}" unless id.include?(path) HTTP.get(id, params) end diff --git a/lib/ontologies_api_client/config.rb b/lib/ontologies_api_client/config.rb index 25805a8..a08a8ec 100644 --- a/lib/ontologies_api_client/config.rb +++ b/lib/ontologies_api_client/config.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'ostruct' require 'faraday' require 'faraday/excon' @@ -16,27 +18,30 @@ module Client def config(&block) return if @settings_run + @settings_run = true yield @settings if block_given? # Set defaults - @settings.rest_url ||= "http://stagedata.bioontology.org" - @settings.apikey ||= "4ea81d74-8960-4525-810b-fa1baab576ff" - @settings.links_attr ||= "links" + @settings.rest_url ||= 'http://stagedata.bioontology.org' + @settings.apikey ||= '4ea81d74-8960-4525-810b-fa1baab576ff' + @settings.links_attr ||= 'links' @settings.cache ||= false @settings.enable_long_request_log ||= false - @settings.purl_prefix ||= "http://purl.example.org" + @settings.purl_prefix ||= 'http://purl.example.org' + @settings.purl_host ||= 'purl.example.org' # Remove trailing slash - @settings.rest_url = @settings.rest_url.chomp("/") - @settings.purl_prefix = @settings.purl_prefix.chomp("/") + @settings.rest_url = @settings.rest_url.chomp('/') + @settings.purl_prefix = @settings.purl_prefix.chomp('/') @settings_run = true end def config_connection(options = {}) return if @settings_run_connection + store = options[:cache_store] @settings.conn = Faraday.new(@settings.rest_url) do |faraday| if @settings.enable_long_request_log @@ -57,10 +62,10 @@ def config_connection(options = {}) begin require_relative 'middleware/faraday-object-cache' faraday.use :object_cache, store: store - puts "=> faraday caching enabled" + puts '=> faraday caching enabled' puts "=> faraday cache store: #{store.class}" rescue LoadError - puts "=> WARNING: faraday http cache gem is not available, caching disabled" + puts '=> WARNING: faraday http cache gem is not available, caching disabled' end end @@ -68,9 +73,9 @@ def config_connection(options = {}) faraday.request :url_encoded faraday.adapter :excon faraday.headers = { - "Accept" => "application/json", - "Authorization" => "apikey token=#{@settings.apikey}", - "User-Agent" => "NCBO API Ruby Client v0.1.0" + 'Accept' => 'application/json', + 'Authorization' => "apikey token=#{@settings.apikey}", + 'User-Agent' => "NCBO API Ruby Client v#{LinkedData::Client::VERSION}" } end @settings_run_connection = true @@ -80,4 +85,4 @@ def connection_configured? @settings_run_connection end end -end \ No newline at end of file +end diff --git a/lib/ontologies_api_client/http.rb b/lib/ontologies_api_client/http.rb index 8b736eb..b0a4355 100644 --- a/lib/ontologies_api_client/http.rb +++ b/lib/ontologies_api_client/http.rb @@ -38,6 +38,8 @@ def values_at(*selectors) module LinkedData module Client module HTTP + $DEBUG_API_CLIENT ||= false + class Link < String attr_accessor :media_type; end @@ -61,7 +63,7 @@ def self.get(path, params = {}, options = {}) invalidate_cache = params.delete(:invalidate_cache) || false begin - puts "Getting: #{path} with #{params}" if $DEBUG + puts "Getting: #{path} with #{params}" if $DEBUG_API_CLIENT begin response = conn.get do |req| req.url path @@ -87,7 +89,7 @@ def self.get(path, params = {}, options = {}) obj = recursive_struct(load_json(response.body)) end rescue StandardError => e - puts "Problem getting #{path}" if $DEBUG + puts "Problem getting #{path}" if $DEBUG_API_CLIENT raise e end obj @@ -143,7 +145,7 @@ def self.patch(path, obj) end def self.delete(id) - puts "Deleting #{id}" if $DEBUG + puts "Deleting #{id}" if $DEBUG_API_CLIENT response = conn.delete id raise StandardError, response.body if response.status >= 500 diff --git a/lib/ontologies_api_client/models/class.rb b/lib/ontologies_api_client/models/class.rb index 20d1b78..7c2e250 100644 --- a/lib/ontologies_api_client/models/class.rb +++ b/lib/ontologies_api_client/models/class.rb @@ -7,7 +7,7 @@ module Client module Models class Class < LinkedData::Client::Base HTTP = LinkedData::Client::HTTP - @media_type = "http://www.w3.org/2002/07/owl#Class" + @media_type = %w[http://www.w3.org/2002/07/owl#Class http://www.w3.org/2004/02/skos/core#Concept] @include_attrs = "prefLabel,definition,synonym,obsolete,hasChildren" @include_attrs_full = "prefLabel,definition,synonym,obsolete,properties,hasChildren,children" @attrs_always_present = :prefLabel, :definition, :synonym, :obsolete, :properties, :hasChildren, :children @@ -47,7 +47,7 @@ def to_jsonld def purl return "" if self.links.nil? - return self.id if self.id.include?("purl.") + return self.id if self.id.include? LinkedData::Client.settings[:purl_host] ont = self.explore.ontology encoded_id = Addressable::URI.encode_component(self.id, Addressable::URI::CharacterClasses::UNRESERVED) diff --git a/lib/ontologies_api_client/models/ontology.rb b/lib/ontologies_api_client/models/ontology.rb index 5db6ed4..f595ae2 100644 --- a/lib/ontologies_api_client/models/ontology.rb +++ b/lib/ontologies_api_client/models/ontology.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'cgi' -require_relative "../base" +require_relative '../base' module LinkedData module Client @@ -8,19 +10,19 @@ class Ontology < LinkedData::Client::Base include LinkedData::Client::Collection include LinkedData::Client::ReadWrite - @media_type = "http://data.bioontology.org/metadata/Ontology" - @include_attrs = "all" + @media_type = 'http://data.bioontology.org/metadata/Ontology' + @include_attrs = 'all' def flat? self.flat end def private? - viewingRestriction && viewingRestriction.downcase.eql?("private") + viewingRestriction && viewingRestriction.downcase.eql?('private') end def licensed? - viewingRestriction && viewingRestriction.downcase.eql?("licensed") + viewingRestriction && viewingRestriction.downcase.eql?('licensed') end def viewing_restricted? @@ -35,7 +37,7 @@ def purl if self.acronym "#{LinkedData::Client.settings.purl_prefix}/#{acronym}" else - "" + '' end end @@ -43,18 +45,13 @@ def access?(user) return true if !viewing_restricted? return false if user.nil? return true if user.admin? - return self.full_acl.any? {|u| u == user.id} + return self.full_acl.any? { |u| u == user.id } end def admin?(user) return false if user.nil? return true if user.admin? - return administeredBy.any? {|u| u == user.id} - end - - def invalidate_cache(cache_refresh_all = true) - self.class.all(invalidate_cache: true, include_views: true) - super(cache_refresh_all) + return administeredBy.any? { |u| u == user.id } end # ACL with administrators @@ -75,12 +72,12 @@ def acl_select # Method to get the property tree for a given ontology # Gets the properties from the REST API and then returns a tree def property_tree - properties = Hash[self.explore.properties.map {|p| [p.id, p]}] - properties.keys.each do |key| + properties = Hash[self.explore.properties.map { |p| [p.id, p] }] + properties.each_key do |key| prop = properties[key] - prop.parents.each {|par| properties[par].children << prop if properties[par]} + prop.parents.each { |par| properties[par].children << prop if properties[par] } end - roots = properties.values.select {|p| p.parents.empty?} + roots = properties.values.select { |p| p.parents.empty? } root = LinkedData::Client::Models::Property.new root.children = roots root @@ -97,7 +94,7 @@ def self.find_by(attrs, *args) params[:include_views] = params[:include_views] || true else # Stick params back and create a new one - args.push({include_views: true}) + args.push({ include_views: true }) end args.unshift(params) super(attrs, *args) @@ -108,17 +105,20 @@ def self.find_by(attrs, *args) # Override to search for views as well by default # Views get hidden on the REST service unless the `include_views` # parameter is set to `true` - def find(id, params = {}) + def self.find(id, params = {}) params[:include_views] = params[:include_views] || true super(id, params) end + def self.find_by_acronym(acronym, params = {}) + [find(acronym, params)] + end + ## # Include parameters commonly used with ontologies def self.include_params - "acronym,administeredBy,group,hasDomain,name,notes,projects,reviews,summaryOnly,viewingRestriction" + 'acronym,administeredBy,group,hasDomain,name,notes,projects,reviews,summaryOnly,viewingRestriction' end - end end end diff --git a/lib/ontologies_api_client/read_write.rb b/lib/ontologies_api_client/read_write.rb index 721c669..97157fa 100644 --- a/lib/ontologies_api_client/read_write.rb +++ b/lib/ontologies_api_client/read_write.rb @@ -90,18 +90,7 @@ def invalidate_cache(cache_refresh_all = true) HTTP.get(self.id, invalidate_cache: true) if self.id session = Thread.current[:session] session[:last_updated] = Time.now.to_f if session - refresh_cache end - - def refresh_cache - Spawnling.new do - LinkedData::Client::Models::Ontology.all - LinkedData::Client::Models::OntologySubmission.all - LinkedData::Client::Models::User.all - exit - end - end - end end end diff --git a/lib/ontologies_api_client/version.rb b/lib/ontologies_api_client/version.rb new file mode 100644 index 0000000..ef2e69e --- /dev/null +++ b/lib/ontologies_api_client/version.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module LinkedData + module Client + VERSION = '2.4.0' + end +end diff --git a/ontologies_api_client.gemspec b/ontologies_api_client.gemspec index 4190b07..8e2ec66 100644 --- a/ontologies_api_client.gemspec +++ b/ontologies_api_client.gemspec @@ -1,12 +1,14 @@ # frozen_string_literal: true +require_relative 'lib/ontologies_api_client/version' + Gem::Specification.new do |gem| gem.authors = ['Paul R Alexander'] gem.email = ['support@bioontology.org'] - gem.description = 'Models and serializers for ontologies and related artifacts backed by 4store' - gem.summary = 'This library can be used for interacting with a 4store instance that stores NCBO-based ' \ - 'ontology information. Models in the library are based on Goo. Serializers support RDF ' \ - 'serialization as Rack Middleware and automatic generation of hypermedia links.' + gem.description = 'Models and serializers for ontologies and related artifacts backed by an RDF database' + gem.summary = 'This library can be used for interacting with an AllegroGraph or 4store instance that stores ' \ + 'BioPortal-based ontology information. Models in the library are based on Goo. Serializers ' \ + 'support RDF serialization as Rack Middleware and automatic generation of hypermedia links.' gem.homepage = 'https://github.com/ncbo/ontologies_api_ruby_client' gem.files = `git ls-files`.split($\) @@ -14,9 +16,9 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.name = 'ontologies_api_client' gem.require_paths = ['lib'] - gem.version = '2.2.2' + gem.version = LinkedData::Client::VERSION - gem.add_dependency('activesupport', '6.1.7.3') + gem.add_dependency('activesupport', '7.0.8') gem.add_dependency('addressable', '~> 2.8') gem.add_dependency('excon') gem.add_dependency('faraday') @@ -25,5 +27,8 @@ Gem::Specification.new do |gem| gem.add_dependency('lz4-ruby') gem.add_dependency('multi_json') gem.add_dependency('oj') - gem.add_dependency('spawnling', '2.1.5') + + gem.add_development_dependency('faraday-follow_redirects', '~> 0.3') + gem.add_development_dependency('minitest', '~> 5.25') + gem.add_development_dependency('minitest-hooks', '~> 1.5') end diff --git a/test/models/test_class.rb b/test/models/test_class.rb index e5abe44..0229ffe 100644 --- a/test/models/test_class.rb +++ b/test/models/test_class.rb @@ -1,3 +1,6 @@ +# frozen_string_literal: true + +require 'faraday/follow_redirects' require_relative '../test_case' class ClassTest < LinkedData::Client::TestCase @@ -11,6 +14,60 @@ def test_find assert_equal 'http://www.w3.org/2002/07/owl#Class', cls.type assert_equal 'Activity', cls.prefLabel assert_equal ontology, cls.links['ontology'] - assert_true cls.hasChildren + assert cls.hasChildren + end + + # Test PURL generation for a class in an OWL format ontology + def test_purl_owl + cls = LinkedData::Client::Models::Class.find( + 'http://bioontology.org/ontologies/Activity.owl#Activity', + 'https://data.bioontology.org/ontologies/BRO' + ) + refute_nil cls + + res = fetch_response(cls.purl) + assert_equal 200, res.status + assert_equal 'https://bioportal.bioontology.org/ontologies/BRO'\ + '?p=classes&conceptid=http%3A%2F%2Fbioontology.org%2Fontologies%2FActivity.owl%23Activity', + res.env[:url].to_s + end + + # Test PURL generation for a class in a UMLS format ontology + def test_purl_umls + cls = LinkedData::Client::Models::Class.find( + 'http://purl.bioontology.org/ontology/SNOMEDCT/64572001', + 'https://bioportal.bioontology.org/ontologies/SNOMEDCT' + ) + refute_nil cls + + res = fetch_response(cls.purl) + assert_equal 200, res.status + assert_equal 'https://bioportal.bioontology.org/ontologies/SNOMEDCT?p=classes&conceptid=64572001', + res.env[:url].to_s + end + + # Test PURL generation for a class in an OBO format ontology + def test_purl_obo + cls = LinkedData::Client::Models::Class.find( + 'http://purl.obolibrary.org/obo/DOID_4', + 'https://bioportal.bioontology.org/ontologies/DOID' + ) + refute_nil cls + + res = fetch_response(cls.purl) + assert_equal 200, res.status + assert_equal 'https://bioportal.bioontology.org/ontologies/DOID'\ + '?p=classes&conceptid=http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2FDOID_4', + res.env[:url].to_s + end + + private + + def fetch_response(url) + conn = Faraday.new do |f| + f.response :follow_redirects + f.adapter Faraday.default_adapter + end + conn.get(url) end end diff --git a/test/models/test_collection.rb b/test/models/test_collection.rb index 1de9f8c..d97cd79 100644 --- a/test/models/test_collection.rb +++ b/test/models/test_collection.rb @@ -1,12 +1,14 @@ -require_relative '../test_case' +# frozen_string_literal: true + require 'pry' +require_relative '../test_case' class TestOntology < LinkedData::Client::Base include LinkedData::Client::Collection include LinkedData::Client::ReadWrite - @media_type = "http://data.bioontology.org/metadata/Ontology" - @include_attrs = "all" + @media_type = 'http://data.bioontology.org/metadata/Ontology' + @include_attrs = 'all' end class CollectionTest < LinkedData::Client::TestCase @@ -16,30 +18,44 @@ def test_all end def test_class_for_type - media_type = "http://data.bioontology.org/metadata/Category" + media_type = 'http://data.bioontology.org/metadata/Category' type_cls = LinkedData::Client::Base.class_for_type(media_type) - assert type_cls == LinkedData::Client::Models::Category + assert_equal LinkedData::Client::Models::Category, type_cls end def test_find_by - bro = TestOntology.find_by_acronym("BRO") + bro = TestOntology.find_by_acronym('BRO') assert bro.length >= 1 - assert bro.any? {|o| o.acronym.eql?("BRO")} + assert(bro.any? { |o| o.acronym.eql?('BRO') }) - onts = TestOntology.find_by_hasDomain_and_doNotUpdate("https://data.bioontology.org/categories/Health", true) + onts = TestOntology.find_by_hasDomain_and_doNotUpdate('https://data.bioontology.org/categories/Health', true) assert onts.length >= 1 - onts = TestOntology.find_by_hasDomain_and_hasDomain("https://data.bioontology.org/categories/Phenotype", "https://data.bioontology.org/categories/Human") + onts = TestOntology.find_by_hasDomain_and_hasDomain('https://data.bioontology.org/categories/Phenotype', 'https://data.bioontology.org/categories/Human') assert onts.length >= 1 end def test_where - onts = TestOntology.where {|o| o.name.downcase.start_with?("c")} + onts = TestOntology.where { |o| o.name.downcase.start_with?('c') } assert onts.length >= 1 end def test_find - ont = TestOntology.find("https://data.bioontology.org/ontologies/SNOMEDCT") - assert !ont.nil? + ont = TestOntology.find('https://data.bioontology.org/ontologies/SNOMEDCT') + refute_nil ont + end + + def test_get + ont = TestOntology.get('https://data.bioontology.org/ontologies/SNOMEDCT') + refute_nil ont + assert_instance_of LinkedData::Client::Models::Ontology, ont + assert_equal 'https://data.bioontology.org/ontologies/SNOMEDCT', ont.id + assert_equal 'SNOMEDCT', ont.acronym + + ont = TestOntology.get('SNOMEDCT') + refute_nil ont + assert_instance_of LinkedData::Client::Models::Ontology, ont + assert_equal 'https://data.bioontology.org/ontologies/SNOMEDCT', ont.id + assert_equal 'SNOMEDCT', ont.acronym end end diff --git a/test/models/test_ontology.rb b/test/models/test_ontology.rb new file mode 100644 index 0000000..c865d41 --- /dev/null +++ b/test/models/test_ontology.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require_relative '../test_case' + +class OntologyTest < LinkedData::Client::TestCase + def test_find_by_acronym + result = LinkedData::Client::Models::Ontology.find_by_acronym('SNOMEDCT') + refute_empty result + assert_instance_of Array, result + assert_equal 1, result.length + + ont = result.first + assert_instance_of LinkedData::Client::Models::Ontology, ont + assert_equal 'https://data.bioontology.org/ontologies/SNOMEDCT', ont.id + assert_equal 'SNOMEDCT', ont.acronym + end + + def test_find + ont = LinkedData::Client::Models::Ontology.find('SNOMEDCT') + refute_nil ont + assert_instance_of LinkedData::Client::Models::Ontology, ont + assert_equal 'https://data.bioontology.org/ontologies/SNOMEDCT', ont.id + assert_equal 'SNOMEDCT', ont.acronym + + ont = LinkedData::Client::Models::Ontology.find('BiositemapIM') + refute_nil ont + assert_instance_of LinkedData::Client::Models::Ontology, ont + assert_equal 'https://data.bioontology.org/ontologies/BiositemapIM', ont.id + assert_equal 'BiositemapIM', ont.acronym + end +end diff --git a/test/test_case.rb b/test/test_case.rb index cd9fe2e..3e5a855 100644 --- a/test/test_case.rb +++ b/test/test_case.rb @@ -1,10 +1,23 @@ -require 'test-unit' +# frozen_string_literal: true + +require 'minitest/autorun' +require 'minitest/hooks/test' require_relative '../lib/ontologies_api_client' require_relative '../config/config' module LinkedData module Client - class TestCase < Test::Unit::TestCase + class TestCase < Minitest::Test + include Minitest::Hooks + + def before_all + super + params = { q: 'Conceptual Entity', ontologies: 'STY', require_exact_match: 'true', display_links: 'false' } + response = LinkedData::Client::HTTP.get('/search', params) + if response.respond_to?('status') && response.status.eql?(401) + abort('ABORTED! You must provide a valid API key.') + end + end end end end