Skip to main content
Glama
python-parser.test.ts2.88 kB
import { PythonParser } from '../../../src/scanner/parsers/python-parser.js'; describe('PythonParser', () => { let parser: PythonParser; beforeEach(() => { parser = new PythonParser(); jest.clearAllMocks(); }); describe('canParse', () => { test('should accept Python files', () => { expect(parser.canParse('test.py')).toBe(true); expect(parser.canParse('script.py')).toBe(true); expect(parser.canParse('/path/to/file.py')).toBe(true); }); test('should reject non-Python files', () => { expect(parser.canParse('test.ts')).toBe(false); expect(parser.canParse('test.js')).toBe(false); expect(parser.canParse('test.java')).toBe(false); expect(parser.canParse('test.txt')).toBe(false); }); test('should handle case insensitive extensions', () => { expect(parser.canParse('test.PY')).toBe(true); expect(parser.canParse('test.Py')).toBe(true); }); }); describe('parseFile', () => { const projectId = 'test-project'; const filePath = '/test/test.py'; test('should handle empty file', async () => { const result = await parser.parseFile(filePath, '', projectId); // Empty file might still create a module entity, which is acceptable expect(Array.isArray(result.entities)).toBe(true); expect(Array.isArray(result.relationships)).toBe(true); expect(Array.isArray(result.errors)).toBe(true); }); test('should return valid parse result structure', async () => { const pythonCode = `def test_function():\n pass`; const result = await parser.parseFile(filePath, pythonCode, projectId); expect(result).toHaveProperty('entities'); expect(result).toHaveProperty('relationships'); expect(result).toHaveProperty('errors'); expect(Array.isArray(result.entities)).toBe(true); expect(Array.isArray(result.relationships)).toBe(true); expect(Array.isArray(result.errors)).toBe(true); }); test('should handle simple Python code without throwing', async () => { const pythonCode = ` class TestClass: def __init__(self): self.value = 42 def get_value(self): return self.value `; expect(async () => { await parser.parseFile(filePath, pythonCode, projectId); }).not.toThrow(); }); test('should handle syntax errors gracefully', async () => { const pythonCode = ` def broken_function( # Missing closing parenthesis and colon print("This will cause a syntax error") `; const result = await parser.parseFile(filePath, pythonCode, projectId); // Should not throw, may have errors in result expect(result).toBeDefined(); expect(result).toHaveProperty('entities'); expect(result).toHaveProperty('relationships'); expect(result).toHaveProperty('errors'); }); }); });

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/JonnoC/CodeRAG'

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