Passed
Push — main ( 97b175...ba1019 )
by Sat CFDI
05:01
created

test_diot.test_create_declaracion_diot()   B

Complexity

Conditions 8

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 37
nop 0
dl 0
loc 53
rs 7.1253
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import os
2
from binascii import unhexlify
3
from datetime import datetime, date
4
from io import BytesIO
5
from unittest import mock
6
from zipfile import ZipInfo
7
8
from satcfdi.diot.code import Periodo, TipoOperacion, TipoTercero, Pais
9
from satcfdi.diot import DatosIdentificacion, DatosComplementaria, ProveedorTercero, DIOT
10
from tests.utils import get_signer, verify_result
11
12
current_dir = os.path.dirname(__file__)
13
module = 'satcfdi'
14
15
16
def test_encrypt_signature():
17
    signer = get_signer('cacx7605101p8')
18
    key = b'\x9e@@hL\xcd\xf2\x9eR\xe6JC\xc8\xba\x8a\xf1C\xec\rE*\x16 4'
19
20
    # from DIOT project try 1
21
    sig = "1a7f1f4d8ef5091a4dc797a80327756110878b08555d84761475e444ad4effe855b44aafb7c47a3627d21f34d40a59fe103d53eda40721d8196d745ce39a6bce97e064a3725840dcf340f7c9ad84f8ab891198f25bf81802f0e8363d4ce02c78268ce75bda284d0771bde7cdf6b6c76adbc607823d54edfa8f9e5385ee8ffa406e14811738781678faff32d0d6b9c843e38ba7a29b8b2414930a200d0512ee69ca693ba341b9462476fbaa085d56178915c03d9bc61ae37d8609fe75c87659e687bd0ca56bcf5a52e2b74fbb67bcd1761ee8360cddb82470a2106a30a9299a7415ccc12a5f37d5b5732e84fd21aba7b3caba49eb913925c9869ca643df1dba8c"
22
    sig = unhexlify(sig)
23
    res = signer.decrypt(sig)
24
    assert res == key
25
26
    # from DIOT project try 2
27
    sig = "768cce9ca1955dbe4b9e768413e37d23f0b1c3e2d3d313278053f2c66aae14d7ecaacdf4a942dff564489156dfb805a3713b7ad48f230762acb493840a1236e445844ba41ca299d715a63dca227f86679322a4864aa30a28b9f43200c83748816900e9b06f224d3ba3bae284ec9916c752da12f0bb189ef1eb64f71bae6ced280f3009798002dcae9129c5105efef1179d93a434c8ebd6144a0f5721deea7c864ed273b8410e2dbdc1d0e684f49c37d58a3311a673ff347f59115ecdf6ba5d05b5e2d2caa45f1e0d2a842057467ed8069de8f79e6abe449c7f1a3222d873d859ad6e6f9608342e1dc709c8b50cb67a07016c75230495938b15e174e0277ac9f1"
28
    sig = unhexlify(sig)
29
    res = signer.decrypt(sig)
30
    assert res == key
31
32
    enc_key = signer.encrypt(data=key)
33
    dec_key = signer.decrypt(data=enc_key)
34
35
    assert dec_key == key
36
37
38
def test_create_filename():
39
    rfc = 'XIQB891116QE4'
40
    with mock.patch(f'{module}.diot.datetime') as m:
41
        m.now = mock.Mock(return_value=datetime(2022, 11, 8, 11, 40))
42
        diot = DIOT(
43
            datos_identificacion=DatosIdentificacion(
44
                rfc=rfc,
45
                ejercicio=2022,
46
            ),
47
            periodo=Periodo.OCTUBRE,
48
            proveedores=[]
49
        )
50
        assert diot._tmp_filename() == 'XIQB891116QE40DOTAAM1MCMB811401'
51
52
    with mock.patch(f'{module}.diot.datetime') as m:
53
        p = ProveedorTercero(
54
            tipo_tercero=TipoTercero.PROVEEDOR_GLOBAL,
55
            tipo_operacion=TipoOperacion.OTROS
56
        )
57
        m.now = mock.Mock(return_value=datetime(2022, 11, 8, 13, 20))
58
        diot = DIOT(
59
            datos_identificacion=DatosIdentificacion(
60
                rfc=rfc,
61
                ejercicio=2022,
62
            ),
63
            periodo=Periodo.OCTUBRE,
64
            proveedores=[p for _ in range(40001)]
65
        )
66
        assert diot._tmp_filename() == 'XIQB891116QE40DOTAAM1MCMB813202'
