Skip to content

Commit

Permalink
Merge pull request #514 from datacite/fix_prefixes_build_order
Browse files Browse the repository at this point in the history
Fix prefixes build order in repository transfer
  • Loading branch information
kjgarza authored May 14, 2020
2 parents 4fb61c0 + 5de6edb commit 37bc1eb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
11 changes: 7 additions & 4 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,16 @@ def transfer_prefixes(target_id)

if prefix_ids.present?
response = ProviderPrefix.where("prefix_id IN (?)", prefix_ids).destroy_all
puts "#{response.count} provider prefixes deleted."
Rails.logger.info "[Transfer] #{response.count} provider prefixes deleted."
end

# Assign prefix(es) to provider
# Assign prefix(es) to provider and client
prefixes_names.each do |prefix|
ProviderPrefix.create(provider_id: target_id, prefix_id: prefix)
puts "Provider prefix for provider #{target_id} and prefix #{prefix} created."
provider_prefix = ProviderPrefix.create(provider_id: target_id, prefix_id: prefix)
Rails.logger.info "[Transfer] Provider prefix for provider #{target_id} and prefix #{prefix} created."

ClientPrefix.create(client_id: symbol, provider_prefix_id: provider_prefix.uid, prefix_id: prefix)
Rails.logger.info "Client prefix for client #{symbol} and prefix #{prefix} created."
end
end

Expand Down
2 changes: 1 addition & 1 deletion db/seeds/development/base.seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
client = Client.where(symbol: "DATACITE.TEST").first || FactoryBot.create(:client, provider: provider, symbol: ENV["MDS_USERNAME"], password: ENV["MDS_PASSWORD"])
if Prefix.where(uid: "10.14454").blank?
prefix = FactoryBot.create(:prefix, uid: "10.14454")
FactoryBot.create(:provider_prefix, provider_id: provider.symbol, prefix_id: prefix.uid)
### This creates both the client_prefix and the pprovider association
FactoryBot.create(:client_prefix, client_id: client.symbol, prefix_id: prefix.uid)
end
dois = FactoryBot.create_list(:doi, 10, client: client, state: "findable")
Expand Down
6 changes: 3 additions & 3 deletions db/seeds/development/consortium_transfer.seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
fail "Seed tasks can only be used in the development enviroment" if Rails.env.production?

after "development:base" do

provider = Provider.where(symbol: "QUECHUA").first || FactoryBot.create(:provider, symbol: "QUECHUA")
client = Client.where(symbol: "QUECHUA.TEXT").first || FactoryBot.create(:client, provider: provider, symbol: "QUECHUA.TEXT", password: ENV["MDS_PASSWORD"])
if Prefix.where(uid: "10.14459").blank?
prefix = FactoryBot.create(:prefix, uid: "10.14459")
FactoryBot.create(:provider_prefix, provider_id: provider.symbol, prefix_id: prefix.uid)
FactoryBot.create(:client_prefix, client_id: client.symbol, prefix_id: prefix.uid)
## one needs to create the provider first so the assignation is made
provider_prefix_id = FactoryBot.create(:provider_prefix, provider_id: provider.symbol, prefix_id: prefix.uid)
FactoryBot.create(:client_prefix, client_id: client.symbol, prefix_id: prefix.uid, provider_prefix_id: provider_prefix_id.uid)
end
dois = FactoryBot.create_list(:doi, 10, client: client, state: "findable")
FactoryBot.create_list(:event_for_datacite_related, 3, obj_id: dois.first.doi)
Expand Down
13 changes: 9 additions & 4 deletions spec/models/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
let!(:prefixes) { create_list(:prefix, 3) }
let!(:prefix) { prefixes.first }

let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix) }
### Order is important in creating prefixes relations
let!(:provider_prefix) { create(:provider_prefix, provider: provider, prefix: prefix) }
let!(:provider_prefix_more) { create(:provider_prefix, provider: provider, prefix: prefixes.last) }
let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix, provider_prefix_id: provider_prefix.uid) }

