|
| 1 | +RSpec.describe "Export tasks" do |
| 2 | + include ActiveJob::TestHelper |
| 3 | + |
| 4 | + describe "export:live_document_and_assets" do |
| 5 | + before do |
| 6 | + Rake::Task["export:live_document_and_assets"].reenable |
| 7 | + end |
| 8 | + |
| 9 | + it "calls WhitehallMigration::DocumentExport.export_to_hash with correct arguments" do |
| 10 | + document = create(:document, :with_live_edition) |
| 11 | + allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash) |
| 12 | + Rake::Task["export:live_document_and_assets"].invoke(document.content_id) |
| 13 | + expect(WhitehallMigration::DocumentExport).to have_received(:export_to_hash).with(document) |
| 14 | + end |
| 15 | + |
| 16 | + it "pretty-prints the result to STDOUT if no output_file is specified" do |
| 17 | + document = create(:document, :with_live_edition) |
| 18 | + allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash).and_return({ foo: "bar" }) |
| 19 | + expect { Rake::Task["export:live_document_and_assets"].invoke(document.content_id) }.to output("{:foo=>\"bar\"}\n").to_stdout |
| 20 | + end |
| 21 | + |
| 22 | + it "writes the result as JSON to the given output_file if specified" do |
| 23 | + document = create(:document, :with_live_edition) |
| 24 | + allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash).and_return({ foo: "bar", baz: "qux" }) |
| 25 | + |
| 26 | + output_file = Tempfile.new("export") |
| 27 | + Rake::Task["export:live_document_and_assets"].invoke(document.content_id, output_file.path) |
| 28 | + |
| 29 | + expected = <<~JSON |
| 30 | + { |
| 31 | + "foo": "bar", |
| 32 | + "baz": "qux" |
| 33 | + } |
| 34 | + JSON |
| 35 | + expect(File.read(output_file.path)).to match(expected.strip) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + describe "export:live_documents_and_assets" do |
| 40 | + before do |
| 41 | + allow($stdout).to receive(:puts) # suppress output for cleanliness |
| 42 | + Rake::Task["export:live_documents_and_assets"].reenable |
| 43 | + |
| 44 | + # create 3 documents |
| 45 | + Document.find_each(&:destroy) # Clean slate |
| 46 | + @documents = [ |
| 47 | + create(:document, :with_live_edition), |
| 48 | + create(:document, :with_live_edition), |
| 49 | + create(:document, :with_live_edition), |
| 50 | + ] |
| 51 | + allow(WhitehallMigration::DocumentExport).to receive(:exportable_documents).and_return(@documents) |
| 52 | + end |
| 53 | + |
| 54 | + it "lists how many documents it is about to export" do |
| 55 | + expect { Rake::Task["export:live_documents_and_assets"].invoke }.to output(/^Exporting 3 live editions/).to_stdout |
| 56 | + end |
| 57 | + |
| 58 | + it "calls WhitehallMigration::DocumentExport.export_to_hash with correct arguments" do |
| 59 | + allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash) |
| 60 | + Rake::Task["export:live_documents_and_assets"].invoke |
| 61 | + expect(WhitehallMigration::DocumentExport).to have_received(:export_to_hash).with(@documents[0]) |
| 62 | + expect(WhitehallMigration::DocumentExport).to have_received(:export_to_hash).with(@documents[1]) |
| 63 | + expect(WhitehallMigration::DocumentExport).to have_received(:export_to_hash).with(@documents[2]) |
| 64 | + end |
| 65 | + |
| 66 | + it "pretty-prints the result to STDOUT if no output_directory is specified" do |
| 67 | + allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash).and_return({ foo: "bar" }) |
| 68 | + expect { Rake::Task["export:live_documents_and_assets"].invoke }.to output(/{:foo=>"bar"}\n{:foo=>"bar"}\n{:foo=>"bar"}\n$/).to_stdout |
| 69 | + end |
| 70 | + |
| 71 | + it "writes the result as JSON files to the given output_directory if specified" do |
| 72 | + allow(WhitehallMigration::DocumentExport).to receive(:export_to_hash) do |document| |
| 73 | + { base_path: "/news/example-path-#{document.id}" } |
| 74 | + end |
| 75 | + output_directory = Dir.mktmpdir |
| 76 | + Rake::Task["export:live_documents_and_assets"].invoke(output_directory) |
| 77 | + |
| 78 | + expected_files = @documents.map { |doc| "#{output_directory}/example-path-#{doc.id}.json" } |
| 79 | + actual_files = Dir.glob("#{output_directory}/*.json").sort |
| 80 | + expect(actual_files).to match_array(expected_files) |
| 81 | + end |
| 82 | + end |
| 83 | +end |
0 commit comments