67
68
    rfc = "OÑO120726RX3"
69
    with mock.patch(f'{module}.diot.datetime') as m:
70
        m.now = mock.Mock(return_value=datetime(2022, 11, 8, 17, 18))
71
        diot = DIOT(
72
            datos_identificacion=DatosIdentificacion(
73
                rfc=rfc,
74
                ejercicio=2022,
75
            ),
76
            periodo=Periodo.OCTUBRE,
77
            proveedores=[]
78
        )
79
        assert diot._tmp_filename() == '_OÑO120726RX30DOTAAM1MCMB817181'
80
81
82
xiqb891116qe4_issuer = DatosIdentificacion(
83
    rfc='xiqb891116qe4',
84
    ejercicio=2022,
85
    curp='xiqb891116mgrmzr05',
86
    nombre='BERENICE',
87
    apellido_paterno='XIMO',
88
    apellido_materno='QUEZADA'
89
)
90
91
92
def test_create_declaracion_diot():
93
    text_file = "XIQB891116QE40DOTAAM1MCMB811401"
94
    diot = DIOT(
95
        datos_identificacion=xiqb891116qe4_issuer,
96
        periodo=Periodo.OCTUBRE
97
    )
98
    # Verify TXT
99
    original_txt_data = open(os.path.join(current_dir, 'diot', f'{text_file}.txt'), 'rb').read()
100
    with BytesIO() as b:
101
        diot.plain_write(target=b)
102
        assert original_txt_data == b.getvalue()
103
104
    # Verify FILENAME
105
    with mock.patch(f'{module}.diot.datetime') as m:
106
        m.now = mock.Mock(return_value=datetime(2022, 11, 8, 11, 40))
107
        tmp_filename = diot._tmp_filename()
108
        assert tmp_filename == text_file
109
110
    # Verify ZIP
111
    def zi(filename):
112
        return ZipInfo(
113
            filename=filename,
114
            date_time=(2022, 11, 8, 11, 41, 8)  # time.localtime(time.time())[:6]
115
        )
116
117
    with mock.patch(f'{module}.zip.ZipInfo', zi) as m:
118
        my_zip = diot._zip_bytes(tmp_filename)
119
120
        original_zip_data = open(os.path.join(current_dir, 'diot', f'{text_file}.zpi'), 'rb').read()
121
        assert my_zip == original_zip_data
122
123
        # Verify DEC
124
        def encrypt(_, data):
125
            return b'HB\x9cWQ\xbf\x975i\xfd\xcbH\x1c\xacD\x00pp\'%\xf3"\xa7\x802O\xcagG\x8c6\x07\xa7n9\xe6\x10*P\xd1\x9d\xf8,\x7f\x84T%\x9eR\xea\xab\x00\xeeX7\x00\x87\xfe\xb5\xf7\x9d\xc4\xa2\xbcu\xa4\x13SX\xee\xe6\xfeSv\x16\xcc\xb2y\xbd4nD\xb0\xbd\x10^y\x9f/PZ\x9b\xd1\xc0\x98\x84\xb6\x90\xc1"\xe0\xb9\x83y\x92\x85\x832\x98C\xd0\xe8\x81P\x0b\xde\x14\xaa\x19_\x0c\xf0\xf2\xec\xdc\x9f\x06i'
126
127
        def rand(dig):
128
            if dig == 8:
129
                return b'p*A\xe4\xcb\x88\xa3\x8f'
130
            if dig == 24:
131
                return b'\x9e@@hL\xcd\xf2\x9eR\xe6JC\xc8\xba\x8a\xf1C\xec\rE*\x16 4'
132
133
        with mock.patch(f'{module}.Certificate.encrypt', encrypt), mock.patch('os.urandom', rand):
134
            dec_data = diot._encrypted_bytes(tmp_filename)
135
            assert dec_data == open(os.path.join(current_dir, 'diot', f'{text_file}.dec'), 'rb').read()
136
137
            with mock.patch(f'{module}.diot.datetime') as m:
138
                m.now = mock.Mock(return_value=datetime(2022, 11, 8, 11, 40))
139
140
                res_filename = diot.generate_package(
141
                    os.path.join(current_dir, 'test_diot')
142
                )
143
                assert dec_data == open(os.path.join(current_dir, 'test_diot', f'{text_file}.dec'), 'rb').read()
144
                assert os.path.basename(res_filename) == f'{text_file}.dec'
