|
1 | 1 | require 'test_helper' |
2 | 2 |
|
3 | 3 | class ImportVersionsJobTest < ActiveJob::TestCase |
| 4 | + make_my_diffs_pretty! |
| 5 | + |
4 | 6 | class FakeLogger |
5 | 7 | def logs |
6 | 8 | @logs ||= [] |
@@ -41,6 +43,99 @@ def error(message) |
41 | 43 | end |
42 | 44 | end |
43 | 45 |
|
| 46 | + def assert_field(raw, version, name, expected: nil) |
| 47 | + expected = raw[name] if expected.nil? |
| 48 | + assert_equal(expected, version.send(name), "#{name} was not set") |
| 49 | + end |
| 50 | + |
| 51 | + test 'imports a version' do |
| 52 | + raw_data = { |
| 53 | + url: pages(:home_page).url, |
| 54 | + capture_time: '2025-01-01T00:00:00Z', |
| 55 | + body_url: 'https://test-bucket.s3.amazonaws.com/some-archived-copy.html', |
| 56 | + body_hash: 'INVALID_HASH', |
| 57 | + source_type: 'test-source', |
| 58 | + source_metadata: { test_meta: 'data' }, |
| 59 | + status: 200, |
| 60 | + headers: { 'Some-Header' => 'value' }, |
| 61 | + title: pages(:home_page).title, |
| 62 | + content_length: nil, |
| 63 | + media_type: nil, |
| 64 | + page_maintainers: ['The Federal Example Agency'], |
| 65 | + page_tags: ['Some tag'] |
| 66 | + } |
| 67 | + |
| 68 | + import = Import.create_with_data( |
| 69 | + { user: users(:alice) }, |
| 70 | + [raw_data].map(&:to_json).join("\n") |
| 71 | + ) |
| 72 | + |
| 73 | + ImportVersionsJob.perform_now(import) |
| 74 | + assert_equal([], import.processing_errors, 'There were processing errors') |
| 75 | + |
| 76 | + page = Page.find(pages(:home_page).uuid) |
| 77 | + version = Version.where(url: raw_data[:url], capture_time: raw_data[:capture_time]).first |
| 78 | + assert_not_nil(version) |
| 79 | + assert_equal( |
| 80 | + { |
| 81 | + **raw_data.with_indifferent_access.except(:page_maintainers, :page_tags), |
| 82 | + 'page_uuid' => page.uuid, |
| 83 | + 'capture_time' => Time.parse(raw_data[:capture_time]), |
| 84 | + 'headers' => raw_data[:headers].transform_keys(&:downcase), |
| 85 | + 'network_error' => nil |
| 86 | + }.sort.to_h, |
| 87 | + { |
| 88 | + **version.attributes.except('uuid', 'created_at', 'updated_at', 'different'), |
| 89 | + 'capture_time' => version.capture_time |
| 90 | + }.sort.to_h |
| 91 | + ) |
| 92 | + |
| 93 | + page_tags = page.tags.map(&:name) |
| 94 | + raw_data[:page_tags].each { |name| assert_includes(page_tags, name) } |
| 95 | + page_maintainers = page.maintainers.map(&:name) |
| 96 | + raw_data[:page_maintainers].each { |name| assert_includes(page_maintainers, name) } |
| 97 | + end |
| 98 | + |
| 99 | + test 'imports an error version' do |
| 100 | + raw_data = { |
| 101 | + url: pages(:home_page).url, |
| 102 | + capture_time: '2025-01-01T00:00:00Z', |
| 103 | + network_error: 'net::ERR_NAME_NOT_RESOLVED', |
| 104 | + source_type: 'test-source', |
| 105 | + source_metadata: { test_meta: 'data' } |
| 106 | + } |
| 107 | + |
| 108 | + import = Import.create_with_data( |
| 109 | + { user: users(:alice) }, |
| 110 | + [raw_data].map(&:to_json).join("\n") |
| 111 | + ) |
| 112 | + |
| 113 | + ImportVersionsJob.perform_now(import) |
| 114 | + assert_equal([], import.processing_errors, 'There were processing errors') |
| 115 | + |
| 116 | + page = Page.find(pages(:home_page).uuid) |
| 117 | + version = Version.where(url: raw_data[:url], capture_time: raw_data[:capture_time]).first |
| 118 | + assert_not_nil(version) |
| 119 | + assert_equal( |
| 120 | + { |
| 121 | + **raw_data.with_indifferent_access.except(:page_maintainers, :page_tags), |
| 122 | + 'page_uuid' => page.uuid, |
| 123 | + 'capture_time' => Time.parse(raw_data[:capture_time]), |
| 124 | + 'headers' => nil, |
| 125 | + 'body_url' => nil, |
| 126 | + 'body_hash' => nil, |
| 127 | + 'content_length' => nil, |
| 128 | + 'media_type' => nil, |
| 129 | + 'status' => nil, |
| 130 | + 'title' => nil |
| 131 | + }.sort.to_h, |
| 132 | + { |
| 133 | + **version.attributes.except('uuid', 'created_at', 'updated_at', 'different'), |
| 134 | + 'capture_time' => version.capture_time |
| 135 | + }.sort.to_h |
| 136 | + ) |
| 137 | + end |
| 138 | + |
44 | 139 | test 'does not add or modify a version if it already exists' do |
45 | 140 | page_versions_count = pages(:home_page).versions.count |
46 | 141 | original_data = versions(:page1_v1).as_json |
|
0 commit comments