← Files Power BI Report CopilotARCHIVED FILE

tests/test_rdl_lint.py

4.48 KB · Nov 20, 2026 · 06:46 UTC

↓ Download file

See the change to this file →

import importlib.util, pathlib, sys, unittest
from decimal import Decimal
P=pathlib.Path(__file__).resolve().parents[1]/'scripts'/'rdl_lint.py'
spec=importlib.util.spec_from_file_location('rdl_lint',P)
mod=importlib.util.module_from_spec(spec);spec.loader.exec_module(mod)

def doc(body='7.4in',left='0.5in',right='0.5in',width='8.5in',section=True,queries=''):
    page=f'<Page><PageWidth>{width}</PageWidth><LeftMargin>{left}</LeftMargin><RightMargin>{right}</RightMargin></Page>'
    b=f'<Body><Width>{body}</Width></Body>'
    content=f'<ReportSections><ReportSection>{page}{b}</ReportSection></ReportSections>' if section else f'{page}{b}'
    return (f'<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition">'
            f'{content}{queries}</Report>').encode()

class RDLStaticChecks(unittest.TestCase):
    def test_inch(self):self.assertEqual(mod.inch('8.5in'),Decimal('8.5'))
    def test_mm(self):self.assertAlmostEqual(float(mod.inch('25.4mm')),1)
    def test_cm(self):self.assertAlmostEqual(float(mod.inch('2.54 cm')),1)
    def test_pt(self):self.assertEqual(mod.inch('72pt'),Decimal('1'))
    def test_pc(self):self.assertEqual(mod.inch('6pc'),Decimal('1'))
    def test_unknown_units(self):self.assertIsNone(mod.inch('1px'))
    def test_none_measure(self):self.assertIsNone(mod.inch(None))
    def test_ok_page(self):self.assertFalse(mod.inspect_rdl(doc())['findings'])
    def test_overflow(self):self.assertIn('PAGE_WIDTH_OVERFLOW',[x['code'] for x in mod.inspect_rdl(doc(body='8in'))['findings']])
    def test_equal_width_is_valid(self):self.assertFalse(mod.inspect_rdl(doc(body='7.5in'))['findings'])
    def test_legacy_page(self):self.assertEqual(mod.inspect_rdl(doc(section=False))['section_count'],1)
    def test_multiple_sections(self):
        a=doc().decode();a=a.replace('</ReportSection>','</ReportSection><ReportSection><Page><PageWidth>8.5in</PageWidth></Page><Body><Width>2in</Width></Body></ReportSection>');self.assertEqual(mod.inspect_rdl(a.encode())['section_count'],2)
    def test_missing_geometry(self):
        a=b'<Report><ReportSections><ReportSection><Body /></ReportSection></ReportSections></Report>'
        self.assertEqual(mod.inspect_rdl(a)['findings'][0]['code'],'PAGE_NOT_INSPECTABLE')
    def test_bad_root(self):
        with self.assertRaisesRegex(ValueError,'Expected'):mod.inspect_rdl(b'<Nope/>')
    def test_invalid_xml(self):
        with self.assertRaisesRegex(ValueError,'Invalid'):mod.inspect_rdl(b'<Report>')
    def test_doctype_rejected(self):
        with self.assertRaisesRegex(ValueError,'DOCTYPE'):mod.inspect_rdl(b'<!DOCTYPE Report><Report/>')
    def test_entity_rejected(self):
        with self.assertRaisesRegex(ValueError,'DOCTYPE'):mod.inspect_rdl(b'<!ENTITY xx "foo"><Report/>')
    def test_too_big(self):
        with self.assertRaisesRegex(ValueError,'exceeds'):mod.inspect_rdl(b'a'*(mod.MAX_BYTES+1))
    def test_parameter_reference_declared(self):
        more='<ReportParameters><ReportParameter Name="Region"/></ReportParameters><DataSets><DataSet Name="X"><Query><QueryParameters><QueryParameter Name="@P"><Value>=Parameters!Region.Value</Value></QueryParameter></QueryParameters></Query></DataSet></DataSets>'
        self.assertFalse(mod.inspect_rdl(doc(queries=more))['findings'])
    def test_parameter_reference_missing(self):
        more='<DataSets><DataSet Name="X"><Query><QueryParameters><QueryParameter Name="@P"><Value>=Parameters!Region.Value</Value></QueryParameter></QueryParameters></Query></DataSet></DataSets>'
        self.assertEqual(mod.inspect_rdl(doc(queries=more))['findings'][0]['code'],'UNKNOWN_REPORT_PARAMETER')
    def test_parameter_bracket_syntax(self):
        more='<DataSets><DataSet Name="X"><Query><QueryParameters><QueryParameter Name="@P"><Value>=Parameters("Region").Value</Value></QueryParameter></QueryParameters></Query></DataSet></DataSets>'
        self.assertEqual(mod.inspect_rdl(doc(queries=more))['findings'][0]['code'],'UNKNOWN_REPORT_PARAMETER')
    def test_no_query_execution(self):self.assertEqual(mod.inspect_rdl(doc())['status'],'STATIC_ONLY')
    def test_case_sensitive_params(self):
        more='<ReportParameters><ReportParameter Name="region"/></ReportParameters><DataSets><DataSet Name="X"><Query><QueryParameters><QueryParameter Name="@P"><Value>=Parameters!Region.Value</Value></QueryParameter></QueryParameters></Query></DataSet></DataSets>'
        self.assertEqual(mod.inspect_rdl(doc(queries=more))['findings'][0]['code'],'UNKNOWN_REPORT_PARAMETER')

if __name__=='__main__':unittest.main()

SHA-256: 6cd2a020b1c4a71a6f10d8fe526b287f57c953bd2c9f64d9bcb621a55f07c52e