Skip to content

Commit

Permalink
Merge pull request #2873 from alphagov/licence-report
Browse files Browse the repository at this point in the history
Add rake task to report on licences
  • Loading branch information
KludgeKML authored Nov 26, 2024
2 parents 08fae1a + 9566de7 commit 8d1e8c3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/tasks/licence_reports.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "csv"

namespace :licence_reports do
desc "CSV report on licences"
task all: :environment do
include OrganisationsHelper

csv_string = CSV.generate do |csv|
csv << %w[title link_to_competent_authority licence_identifier location publishing_organisation other_associated_organisations publication_state last_updated_at]

LicenceTransaction.find_each do |doc|
csv << [doc.title,
doc.licence_transaction_continuation_link,
doc.licence_transaction_licence_identifier,
doc.licence_transaction_location.join(" / "),
organisation_name(doc.primary_publishing_organisation),
doc.organisations.map { |org| organisation_name(org) }.join(" / "),
doc.publication_state,
doc.public_updated_at]
end
end

puts(csv_string)
end
end
34 changes: 34 additions & 0 deletions spec/lib/tasks/licence_reports_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "rails_helper"

RSpec.describe "licence_reports", type: :task do
describe "licence_reports:all" do
let(:task) { Rake::Task["licence_reports:all"] }
before(:each) { task.reenable }

let(:licence_transaction) do
lt = FactoryBot.create(:licence_transaction)
lt["details"]["metadata"]["licence_transaction_location"] = %w[england wales]
lt
end
let(:content_id) { licence_transaction["content_id"] }
let(:organisations) do
[
{ "content_id" => "6de6b795-9d30-4bd8-a257-ab9a6879e1ea", "title" => "PPO Org" },
{ "content_id" => "d31d9806-2644-4023-be70-5376cae84a06", "title" => "Other Org" },
]
end

before do
stub_publishing_api_has_content([licence_transaction], hash_including(document_type: LicenceTransaction.document_type))
stub_publishing_api_has_item(licence_transaction)
stub_publishing_api_has_content(organisations, hash_including(document_type: Organisation.document_type))
end

it "reports on licences" do
expect { task.invoke }.to output(
"title,link_to_competent_authority,licence_identifier,location,publishing_organisation,other_associated_organisations,publication_state,last_updated_at\n" \
"Example document,https://www.gov.uk,,england / wales,Other Org,PPO Org,draft,2015-11-16T11:53:30+00:00\n",
).to_stdout
end
end
end

0 comments on commit 8d1e8c3

Please sign in to comment.