|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Focused test script for the _extract_repo_name_from_url method |
| 4 | +
|
| 5 | +Run this script to test only the repository name extraction functionality. |
| 6 | +Usage: python test_extract_repo_name.py |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | +import os |
| 11 | +import sys |
| 12 | +from unittest.mock import Mock, patch |
| 13 | + |
| 14 | +# Add the parent directory to the path to import the data_pipeline module |
| 15 | +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
| 16 | + |
| 17 | +# Import the modules under test |
| 18 | +from api.data_pipeline import DatabaseManager |
| 19 | + |
| 20 | + |
| 21 | +class TestExtractRepoNameFromUrl: |
| 22 | + """Comprehensive tests for the _extract_repo_name_from_url method""" |
| 23 | + |
| 24 | + def setup_method(self): |
| 25 | + """Set up test fixtures before each test method.""" |
| 26 | + self.db_manager = DatabaseManager() |
| 27 | + |
| 28 | + def test_extract_repo_name_github_standard_url(self): |
| 29 | + |
| 30 | + # Test standard GitHub URL |
| 31 | + github_url = "https://github.com/owner/repo" |
| 32 | + result = self.db_manager._extract_repo_name_from_url(github_url, "github") |
| 33 | + assert result == "owner_repo" |
| 34 | + |
| 35 | + # Test GitHub URL with .git suffix |
| 36 | + github_url_git = "https://github.com/owner/repo.git" |
| 37 | + result = self.db_manager._extract_repo_name_from_url(github_url_git, "github") |
| 38 | + assert result == "owner_repo" |
| 39 | + |
| 40 | + # Test GitHub URL with trailing slash |
| 41 | + github_url_slash = "https://github.com/owner/repo/" |
| 42 | + result = self.db_manager._extract_repo_name_from_url(github_url_slash, "github") |
| 43 | + assert result == "owner_repo" |
| 44 | + |
| 45 | + print("✓ GitHub URL tests passed") |
| 46 | + |
| 47 | + def test_extract_repo_name_gitlab_urls(self): |
| 48 | + """Test repository name extraction from GitLab URLs""" |
| 49 | + |
| 50 | + # Test standard GitLab URL |
| 51 | + gitlab_url = "https://gitlab.com/owner/repo" |
| 52 | + result = self.db_manager._extract_repo_name_from_url(gitlab_url, "gitlab") |
| 53 | + assert result == "owner_repo" |
| 54 | + |
| 55 | + # Test GitLab URL with subgroups |
| 56 | + gitlab_subgroup = "https://gitlab.com/group/subgroup/repo" |
| 57 | + result = self.db_manager._extract_repo_name_from_url(gitlab_subgroup, "gitlab") |
| 58 | + assert result == "subgroup_repo" |
| 59 | + |
| 60 | + print("✓ GitLab URL tests passed") |
| 61 | + |
| 62 | + def test_extract_repo_name_bitbucket_urls(self): |
| 63 | + """Test repository name extraction from Bitbucket URLs""" |
| 64 | + bitbucket_url = "https://bitbucket.org/owner/repo" |
| 65 | + result = self.db_manager._extract_repo_name_from_url(bitbucket_url, "bitbucket") |
| 66 | + assert result == "owner_repo" |
| 67 | + |
| 68 | + print("✓ Bitbucket URL tests passed") |
| 69 | + |
| 70 | + def test_extract_repo_name_local_paths(self): |
| 71 | + """Test repository name extraction from local paths""" |
| 72 | + result = self.db_manager._extract_repo_name_from_url("/home/user/projects/my-repo", "local") |
| 73 | + assert result == "my-repo" |
| 74 | + |
| 75 | + result = self.db_manager._extract_repo_name_from_url("/var/repos/project.git", "local") |
| 76 | + assert result == "project" |
| 77 | + |
| 78 | + print("✓ Local path tests passed") |
| 79 | + |
| 80 | + def test_extract_repo_name_current_implementation_bug(self): |
| 81 | + """Test that demonstrates the current implementation bug""" |
| 82 | + # The current implementation references 'type' which is not in scope |
| 83 | + try: |
| 84 | + # This should raise a NameError due to undefined 'type' variable |
| 85 | + result = self.db_manager._extract_repo_name_from_url("https://github.com/owner/repo") |
| 86 | + print("⚠️ WARNING: Expected the current implementation to fail due to undefined 'type' variable") |
| 87 | + print(f" But got result: {result}") |
| 88 | + except (NameError, TypeError) as e: |
| 89 | + print(f"✓ Current implementation correctly fails with: {type(e).__name__}: {e}") |
| 90 | + except Exception as e: |
| 91 | + print(f"⚠️ Unexpected error: {type(e).__name__}: {e}") |
| 92 | + |
| 93 | + # Test absolute local path |
| 94 | + local_path = "/home/user/projects/my-repo" |
| 95 | + result = self.db_manager._extract_repo_name_from_url(local_path, "local") |
| 96 | + assert result == "my-repo" |
| 97 | + |
| 98 | + # Test local path with .git suffix |
| 99 | + local_git = "/var/repos/project.git" |
| 100 | + result = self.db_manager._extract_repo_name_from_url(local_git, "local") |
| 101 | + assert result == "project" |
| 102 | + |
| 103 | + print("✓ Local path tests passed") |
| 104 | + |
| 105 | + def test_extract_repo_name_edge_cases(self): |
| 106 | + """Test edge cases for repository name extraction""" |
| 107 | + |
| 108 | + # Test URL with insufficient parts (should use fallback) |
| 109 | + short_url = "https://github.com/repo" |
| 110 | + result = self.db_manager._extract_repo_name_from_url(short_url, "github") |
| 111 | + assert result == "repo" |
| 112 | + |
| 113 | + # Test single directory name |
| 114 | + single_name = "my-repo" |
| 115 | + result = self.db_manager._extract_repo_name_from_url(single_name, "local") |
| 116 | + assert result == "my-repo" |
| 117 | + |
| 118 | + print("✓ Edge case tests passed") |
0 commit comments