145
146
147
def test_create_declaracion_diot_complementary():
148
    diot_file = "XIQB891116QE40DOTAAM1MCMB816201"
149
    diot = DIOT(
150
        datos_identificacion=xiqb891116qe4_issuer,
151
        periodo=Periodo.MARZO_ABRIL,
152
        complementaria=DatosComplementaria(
153
            folio_anterior="6565",
154
            fecha_presentacion_anterior=date(2022, 11, 7)
155
        )
156
    )
157
    reference_data = open(os.path.join(current_dir, 'diot', f'{diot_file}.txt'), 'rb').read()
158
    assert reference_data == diot.plain_bytes()
159
160
161
def test_create_declaracion_diot_complementary2():
162
    diot_file = "XIQB891116QE40DOTAAM1MCMB901581"
163
    diot = DIOT(
164
        datos_identificacion=xiqb891116qe4_issuer,
165
        periodo=Periodo.ENERO,
166
        complementaria=DatosComplementaria(
167
            folio_anterior="4567",
168
            fecha_presentacion_anterior=date(2022, 2, 13)
169
        )
170
    )
171
    reference_data = open(os.path.join(current_dir, 'diot', f'{diot_file}.txt'), 'rb').read()
172
    assert reference_data == diot.plain_bytes()
173
174
175
def test_create_declaracion_diot_trimestral():
176
    diot_file = "XIQB891116QE40DOTAAM1MCMB816531"
177
    diot = DIOT(
178
        datos_identificacion=xiqb891116qe4_issuer,
179
        periodo=Periodo.JULIO_SEPTIEMBRE
180
    )
181
    reference_data = open(os.path.join(current_dir, 'diot', f'{diot_file}.txt'), 'rb').read()
182
    assert reference_data == diot.plain_bytes()
183
184
185
ono120726rx3_issue = DatosIdentificacion(
186
    rfc="OÑO120726RX3",
187
    razon_social="ORGANICOS ÑAVEZ OSORIO S.A DE C.V",
188
    ejercicio=2022,
189
)
190
191
192
def test_create_declaracion_diot_moral():
193
    diot_file = "_OÑO120726RX30DOTAAM1MCMB817181"
194
    diot = DIOT(
195
        datos_identificacion=ono120726rx3_issue,
196
        periodo=Periodo.MAYO_AGOSTO
197
    )
198
    reference_data = open(os.path.join(current_dir, 'diot', f'{diot_file}.txt'), 'rb').read()
199
    assert reference_data == diot.plain_bytes()
200
201
202
def test_create_declaracion_diot_moral_da():
203
    diot_file = "_OÑO120726RX30DOTAAM1MCMB820181"
204
    diot = DIOT(
205
        datos_identificacion=ono120726rx3_issue,
206
        periodo=Periodo.OCTUBRE,
207
        proveedores=[
208
            ProveedorTercero(
209
                tipo_tercero=TipoTercero.PROVEEDOR_NACIONAL,
210
                tipo_operacion=TipoOperacion.PRESTACION_DE_SERVICIOS_PROFESIONALES,
211
                rfc="LIÑI920228KS8",
212
                iva16=52000,
213
                iva16_na=48521,
214
                iva_rfn=42000,
215
                iva_rfn_na=32000,
216
                iva0=31000,
217
                iva_exento=21000,
218
                retenido=11000,
219
                devoluciones=10000
220
            ),
221
            ProveedorTercero(
222
                tipo_tercero=TipoTercero.PROVEEDOR_NACIONAL,
223
                tipo_operacion=TipoOperacion.ARRENDAMIENTO_DE_INMUEBLES,
224
                rfc="IVD920810GU2",
225
                iva16=987,
226
                iva16_na=876,
227
                iva_rfn=765,
228
                iva_rfn_na=654,
229
                iva0=324,
230
                iva_exento=251,
231
                retenido=125,
232
                devoluciones=444
233
            ),
234
        ]
235
    )
236
    reference_data = open(os.path.join(current_dir, 'diot', f'{diot_file}.txt'), 'rb').read()
237
    assert reference_data == diot.plain_bytes()
238
239
240
def test_create_declaracion_diot_moral_full():
241
    diot_file = "_OÑO120726RX30DOTAAL1LCMB823021"
