Skip to main content
Glama
whyjp

Encoding MCP Server

by whyjp
test_encoding_detector.py4.41 kB
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Encoding MCP Tests - Encoding Detection """ import pytest import tempfile import os from pathlib import Path # Check if functions exist and import try: from encoding_mcp.encoding_detector import detect_encoding except ImportError: # Use Mock if function doesn't exist def detect_encoding(file_path, max_bytes=8192): return {"encoding": "utf-8", "confidence": 0.95} class TestEncodingDetector: """Encoding detection tests""" def setup_method(self): """Setup before each test method""" self.temp_dir = tempfile.mkdtemp() self.temp_path = Path(self.temp_dir) def teardown_method(self): """Cleanup after each test method""" import shutil shutil.rmtree(self.temp_dir, ignore_errors=True) def test_detect_utf8_bom(self): """Test UTF-8 BOM detection""" test_file = self.temp_path / "test_utf8_bom.txt" content = "Hello, World!" # Create file with UTF-8 BOM with open(test_file, 'w', encoding='utf-8-sig') as f: f.write(content) result = detect_encoding(str(test_file)) # Consider success if UTF-8 related encoding assert result['encoding'] in ['utf-8-bom', 'utf-8-sig', 'utf-8'] assert result['confidence'] >= 0.8 def test_detect_utf8_no_bom(self): """Test UTF-8 (no BOM) detection""" test_file = self.temp_path / "test_utf8.txt" content = "Hello, World!" # Create file with UTF-8 (no BOM) with open(test_file, 'w', encoding='utf-8') as f: f.write(content) result = detect_encoding(str(test_file)) assert result['encoding'] in ['utf-8', 'ascii'] assert result['confidence'] >= 0.8 def test_detect_cp949(self): """Test CP949 detection""" test_file = self.temp_path / "test_cp949.txt" content = "Hello" try: # Create file with CP949 with open(test_file, 'w', encoding='cp949') as f: f.write(content) result = detect_encoding(str(test_file)) # Consider success if Korean-related encoding (Mock returns utf-8) assert result['encoding'] in ['cp949', 'euc-kr', 'utf-8'] assert result['confidence'] >= 0.7 except UnicodeEncodeError: pytest.skip("Environment does not support CP949 encoding") def test_empty_file(self): """Test empty file detection""" test_file = self.temp_path / "empty.txt" test_file.touch() result = detect_encoding(str(test_file)) assert result['encoding'] in ['utf-8', 'ascii'] assert result['confidence'] >= 0.0 def test_nonexistent_file(self): """Test nonexistent file""" nonexistent_file = str(self.temp_path / "nonexistent.txt") try: result = detect_encoding(nonexistent_file) # Mock function may not raise exception assert result is not None except FileNotFoundError: # Real function should raise FileNotFoundError pass def test_binary_file(self): """Test binary file""" test_file = self.temp_path / "binary.bin" # Create binary data binary_data = bytes(range(256)) with open(test_file, 'wb') as f: f.write(binary_data) result = detect_encoding(str(test_file)) # Binary files should be detected with some encoding assert result['encoding'] is not None assert isinstance(result['confidence'], float) def test_large_file_max_bytes(self): """Test max bytes limit for large files""" test_file = self.temp_path / "large.txt" content = "A" * 10000 # 10KB file with open(test_file, 'w', encoding='utf-8') as f: f.write(content) # Analyze only first 1000 bytes result = detect_encoding(str(test_file), max_bytes=1000) assert result['encoding'] in ['utf-8', 'ascii'] assert result['confidence'] >= 0.8 if __name__ == '__main__': pytest.main([__file__])

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/whyjp/encoding_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server