let(:new_provider) { create(:provider, symbol: "QUECHUA", member_type: "direct_member") }
let(:options) { { target_id: new_provider.symbol } }
let(:bad_options) { { target_id: "SALS" } }
Expand All @@ -44,6 +46,8 @@

expect(new_provider.prefix_ids).to include(prefix.uid)
expect(provider.prefix_ids).not_to include(prefix.uid)

expect(client.prefix_ids).to include(prefix.uid)
end

it "it doesn't transfer" do
Expand Down Expand Up @@ -101,9 +105,10 @@
describe "Client prefixes transfer" do
let!(:prefixes) { create_list(:prefix, 3) }
let!(:prefix) { prefixes.first }
let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix) }
let!(:provider_prefix) { create(:provider_prefix, provider: provider, prefix: prefix) }
### Order is important in creating prefixes relations
let!(:provider_prefix) { create(:provider_prefix, provider: provider, prefix: prefix) }
let!(:provider_prefix_more) { create(:provider_prefix, provider: provider, prefix: prefixes.last) }
let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix , provider_prefix_id: provider_prefix.uid) }
let(:new_provider) { create(:provider, symbol: "QUECHUA") }

it "works" do
Expand All @@ -114,10 +119,10 @@

expect(new_provider.prefix_ids).to include(prefix.uid)
expect(provider.prefix_ids).not_to include(prefix.uid)
expect(client.prefix_ids).to include(prefix.uid)
end
end


describe "methods" do
it "should not update the symbol" do
client.update_attributes :symbol => client.symbol+'foo.bar'
Expand Down
10 changes: 5 additions & 5 deletions spec/requests/clients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,10 @@
let(:new_provider) { create(:provider, symbol: "QUECHUA", password_input: "12345") }
let!(:prefixes) { create_list(:prefix, 3) }
let!(:prefix) { prefixes.first }
let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix) }
let!(:provider_prefix) { create(:provider_prefix, provider: provider, prefix: prefix) }
let!(:provider_prefix_more) { create(:provider_prefix, provider: provider, prefix: prefixes.last) }
let!(:provider_prefix) { create(:provider_prefix, provider: provider, prefix: prefix) }
let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix, provider_prefix_id: provider_prefix.uid) }
let(:doi) { create_list(:doi, 10, client: client) }


let(:params) do
{
"data" => {
Expand All @@ -254,8 +252,10 @@
expect(json.dig("data", "relationships", "prefixes", "data").first.dig("id")).to eq(prefixes.last.uid)

get "/providers/#{new_provider.symbol}"

expect(json.dig("data", "relationships", "prefixes", "data").first.dig("id")).to eq(prefix.uid)

get "/prefixes/#{prefix.uid}"
expect(json.dig("data", "relationships", "clients", "data").first.dig("id")).to eq(client.symbol.downcase)
end
end

Expand Down
5 changes: 4 additions & 1 deletion spec/requests/repositories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@

let(:new_provider) { create(:provider, symbol: "QUECHUA", password_input: "12345") }
let!(:prefix) { create(:prefix) }
let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix) }
let!(:provider_prefix) { create(:provider_prefix, provider: provider, prefix: prefix) }
let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix, provider_prefix_id: provider_prefix.uid) }
let(:doi) { create_list(:doi, 10, client: client) }

let(:params) do
Expand Down Expand Up @@ -318,6 +318,9 @@
get "/providers/#{new_provider.symbol}"

expect(json.dig("data", "relationships", "prefixes", "data").first.dig("id")).to eq(prefix.uid)

get "/prefixes/#{prefix.uid}"
expect(json.dig("data", "relationships", "clients", "data").first.dig("id")).to eq(client.symbol.downcase)
end
end

Expand Down

0 comments on commit 37bc1eb

Please sign in to comment.