|
| 1 | +# Copyright 2025 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Tests for demos module. |
| 17 | +""" |
| 18 | + |
| 19 | +import pytest |
| 20 | +import tempfile |
| 21 | +from pathlib import Path |
| 22 | +from unittest.mock import patch, MagicMock |
| 23 | +from codeflare_sdk.common.utils.demos import copy_demo_nbs |
| 24 | + |
| 25 | + |
| 26 | +class TestCopyDemoNbs: |
| 27 | + """Test cases for copy_demo_nbs function.""" |
| 28 | + |
| 29 | + def test_copy_demo_nbs_directory_exists_error(self): |
| 30 | + """Test that FileExistsError is raised when directory exists and overwrite=False.""" |
| 31 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 32 | + # Create a subdirectory that will conflict |
| 33 | + conflict_dir = Path(temp_dir) / "demo-notebooks" |
| 34 | + conflict_dir.mkdir() |
| 35 | + |
| 36 | + with pytest.raises(FileExistsError, match="Directory.*already exists"): |
| 37 | + copy_demo_nbs(dir=str(conflict_dir), overwrite=False) |
| 38 | + |
| 39 | + def test_copy_demo_nbs_overwrite_true(self): |
| 40 | + """Test that overwrite=True allows copying to existing directory.""" |
| 41 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 42 | + # Create a subdirectory that will conflict |
| 43 | + conflict_dir = Path(temp_dir) / "demo-notebooks" |
| 44 | + conflict_dir.mkdir() |
| 45 | + |
| 46 | + # Mock the demo_dir to point to a real directory |
| 47 | + with patch("codeflare_sdk.common.utils.demos.demo_dir", temp_dir): |
| 48 | + # Should not raise an error with overwrite=True |
| 49 | + copy_demo_nbs(dir=str(conflict_dir), overwrite=True) |
| 50 | + |
| 51 | + def test_copy_demo_nbs_default_parameters(self): |
| 52 | + """Test copy_demo_nbs with default parameters.""" |
| 53 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 54 | + # Mock the demo_dir to point to a real directory |
| 55 | + with patch("codeflare_sdk.common.utils.demos.demo_dir", temp_dir): |
| 56 | + # Should work with default parameters |
| 57 | + copy_demo_nbs(dir=temp_dir, overwrite=True) |
0 commit comments