Beispiel #1
0
    def test_nested_directories(self, grepo_with_scenario):
        # Given: A repo containing multiple dirs, one of them is empty | See: `mocked_contents_scenarios`
        grepo_with_scenario.init_scenario('nested_directories')

        # When: Fetching the root path: ''
        result = grepo_with_scenario.get_files_to_download(path='')

        # Then: Files hierarchy is flattened
        assert sort_by_file_path(result) == sort_by_file_path([
            DownloadableFile(
                file_path=Path('file_at_root.txt'),
                download_url='https://github_url_for/file_at_root.txt'
            ),
            DownloadableFile(
                file_path=Path('dir_at_root/file_at_level_1.txt'),
                download_url='https://github_url_for/dir_at_root/file_at_level_1.txt'
            ),
            DownloadableFile(
                file_path=Path('dir_at_root/dir_at_level_1/file_at_level_2.txt'),
                download_url='https://github_url_for/dir_at_root/dir_at_level_1/file_at_level_2.txt'
            ),
            DownloadableFile(
                file_path=Path('dir_at_root/dir_at_level_1/dir_at_level_2/file_at_level_3.txt'),
                download_url='https://github_url_for/dir_at_root/dir_at_level_1/dir_at_level_2/file_at_level_3.txt'
            )])
Beispiel #2
0
        def test_empty_file(self, tmp_path: Path, single_file_test_helper):
            file = DownloadableFile(file_path=Path('file.txt'), download_url='http://this_is_the_url/file.txt')

            single_file_test_helper.test_file_is_downloaded_and_saved_with_correct_content(
                root_dir=tmp_path,
                file_to_download=file,
                file_content='')
Beispiel #3
0
        def test_in_sub_path(self, tmp_path: Path, single_file_test_helper):
            file = DownloadableFile(file_path=Path('this/is/a/sub_path/file.txt'),
                                    download_url='http://this_is_the_url/this/is/a/sub_path/file.txt')

            single_file_test_helper.test_file_is_downloaded_and_saved_with_correct_content(
                root_dir=tmp_path,
                file_to_download=file,
                file_content='EXPECTED TEXT CONTENT')
Beispiel #4
0
        def test_root_dir_doesnt_exist_then_create_it(self, tmp_path: Path, single_file_test_helper):
            file = DownloadableFile(file_path=Path('this/is/a/sub_path/file.txt'),
                                    download_url='http://this_is_the_url/this/is/a/sub_path/file.txt')

            root_dir = tmp_path / 'does_not_exist_yet'
            single_file_test_helper.test_file_is_downloaded_and_saved_with_correct_content(
                root_dir=root_dir,
                file_to_download=file,
                file_content='EXPECTED TEXT CONTENT')
Beispiel #5
0
        def test_diverse_multiple_files(self, tmp_path: Path, mock_api: GithubApi, grepo: GRepo):
            def return_file_content_for_correct_url(url):
                mock_content_for_url = {
                    'http://this_is_the_url/file_1.md': "CONTENT FOR 'file_1.md'",
                    'http://this_is_the_url/file_2.md': "CONTENT FOR 'file_2.md'",
                    'http://this_is_the_url/file_with_wrong_name_in_url.txt': "CONTENT FOR 'file_3.md'",
                    'http://this_is_the_url/file_empty.md': "",
                    'http://this_is_the_url/sub/path/file_in_sub_path.md': "CONTENT FOR 'file_in_sub_path.md'",
                }
                if url not in mock_content_for_url:
                    pytest.fail(f"Api wasn't called with the correct url | Incorrect URL: {url}")
                return mock_content_for_url[url]

            # GIVEN: A list of DownloadableFiles w/ content available in the Mock Api
            mock_api.download_raw_text_file.side_effect = return_file_content_for_correct_url
            files_to_download = [
                DownloadableFile(file_path=Path('file_1.md'),
                                 download_url='http://this_is_the_url/file_1.md'),
                DownloadableFile(file_path=Path('file_2.md'),
                                 download_url='http://this_is_the_url/file_2.md'),
                DownloadableFile(file_path=Path('file_3.md'),
                                 download_url='http://this_is_the_url/file_with_wrong_name_in_url.txt'),
                DownloadableFile(file_path=Path('file_empty.md'),
                                 download_url='http://this_is_the_url/file_empty.md'),
                DownloadableFile(file_path=Path('sub/path/file_in_sub_path.md'),
                                 download_url='http://this_is_the_url/sub/path/file_in_sub_path.md')]

            # WHEN: Downloading all files
            root_dir = tmp_path
            grepo.download_files_at_location(root_dir=root_dir, files_to_download=files_to_download)

            # THEN: All files have been correctly downloaded
            def assert_file_at_path_has_content(file_path: Path, expected_content: str):
                assert file_path.exists()
                with file_path.open('r') as downloaded_file:
                    assert downloaded_file.read() == expected_content

            assert_file_at_path_has_content(root_dir / 'file_1.md', "CONTENT FOR 'file_1.md'")
            assert_file_at_path_has_content(root_dir / 'file_2.md', "CONTENT FOR 'file_2.md'")
            assert_file_at_path_has_content(root_dir / 'file_3.md', "CONTENT FOR 'file_3.md'")
            assert_file_at_path_has_content(root_dir / 'file_empty.md', "")
            assert_file_at_path_has_content(root_dir / 'sub/path/file_in_sub_path.md',
                                            "CONTENT FOR 'file_in_sub_path.md'")
