Coverage for src/qdrant_loader/core/chunking/strategy/code/metadata/testing.py: 89%

19 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-08 06:05 +0000

1from __future__ import annotations 

2 

3 

4def identify_test_code(content: str) -> dict[str, int | bool | str]: 

5 indicators = { 

6 "is_test_file": False, 

7 "test_framework": "none", 

8 "test_count": 0, 

9 "assertion_count": 0, 

10 "mock_usage": False, 

11 "fixture_usage": False, 

12 } 

13 

14 content_lower = content.lower() 

15 indicators["is_test_file"] = any( 

16 keyword in content_lower 

17 for keyword in ["test_", "test", "spec", "unittest", "pytest"] 

18 ) 

19 

20 if "pytest" in content_lower or "@pytest" in content: 

21 indicators["test_framework"] = "pytest" 

22 elif "unittest" in content_lower: 

23 indicators["test_framework"] = "unittest" 

24 elif "jest" in content_lower or "describe(" in content: 

25 indicators["test_framework"] = "jest" 

26 elif "mocha" in content_lower: 

27 indicators["test_framework"] = "mocha" 

28 

29 indicators["test_count"] = content.count("def test_") + content.count("it(") 

30 

31 assertion_patterns = [ 

32 "assert ", 

33 "assert(", 

34 "expect(", 

35 "should", 

36 "assertEqual", 

37 "assertTrue", 

38 "pytest.raises", 

39 "self.assert", 

40 "with pytest.raises", 

41 "raises(", 

42 ] 

43 indicators["assertion_count"] = sum(content.count(p) for p in assertion_patterns) 

44 

45 indicators["mock_usage"] = any( 

46 k in content_lower for k in ["mock", "stub", "spy", "patch"] 

47 ) 

48 indicators["fixture_usage"] = any( 

49 k in content_lower for k in ["fixture", "setup", "teardown"] 

50 ) 

51 

52 return indicators