Coverage for skema/program_analysis/tests/test_module_locate.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1import pytest 

2from unittest.mock import patch 

3from skema.program_analysis.module_locate import identify_source_type, module_locate 

4 

5# Testing identify_source_type 

6@pytest.mark.parametrize("source,expected_type", [ 

7 ("https://github.com/python/cpython", "Compiled"), 

8 ("https://github.com/other/repository", "Repository"), 

9 ("http://example.com", "Url"), 

10 ("local/path/to/module", "Local"), 

11 ("", "Unknown"), 

12]) 

13def test_identify_source_type(source, expected_type): 

14 assert identify_source_type(source) == expected_type 

15 

16# Mocking requests.get to test module_locate without actual HTTP requests 

17@pytest.fixture 

18def mock_requests_get(mocker): 

19 mock = mocker.patch('skema.program_analysis.module_locate.requests.get') 

20 return mock 

21 

22def test_module_locate_builtin_module(): 

23 assert module_locate("sys") == "https://github.com/python/cpython" 

24 

25def test_module_locate_from_pypi_with_github_source(mock_requests_get): 

26 mock_requests_get.return_value.json.return_value = { 

27 'info': {'version': '1.0.0', 'project_urls': {'Source': 'https://github.com/example/project'}}, 

28 'releases': {'1.0.0': [{'filename': 'example-1.0.0.tar.gz', 'url': 'https://example.com/example-1.0.0.tar.gz'}]} 

29 } 

30 assert module_locate("example") == "https://github.com/example/project" 

31 

32def test_module_locate_from_pypi_with_tarball_url(mock_requests_get): 

33 mock_requests_get.return_value.json.return_value = { 

34 'info': {'version': '1.2.3'}, 

35 'releases': {'1.2.3': [{'filename': 'package-1.2.3.tar.gz', 'url': 'https://pypi.org/package-1.2.3.tar.gz'}]} 

36 } 

37 assert module_locate("package") == "https://pypi.org/package-1.2.3.tar.gz" 

38 

39def test_module_locate_not_found(mock_requests_get): 

40 mock_requests_get.side_effect = Exception("Module not found") 

41 assert module_locate("nonexistent") is None