|
4 | 4 | from pathlib import Path |
5 | 5 | from unittest.mock import MagicMock, mock_open, patch |
6 | 6 |
|
| 7 | +import pytest |
7 | 8 | import yaml |
8 | 9 | from pystac import Catalog |
9 | 10 |
|
10 | 11 | from deep_code.tools.publish import Publisher |
| 12 | +from deep_code.utils.ogc_api_record import LinksBuilder |
11 | 13 |
|
12 | 14 |
|
13 | 15 | class TestPublisher(unittest.TestCase): |
@@ -107,3 +109,59 @@ def test_read_config_files(self): |
107 | 109 | # Assertions |
108 | 110 | self.assertEqual(self.publisher.dataset_config, dataset_config) |
109 | 111 | self.assertEqual(self.publisher.workflow_config, workflow_config) |
| 112 | + |
| 113 | + |
| 114 | +class TestParseGithubNotebookUrl: |
| 115 | + @pytest.mark.parametrize( |
| 116 | + "url,repo_url,repo_name,branch,file_path", |
| 117 | + [ |
| 118 | + ( |
| 119 | + "https://github.com/deepesdl/cube-gen/blob/main/Permafrost/Create-CCI-Permafrost-cube-EarthCODE.ipynb", |
| 120 | + "https://github.com/deepesdl/cube-gen", |
| 121 | + "cube-gen", |
| 122 | + "main", |
| 123 | + "Permafrost/Create-CCI-Permafrost-cube-EarthCODE.ipynb", |
| 124 | + ), |
| 125 | + ( |
| 126 | + "https://github.com/deepesdl/cube-gen/tree/release-1.0/Permafrost/Create-CCI-Permafrost-cube-EarthCODE.ipynb", |
| 127 | + "https://github.com/deepesdl/cube-gen", |
| 128 | + "cube-gen", |
| 129 | + "release-1.0", |
| 130 | + "Permafrost/Create-CCI-Permafrost-cube-EarthCODE.ipynb", |
| 131 | + ), |
| 132 | + ( |
| 133 | + "https://raw.githubusercontent.com/deepesdl/cube-gen/main/Permafrost/Create-CCI-Permafrost-cube-EarthCODE.ipynb", |
| 134 | + "https://github.com/deepesdl/cube-gen", |
| 135 | + "cube-gen", |
| 136 | + "main", |
| 137 | + "Permafrost/Create-CCI-Permafrost-cube-EarthCODE.ipynb", |
| 138 | + ), |
| 139 | + ], |
| 140 | + ) |
| 141 | + def test_valid_urls(self, url, repo_url, repo_name, branch, file_path): |
| 142 | + got_repo_url, got_repo_name, got_branch, got_file_path = LinksBuilder._parse_github_notebook_url( |
| 143 | + url |
| 144 | + ) |
| 145 | + assert got_repo_url == repo_url |
| 146 | + assert got_repo_name == repo_name |
| 147 | + assert got_branch == branch |
| 148 | + assert got_file_path == file_path |
| 149 | + |
| 150 | + def test_invalid_domain(self): |
| 151 | + url = "https://gitlab.com/deepesdl/cube-gen/-/blob/main/Permafrost/Create-CCI-Permafrost-cube-EarthCODE.ipynb" |
| 152 | + with pytest.raises(ValueError) as e: |
| 153 | + LinksBuilder._parse_github_notebook_url(url) |
| 154 | + assert "Only GitHub URLs are supported" in str(e.value) |
| 155 | + |
| 156 | + def test_unexpected_github_format_missing_blob_or_tree(self): |
| 157 | + # Missing the "blob" or "tree" segment |
| 158 | + url = "https://github.com/deepesdl/cube-gen/main/Permafrost/Create-CCI-Permafrost-cube-EarthCODE.ipynb" |
| 159 | + with pytest.raises(ValueError) as e: |
| 160 | + LinksBuilder._parse_github_notebook_url(url) |
| 161 | + assert "Unexpected GitHub URL format" in str(e.value) |
| 162 | + |
| 163 | + def test_unexpected_raw_format_too_short(self): |
| 164 | + url = "https://raw.githubusercontent.com/deepesdl/cube-gen/main" |
| 165 | + with pytest.raises(ValueError) as e: |
| 166 | + LinksBuilder._parse_github_notebook_url(url) |
| 167 | + assert "Unexpected raw.githubusercontent URL format" in str(e.value) |
0 commit comments