import os import unittest import shutil import tempfile import json from pathlib import Path from unittest.mock import MagicMock, patch from semantica.export import ( ArrowExporter, JSONExporter, CSVExporter, RDFExporter, GraphExporter, SemanticNetworkYAMLExporter, YAMLSchemaExporter, OWLExporter, VectorExporter, LPGExporter, ReportGenerator, MethodRegistry, method_registry ) from semantica.export.rdf_exporter import NamespaceManager, RDFSerializer, RDFValidator class TestExportModule(unittest.TestCase): def setUp(self): self.test_dir = tempfile.mkdtemp() self.entities = [ {"id": "e1", "type": "Person", "name": "Alice", "label": "Alice", "properties": {"age": 30}}, {"id": "e2", "type": "Organization", "name": "Acme Corp", "label": "Acme Corp", "properties": {"loc": "NY"}} ] self.relationships = [ {"id": "r1", "source": "e1", "target": "e2", "type": "WORKS_FOR", "properties": {"role": "Engineer"}} ] self.kg = { "entities": self.entities, "relationships": self.relationships, "metadata": {"version": "1.0"} } def tearDown(self): shutil.rmtree(self.test_dir) def test_json_exporter(self): exporter = JSONExporter(indent=2) output_path = Path(self.test_dir) / "output.json" # Test export_knowledge_graph exporter.export_knowledge_graph(self.kg, str(output_path)) self.assertTrue(output_path.exists()) with open(output_path, 'r') as f: data = json.load(f) self.assertEqual(len(data['entities']), 2) self.assertEqual(len(data['relationships']), 1) # Test export_entities entities_path = Path(self.test_dir) / "entities.json" exporter.export_entities(self.entities, str(entities_path)) self.assertTrue(entities_path.exists()) def test_csv_exporter(self): exporter = CSVExporter() output_path = Path(self.test_dir) / "output.csv" # Test export_entities directly ent_path = Path(self.test_dir) / "entities.csv" exporter.export_entities(self.entities, str(ent_path)) self.assertTrue(ent_path.exists()) # Verify CSV content with open(ent_path, 'r', encoding='utf-8') as f: lines = f.readlines() header = lines[0].strip() # Check for presence of fields (order might vary) self.assertIn("id", header) self.assertIn("text", header) self.assertIn("type", header) content = "".join(lines) self.assertIn("e1", content) self.assertIn("Alice", content) self.assertIn("Person", content) self.assertIn("e2", content) self.assertIn("Acme Corp", content) self.assertIn("Organization", content) rel_path = Path(self.test_dir) / "rels.csv" exporter.export_relationships(self.relationships, str(rel_path)) self.assertTrue(rel_path.exists()) # Verify Relationship CSV content with open(rel_path, 'r', encoding='utf-8') as f: lines = f.readlines() header = lines[0].strip() self.assertIn("id", header) self.assertIn("source_id", header) self.assertIn("target_id", header) self.assertIn("type", header) content = "".join(lines) self.assertIn("r1", content) self.assertIn("e1", content) self.assertIn("e2", content) self.assertIn("WORKS_FOR", content) def test_rdf_exporter(self): exporter = RDFExporter() output_path = Path(self.test_dir) / "output.ttl" try: exporter.export(self.kg, str(output_path), format="turtle") if output_path.exists(): self.assertTrue(output_path.exists()) # Verify RDF content with open(output_path, 'r', encoding='utf-8') as f: content = f.read() # Basic checks for Turtle format # self.assertIn("@prefix", content) # Prefix might not be present if full URIs used or defaults self.assertIn("Person", content) self.assertIn("Alice", content) self.assertIn("WORKS_FOR", content) except ImportError: print("Skipping RDF test due to missing dependencies") except Exception as e: self.fail(f"RDF Export failed: {e}") def test_graph_exporter(self): exporter = GraphExporter() output_path = Path(self.test_dir) / "output.graphml" try: exporter.export_knowledge_graph(self.kg, str(output_path), format="graphml") self.assertTrue(output_path.exists()) # Verify GraphML content with open(output_path, 'r', encoding='utf-8') as f: content = f.read() self.assertIn("