Files
GTI_GERENCIADOR_FISCAL/fiscal/tests/test_import_fiscal_context.py
gti d21a9d1140 Add 'fiscal/' from commit '102ba84500dbd9655a0471288c3e278652593ca9'
git-subtree-dir: fiscal
git-subtree-mainline: d95165fc8f
git-subtree-split: 102ba84500
2026-09-29 14:43:23 -03:00

35 lines
1.5 KiB
Python

import importlib.util
import json
import sys
from pathlib import Path
import pytest
ROOT = Path(__file__).parents[1]
sys.path.insert(0, str(ROOT))
spec = importlib.util.spec_from_file_location("import_fiscal_context", ROOT / "scripts" / "import_fiscal_context.py")
mod = importlib.util.module_from_spec(spec); sys.modules[spec.name] = mod; spec.loader.exec_module(mod)
HEADER = "company_key,scope,scope_key,key,value,reason\n"
def test_csv_answers_are_validated_and_unanswered_rows_skipped(tmp_path, capsys):
f = tmp_path / "a.csv"
f.write_text(HEADER + "abc,GROUP,000019,product_role,RESALE,revenda\nabc,COMPANY,,entry_scope,INTRA,\nabc,GROUP,000020,product_role,,\n", encoding="utf-8")
mod.main([str(f), "--token", "t", "--dry-run"])
assert "validated 2 entries (1 unanswered rows skipped)" in capsys.readouterr().out
def test_invalid_rows_block_the_whole_import(tmp_path):
f = tmp_path / "b.csv"
f.write_text(HEADER + "abc,GROUP,000019,product_role,RESALE,\nabc,COMPANY,,rounding,Z,\n", encoding="utf-8")
with pytest.raises(SystemExit, match="invalid"):
mod.main([str(f), "--token", "t", "--dry-run"])
def test_json_format_and_questionnaire_csv_columns(tmp_path, capsys):
f = tmp_path / "c.json"
f.write_text(json.dumps({"company_key": "abc", "entries": [{"scope": "NCM", "scope_key": "22030000", "key": "cest", "value": "03.021.03"}]}), encoding="utf-8")
mod.main([str(f), "--token", "t", "--dry-run"])
assert "validated 1 entries" in capsys.readouterr().out