4 from unittest.mock import patch
7 from manager.utils import FileRepository, Repository, RepositoryStrategy
10 class MockZipFile(zipfile.ZipFile):
11 def __init__(self, *args, **kwargs):
14 def extractall(self, path: str) -> None:
17 def write(self, *arg, **kwargs) -> None:
21 def test_fetch_proper_repository():
22 repo: Repository = RepositoryStrategy.get_reporitory()
23 assert repo.__class__ is FileRepository
26 def test_blueprint_upload():
27 repo: Repository = RepositoryStrategy.get_reporitory()
29 with patch.object(manager.utils, "is_zipfile", return_value=True) as mock_is_zip, \
30 patch.object(os, "makedirs", return_value=None) as mock_mkdirs, \
31 patch.object(manager.utils, 'ZipFile', return_value=MockZipFile()
33 repo.upload_blueprint(b"abcd", "test_cba", "1.0.a")
34 mock_is_zip.assert_called_once()
35 mock_mkdirs.assert_called_once_with('/tmp/test_cba/1.0.a', mode=0o744)
39 def test_blueprint_download():
40 repo: Repository = RepositoryStrategy.get_reporitory()
42 ("test_cba", ["1.0.a"], []),
43 ("test_cba/1.0.a", [], ["file.txt"]),
46 with patch.object(os, "walk", return_value=mock_path) as mock_walk, \
47 patch.object(manager.utils, 'ZipFile', return_value=MockZipFile()), \
48 patch.object(os.path, 'exists', return_value=True
50 repo.download_blueprint("test_cba", "1.0.a")
51 mock_walk.assert_called_once_with('/tmp/test_cba/1.0.a')
55 def test_remove_blueprint():
56 repo: Repository = RepositoryStrategy.get_reporitory()
57 with patch.object(shutil, "rmtree", return_value=None) as mock_rmtree:
58 repo.remove_blueprint("cba", "1.0a")
59 mock_rmtree.assert_called_once()