From b49ad10781e83360a5dd1968972c4048592eefda Mon Sep 17 00:00:00 2001 From: Syphax bouazzouni Date: Fri, 3 Nov 2023 11:28:52 +0100 Subject: [PATCH] add usage method to the agent models (#109) --- Gemfile.lock | 3 +- .../models/agents/agent.rb | 27 ++++++- test/models/test_agent.rb | 79 +++++++++++++++---- 3 files changed, 93 insertions(+), 16 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 6ac2e180..1aae8d9d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -213,6 +213,7 @@ GEM PLATFORMS x86_64-darwin-21 + x86_64-linux DEPENDENCIES activesupport (~> 4) @@ -248,4 +249,4 @@ DEPENDENCIES webmock BUNDLED WITH - 2.4.12 + 2.3.26 diff --git a/lib/ontologies_linked_data/models/agents/agent.rb b/lib/ontologies_linked_data/models/agents/agent.rb index f9574a0c..62495f48 100644 --- a/lib/ontologies_linked_data/models/agents/agent.rb +++ b/lib/ontologies_linked_data/models/agents/agent.rb @@ -16,13 +16,38 @@ class Agent < LinkedData::Models::Base attribute :identifiers, namespace: :adms, property: :identifier, enforce: %i[Identifier list unique_identifiers] attribute :affiliations, enforce: %i[Agent list is_organization], namespace: :org, property: :memberOf attribute :creator, type: :user, enforce: [:existence] - embed :identifiers, :affiliations embed_values affiliations: LinkedData::Models::Agent.goo_attrs_to_load + [identifiers: LinkedData::Models::AgentIdentifier.goo_attrs_to_load] + serialize_methods :usages write_access :creator access_control_load :creator + def usages + id = self.id + q = Goo.sparql_query_client.select(:id, :property, :status).distinct + .from(LinkedData::Models::OntologySubmission.uri_type) + .where( + [:id, + RDF::URI.new('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), + LinkedData::Models::OntologySubmission.uri_type + ], + [:id, + LinkedData::Models::OntologySubmission.attribute_uri(:submissionStatus), + :status + ] + ) + + q = q.union([[:id, :property, id]]) + q.filter("?status = <#{RDF::URI.new(LinkedData::Models::SubmissionStatus.id_prefix + 'RDF')}> || ?status = <#{RDF::URI.new(LinkedData::Models::SubmissionStatus.id_prefix + 'UPLOADED')}>") + data = q.each_solution.map { |x| [x[:id], x[:property], x[:status]] } + data = data.group_by(&:shift) + data.transform_values do |values| + r = values.select { |value| value.last['RDF'] } + r = values.select { |value| value.last['UPLOADED'] } if r.empty? + r.map(&:first) + end + end def unique_identifiers(inst, attr) inst.bring(attr) if inst.bring?(attr) diff --git a/test/models/test_agent.rb b/test/models/test_agent.rb index ae7d199d..15342c60 100644 --- a/test/models/test_agent.rb +++ b/test/models/test_agent.rb @@ -3,8 +3,9 @@ class TestAgent < LinkedData::TestCase def self.before_suite + backend_4s_delete self.new("before_suite").teardown - @@user1 = LinkedData::Models::User.new(:username => "user11111", :email => "some1111@email.org" ) + @@user1 = LinkedData::Models::User.new(:username => "user11111", :email => "some1111@email.org") @@user1.passwordHash = "some random pass hash" @@user1.save end @@ -15,32 +16,28 @@ def self.after_suite @@user1.delete end - - def test_agent_no_valid - @agents =[ - LinkedData::Models::Agent.new(name:"name 0", email:"test_0@test.com", agentType: 'organization',creator: @@user1 ), - LinkedData::Models::Agent.new(name:"name 1", email:"test_1@test.com", agentType: 'person', creator: @@user1 ), - LinkedData::Models::Agent.new(name:"name 2", email:"test_2@test.com", agentType: 'person', creator: @@user1 ) + @agents = [ + LinkedData::Models::Agent.new(name: "name 0", email: "test_0@test.com", agentType: 'organization', creator: @@user1), + LinkedData::Models::Agent.new(name: "name 1", email: "test_1@test.com", agentType: 'person', creator: @@user1), + LinkedData::Models::Agent.new(name: "name 2", email: "test_2@test.com", agentType: 'person', creator: @@user1) ] @identifiers = [ LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ROR', creator: @@user1), LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ORCID', creator: @@user1), ] - @identifiers.each {|i| i.save} + @identifiers.each { |i| i.save } - affiliations = @agents[0..2].map{ |a| a.save } + affiliations = @agents[0..2].map { |a| a.save } agent = @agents.last agent.affiliations = affiliations - refute agent.valid? refute_nil agent.errors[:affiliations][:is_organization] - affiliations.each{|x| x.delete} - + affiliations.each { |x| x.delete } agents = @agents[0..2].map do |a| a.identifiers = @identifiers @@ -54,8 +51,7 @@ def test_agent_no_valid refute second_agent.valid? refute_nil second_agent.errors[:identifiers][:unique_identifiers] - - @identifiers.each{|i| i.delete} + @identifiers.each { |i| i.delete } end def test_identifier_find @@ -69,6 +65,7 @@ def test_identifier_find id.delete end + def test_identifier_no_valid refute LinkedData::Models::AgentIdentifier.new(notation: 'https://ror.org/000h6jb29', schemaAgency: 'ROR', creator: @@user1).valid? id = LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29"', schemaAgency: 'ROR', creator: @@user1) @@ -82,4 +79,58 @@ def test_identifier_no_valid id.delete end + def test_agent_usages + count, acronyms, ontologies = create_ontologies_and_submissions(ont_count: 3, submission_count: 1, + process_submission: false) + + o1 = ontologies[0] + o2 = ontologies[1] + o3 = ontologies[2] + sub1 = o1.latest_submission(status: :any) + sub2 = o2.latest_submission(status: :any) + sub3 = o3.latest_submission(status: :any) + refute_nil sub1 + refute_nil sub2 + refute_nil sub3 + + agents = [LinkedData::Models::Agent.new(name: "name 0", email: "test_0@test.com", agentType: 'organization', creator: @@user1).save, + LinkedData::Models::Agent.new(name: "name 1", email: "test_1@test.com", agentType: 'organization', creator: @@user1).save, + LinkedData::Models::Agent.new(name: "name 2", email: "test_2@test.com", agentType: 'person', creator: @@user1).save] + + sub1.hasCreator = [agents.last] + sub1.publisher = agents[0..1] + sub1.fundedBy = [agents[0]] + sub1.bring_remaining + assert sub1.valid? + sub1.save + + sub2.hasCreator = [agents.last] + sub2.endorsedBy = [agents[0]] + sub2.fundedBy = agents[0..1] + sub2.bring_remaining + assert sub2.valid? + sub2.save + + usages = agents[0].usages + + assert_equal 2, usages.size + + refute_nil usages[sub1.id] + assert_equal usages[sub1.id].map(&:to_s).sort, ["http://purl.org/dc/terms/publisher", "http://xmlns.com/foaf/0.1/fundedBy"].sort + refute_nil usages[sub2.id].map(&:to_s).sort, ["http://omv.ontoware.org/2005/05/ontology#endorsedBy", "http://xmlns.com/foaf/0.1/fundedBy"].sort + + sub3.copyrightHolder = agents[0] + sub3.bring_remaining + sub3.save + + usages = agents[0].usages + assert_equal 3, usages.size + + refute_nil usages[sub1.id] + assert_equal usages[sub1.id].map(&:to_s).sort, ["http://purl.org/dc/terms/publisher", "http://xmlns.com/foaf/0.1/fundedBy"].sort + assert_equal usages[sub2.id].map(&:to_s).sort, ["http://omv.ontoware.org/2005/05/ontology#endorsedBy", "http://xmlns.com/foaf/0.1/fundedBy"].sort + assert_equal usages[sub3.id].map(&:to_s), ["http://schema.org/copyrightHolder"] + + agents.each{|x| x.delete} + end end