-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtind_validation_spec.rb
More file actions
133 lines (117 loc) · 5.44 KB
/
Copy pathtind_validation_spec.rb
File metadata and controls
133 lines (117 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require 'rspec'
require 'net/http'
require 'open-uri'
require_relative '../../../app/lib/tind_spread/tind_validation'
RSpec.describe TindSpread::TindValidation do
describe '.validate_row' do
it 'validates a row and returns errors' do
row = { 'Filename' => 'value', 'FFT__a' => 'invalid_url', '500__3' => 'value', '800__6' => 'value' }
allow(described_class).to receive(:valid_url?).with('invalid_url').and_return(false)
allow(described_class).to receive(:fft_jpg_or_pdf?).with('invalid_url').and_return(false)
allow(described_class).to receive(:fft_jpg_or_pdf?).with('invalid_url').and_return(false)
allow(described_class).to receive(:valid_500__3?).with('500__3', row).and_return(false)
allow(described_class).to receive(:corresponding_6?).with('800__6', row).and_return(false)
expected_errors = [
'header: FFT__a-1 No files found for value',
'header: FFT__a URL: invalid_url inaccessible',
'header: FFT__a URL: invalid_url invalid. needs to be .jpg or .pdf',
'header: 500__3 There is a 500__3 without a corresponding 500__a. Value for 500__3 is value',
'header: 800__6 There is no matching $6 for value value'
]
expect(described_class.validate_row(row)).to eq(expected_errors)
end
end
describe '.validate_fft' do
it 'validates FFT field and adds errors' do
errors = []
allow(described_class).to receive(:valid_url?).with('invalid_url').and_return(false)
allow(described_class).to receive(:fft_jpg_or_pdf?).with('invalid_url').and_return(false)
described_class.send(:validate_fft, 'FFT__a', 'invalid_url', errors)
expect(errors).to include('header: FFT__a URL: invalid_url inaccessible')
expect(errors).to include('header: FFT__a URL: invalid_url invalid. needs to be .jpg or .pdf')
end
end
describe '.validate_500__3' do
it 'validates 500__3 field and adds errors' do
row = { '500__3' => 'value' }
errors = []
allow(described_class).to receive(:valid_500__3?).with('500__3', row).and_return(false)
described_class.send(:validate_500__3, '500__3', row, 'value', errors)
expect(errors).to include('header: 500__3 There is a 500__3 without a corresponding 500__a. Value for 500__3 is value')
end
end
describe '.validate_800__6' do
it 'validates 800__6 field and adds errors' do
row = { '800__6' => 'value' }
errors = []
allow(described_class).to receive(:corresponding_6?).with('800__6', row).and_return(false)
described_class.send(:validate_800__6, '800__6', row, 'value', errors)
expect(errors).to include('header: 800__6 There is no matching $6 for value value')
end
end
describe '.valid_url?' do
it 'returns true for a valid URL' do
url = 'https://example.com'
stub_request(:get, url).to_return(status: 200)
expect(described_class.send(:valid_url?, url)).to be true
end
it 'returns false for an invalid URL' do
url = 'https://invalid-url.com'
stub_request(:get, url).to_return(status: 404)
expect(described_class.send(:valid_url?, url)).to be false
end
end
describe '.fft_jpg_or_pdf?' do
it 'returns true for a URL ending with .jpg' do
url = 'https://example.com/image.jpg'
expect(described_class.send(:fft_jpg_or_pdf?, url)).to be true
end
it 'returns true for a URL ending with .pdf' do
url = 'https://example.com/document.pdf'
expect(described_class.send(:fft_jpg_or_pdf?, url)).to be true
end
it 'returns false for a URL not ending with .jpg or .pdf' do
url = 'https://example.com/document.txt'
expect(described_class.send(:fft_jpg_or_pdf?, url)).to be false
end
end
describe '.valid_500__3?' do
it 'returns true if there is a corresponding 500__a' do
row = { '500__3' => 'value', '500__a' => 'value' }
expect(described_class.send(:valid_500__3?, '500__3', row)).to be true
end
it 'returns false if there is no corresponding 500__a' do
row = { '500__3' => 'value' }
expect(described_class.send(:valid_500__3?, '500__3', row)).to be false
end
end
describe '.corresponding_6?' do
it 'returns true if there is a matching $6 field' do
row = { '800__6' => '111-11', '111__6' => '111-11' }
expect(described_class.send(:corresponding_6?, '800__6', row)).to be true
end
it 'returns false if there is no matching $6 field' do
row = { '800__6' => '111-11' }
expect(described_class.send(:corresponding_6?, '800__6', row)).to be false
end
end
describe '.valid_header?' do
it 'returns true for a valid header with standard format' do
expect(described_class.valid_header?('001__a')).to be true
expect(described_class.valid_header?('245__a')).to be true
expect(described_class.valid_header?('Filename')).to be true
expect(described_class.valid_header?('100_1a')).to be true
end
it 'returns true for a valid header with suffix format' do
expect(described_class.valid_header?('001__a-1')).to be true
expect(described_class.valid_header?('245__a-2')).to be true
expect(described_class.valid_header?('500__3-5')).to be true
end
it 'returns false for invalid headers' do
expect(described_class.valid_header?('abc')).to be false
expect(described_class.valid_header?('12__a')).to be false
expect(described_class.valid_header?('001__a-')).to be false
expect(described_class.valid_header?('001__a-ab')).to be false
end
end
end