242
    diot = DIOT(
243
        datos_identificacion=DatosIdentificacion(
244
            rfc="OÑO120726RX3",
245
            razon_social="ORGANICOS ÑAVEZ OSORIO S.A DE C.V",
246
            ejercicio=2021,
247
        ),
248
        periodo=Periodo.JULIO_SEPTIEMBRE,
249
        proveedores=[
250
            ProveedorTercero(
251
                tipo_tercero=TipoTercero.PROVEEDOR_EXTRANJERO,
252
                tipo_operacion=TipoOperacion.OTROS,
253
                id_fiscal="1254TAXID",
254
                nombre_extranjero="NOMBREEXTRANJERO",
255
                pais=Pais.ANTIGUA_Y_BERMUDA,
256
                nacionalidad="BERMUDO",
257
                iva16=456,
258
                iva16_na=752,
259
                iva_rfn=782,
260
                iva_rfn_na=456,
261
                iva_import16=123,
262
                iva_import16_na=475,
263
                iva_import_exento=7575,
264
                iva0=45213,
265
                iva_exento=1247,
266
                retenido=235,
267
                devoluciones=786
268
            ),
269
            ProveedorTercero(
270
                tipo_tercero=TipoTercero.PROVEEDOR_GLOBAL,
271
                tipo_operacion=TipoOperacion.ARRENDAMIENTO_DE_INMUEBLES,
272
                iva16=9874,
273
                iva16_na=8521,
274
                iva_rfn=7632,
275
                iva_rfn_na=6541,
276
                iva_import16=5241,
277
                iva_import16_na=4123,
278
                iva_import_exento=3562,
279
                iva0=2415,
280
                iva_exento=1235,
281
                retenido=985,
282
                devoluciones=874
283
            ),
284
            ProveedorTercero(
285
                tipo_tercero=TipoTercero.PROVEEDOR_NACIONAL,
286
                tipo_operacion=TipoOperacion.OTROS,
287
                rfc="L&O950913MSA",
288
                iva16=96208900,
289
                iva16_na=85100,
290
                iva_rfn=74300,
291
                iva_rfn_na=67600,
292
                iva0=58900,
293
                iva_exento=47700,
294
                retenido=36400,
295
                devoluciones=24864
296
            ),
297
        ]
298
    )
299
    reference_data = open(os.path.join(current_dir, 'diot', f'{diot_file}.txt'), 'rb').read()
300
    assert reference_data == diot.plain_bytes()
301
302
303
def verify_invoice(invoice, path):
304
    verify = verify_result(data=invoice.html_str(), filename=f"{path}.html")
305
    assert verify
306
307
308
def test_create_declaracion_diot_moral_full_2():
309
    diot_file = "_OÑO120726RX30DOTAAL1LCMB901151"
310
    diot = DIOT(
311
        datos_identificacion=DatosIdentificacion(
312
            rfc="OÑO120726RX3",
313
            razon_social="ORGANICOS ÑAVEZ OSORIO S.A DE C.V",
314
            ejercicio=2021,
315
        ),
316
        periodo=Periodo.JULIO_SEPTIEMBRE,
317
        proveedores=[
318
            ProveedorTercero(
319
                tipo_tercero=TipoTercero.PROVEEDOR_EXTRANJERO,
320
                tipo_operacion=TipoOperacion.OTROS,
321
                id_fiscal="1254TAXID",
322
                nombre_extranjero="NOMBREEXTRANJERO",
323
                pais=Pais.ANTIGUA_Y_BERMUDA,
324
                nacionalidad="BERMUDO",
325
                iva16=456,
326
                iva16_na=752,
327
                iva_rfn=782,
328
                iva_rfn_na=456,
329
                iva_import16=123,
330
                iva_import16_na=475,
331
                iva_import_exento=7575,
332
                iva0=45213,
333
                iva_exento=1247,
334
                retenido=235,
335
                devoluciones=786
336
            ),
337
            ProveedorTercero(
338
                tipo_tercero=TipoTercero.PROVEEDOR_GLOBAL,
339
                tipo_operacion=TipoOperacion.ARRENDAMIENTO_DE_INMUEBLES,
340
                iva16=9874,
341
                iva16_na=8521,
342
                iva_rfn=7632,
343
                iva_rfn_na=6541,
344
                iva_import16=5241,
345
                iva_import16_na=4123,
346
                iva_import_exento=3562,
347
                iva0=2415,
348
                iva_exento=1235,
349
                retenido=985,
350
                devoluciones=874
351
            ),
352
            ProveedorTercero(
353
                tipo_tercero=TipoTercero.PROVEEDOR_NACIONAL,
354
                tipo_operacion=TipoOperacion.OTROS,
355
                rfc="L&O950913MSA",
356
                iva16=96208900,
357
                iva16_na=85100,
358
                iva_rfn=74300,
359
                iva_rfn_na=67600,
360
                iva0=58900,
361
                iva_exento=47700,
362
                retenido=36400,
363
                devoluciones=24864
364
            ),
365
            ProveedorTercero(
366
                tipo_tercero=TipoTercero.PROVEEDOR_GLOBAL,
367
                tipo_operacion=TipoOperacion.PRESTACION_DE_SERVICIOS_PROFESIONALES,
368
                iva16=77757987856,
369
            ),
370
            ProveedorTercero(
371
                tipo_tercero=TipoTercero.PROVEEDOR_NACIONAL,
372
                tipo_operacion=TipoOperacion.PRESTACION_DE_SERVICIOS_PROFESIONALES,
373
                rfc="IXS7607092R5",
374
                iva16_na=500,
375
                iva_rfn=0
376
            )
377
        ]
378
    )