Beispiel #6
0
    def test_mix_of_files_and_directory(self, grepo_with_scenario):
        # Given: A repo containing multiple dirs, one of them is empty | See: `mocked_contents_scenarios`
        grepo_with_scenario.init_scenario('mix_of_files_and_directory')

        # When: Fetching the root path: ''
        result = grepo_with_scenario.get_files_to_download(path='')

        # Then: Files hierarchy is flattened
        assert sort_by_file_path(result) == sort_by_file_path([
            DownloadableFile(
                file_path=Path('a_file.txt'),
                download_url='https://github_url_for/a_file.txt'
            ),
            DownloadableFile(
                file_path=Path('some_dir/a_file.txt'),
                download_url='https://github_url_for/some_dir/a_file.txt'
            ),
            DownloadableFile(
                file_path=Path('some_dir/another_file.py'),
                download_url='https://github_url_for/some_dir/another_file.py'
            )])
Beispiel #7
0
    def test_multiple_directories_one_is_empty(self, grepo_with_scenario):
        # Given: A repo containing multiple dirs, one of them is empty | See: `mocked_contents_scenarios`
        grepo_with_scenario.init_scenario('multiple_directories_one_is_empty')

        # When: Fetching the root path: ''
        result = grepo_with_scenario.get_files_to_download(path='')

        # Then: Empty dir is ignored
        assert sort_by_file_path(result) == sort_by_file_path([
            DownloadableFile(
                file_path=Path('some_dir/a_file.txt'),
                download_url='https://github_url_for/some_dir/a_file.txt'
            ),
            DownloadableFile(
                file_path=Path('some_dir/another_file.py'),
                download_url='https://github_url_for/some_dir/another_file.py'
            ),
            DownloadableFile(
                file_path=Path('some_dir/a_third_file.md'),
                download_url='https://github_url_for/some_dir/a_third_file.md'
            )])
Beispiel #8
0
    def test_directory_containing_files(self, grepo_with_scenario):
        # Given: A repo containing a dir, itself containing files | See: `mocked_contents_scenarios`
        grepo_with_scenario.init_scenario('directory_containing_files')

        # When: Fetching the root path: ''
        result = grepo_with_scenario.get_files_to_download(path='')

        # Then: List all the files recursively found
        assert sort_by_file_path(result) == sort_by_file_path([
            DownloadableFile(
                file_path=Path('some_dir/a_file.txt'),
                download_url='https://github_url_for/some_dir/a_file.txt'
            ),
            DownloadableFile(
                file_path=Path('some_dir/another_file.py'),
                download_url='https://github_url_for/some_dir/another_file.py'
            ),
            DownloadableFile(
                file_path=Path('some_dir/a_third_file.md'),
                download_url='https://github_url_for/some_dir/a_third_file.md'
            )])
Beispiel #9
0
        def test_file_has_different_path_in_url_use_file_path(self, tmp_path: Path, single_file_test_helper):
            file = DownloadableFile(file_path=Path('expected/path/expected_name.md'),
                                    download_url='http://this_is_the_url/different/path/different_name.md')
            root_dir = tmp_path

            single_file_test_helper.test_file_is_downloaded_and_saved_with_correct_content(
                root_dir=root_dir,
                file_to_download=file,
                file_content='EXPECTED TEXT CONTENT')

            # Already tested in helper, but re-tested here for clarity of intent
            expected_written_file_path = root_dir / 'expected/path/expected_name.md'
            assert expected_written_file_path.exists()
Beispiel #10
0
        def test_root_dir_isn_t_a_dir(self, tmp_path: Path, grepo: GRepo):
            # Given: Root dir isn't a 'directory'
            not_a_dir = tmp_path / 'not_a_dir'
            with not_a_dir.open('w') as file:
                file.write('I am not a dir')
            assert not not_a_dir.is_dir()

            # When: Trying to download files
            # Then: An exception is raised
            with pytest.raises(FileExistsError) as raised_exception:
                grepo.download_files_at_location(root_dir=not_a_dir,
                                                 files_to_download=[DownloadableFile(Path('file.txt'),
                                                                                     'http://url.com/file.txt')])
            assert raised_exception.match(r"Root dir '.*not_a_dir' is not a directory")
Beispiel #11
0
from unittest.mock import MagicMock

import pytest

from kata.data.repos import HardCoded
from kata.defaults import DEFAULT_CONFIG
from kata.domain.exceptions import InvalidKataName, KataLanguageNotFound, KataTemplateNotFound
from kata.domain.grepo import GRepo
from kata.domain.models import DownloadableFile, KataLanguage, KataTemplate
from kata.domain.services import InitKataService, LoginService

NOT_USED = 'Not Used'
VALID_KATA_NAME = 'kata_name'

MOCK_FILES_TO_DOWNLOAD = [
    DownloadableFile(Path('fake.txt'), 'http://hello.com/fake.txt'),
    DownloadableFile(Path('hey/fake.txt'), 'http://hello.com/hey/fake.txt'),
    DownloadableFile(Path('hey/fake.md'), 'http://hello.com/hey/fake.md')
]


@pytest.fixture
def config_repo():
    return HardCoded.ConfigRepo()


class TestInitKataService:
    @pytest.fixture
    @mock.patch('src.kata.domain.grepo.GRepo')
    def mock_grepo(
        self,
Beispiel #12
0
 def _map_to_model(contents):
     return [
         DownloadableFile(
             file_path=Path(file['path']),
             download_url=file['download_url']
         ) for file in contents]
Beispiel #13
0
 def files_with_sub_path_at_root():
     for file in files:
         yield DownloadableFile(file_path=file.file_path.relative_to(sub_path),
                                download_url=file.download_url)