69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
import io
|
|
import sys
|
|
import json
|
|
import unittest
|
|
from src import index
|
|
|
|
class TestIndex(unittest.TestCase):
|
|
def setUp(self):
|
|
# Capture stdout
|
|
self._stdout = sys.stdout
|
|
sys.stdout = io.StringIO()
|
|
|
|
def tearDown(self):
|
|
sys.stdout = self._stdout
|
|
|
|
def test_plain_output_contains_all_strings(self):
|
|
# Run main without arguments
|
|
index.main()
|
|
output = sys.stdout.getvalue()
|
|
# Check that all labels are present
|
|
for label in index.LABELS:
|
|
self.assertIn(label, output, f"Missing label: {label}")
|
|
# Check that all metadata key/value pairs are present
|
|
for key, value in index.METADATA.items():
|
|
self.assertIn(f"{key}: {value}", output, f"Missing metadata: {key}")
|
|
|
|
def test_json_output_structure(self):
|
|
# Get JSON output via get_output
|
|
json_str = index.get_output(json_output=True)
|
|
data = json.loads(json_str)
|
|
# Verify top-level keys
|
|
self.assertIn("metadata", data)
|
|
self.assertIn("labels", data)
|
|
# Verify metadata content
|
|
self.assertEqual(data["metadata"], index.METADATA)
|
|
# Verify labels content
|
|
self.assertEqual(data["labels"], index.LABELS)
|
|
|
|
def test_main_returns_none(self):
|
|
# main should return None
|
|
result = index.main()
|
|
self.assertIsNone(result)
|
|
|
|
def test_output_is_not_empty(self):
|
|
index.main()
|
|
output = sys.stdout.getvalue()
|
|
self.assertTrue(len(output.strip()) > 0)
|
|
|
|
def test_get_output_plain(self):
|
|
plain = index.get_output(json_output=False)
|
|
# Should contain all labels and metadata
|
|
for label in index.LABELS:
|
|
self.assertIn(label, plain)
|
|
for key, value in index.METADATA.items():
|
|
self.assertIn(f"{key}: {value}", plain)
|
|
|
|
def test_get_output_json(self):
|
|
json_output = index.get_output(json_output=True)
|
|
# Should be valid JSON
|
|
try:
|
|
data = json.loads(json_output)
|
|
except json.JSONDecodeError as e:
|
|
self.fail(f"JSON output is invalid: {e}")
|
|
# Check that keys exist
|
|
self.assertIn("metadata", data)
|
|
self.assertIn("labels", data)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main() |