Project

General

Profile

Feature #152 » test_nodata.py

Python script: import sys, tempfile - Akshath Shettigara, 10/15/2026 04:35 PM

 
import sys, tempfile
from pathlib import Path
sys.path.insert(0, "app/phase_a_fetch"); sys.path.insert(0, ".")
import pandas as pd
import nsdl_download as nd
import nsdl_parser

fails = []
def check(name, cond, detail=""):
print(("PASS " if cond else "FAIL ") + name + (f" [{detail}]" if not cond and detail else ""))
if not cond: fails.append(name)

tmp = Path(tempfile.mkdtemp())
header = "\t".join(nd.REQUIRED_COLUMNS)

def write(name, text):
p = tmp / name; p.write_text(text, encoding="utf-8"); return p

# header only (NSDL title line, blank line, header, no rows)
p = write("hdr.xls", "Bill of Entry report\n\n" + header + "\n")
try: nd.inspect_export_file(p); check("header-only file -> no data", False)
except nd.NoExportDataError: check("header-only file -> no data", True)
# message instead of table
p = write("msg.xls", "No records found for the selected period\n")
try: nd.inspect_export_file(p); check("message file -> no data", False)
except nd.NoExportDataError: check("message file -> no data", True)
# empty
p = write("empty.xls", "")
try: nd.inspect_export_file(p); check("empty file -> no data", False)
except nd.NoExportDataError: check("empty file -> no data", True)
# real rows
rows = "\t".join(["1", "0300014953", "S", "E", "item", "OutOfCharge", "9"])
p = write("ok.xls", "Report\n\n" + header + "\n" + rows + "\n" + rows + "\n")
check("file with 2 data rows counted", nd.inspect_export_file(p) == 2)
check("validate_nsdl_export agrees on the same file", nd.validate_nsdl_export(str(p), "BE") == 2)

# transform treats a missing module as an export with no rows
empty = pd.DataFrame({"CLIENT IEC": pd.Series(dtype=str)})
parts, unmapped = nsdl_parser.partition_by_client(empty, {"0300014953": "eap"})
check("empty module partitions to nothing, no error", parts == {} and unmapped == {})

# the user's own real sample export, if present in the extracted reference (read-only)
sample = Path("../_otp_ref2/NSDL_SEZ_AUTOMATION/sample_data/DTASale_11092026_0205PM.xls")
if sample.exists():
n = nd.inspect_export_file(sample)
check("real sample export is recognised as data", n > 0, str(n))
df = nsdl_parser.read_nsdl_export(str(sample))
check("real sample export parses with required columns", all(c in df.columns for c in nd.REQUIRED_COLUMNS))
print("ALL PASSED" if not fails else f"FAILED: {fails}")
sys.exit(1 if fails else 0)
(4-4/5)