379
    reference_data = open(os.path.join(current_dir, 'diot', f'{diot_file}.txt'), 'rb').read()
380
    assert reference_data == diot.plain_bytes()
381
382
    verify_invoice(diot, 'test_create_declaracion_diot_moral_full_2')
383
384
385
def test_create_declaracion_diot_moral_full_2_comp():
386
    diot = DIOT(
387
        datos_identificacion=DatosIdentificacion(
388
            rfc="OÑO120726RX3",
389
            razon_social="ORGANICOS ÑAVEZ OSORIO S.A DE C.V",
390
            ejercicio=2021,
391
        ),
392
        periodo=Periodo.JULIO_SEPTIEMBRE,
393
        complementaria=DatosComplementaria(
394
            folio_anterior="12313",
395
            fecha_presentacion_anterior=date(2021, 5, 10)
396
        ),
397
        proveedores=[
398
            ProveedorTercero(
399
                tipo_tercero=TipoTercero.PROVEEDOR_EXTRANJERO,
400
                tipo_operacion=TipoOperacion.OTROS,
401
                id_fiscal="1254TAXID",
402
                nombre_extranjero="NOMBREEXTRANJERO",
403
                pais=Pais.ANTIGUA_Y_BERMUDA,
404
                nacionalidad="BERMUDO",
405
                iva16=456,
406
                iva16_na=752,
407
                iva_rfn=782,
408
                iva_rfn_na=456,
409
                iva_import16=123,
410
                iva_import16_na=475,
411
                iva_import_exento=7575,
412
                iva0=45213,
413
                iva_exento=1247,
414
                retenido=235,
415
                devoluciones=786
416
            ),
417
            ProveedorTercero(
418
                tipo_tercero=TipoTercero.PROVEEDOR_GLOBAL,
419
                tipo_operacion=TipoOperacion.ARRENDAMIENTO_DE_INMUEBLES,
420
                iva16=9874,
421
                iva16_na=8521,
422
                iva_rfn=7632,
423
                iva_rfn_na=6541,
424
                iva_import16=5241,
425
                iva_import16_na=4123,
426
                iva_import_exento=3562,
427
                iva0=2415,
428
                iva_exento=1235,
429
                retenido=985,
430
                devoluciones=874
431
            ),
432
            ProveedorTercero(
433
                tipo_tercero=TipoTercero.PROVEEDOR_NACIONAL,
434
                tipo_operacion=TipoOperacion.OTROS,
435
                rfc="L&O950913MSA",
436
                iva16=96208900,
437
                iva16_na=85100,
438
                iva_rfn=74300,
439
                iva_rfn_na=67600,
440
                iva0=58900,
441
                iva_exento=47700,
442
                retenido=36400,
443
                devoluciones=24864
444
            ),
445
            ProveedorTercero(
446
                tipo_tercero=TipoTercero.PROVEEDOR_GLOBAL,
447
                tipo_operacion=TipoOperacion.PRESTACION_DE_SERVICIOS_PROFESIONALES,
448
                iva16=77757987856,
449
            ),
450
            ProveedorTercero(
451
                tipo_tercero=TipoTercero.PROVEEDOR_NACIONAL,
452
                tipo_operacion=TipoOperacion.PRESTACION_DE_SERVICIOS_PROFESIONALES,
453
                rfc="IXS7607092R5",
454
                iva16_na=500,
455
                iva_rfn=0
456
            )
457
        ]
458
    )
459
460
    verify_invoice(diot, 'test_create_declaracion_diot_moral_full_2_comp')
461