from pathlib import Path
import unittest

from app.repository import RDFRepository


ROOT = Path(__file__).resolve().parents[1]


class RepositoryTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.repository = RDFRepository(ROOT)

    def test_loads_core_extension_and_demo_data(self):
        self.assertGreater(self.repository.triple_count, 2700)
        self.assertEqual(
            self.repository.loaded_file_names,
            [
                "ontology/global-shipment.ttl",
                "ontology/ai-market-demo-extension.ttl",
                "data/ai-market-demo.ttl",
                "data/ai-market-india-mock.ttl",
                "data/ai-market-scenarios.ttl",
                "data/qingdao-export-sample.ttl",
            ],
        )

    def test_every_query_executes_locally(self):
        for path in sorted((ROOT / "queries").glob("*.rq")):
            with self.subTest(query=path.name):
                result = self.repository.query_file(path)
                self.assertIn("rows", result)

    def test_legacy_order_query_still_returns_sample_orders(self):
        result = self.repository.query_file(ROOT / "queries" / "order-status.rq")
        self.assertEqual(len(result["rows"]), 2)


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