afd_parser 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/afd_parser.rb +257 -0
- data/lib/afd_parser/clock_in_out.rb +71 -0
- data/lib/afd_parser/header.rb +144 -0
- data/lib/afd_parser/record_parser.rb +65 -0
- data/lib/afd_parser/set_employee.rb +98 -0
- data/lib/afd_parser/set_employer.rb +109 -0
- data/lib/afd_parser/set_time.rb +77 -0
- data/lib/afd_parser/trailer.rb +83 -0
- data/lib/afd_parser/version.rb +3 -0
- data/test/afd_file +11 -0
- data/test/afd_invalid_utf-8_chars +2 -0
- data/test/afd_parser_test.rb +633 -0
- data/test/clock_in_out_test.rb +32 -0
- data/test/header_test.rb +52 -0
- data/test/set_employee_test.rb +67 -0
- data/test/set_employer_test.rb +29 -0
- data/test/set_time_test.rb +23 -0
- data/test/test_helper.rb +25 -0
- data/test/trailer_test.rb +26 -0
- metadata +85 -0
@@ -0,0 +1,633 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# Controle de Horas - Sistema para gestão de horas trabalhadas
|
3
|
+
# Copyright (C) 2009 O.S. Systems Softwares Ltda.
|
4
|
+
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU Affero General Public License as
|
7
|
+
# published by the Free Software Foundation, either version 3 of the
|
8
|
+
# License, or (at your option) any later version.
|
9
|
+
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU Affero General Public License for more details.
|
14
|
+
|
15
|
+
# You should have received a copy of the GNU Affero General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
# Rua Clóvis Gularte Candiota 132, Pelotas-RS, Brasil.
|
19
|
+
# e-mail: contato@ossystems.com.br
|
20
|
+
|
21
|
+
require 'test_helper'
|
22
|
+
require 'tempfile'
|
23
|
+
require 'afd_parser'
|
24
|
+
|
25
|
+
class AfdParserTest < Test::Unit::TestCase
|
26
|
+
def test_parse_file_one_line_at_the_time
|
27
|
+
parser = AfdParser.new(true)
|
28
|
+
file = File.open("test/afd_file", "r")
|
29
|
+
file.readlines.each_with_index do |line, index|
|
30
|
+
parser.parse_line(line, index)
|
31
|
+
end
|
32
|
+
file.close
|
33
|
+
|
34
|
+
parsed_records = parser.records
|
35
|
+
assert_equal 9, parsed_records.size
|
36
|
+
|
37
|
+
assert_equal 0, parser.header.line_id
|
38
|
+
assert_equal 1, parser.header.record_type_id
|
39
|
+
assert_equal :cnpj, parser.header.employer_type
|
40
|
+
assert_equal Time.local(2011,2,21,10,48), parser.header.afd_creation_time
|
41
|
+
assert_equal Date.civil(2011,1,20), parser.header.afd_start_date
|
42
|
+
assert_equal Date.civil(2011,2,22), parser.header.afd_end_date
|
43
|
+
assert_equal "00004000070004403", parser.header.rep_serial_number
|
44
|
+
assert_equal "RAZAO_SOCIAL", parser.header.employer_name
|
45
|
+
assert_equal 67890, parser.header.employer_document
|
46
|
+
assert_equal 9876, parser.header.employer_cei
|
47
|
+
|
48
|
+
set_time = parsed_records[0]
|
49
|
+
assert_equal 1, set_time.line_id
|
50
|
+
assert_equal Time.local(2011,1,28,11,13), set_time.after_time
|
51
|
+
assert_equal Time.local(2011,1,28,11,12), set_time.before_time
|
52
|
+
assert_equal 4, set_time.record_type_id
|
53
|
+
|
54
|
+
set_employer = parsed_records[1]
|
55
|
+
assert_equal 8682040000172, set_employer.document_number
|
56
|
+
assert_equal 2, set_employer.line_id
|
57
|
+
assert_equal "PELOTAS - RS", set_employer.location
|
58
|
+
assert_equal :cnpj, set_employer.document_type
|
59
|
+
assert_equal Time.local(2011,1,27,17,56), set_employer.creation_time
|
60
|
+
assert_equal 0, set_employer.cei
|
61
|
+
assert_equal "O.S. SYSTEMS SOFTWARES LTDA.", set_employer.name
|
62
|
+
assert_equal 2, set_employer.record_type_id
|
63
|
+
|
64
|
+
set_employee = parsed_records[2]
|
65
|
+
assert_equal 111111111111, set_employee.pis
|
66
|
+
assert_equal 3, set_employee.line_id
|
67
|
+
assert_equal :add, set_employee.operation_type
|
68
|
+
assert_equal Time.local(2011,1,27,17,59), set_employee.creation_time
|
69
|
+
assert_equal "TESTE 1 2 3", set_employee.name
|
70
|
+
assert_equal 5, set_employee.record_type_id
|
71
|
+
|
72
|
+
set_employee = parsed_records[3]
|
73
|
+
assert_equal 222222222222, set_employee.pis
|
74
|
+
assert_equal 4, set_employee.line_id
|
75
|
+
assert_equal :add, set_employee.operation_type
|
76
|
+
assert_equal Time.local(2011,2,8,17,9), set_employee.creation_time
|
77
|
+
assert_equal "TESTE 2", set_employee.name
|
78
|
+
assert_equal 5, set_employee.record_type_id
|
79
|
+
|
80
|
+
set_employee = parsed_records[4]
|
81
|
+
assert_equal 222222222222, set_employee.pis
|
82
|
+
assert_equal 5, set_employee.line_id
|
83
|
+
assert_equal :edit, set_employee.operation_type
|
84
|
+
assert_equal Time.local(2011,2,11,17,19), set_employee.creation_time
|
85
|
+
assert_equal "TESTE 2", set_employee.name
|
86
|
+
assert_equal 5, set_employee.record_type_id
|
87
|
+
|
88
|
+
set_employee = parsed_records[5]
|
89
|
+
assert_equal 222222222222, set_employee.pis
|
90
|
+
assert_equal 6, set_employee.line_id
|
91
|
+
assert_equal :remove, set_employee.operation_type
|
92
|
+
assert_equal Time.local(2011,2,11,17,23), set_employee.creation_time
|
93
|
+
assert_equal "TESTE 2", set_employee.name
|
94
|
+
assert_equal 5, set_employee.record_type_id
|
95
|
+
|
96
|
+
clock_in_out = parsed_records[6]
|
97
|
+
assert_equal 111111111111, clock_in_out.pis
|
98
|
+
assert_equal 7, clock_in_out.line_id
|
99
|
+
assert_equal Time.local(2011,2,19,18,14), clock_in_out.creation_time
|
100
|
+
assert_equal 3, clock_in_out.record_type_id
|
101
|
+
|
102
|
+
clock_in_out = parsed_records[7]
|
103
|
+
assert_equal 111111111111, clock_in_out.pis
|
104
|
+
assert_equal 8, clock_in_out.line_id
|
105
|
+
assert_equal Time.local(2011,2,21,11,33), clock_in_out.creation_time
|
106
|
+
assert_equal 3, clock_in_out.record_type_id
|
107
|
+
|
108
|
+
clock_in_out = parsed_records[8]
|
109
|
+
assert_equal 111111111111, clock_in_out.pis
|
110
|
+
assert_equal 9, clock_in_out.line_id
|
111
|
+
assert_equal Time.local(2011,2,21,11,34), clock_in_out.creation_time
|
112
|
+
assert_equal 3, clock_in_out.record_type_id
|
113
|
+
|
114
|
+
assert_equal 3, parser.trailer.clock_in_out
|
115
|
+
assert_equal 999999999, parser.trailer.line_id
|
116
|
+
assert_equal 1, parser.trailer.set_employer
|
117
|
+
assert_equal 4, parser.trailer.set_employee
|
118
|
+
assert_equal 1, parser.trailer.set_time
|
119
|
+
assert_equal 9, parser.trailer.record_type_id
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_export_simple_file
|
123
|
+
file_data = nil
|
124
|
+
File.open("test/afd_file", "r") do |f|
|
125
|
+
file_data = f.readlines.join("")
|
126
|
+
end
|
127
|
+
parser = AfdParser.new("test/afd_file", true)
|
128
|
+
parser.parse
|
129
|
+
assert_equal file_data, parser.export
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_file_one_line_at_the_time_with_created_header_and_trailer
|
133
|
+
line = "0000000014280120111112280120111113\n"
|
134
|
+
parser = AfdParser.new(true)
|
135
|
+
time = Time.now
|
136
|
+
parser.create_header(:cnpj, 12345678901234, 0, "Company name", "12345678901234567", Date.today, Date.today, time)
|
137
|
+
parser.parse_line(line, 1)
|
138
|
+
parser.create_trailer
|
139
|
+
parsed_records = parser.records
|
140
|
+
assert_equal 1, parsed_records.size
|
141
|
+
|
142
|
+
assert_equal 0, parser.header.line_id
|
143
|
+
assert_equal 1, parser.header.record_type_id
|
144
|
+
assert_equal :cnpj, parser.header.employer_type
|
145
|
+
assert_equal time, parser.header.afd_creation_time
|
146
|
+
assert_equal Date.today, parser.header.afd_start_date
|
147
|
+
assert_equal Date.today, parser.header.afd_end_date
|
148
|
+
assert_equal "12345678901234567", parser.header.rep_serial_number
|
149
|
+
assert_equal "Company name", parser.header.employer_name
|
150
|
+
assert_equal 12345678901234, parser.header.employer_document
|
151
|
+
assert_equal 0, parser.header.employer_cei
|
152
|
+
|
153
|
+
set_time = parsed_records[0]
|
154
|
+
assert_equal 1, set_time.line_id
|
155
|
+
assert_equal Time.local(2011,1,28,11,13), set_time.after_time
|
156
|
+
assert_equal Time.local(2011,1,28,11,12), set_time.before_time
|
157
|
+
assert_equal 4, set_time.record_type_id
|
158
|
+
|
159
|
+
assert_equal 0, parser.trailer.clock_in_out
|
160
|
+
assert_equal 999999999, parser.trailer.line_id
|
161
|
+
assert_equal 0, parser.trailer.set_employer
|
162
|
+
assert_equal 0, parser.trailer.set_employee
|
163
|
+
assert_equal 1, parser.trailer.set_time
|
164
|
+
assert_equal 9, parser.trailer.record_type_id
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_reject_file_with_out_of_order_line_ids
|
168
|
+
data = "0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
169
|
+
"0000000014280120111112280120111113\n" +
|
170
|
+
"0000000032270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS \n" +
|
171
|
+
"9999999990000000010000000030000000010000000049"
|
172
|
+
|
173
|
+
file_path = create_temp_afd_file(data)
|
174
|
+
exception = assert_raise AfdParser::AfdParserException do
|
175
|
+
parser = AfdParser.new(file_path, true)
|
176
|
+
parser.parse
|
177
|
+
parsed_records = parser.records
|
178
|
+
end
|
179
|
+
assert_equal "Out-of-order line id on line 1; expected '2', got '3'", exception.message
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_accept_file_with_unexpected_line_id_start_if_option_is_provided
|
183
|
+
data = "0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\r\n" +
|
184
|
+
"0000000024280120111112280120111113\r\n" +
|
185
|
+
"0000000032270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS \r\n" +
|
186
|
+
"9999999990000000010000000000000000010000000009\r\n"
|
187
|
+
|
188
|
+
file_path = create_temp_afd_file(data)
|
189
|
+
parser = AfdParser.new(file_path, {validate_expected_afd_records_ids: false, validate_structure: true})
|
190
|
+
parser.parse
|
191
|
+
assert_equal data, parser.export
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_reject_file_with_unexpected_line_id_start
|
195
|
+
data = "0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
196
|
+
"0000000024280120111112280120111113\n" +
|
197
|
+
"0000000012270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS \n" +
|
198
|
+
"9999999990000000010000000030000000010000000049"
|
199
|
+
|
200
|
+
file_path = create_temp_afd_file(data)
|
201
|
+
exception = assert_raise AfdParser::AfdParserException do
|
202
|
+
parser = AfdParser.new(file_path, true)
|
203
|
+
parser.parse
|
204
|
+
parsed_records = parser.records
|
205
|
+
end
|
206
|
+
assert_equal "AFD records starts at an unexpected line ID; expected '1', got '2'.", exception.message
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_reject_file_with_unknown_record_type
|
210
|
+
# The second line contains a set_time record, but with type number 8,
|
211
|
+
# which doesn't exists:
|
212
|
+
data = "0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
213
|
+
"0000000018280120111112280120111113\n" +
|
214
|
+
"0000000022270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS \n" +
|
215
|
+
"9999999990000000010000000030000000010000000049"
|
216
|
+
|
217
|
+
file_path = create_temp_afd_file(data)
|
218
|
+
exception = assert_raise AfdParser::AfdParserException do
|
219
|
+
parser = AfdParser.new(file_path, true)
|
220
|
+
parser.parse
|
221
|
+
parsed_records = parser.records
|
222
|
+
end
|
223
|
+
assert_equal "Unknown record type found in AFD file, line 1: '0000000018280120111112280120111113\n'", exception.message
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_reject_file_with_second_header
|
227
|
+
data = "0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
228
|
+
"0000000011100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
229
|
+
"0000000024280120111112280120111113\n" +
|
230
|
+
"9999999990000000000000000000000000010000000009\n"
|
231
|
+
file_path = create_temp_afd_file(data)
|
232
|
+
exception = assert_raise AfdParser::AfdParserException do
|
233
|
+
parser = AfdParser.new(file_path, true)
|
234
|
+
parser.parse
|
235
|
+
parsed_records = parser.records
|
236
|
+
end
|
237
|
+
assert_equal "Unexpected second AFD header found, line 1: '0000000011100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n'", exception.message
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_reject_file_with_data_before_header
|
241
|
+
data = "0000000002270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS \n"+
|
242
|
+
"0000000011100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
243
|
+
"0000000024280120111112280120111113\n" +
|
244
|
+
"9999999990000000000000000000000000010000000009\n"
|
245
|
+
file_path = create_temp_afd_file(data)
|
246
|
+
exception = assert_raise AfdParser::AfdParserException do
|
247
|
+
parser = AfdParser.new(file_path, true)
|
248
|
+
parser.parse
|
249
|
+
parsed_records = parser.records
|
250
|
+
end
|
251
|
+
assert_equal "Unexpected AFD record found before header, line 0: '0000000002270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS \n'", exception.message
|
252
|
+
end
|
253
|
+
|
254
|
+
def test_reject_file_with_no_trailer
|
255
|
+
data = "0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
256
|
+
"0000000014280120111112280120111113\n" +
|
257
|
+
"0000000022270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS "
|
258
|
+
file_path = create_temp_afd_file(data)
|
259
|
+
exception = assert_raise AfdParser::AfdParserException do
|
260
|
+
parser = AfdParser.new(file_path, true)
|
261
|
+
parser.parse
|
262
|
+
parsed_records = parser.records
|
263
|
+
end
|
264
|
+
assert_equal "AFD ended without a trailer record", exception.message
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_reject_file_with_data_after_trailer
|
268
|
+
data = "0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
269
|
+
"0000000014280120111112280120111113\n" +
|
270
|
+
"9999999990000000000000000000000000010000000009\n" +
|
271
|
+
"0000000032270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS "
|
272
|
+
file_path = create_temp_afd_file(data)
|
273
|
+
exception = assert_raise AfdParser::AfdParserException do
|
274
|
+
parser = AfdParser.new(file_path, true)
|
275
|
+
parser.parse
|
276
|
+
parsed_records = parser.records
|
277
|
+
end
|
278
|
+
assert_equal "Unexpected AFD record found after trailer, line 3: '0000000032270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS '", exception.message
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_reject_file_with_unknown_employer_type_id_in_header
|
282
|
+
data = "0000000001300000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
283
|
+
"0000000014280120111112280120111113\n" +
|
284
|
+
"0000000022270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS \n" +
|
285
|
+
"9999999990000000010000000030000000010000000049"
|
286
|
+
|
287
|
+
file_path = create_temp_afd_file(data)
|
288
|
+
exception = assert_raise AfdParser::AfdParserException do
|
289
|
+
parser = AfdParser.new(file_path, true)
|
290
|
+
parser.parse
|
291
|
+
parsed_records = parser.records
|
292
|
+
end
|
293
|
+
assert_equal "Unknown employer type id '3' found in AFD header", exception.message
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_reject_file_with_unknown_employee_operation_type_id
|
297
|
+
data = "0000000001200000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
298
|
+
"0000000014280120111112280120111113\n" +
|
299
|
+
"0000000025080220111709C222222222222TESTE 2 \n" +
|
300
|
+
"9999999990000000000000000010000000010000000019"
|
301
|
+
|
302
|
+
file_path = create_temp_afd_file(data)
|
303
|
+
exception = assert_raise AfdParser::AfdParserException do
|
304
|
+
parser = AfdParser.new(file_path, true)
|
305
|
+
parser.parse
|
306
|
+
parsed_records = parser.records
|
307
|
+
end
|
308
|
+
assert_equal "Unknown employee operation type letter 'C' found in line 2", exception.message
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_reject_file_with_unknown_employer_type_id
|
312
|
+
data = "0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
313
|
+
"0000000014280120111112280120111113\n" +
|
314
|
+
"0000000022270120111756308682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS \n" +
|
315
|
+
"9999999990000000010000000000000000010000000009"
|
316
|
+
|
317
|
+
file_path = create_temp_afd_file(data)
|
318
|
+
exception = assert_raise AfdParser::AfdParserException do
|
319
|
+
parser = AfdParser.new(file_path, true)
|
320
|
+
parser.parse
|
321
|
+
parsed_records = parser.records
|
322
|
+
end
|
323
|
+
assert_equal "Unknown employer type id '3' found in set employer record on line 2", exception.message
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_reject_file_with_difference_in_trailer_count_data
|
327
|
+
data = "0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048\n" +
|
328
|
+
"0000000014280120111112280120111113\n" +
|
329
|
+
"0000000022270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS \n"
|
330
|
+
trailer = "9999999990000000020000000000000000010000000009"
|
331
|
+
|
332
|
+
file_path = create_temp_afd_file(data + trailer)
|
333
|
+
exception = assert_raise AfdParser::AfdParserException do
|
334
|
+
parser = AfdParser.new(file_path, true)
|
335
|
+
parser.parse
|
336
|
+
parsed_records = parser.records
|
337
|
+
end
|
338
|
+
assert_equal "Mismatch counting changes of set_employer in REP!\nREP: 2 | System: 1", exception.message
|
339
|
+
|
340
|
+
trailer = "9999999990000000010000000010000000010000000009"
|
341
|
+
|
342
|
+
file_path = create_temp_afd_file(data + trailer)
|
343
|
+
exception = assert_raise AfdParser::AfdParserException do
|
344
|
+
parser = AfdParser.new(file_path, true)
|
345
|
+
parser.parse
|
346
|
+
parsed_records = parser.records
|
347
|
+
end
|
348
|
+
assert_equal "Mismatch counting changes of clock_in_out in REP!\nREP: 1 | System: 0", exception.message
|
349
|
+
|
350
|
+
trailer = "9999999990000000010000000000000000020000000009"
|
351
|
+
|
352
|
+
file_path = create_temp_afd_file(data + trailer)
|
353
|
+
exception = assert_raise AfdParser::AfdParserException do
|
354
|
+
parser = AfdParser.new(file_path, true)
|
355
|
+
parser.parse
|
356
|
+
parsed_records = parser.records
|
357
|
+
end
|
358
|
+
assert_equal "Mismatch counting changes of set_time in REP!\nREP: 2 | System: 1", exception.message
|
359
|
+
|
360
|
+
trailer = "9999999990000000010000000000000000010000000029"
|
361
|
+
|
362
|
+
file_path = create_temp_afd_file(data + trailer)
|
363
|
+
exception = assert_raise AfdParser::AfdParserException do
|
364
|
+
parser = AfdParser.new(file_path, true)
|
365
|
+
parser.parse
|
366
|
+
parsed_records = parser.records
|
367
|
+
end
|
368
|
+
assert_equal "Mismatch counting changes of set_employee in REP!\nREP: 2 | System: 0", exception.message
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_first_creation_date
|
372
|
+
parser = AfdParser.new(true)
|
373
|
+
assert_nil parser.first_creation_date
|
374
|
+
|
375
|
+
file = File.open("test/afd_file", "r")
|
376
|
+
file.readlines.each_with_index do |line, index|
|
377
|
+
parser.parse_line(line, index)
|
378
|
+
end
|
379
|
+
file.close
|
380
|
+
|
381
|
+
assert_equal Date.civil(2011,1,28), parser.first_creation_date
|
382
|
+
end
|
383
|
+
|
384
|
+
def test_last_creation_date
|
385
|
+
parser = AfdParser.new(true)
|
386
|
+
assert_nil parser.last_creation_date
|
387
|
+
|
388
|
+
file = File.open("test/afd_file", "r")
|
389
|
+
file.readlines.each_with_index do |line, index|
|
390
|
+
parser.parse_line(line, index)
|
391
|
+
end
|
392
|
+
file.close
|
393
|
+
|
394
|
+
assert_equal Date.civil(2011,2,21), parser.last_creation_date
|
395
|
+
end
|
396
|
+
|
397
|
+
def test_equal
|
398
|
+
parser1 = AfdParser.new(true)
|
399
|
+
file = File.open("test/afd_file", "r")
|
400
|
+
file.readlines.each_with_index do |line, index|
|
401
|
+
parser1.parse_line(line, index)
|
402
|
+
end
|
403
|
+
file.close
|
404
|
+
|
405
|
+
parser2 = AfdParser.new(true)
|
406
|
+
file = File.open("test/afd_file", "r")
|
407
|
+
file.readlines.each_with_index do |line, index|
|
408
|
+
parser2.parse_line(line, index)
|
409
|
+
end
|
410
|
+
file.close
|
411
|
+
assert_equal parser2, parser1
|
412
|
+
|
413
|
+
different_data = ["0000000001100000000067890000000009876RAZAO_SOCIAL 000040000700044032001201122022011210220111048",
|
414
|
+
"0000000014280120111112280120111113",
|
415
|
+
"0000000022270120111756108682040000172000000000000O.S. SYSTEMS SOFTWARES LTDA. PELOTAS - RS ",
|
416
|
+
"0000000035270120111759I111111111111TESTE 1 2 3 ",
|
417
|
+
"0000000045110220111723E222222222222TESTE 2 ",
|
418
|
+
"0000000053210220111134111111111111",
|
419
|
+
"9999999990000000010000000010000000010000000029"]
|
420
|
+
|
421
|
+
parser2 = AfdParser.new(true)
|
422
|
+
different_data.each_with_index do |line, index|
|
423
|
+
parser2.parse_line(line, index)
|
424
|
+
end
|
425
|
+
assert_not_equal parser2, parser1
|
426
|
+
end
|
427
|
+
|
428
|
+
def test_get_last_id
|
429
|
+
parser = AfdParser.new(true)
|
430
|
+
assert_nil parser.first_id
|
431
|
+
assert_nil parser.last_id
|
432
|
+
|
433
|
+
parser = AfdParser.new(true)
|
434
|
+
file = File.open("test/afd_file", "r")
|
435
|
+
file.readlines.each_with_index do |line, index|
|
436
|
+
parser.parse_line(line, index)
|
437
|
+
end
|
438
|
+
file.close
|
439
|
+
assert_equal 1, parser.first_id
|
440
|
+
assert_equal 9, parser.last_id
|
441
|
+
|
442
|
+
different_data = [["0000000001100000000067890000000009876NOVISSIMA EMPRESA SA. 000040000700044032001201105082011050820111711",0],
|
443
|
+
["0000000102270120111756108682040000172000000000000NOVISSIMA EMPRESA SA. PELOTAS - RS ",10],
|
444
|
+
["0000000113210220111134111111111111",11]]
|
445
|
+
|
446
|
+
parser = AfdParser.new(true)
|
447
|
+
different_data.each do |line, index|
|
448
|
+
parser.parse_line(line, index)
|
449
|
+
end
|
450
|
+
|
451
|
+
assert_equal 10, parser.first_id
|
452
|
+
assert_equal 11, parser.last_id
|
453
|
+
|
454
|
+
different_data = [["0000000001100000000067890000000009876NOVISSIMA EMPRESA SA. 000040000700044032001201105082011050820111711",0],
|
455
|
+
["0000000102270120111756108682040000172000000000000NOVISSIMA EMPRESA SA. PELOTAS - RS ",10]]
|
456
|
+
|
457
|
+
parser = AfdParser.new(true)
|
458
|
+
different_data.each do |line, index|
|
459
|
+
parser.parse_line(line, index)
|
460
|
+
end
|
461
|
+
|
462
|
+
assert_equal 10, parser.last_id
|
463
|
+
assert_equal 10, parser.last_id
|
464
|
+
end
|
465
|
+
|
466
|
+
def test_merge
|
467
|
+
parser1 = AfdParser.new(true)
|
468
|
+
file = File.open("test/afd_file", "r")
|
469
|
+
file.readlines.each_with_index do |line, index|
|
470
|
+
parser1.parse_line(line, index)
|
471
|
+
end
|
472
|
+
|
473
|
+
different_data = [["0000000001100000000067890000000009876NOVISSIMA EMPRESA SA. 000040000700044032001201105082011050820111711",0],
|
474
|
+
["0000000102270120111756108682040000172000000000000NOVISSIMA EMPRESA SA. PELOTAS - RS ",10],
|
475
|
+
["0000000113050820111134111111111111",11]]
|
476
|
+
|
477
|
+
parser2 = AfdParser.new(true)
|
478
|
+
exception = assert_raise AfdParser::AfdParserException do
|
479
|
+
parser1.merge parser2
|
480
|
+
end
|
481
|
+
assert_equal "Cannot merge with a empty parser", exception.message
|
482
|
+
|
483
|
+
different_data.each do |line, index|
|
484
|
+
parser2.parse_line(line, index)
|
485
|
+
end
|
486
|
+
parser1.merge parser2
|
487
|
+
|
488
|
+
parsed_records = parser1.records
|
489
|
+
assert_equal 11, parsed_records.size
|
490
|
+
|
491
|
+
assert_equal 0, parser1.header.line_id
|
492
|
+
assert_equal 1, parser1.header.record_type_id
|
493
|
+
assert_equal :cnpj, parser1.header.employer_type
|
494
|
+
assert_equal Time.local(2011,8,5,17,11), parser1.header.afd_creation_time
|
495
|
+
assert_equal Date.civil(2011,1,20), parser1.header.afd_start_date
|
496
|
+
assert_equal Date.civil(2011,8,5), parser1.header.afd_end_date
|
497
|
+
assert_equal "00004000070004403", parser1.header.rep_serial_number
|
498
|
+
assert_equal "NOVISSIMA EMPRESA SA.", parser1.header.employer_name
|
499
|
+
assert_equal 67890, parser1.header.employer_document
|
500
|
+
assert_equal 9876, parser1.header.employer_cei
|
501
|
+
|
502
|
+
set_time = parsed_records[0]
|
503
|
+
assert_equal 1, set_time.line_id
|
504
|
+
assert_equal Time.local(2011,1,28,11,13), set_time.after_time
|
505
|
+
assert_equal Time.local(2011,1,28,11,12), set_time.before_time
|
506
|
+
assert_equal 4, set_time.record_type_id
|
507
|
+
|
508
|
+
set_employer = parsed_records[1]
|
509
|
+
assert_equal 8682040000172, set_employer.document_number
|
510
|
+
assert_equal 2, set_employer.line_id
|
511
|
+
assert_equal "PELOTAS - RS", set_employer.location
|
512
|
+
assert_equal :cnpj, set_employer.document_type
|
513
|
+
assert_equal Time.local(2011,1,27,17,56), set_employer.creation_time
|
514
|
+
assert_equal 0, set_employer.cei
|
515
|
+
assert_equal "O.S. SYSTEMS SOFTWARES LTDA.", set_employer.name
|
516
|
+
assert_equal 2, set_employer.record_type_id
|
517
|
+
|
518
|
+
set_employee = parsed_records[2]
|
519
|
+
assert_equal 111111111111, set_employee.pis
|
520
|
+
assert_equal 3, set_employee.line_id
|
521
|
+
assert_equal :add, set_employee.operation_type
|
522
|
+
assert_equal Time.local(2011,1,27,17,59), set_employee.creation_time
|
523
|
+
assert_equal "TESTE 1 2 3", set_employee.name
|
524
|
+
assert_equal 5, set_employee.record_type_id
|
525
|
+
|
526
|
+
set_employee = parsed_records[3]
|
527
|
+
assert_equal 222222222222, set_employee.pis
|
528
|
+
assert_equal 4, set_employee.line_id
|
529
|
+
assert_equal :add, set_employee.operation_type
|
530
|
+
assert_equal Time.local(2011,2,8,17,9), set_employee.creation_time
|
531
|
+
assert_equal "TESTE 2", set_employee.name
|
532
|
+
assert_equal 5, set_employee.record_type_id
|
533
|
+
|
534
|
+
set_employee = parsed_records[4]
|
535
|
+
assert_equal 222222222222, set_employee.pis
|
536
|
+
assert_equal 5, set_employee.line_id
|
537
|
+
assert_equal :edit, set_employee.operation_type
|
538
|
+
assert_equal Time.local(2011,2,11,17,19), set_employee.creation_time
|
539
|
+
assert_equal "TESTE 2", set_employee.name
|
540
|
+
assert_equal 5, set_employee.record_type_id
|
541
|
+
|
542
|
+
set_employee = parsed_records[5]
|
543
|
+
assert_equal 222222222222, set_employee.pis
|
544
|
+
assert_equal 6, set_employee.line_id
|
545
|
+
assert_equal :remove, set_employee.operation_type
|
546
|
+
assert_equal Time.local(2011,2,11,17,23), set_employee.creation_time
|
547
|
+
assert_equal "TESTE 2", set_employee.name
|
548
|
+
assert_equal 5, set_employee.record_type_id
|
549
|
+
|
550
|
+
clock_in_out = parsed_records[6]
|
551
|
+
assert_equal 111111111111, clock_in_out.pis
|
552
|
+
assert_equal 7, clock_in_out.line_id
|
553
|
+
assert_equal Time.local(2011,2,19,18,14), clock_in_out.creation_time
|
554
|
+
assert_equal 3, clock_in_out.record_type_id
|
555
|
+
|
556
|
+
clock_in_out = parsed_records[7]
|
557
|
+
assert_equal 111111111111, clock_in_out.pis
|
558
|
+
assert_equal 8, clock_in_out.line_id
|
559
|
+
assert_equal Time.local(2011,2,21,11,33), clock_in_out.creation_time
|
560
|
+
assert_equal 3, clock_in_out.record_type_id
|
561
|
+
|
562
|
+
clock_in_out = parsed_records[8]
|
563
|
+
assert_equal 111111111111, clock_in_out.pis
|
564
|
+
assert_equal 9, clock_in_out.line_id
|
565
|
+
assert_equal Time.local(2011,2,21,11,34), clock_in_out.creation_time
|
566
|
+
assert_equal 3, clock_in_out.record_type_id
|
567
|
+
|
568
|
+
set_employer = parsed_records[9]
|
569
|
+
assert_equal 8682040000172, set_employer.document_number
|
570
|
+
assert_equal 10, set_employer.line_id
|
571
|
+
assert_equal "PELOTAS - RS", set_employer.location
|
572
|
+
assert_equal :cnpj, set_employer.document_type
|
573
|
+
assert_equal Time.local(2011,1,27,17,56), set_employer.creation_time
|
574
|
+
assert_equal 0, set_employer.cei
|
575
|
+
assert_equal "NOVISSIMA EMPRESA SA.", set_employer.name
|
576
|
+
assert_equal 2, set_employer.record_type_id
|
577
|
+
|
578
|
+
clock_in_out = parsed_records[10]
|
579
|
+
assert_equal 111111111111, clock_in_out.pis
|
580
|
+
assert_equal 11, clock_in_out.line_id
|
581
|
+
assert_equal Time.local(2011,8,5,11,34), clock_in_out.creation_time
|
582
|
+
assert_equal 3, clock_in_out.record_type_id
|
583
|
+
|
584
|
+
assert_equal 4, parser1.trailer.clock_in_out
|
585
|
+
assert_equal 999999999, parser1.trailer.line_id
|
586
|
+
assert_equal 2, parser1.trailer.set_employer
|
587
|
+
assert_equal 4, parser1.trailer.set_employee
|
588
|
+
assert_equal 1, parser1.trailer.set_time
|
589
|
+
assert_equal 9, parser1.trailer.record_type_id
|
590
|
+
end
|
591
|
+
|
592
|
+
def test_load_ignoring_invalid_utf_8_data
|
593
|
+
parser = AfdParser.new(true)
|
594
|
+
file = File.open("test/afd_invalid_utf-8_chars", "r")
|
595
|
+
file.readlines.each_with_index do |line, index|
|
596
|
+
parser.parse_line(line, index)
|
597
|
+
end
|
598
|
+
|
599
|
+
parsed_records = parser.records
|
600
|
+
assert_equal 1, parsed_records.size
|
601
|
+
|
602
|
+
assert_equal 0, parser.header.line_id
|
603
|
+
assert_equal 1, parser.header.record_type_id
|
604
|
+
assert_equal :cnpj, parser.header.employer_type
|
605
|
+
assert_equal Time.local(2011,8,5,17,11), parser.header.afd_creation_time
|
606
|
+
assert_equal Date.civil(2011,1,20), parser.header.afd_start_date
|
607
|
+
assert_equal Date.civil(2011,8,5), parser.header.afd_end_date
|
608
|
+
assert_equal "00004000070004403", parser.header.rep_serial_number
|
609
|
+
assert_equal "NOVISSIMA EMPRESA SA.", parser.header.employer_name
|
610
|
+
assert_equal 67890, parser.header.employer_document
|
611
|
+
assert_equal 9876, parser.header.employer_cei
|
612
|
+
|
613
|
+
set_employer = parsed_records[0]
|
614
|
+
assert_equal 11111111111111, set_employer.document_number
|
615
|
+
assert_equal 1, set_employer.line_id
|
616
|
+
assert_equal "INVALID COMPANY ERRORs\u0087¼g¾\u008F¢ñöÊf\u008F&\u0001þ", set_employer.name
|
617
|
+
assert_equal "COMPANY LOCATION WITH ERROR s\u0087¼g¾\u008F¢ñöÊf\u008F&\u0001þ", set_employer.location
|
618
|
+
assert_equal :cnpj, set_employer.document_type
|
619
|
+
assert_equal Time.local(2011,1,24,16,59), set_employer.creation_time
|
620
|
+
assert_equal 0, set_employer.cei
|
621
|
+
assert_equal 2, set_employer.record_type_id
|
622
|
+
|
623
|
+
assert_nil parser.trailer
|
624
|
+
end
|
625
|
+
|
626
|
+
private
|
627
|
+
def create_temp_afd_file(data)
|
628
|
+
file = Tempfile.new('afd_error')
|
629
|
+
file.write data
|
630
|
+
file.close
|
631
|
+
file.path
|
632
|
+
end
|
633
|
+
end
|