Пример #1
0
    def test_mock_envvar(self):
        """Test that environment variables can be mocked properly."""
        name, value = n(), n()

        self.assertNotIn(name, os.environ)
        with mock_envvar(name, value):
            self.assertIn(name, os.environ)
            self.assertEqual(value, os.getenv(name))
        self.assertNotIn(name, os.environ)
Пример #2
0
 def test_ensure_open_lzma(self):
     """Test opening lzma-encoded files."""
     with tempfile.TemporaryDirectory() as directory, self.mock_directory():
         path = Path(directory) / n()
         with self.mock_download_once(path):
             with lzma.open(path, "wt") as file:
                 for row in TEST_TSV_ROWS:
                     print(*row, sep="\t", file=file)  # noqa:T001,T201
             with pystow.ensure_open_lzma("test", url=n()) as file:
                 df = pd.read_csv(file, sep="\t")
                 self.assertEqual(3, len(df.columns))
Пример #3
0
 def test_ensure_open_tarfile(self):
     """Test opening tarfile-encoded files."""
     with tempfile.TemporaryDirectory() as directory, self.mock_directory():
         path = Path(directory) / n()
         inner_path = n()
         with self.mock_download_once(path):
             write_tarfile_csv(TEST_DF, path, inner_path)
             with pystow.ensure_open_tarfile("test",
                                             url=n(),
                                             inner_path=inner_path) as file:
                 df = pd.read_csv(file, sep="\t")
                 self.assertEqual(3, len(df.columns))
Пример #4
0
    def test_getenv_path(self):
        """Test that :func:`getenv_path` works properly."""
        envvar = n()

        with tempfile.TemporaryDirectory() as directory:
            directory = Path(directory)
            value = directory / n()
            default = directory / n()

            self.assertEqual(default, getenv_path(envvar, default))
            with mock_envvar(envvar, value.as_posix()):
                self.assertEqual(value, getenv_path(envvar, default))
            # Check that it goes back
            self.assertEqual(default, getenv_path(envvar, default))
Пример #5
0
    def test_mock_home(self):
        """Test that home can be properly mocked."""
        name = n()

        with tempfile.TemporaryDirectory() as d:
            expected_path = Path(d) / name
            self.assertFalse(expected_path.exists())

            with mock_envvar(PYSTOW_HOME_ENVVAR, expected_path.as_posix()):
                self.assertFalse(expected_path.exists())
                self.assertEqual(expected_path, get_home(ensure_exists=False))
                self.assertFalse(expected_path.exists())
Пример #6
0
    def test_mock_name(self):
        """Test that the name can be properly mocked."""
        name = n()

        expected_path = Path.home() / name
        self.assertFalse(expected_path.exists())

        with mock_envvar(PYSTOW_NAME_ENVVAR, name):
            self.assertEqual(name, get_name())

            self.assertFalse(expected_path.exists())
            self.assertEqual(expected_path, get_home(ensure_exists=False))
            self.assertFalse(expected_path.exists())
Пример #7
0
 def test_get(self):
     """Test the :func:`get` function."""
     parts_examples = [
         [n()],
         [n(), n()],
         [n(), n(), n()],
     ]
     with self.mock_directory():
         for parts in parts_examples:
             with self.subTest(parts=parts):
                 self.assertEqual(self.join(*parts), join(*parts))
Пример #8
0
    def test_ensure_custom(self):
        """Test ensure with custom provider."""
        with self.mock_directory():

            # create a minimal provider
            def touch_file(path: Path, **_kwargs):
                """
                Create a file.

                :param path:
                    the file path
                :param _kwargs:
                    ignored keywords
                """
                path.touch()

            # wrap to record calls
            provider = mock.Mock(wraps=touch_file)

            # the keyword-based parameters for the provider
            kwargs = {"a": 4, "c": {0: 1, 5: 7}}

            # call first time
            name = n()
            path = pystow.ensure_custom("test",
                                        name=name,
                                        provider=provider,
                                        **kwargs)
            self.assertTrue(path.is_file())
            # call a second time
            path = pystow.ensure_custom("test",
                                        name=name,
                                        provider=provider,
                                        **kwargs)
            # ensure that the provider was only called once with the given parameters
            provider.assert_called_once_with(path, **kwargs)
Пример #9
0
    def test_ensure_module(self):
        """Test that the ``ensure_exist`` argument in :meth:`Module.from_key` works properly."""
        parts_examples = [
            [n()],
            [n(), n()],
            [n(), n(), n()],
        ]
        ensure_examples = [False, True]

        for ensure_exists, parts in itt.product(ensure_examples,
                                                parts_examples):
            with self.subTest(ensure_exists=ensure_exists,
                              parts=parts), self.mock_directory():
                expected_directory = self.join(*parts)

                module = Module.from_key(*parts, ensure_exists=ensure_exists)

                self.assertEqual(expected_directory, module.base)
                self.assertIs(
                    expected_directory.exists(),
                    ensure_exists,
                    msg=
                    f'{expected_directory} should{"" if ensure_exists else " not"} exist.',
                )