File size: 27,654 Bytes
d9d7b41 | 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 | """Testes dos geradores de arquivos SPED e XML fiscal."""
import pytest
import re
from datetime import date
from decimal import Decimal
from pathlib import Path
from src.fiscal.entities import (
Contato,
Empresa,
Endereco,
ItemNotaFiscal,
LancamentoContabil,
NotaFiscal,
PeriodoApuracao,
Produto,
RegimeTributario,
TipoEmpresa,
UF,
)
from src.generators.sped_writer import ArquivoSPED, RegistroSPED, criar_registro
from src.generators.efd_icms_ipi import GeradorEFDICMSIPI
from src.generators.efd_contribuicoes import GeradorEFDContribuicoes
from src.generators.ecd import GeradorECD, PlanoContas
from src.generators.ecf import DadosLucroPresumido, DadosLucroReal, GeradorECF
from src.generators.nfe_xml import GeradorNFeXML
from src.generators.efd_reinf import (
EventoR2010,
GeradorEFDReinf,
PrestadorServico,
)
from src.generators.esocial import (
GeradorESocial,
Trabalhador,
VinculoEmpregaticio,
FolhaPagamento,
ItemFolha,
RUBRICAS_PADRAO,
)
from src.generators.dctf import DebitoDCTF, GeradorDCTF, ItemDCTF, montar_dctf_do_periodo
from src.generators.cte import GeradorCTe, CargaCTe, ParteCTe, DadosTransporte, DocumentoReferenciado
from src.generators.nfce_xml import GeradorNFCeXML, ConsumidorNFCe
from src.generators.mdfe import GeradorMDFe, MunicipioDescarga, DocumentoMDFe, ConductorMDFe, SeguroMDFe
from src.generators.dirf import GeradorDIRF, BeneficiarioDIRF, ResponsavelDIRF
from src.generators.defis import GeradorDEFIS, ReceitaMensalDEFIS, SocioDEFIS
from src.generators.destda import GeradorDeSTDA, OperacaoSTDeSTDA
from src.generators.gia import GeradorGIA, ApuracaoGIA, ApuracaoGIAST
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture
def empresa_sp():
return Empresa(
cnpj="11222333000181",
razao_social="EMPRESA TESTE LTDA",
nome_fantasia="TESTE",
ie="111111111111",
regime_tributario=RegimeTributario.LUCRO_PRESUMIDO,
tipo_empresa=TipoEmpresa.COMERCIO,
endereco=Endereco(
logradouro="Av. Paulista",
numero="1000",
bairro="Bela Vista",
municipio="São Paulo",
uf=UF.SP,
cep="01310100",
cod_municipio="3550308",
),
contato=Contato(telefone="1133334444", email="teste@teste.com"),
)
@pytest.fixture
def cliente_sp():
return Empresa(
cnpj="22333444000181",
razao_social="CLIENTE TESTE SA",
regime_tributario=RegimeTributario.LUCRO_PRESUMIDO,
tipo_empresa=TipoEmpresa.COMERCIO,
endereco=Endereco(
logradouro="Rua das Flores",
numero="100",
bairro="Centro",
municipio="Campinas",
uf=UF.SP,
cep="13010050",
cod_municipio="3509502",
),
)
@pytest.fixture
def produto_padrao():
return Produto(
codigo="PROD001",
descricao="PRODUTO TESTE",
ncm="84713012",
unidade="UN",
aliq_icms=Decimal("18"),
aliq_ipi=Decimal("5"),
cst_icms="000",
cst_ipi="50",
cst_pis="01",
cst_cofins="01",
)
@pytest.fixture
def nota_fiscal(empresa_sp, cliente_sp, produto_padrao):
item = ItemNotaFiscal(
numero_item=1,
produto=produto_padrao,
cfop="5102",
quantidade=Decimal("10"),
valor_unitario=Decimal("100.00"),
)
return NotaFiscal(
numero="000001",
serie="001",
data_emissao=date(2024, 1, 15),
data_saida_entrada=date(2024, 1, 15),
emitente=empresa_sp,
destinatario=cliente_sp,
natureza_operacao="VENDA DE MERCADORIA",
itens=[item],
chave_acesso="35240111222333000181550010000000011000000015",
)
@pytest.fixture
def periodo(empresa_sp, nota_fiscal):
return PeriodoApuracao(
data_inicio=date(2024, 1, 1),
data_fim=date(2024, 1, 31),
empresa=empresa_sp,
notas_saida=[nota_fiscal],
)
# ---------------------------------------------------------------------------
# Testes do escritor base SPED
# ---------------------------------------------------------------------------
class TestSpedWriter:
def test_criar_registro_formato_pipe(self):
linha = criar_registro("C100", "1", "0", "12345678")
assert linha.startswith("|C100|")
assert linha.endswith("|\n")
def test_criar_registro_data(self):
linha = criar_registro("0000", date(2024, 1, 1))
assert "01012024" in linha
def test_criar_registro_decimal(self):
linha = criar_registro("E110", Decimal("1234.56"))
assert "1234,56" in linha
def test_criar_registro_none_vazio(self):
linha = criar_registro("X001", None, "")
parts = linha.strip("|").split("|")
assert parts[1] == ""
assert parts[2] == ""
def test_arquivo_sped_contagem_linhas(self):
arq = ArquivoSPED("teste.txt")
arq.adicionar_linha_raw(criar_registro("0000", "017"))
arq.adicionar_linha_raw(criar_registro("0001", "0"))
assert arq.total_linhas() == 2
def test_registro_sped_builder(self):
reg = RegistroSPED("C100")
reg.add("1").add("0").add("12345678000195")
linha = reg.to_line()
assert "|C100|1|0|12345678000195|" in linha
# ---------------------------------------------------------------------------
# Testes EFD ICMS/IPI
# ---------------------------------------------------------------------------
class TestEFDICMSIPI:
def test_gera_arquivo_existente(self, periodo, tmp_path):
gerador = GeradorEFDICMSIPI(periodo)
caminho = gerador.gerar(tmp_path)
assert caminho.exists()
assert caminho.stat().st_size > 0
def test_registro_0000_presente(self, periodo, tmp_path):
gerador = GeradorEFDICMSIPI(periodo)
caminho = gerador.gerar(tmp_path)
conteudo = caminho.read_text()
assert "|0000|" in conteudo
def test_cnpj_no_registro_0000(self, periodo, tmp_path):
gerador = GeradorEFDICMSIPI(periodo)
caminho = gerador.gerar(tmp_path)
conteudo = caminho.read_text()
assert "11222333000181" in conteudo
def test_registro_c100_presente(self, periodo, tmp_path):
gerador = GeradorEFDICMSIPI(periodo)
caminho = gerador.gerar(tmp_path)
conteudo = caminho.read_text()
assert "|C100|" in conteudo
def test_registro_e110_presente(self, periodo, tmp_path):
gerador = GeradorEFDICMSIPI(periodo)
caminho = gerador.gerar(tmp_path)
conteudo = caminho.read_text()
assert "|E110|" in conteudo
def test_registro_9999_presente(self, periodo, tmp_path):
gerador = GeradorEFDICMSIPI(periodo)
caminho = gerador.gerar(tmp_path)
conteudo = caminho.read_text()
assert "|9999|" in conteudo
def test_todas_linhas_sao_pipe_delimited(self, periodo, tmp_path):
gerador = GeradorEFDICMSIPI(periodo)
caminho = gerador.gerar(tmp_path)
for linha in caminho.read_text().splitlines():
assert linha.startswith("|") and linha.endswith("|"), f"Linha inválida: {linha}"
def test_nome_arquivo_correto(self, periodo):
gerador = GeradorEFDICMSIPI(periodo)
assert "11222333000181" in gerador._arquivo.nome_arquivo
assert "202401" in gerador._arquivo.nome_arquivo
# ---------------------------------------------------------------------------
# Testes EFD Contribuições
# ---------------------------------------------------------------------------
class TestEFDContribuicoes:
def test_gera_arquivo(self, periodo, tmp_path):
gerador = GeradorEFDContribuicoes(periodo)
caminho = gerador.gerar(tmp_path)
assert caminho.exists()
def test_registro_m200_presente(self, periodo, tmp_path):
gerador = GeradorEFDContribuicoes(periodo)
caminho = gerador.gerar(tmp_path)
assert "|M200|" in caminho.read_text()
def test_registro_m600_presente(self, periodo, tmp_path):
gerador = GeradorEFDContribuicoes(periodo)
caminho = gerador.gerar(tmp_path)
assert "|M600|" in caminho.read_text()
# ---------------------------------------------------------------------------
# Testes ECD
# ---------------------------------------------------------------------------
class TestECD:
def test_gera_arquivo(self, periodo, tmp_path):
gerador = GeradorECD(periodo)
caminho = gerador.gerar(tmp_path)
assert caminho.exists()
assert caminho.stat().st_size > 100
def test_plano_contas_no_arquivo(self, periodo, tmp_path):
gerador = GeradorECD(periodo)
caminho = gerador.gerar(tmp_path)
assert "|I050|" in caminho.read_text()
def test_registro_i001_presente(self, periodo, tmp_path):
gerador = GeradorECD(periodo)
caminho = gerador.gerar(tmp_path)
assert "|I001|" in caminho.read_text()
def test_lancamentos_contabeis(self, periodo, empresa_sp, tmp_path):
periodo.lancamentos = [
LancamentoContabil(
data=date(2024, 1, 5),
numero_lancamento="000001",
historico="Venda de mercadorias",
debito_conta="1.1.2.01",
credito_conta="4.1.01",
valor=Decimal("1000.00"),
)
]
gerador = GeradorECD(periodo)
caminho = gerador.gerar(tmp_path)
assert "|I200|" in caminho.read_text()
assert "|I250|" in caminho.read_text()
def test_plano_contas_customizado(self, periodo, tmp_path):
plano = [
PlanoContas("1", "ATIVO", 1, "S", "D"),
PlanoContas("1.01", "CAIXA", 2, "A", "D"),
]
gerador = GeradorECD(periodo, plano_contas=plano)
caminho = gerador.gerar(tmp_path)
assert "CAIXA" in caminho.read_text()
# ---------------------------------------------------------------------------
# Testes ECF
# ---------------------------------------------------------------------------
class TestECF:
def test_gera_ecf_lucro_presumido(self, empresa_sp, tmp_path):
dados = DadosLucroPresumido(
receita_venda_mercadorias=Decimal("500000"),
receita_prestacao_servicos=Decimal("100000"),
)
gerador = GeradorECF(empresa_sp, 2023, dados_lp=dados)
caminho = gerador.gerar(tmp_path)
assert caminho.exists()
def test_registro_0000_ecf(self, empresa_sp, tmp_path):
dados = DadosLucroPresumido(receita_venda_mercadorias=Decimal("100000"))
gerador = GeradorECF(empresa_sp, 2023, dados_lp=dados)
caminho = gerador.gerar(tmp_path)
assert "|0000|" in caminho.read_text()
def test_gera_ecf_lucro_real(self, tmp_path):
empresa = Empresa(
cnpj="11222333000181",
razao_social="EMPRESA LR LTDA",
regime_tributario=RegimeTributario.LUCRO_REAL,
tipo_empresa=TipoEmpresa.COMERCIO,
endereco=Endereco(
logradouro="Rua X", numero="1", bairro="B",
municipio="SP", uf=UF.SP, cep="01310100",
),
)
dados = DadosLucroReal(
lucro_contabil=Decimal("200000"),
adicoes=Decimal("10000"),
exclusoes=Decimal("5000"),
)
gerador = GeradorECF(empresa, 2023, dados_lr=dados)
caminho = gerador.gerar(tmp_path)
assert "|L020|" in caminho.read_text()
# ---------------------------------------------------------------------------
# Testes NF-e XML
# ---------------------------------------------------------------------------
class TestNFeXML:
def test_gera_xml_valido(self, nota_fiscal, tmp_path):
gerador = GeradorNFeXML(nota_fiscal)
xml = gerador.gerar_xml()
assert xml.startswith("<?xml")
assert "<NFe" in xml
assert "<infNFe" in xml
def test_xml_contem_cnpj(self, nota_fiscal):
gerador = GeradorNFeXML(nota_fiscal)
xml = gerador.gerar_xml()
assert "11222333000181" in xml
def test_xml_contem_valor_total(self, nota_fiscal):
gerador = GeradorNFeXML(nota_fiscal)
xml = gerador.gerar_xml()
# 10 itens × R$100 = R$1000 + IPI = total
assert "1000.00" in xml or "1050.00" in xml
def test_xml_contem_imposto(self, nota_fiscal):
gerador = GeradorNFeXML(nota_fiscal)
xml = gerador.gerar_xml()
assert "<ICMS>" in xml
assert "<IPI>" in xml or "<IPITrib>" in xml
def test_chave_acesso_44_digitos(self, nota_fiscal):
gerador = GeradorNFeXML(nota_fiscal)
chave = gerador._gerar_chave()
assert len(chave) == 44
assert chave.isdigit()
def test_salva_arquivo_xml(self, nota_fiscal, tmp_path):
gerador = GeradorNFeXML(nota_fiscal)
caminho = gerador.salvar(tmp_path)
assert caminho.exists()
assert caminho.suffix == ".xml"
# ---------------------------------------------------------------------------
# Testes EFD-Reinf
# ---------------------------------------------------------------------------
class TestEFDReinf:
def test_gera_r2010(self):
gerador = GeradorEFDReinf("11222333000181")
evt = EventoR2010(
cnpj_tomador="11222333000181",
periodo="2024-01",
prestadores=[
PrestadorServico(
cnpj_cpf="22333444000181",
nome="PRESTADOR TESTE",
valor_bruto=Decimal("10000"),
valor_bc_csll=Decimal("10000"),
valor_bc_irrf=Decimal("10000"),
valor_bc_pis=Decimal("10000"),
valor_bc_cofins=Decimal("10000"),
)
],
)
xml = gerador.gerar_r2010(evt)
assert "evtServTom" in xml
assert "22333444000181" in xml
def test_gera_r2099(self):
gerador = GeradorEFDReinf("11222333000181")
xml = gerador.gerar_r2099("2024-01")
assert "evtFechamento" in xml
def test_salva_eventos(self, tmp_path):
gerador = GeradorEFDReinf("11222333000181")
arquivos = gerador.salvar_eventos(tmp_path, periodo="2024-01")
assert len(arquivos) >= 2
assert all(a.exists() for a in arquivos)
# ---------------------------------------------------------------------------
# Testes e-Social
# ---------------------------------------------------------------------------
class TestESocial:
def test_gera_s2200_admissao(self):
gerador = GeradorESocial("11222333000181")
trab = Trabalhador(
cpf="12345678909",
nome="JOAO DA SILVA",
data_nascimento=date(1985, 3, 15),
)
vinculo = VinculoEmpregaticio(
trabalhador=trab,
matricula="001",
data_admissao=date(2024, 1, 2),
salario_base=Decimal("3000"),
)
xml = gerador.gerar_s2200(vinculo)
assert "evtAdmissao" in xml
assert "JOAO DA SILVA" in xml
def test_gera_s1200_folha(self):
gerador = GeradorESocial("11222333000181")
trab = Trabalhador(cpf="12345678909", nome="MARIA SOUZA", data_nascimento=date(1990, 6, 20))
rubrica = RUBRICAS_PADRAO[0]
folha = FolhaPagamento(
trabalhador=trab,
matricula="002",
periodo="2024-01",
itens=[ItemFolha(rubrica=rubrica, valor=Decimal("3000"))],
base_cp=Decimal("3000"),
base_fgts=Decimal("3000"),
valor_fgts=Decimal("240"),
)
xml = gerador.gerar_s1200(folha)
assert "evtRemun" in xml
assert "3000.00" in xml
def test_gera_s1299_fechamento(self):
gerador = GeradorESocial("11222333000181")
xml = gerador.gerar_s1299("2024-01")
assert "evtFechamento" in xml
# ---------------------------------------------------------------------------
# Testes DCTF
# ---------------------------------------------------------------------------
class TestDCTF:
def test_gera_xml_dctf(self, empresa_sp, tmp_path):
item = ItemDCTF(debito=DebitoDCTF(
codigo_receita="2089",
periodo_apuracao="2024-01",
valor_debito=Decimal("5000"),
))
gerador = GeradorDCTF(empresa_sp, "2024-01", [item])
xml = gerador.gerar_xml()
assert "<DCTF" in xml
assert "2089" in xml
assert "5000.00" in xml
def test_salva_arquivo(self, empresa_sp, tmp_path):
gerador = GeradorDCTF(empresa_sp, "2024-01")
caminho = gerador.salvar(tmp_path)
assert caminho.exists()
def test_saldo_a_pagar(self, empresa_sp):
from src.generators.dctf import PagamentoDCTF
item = ItemDCTF(
debito=DebitoDCTF("6912", "2024-01", Decimal("10000")),
pagamentos=[PagamentoDCTF("12345", date(2024, 2, 15), Decimal("7000"))],
)
assert item.saldo_a_pagar == Decimal("3000.00")
assert item.total_pago == Decimal("7000.00")
def test_montar_dctf_do_periodo(self, empresa_sp):
gen = montar_dctf_do_periodo(
empresa=empresa_sp,
periodo="2024-01",
valor_irpj=Decimal("3000"),
valor_csll=Decimal("1200"),
valor_pis=Decimal("650"),
valor_cofins=Decimal("3000"),
)
assert len(gen.itens) == 4
xml = gen.gerar_xml()
assert "3000.00" in xml
def test_relatorio_resumo(self, empresa_sp):
item = ItemDCTF(debito=DebitoDCTF("8109", "2024-01", Decimal("1000")))
gerador = GeradorDCTF(empresa_sp, "2024-01", [item])
relatorio = gerador.relatorio_resumo()
assert "8109" in relatorio
assert "1,000.00" in relatorio
# ---------------------------------------------------------------------------
# CT-e
# ---------------------------------------------------------------------------
class TestCTe:
@pytest.fixture
def gerador_cte(self, empresa_sp):
remetente = ParteCTe("11222333000181", "REMETENTE LTDA", "Rua A, 1", "São Paulo", "SP", "01310100", "3550308")
destinatario = ParteCTe("22333444000181", "DESTINATÁRIO SA", "Av B, 2", "Campinas", "SP", "13010050", "3509502")
carga = CargaCTe("ELETRONICOS", "84", Decimal("50000"), peso_kg=Decimal("500"))
transporte = DadosTransporte("12345678", "ABC1234", "SP", date(2024, 2, 1))
docs = [DocumentoReferenciado("35240111222333000181550010000000011000000015")]
return GeradorCTe(empresa_sp, remetente, destinatario, carga, docs, transporte)
def test_gera_xml_valido(self, gerador_cte):
xml = gerador_cte.gerar_xml()
assert "<?xml" in xml
assert "CTe" in xml or "infCte" in xml
def test_chave_44_digitos(self, gerador_cte):
chave = gerador_cte._gerar_chave()
assert len(chave) == 44
assert chave.isdigit()
def test_modelo_57(self, gerador_cte):
xml = gerador_cte.gerar_xml()
assert "57" in xml
def test_salva_arquivo(self, gerador_cte, tmp_path):
caminho = gerador_cte.salvar(tmp_path)
assert caminho.exists()
assert caminho.suffix == ".xml"
# ---------------------------------------------------------------------------
# NFC-e
# ---------------------------------------------------------------------------
class TestNFCe:
@pytest.fixture
def gerador_nfce(self, empresa_sp, produto_padrao):
from src.fiscal.entities import ItemNotaFiscal
item = ItemNotaFiscal(numero_item=1, produto=produto_padrao, cfop="5102",
quantidade=Decimal("2"), valor_unitario=Decimal("100"))
consumidor = ConsumidorNFCe(cpf="", nome="CONSUMIDOR")
return GeradorNFCeXML(empresa_sp, [item], consumidor=consumidor)
def test_gera_xml_valido(self, gerador_nfce):
xml = gerador_nfce.gerar_xml()
assert "<?xml" in xml
def test_modelo_65(self, gerador_nfce):
xml = gerador_nfce.gerar_xml()
assert "65" in xml
def test_chave_44_digitos(self, gerador_nfce):
chave = gerador_nfce._gerar_chave()
assert len(chave) == 44
def test_salva_arquivo(self, gerador_nfce, tmp_path):
caminho = gerador_nfce.salvar(tmp_path)
assert caminho.exists()
# ---------------------------------------------------------------------------
# MDF-e
# ---------------------------------------------------------------------------
class TestMDFe:
@pytest.fixture
def gerador_mdfe(self, empresa_sp):
docs = [DocumentoMDFe("35240111222333000181550010000000011000000015", "NFe")]
municipios = [MunicipioDescarga("3509502", "Campinas", "SP", docs)]
condutores = [ConductorMDFe("João Silva", "12345678901")]
seguro = SeguroMDFe("1", "11222333000181", "SEGURADORA SA", "APL001", ["1"])
return GeradorMDFe(empresa_sp, municipios, condutores, seguro,
"SP", "SP", "ABC1234", "SP", "12345678")
def test_gera_xml_valido(self, gerador_mdfe):
xml = gerador_mdfe.gerar_xml()
assert "<?xml" in xml
assert "MDF" in xml or "infMDFe" in xml
def test_modelo_58(self, gerador_mdfe):
xml = gerador_mdfe.gerar_xml()
assert "58" in xml
def test_chave_44_digitos(self, gerador_mdfe):
chave = gerador_mdfe._gerar_chave()
assert len(chave) == 44
def test_salva_arquivo(self, gerador_mdfe, tmp_path):
caminho = gerador_mdfe.salvar(tmp_path)
assert caminho.exists()
# ---------------------------------------------------------------------------
# DIRF
# ---------------------------------------------------------------------------
class TestDIRF:
@pytest.fixture
def gerador_dirf(self, empresa_sp):
responsavel = ResponsavelDIRF("12345678901", "CONTADOR", "CONTADOR", "11", "33334444")
beneficiarios = [
BeneficiarioDIRF(
cpf_cnpj="98765432100",
nome="FUNCIONÁRIO TESTE",
tipo="PF",
cod_receita="0561",
rendimentos_por_mes={1: Decimal("5000"), 2: Decimal("5000")},
ir_retido_por_mes={1: Decimal("250"), 2: Decimal("250")},
)
]
return GeradorDIRF(empresa_sp, 2024, beneficiarios, responsavel)
def test_gera_txt(self, gerador_dirf):
txt = gerador_dirf.gerar_txt()
assert "DIRF" in txt
assert "BPFDEC" in txt or "DECPJ" in txt
def test_contem_cnpj(self, gerador_dirf):
txt = gerador_dirf.gerar_txt()
assert "11222333000181" in txt
def test_contem_rendimento(self, gerador_dirf):
txt = gerador_dirf.gerar_txt()
assert "5000" in txt
def test_salva_arquivo(self, gerador_dirf, tmp_path):
caminho = gerador_dirf.salvar(tmp_path)
assert caminho.exists()
assert caminho.suffix == ".txt"
# ---------------------------------------------------------------------------
# DEFIS
# ---------------------------------------------------------------------------
class TestDEFIS:
@pytest.fixture
def gerador_defis(self, empresa_sp):
receitas = [ReceitaMensalDEFIS(mes=m, receita_bruta_total=Decimal("10000")) for m in range(1, 13)]
socios = [SocioDEFIS("12345678901", "SÓCIO TESTE", Decimal("100"))]
return GeradorDEFIS(empresa_sp, 2024, receitas, socios)
def test_gera_xml_valido(self, gerador_defis):
xml = gerador_defis.gerar_xml()
assert "<?xml" in xml
assert "DEFIS" in xml
def test_contem_cnpj(self, gerador_defis):
xml = gerador_defis.gerar_xml()
assert "11222333000181" in xml
def test_total_anual(self, gerador_defis):
xml = gerador_defis.gerar_xml()
assert "120000.00" in xml
def test_socio_presente(self, gerador_defis):
xml = gerador_defis.gerar_xml()
assert "SÓCIO TESTE" in xml or "100.00" in xml
def test_salva_arquivo(self, gerador_defis, tmp_path):
caminho = gerador_defis.salvar(tmp_path)
assert caminho.exists()
# ---------------------------------------------------------------------------
# DeSTDA
# ---------------------------------------------------------------------------
class TestDeSTDA:
@pytest.fixture
def gerador_destda(self, empresa_sp):
ops = [
OperacaoSTDeSTDA("SP", "RJ", "ST", Decimal("10000"), Decimal("18"), Decimal("1800")),
OperacaoSTDeSTDA("SP", "MG", "DIFAL", Decimal("5000"), Decimal("6"), Decimal("300")),
]
return GeradorDeSTDA(empresa_sp, "2024-01", ops)
def test_gera_xml_valido(self, gerador_destda):
xml = gerador_destda.gerar_xml()
assert "<?xml" in xml
assert "DeSTDA" in xml
def test_totais_corretos(self, gerador_destda):
xml = gerador_destda.gerar_xml()
assert "1800.00" in xml
assert "300.00" in xml
def test_data_vencimento(self, gerador_destda):
venc = gerador_destda.data_vencimento()
assert venc.day == 20
assert venc.month == 2
def test_relatorio_resumo(self, gerador_destda):
rel = gerador_destda.relatorio_resumo()
assert "1,800.00" in rel or "1800" in rel
def test_salva_arquivo(self, gerador_destda, tmp_path):
caminho = gerador_destda.salvar(tmp_path)
assert caminho.exists()
# ---------------------------------------------------------------------------
# GIA
# ---------------------------------------------------------------------------
class TestGIA:
@pytest.fixture
def gerador_gia(self, empresa_sp):
apuracao = ApuracaoGIA(
debitos_operacoes_proprias=Decimal("50000"),
creditos_entradas=Decimal("30000"),
)
st = ApuracaoGIAST("MG", Decimal("20000"), Decimal("12"), Decimal("2400"))
return GeradorGIA(empresa_sp, "2024-01", apuracao, [st])
def test_gera_aie(self, gerador_gia):
aie = gerador_gia.gerar_aie()
assert "|GIA|" in aie
assert "|APURACAO_ICMS|" in aie
def test_icms_a_recolher(self, gerador_gia):
assert gerador_gia.icms_a_recolher == Decimal("20000")
def test_gia_st_presente(self, gerador_gia):
aie = gerador_gia.gerar_aie()
assert "|GIA_ST|" in aie
assert "MG" in aie
def test_encerramento_com_hash(self, gerador_gia):
aie = gerador_gia.gerar_aie()
assert "|ENCERRAMENTO|" in aie
def test_salva_arquivo(self, gerador_gia, tmp_path):
caminho = gerador_gia.salvar(tmp_path)
assert caminho.exists()
assert caminho.suffix == ".aie"
# ---------------------------------------------------------------------------
# EFD ICMS/IPI — Blocos G e K
# ---------------------------------------------------------------------------
class TestEFDICMSIPIBlocosGK:
def test_bloco_g_presente(self, periodo, tmp_path):
gen = GeradorEFDICMSIPI(periodo)
caminho = gen.gerar(tmp_path)
conteudo = caminho.read_text()
assert "|G001|" in conteudo
assert "|G990|" in conteudo
def test_bloco_k_presente(self, periodo, tmp_path):
gen = GeradorEFDICMSIPI(periodo)
caminho = gen.gerar(tmp_path)
conteudo = caminho.read_text()
assert "|K001|" in conteudo
assert "|K990|" in conteudo
|