1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
import logging
import os
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import patch
import pytest
from pyproj import CRS, Transformer, get_codes, set_use_global_context
from pyproj.datadir import (
DataDirError,
append_data_dir,
get_data_dir,
get_user_data_dir,
set_data_dir,
)
from pyproj.enums import PJType
from pyproj.exceptions import CRSError
from test.conftest import proj_env
@contextmanager
def proj_logging_env():
"""
Ensure handler is added and then removed at end.
"""
console_handler = logging.StreamHandler()
formatter = logging.Formatter("%(threadName)s:%(levelname)s:%(message)s")
console_handler.setFormatter(formatter)
logger = logging.getLogger("pyproj")
logger.addHandler(console_handler)
logger.setLevel(logging.DEBUG)
try:
yield
finally:
logger.removeHandler(console_handler)
def create_projdb(tmpdir):
Path(tmpdir, "proj.db").write_text("DUMMY proj.db")
_INVALID_PATH = Path("/invalid/path/to/nowhere")
def test_get_data_dir__missing():
with (
proj_env(),
pytest.raises(DataDirError),
patch.dict(os.environ, {}, clear=True),
patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH),
patch("pyproj.datadir.shutil.which", return_value=None),
patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH),
patch("pyproj.datadir.sys.prefix", str(_INVALID_PATH)),
):
assert get_data_dir() is None
@pytest.mark.parametrize("projdir_type", [str, Path])
def test_get_data_dir__from_user(projdir_type, tmp_path):
tmpdir = tmp_path / "proj"
tmpdir.mkdir()
tmpdir_env = tmp_path / "proj_env"
tmpdir_env.mkdir()
with (
proj_env(),
patch.dict(os.environ, {"PROJ_DATA": str(tmpdir_env)}, clear=True),
patch("pyproj.datadir.Path.absolute", return_value=tmpdir / "datadir.py"),
patch("pyproj.datadir.sys.prefix", str(tmpdir_env)),
): # noqa: E501
create_projdb(tmpdir)
create_projdb(tmpdir_env)
set_data_dir(projdir_type(tmpdir))
internal_proj_dir = tmpdir / "proj_dir" / "share" / "proj"
internal_proj_dir.mkdir(parents=True)
create_projdb(internal_proj_dir)
assert get_data_dir() == str(tmpdir)
def test_get_data_dir__internal(tmp_path):
tmpdir = tmp_path / "proj"
tmpdir.mkdir()
tmpdir_fake = tmp_path / "proj_fake"
tmpdir_fake.mkdir()
with (
proj_env(),
patch.dict(
os.environ,
{"PROJ_LIB": str(tmpdir_fake), "PROJ_DATA": str(tmpdir_fake)},
clear=True,
),
patch("pyproj.datadir.Path.absolute", return_value=tmpdir / "datadir.py"),
patch("pyproj.datadir.sys.prefix", str(tmpdir_fake)),
):
create_projdb(tmpdir)
create_projdb(tmpdir_fake)
internal_proj_dir = tmpdir / "proj_dir" / "share" / "proj"
internal_proj_dir.mkdir(parents=True)
create_projdb(internal_proj_dir)
assert get_data_dir() == str(internal_proj_dir)
def test_get_data_dir__from_env_var__proj_lib(tmp_path):
with (
proj_env(),
patch.dict(os.environ, {"PROJ_LIB": str(tmp_path)}, clear=True),
patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH),
patch("pyproj.datadir.sys.prefix", str(_INVALID_PATH)),
):
create_projdb(tmp_path)
assert get_data_dir() == str(tmp_path)
def test_get_data_dir__from_env_var__proj_data(tmp_path):
with (
proj_env(),
patch.dict(os.environ, {"PROJ_DATA": str(tmp_path)}, clear=True),
patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH),
patch("pyproj.datadir.sys.prefix", str(_INVALID_PATH)),
):
create_projdb(tmp_path)
assert get_data_dir() == str(tmp_path)
def test_get_data_dir__from_env_var__multiple(tmp_path):
tmpdir = os.pathsep.join([str(tmp_path) for _ in range(3)])
with (
proj_env(),
patch.dict(os.environ, {"PROJ_DATA": tmpdir}, clear=True),
patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH),
patch("pyproj.datadir.sys.prefix", str(_INVALID_PATH)),
):
create_projdb(tmp_path)
assert get_data_dir() == tmpdir
def test_get_data_dir__from_prefix(tmp_path):
with (
proj_env(),
patch.dict(os.environ, {}, clear=True),
patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH),
patch("pyproj.datadir.sys.prefix", str(tmp_path)),
):
proj_dir = tmp_path / "share" / "proj"
proj_dir.mkdir(parents=True)
create_projdb(proj_dir)
assert get_data_dir() == str(proj_dir)
def test_get_data_dir__from_prefix__conda_windows(tmp_path):
with (
proj_env(),
patch.dict(os.environ, {}, clear=True),
patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH),
patch("pyproj.datadir.sys.prefix", str(tmp_path)),
):
proj_dir = tmp_path / "Library" / "share" / "proj"
proj_dir.mkdir(parents=True)
create_projdb(proj_dir)
assert get_data_dir() == str(proj_dir)
def test_get_data_dir__from_path(tmp_path):
with (
proj_env(),
patch.dict(os.environ, {}, clear=True),
patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH),
patch("pyproj.datadir.sys.prefix", str(_INVALID_PATH)),
patch(
"pyproj.datadir.shutil.which", return_value=str(tmp_path / "bin" / "proj")
),
):
proj_dir = tmp_path / "share" / "proj"
proj_dir.mkdir(parents=True)
create_projdb(proj_dir)
assert get_data_dir() == str(proj_dir)
@pytest.mark.parametrize("projdir_type", [str, Path])
def test_append_data_dir__internal(projdir_type, tmp_path):
with (
proj_env(),
patch.dict(os.environ, {}, clear=True),
patch("pyproj.datadir.Path.absolute", return_value=tmp_path / "datadir.py"),
patch("pyproj.datadir.sys.prefix", str(_INVALID_PATH)),
):
create_projdb(tmp_path)
internal_proj_dir = tmp_path / "proj_dir" / "share" / "proj"
internal_proj_dir.mkdir(parents=True)
create_projdb(internal_proj_dir)
extra_datadir = tmp_path / "extra_datumgrids"
append_data_dir(projdir_type(extra_datadir))
assert get_data_dir() == os.pathsep.join(
[str(internal_proj_dir), str(extra_datadir)]
)
@pytest.mark.slow
def test_creating_multiple_crs_without_file_limit():
"""
This test checks for two things:
1. Ensure database connection is closed for file limit
https://github.com/pyproj4/pyproj/issues/374
2. Ensure core-dumping does not occur when many objects are created
https://github.com/pyproj4/pyproj/issues/678
"""
codes = get_codes("EPSG", PJType.PROJECTED_CRS, False)
assert [CRS.from_epsg(code) for code in codes]
def test_get_user_data_dir():
assert get_user_data_dir().endswith("proj")
@patch.dict("os.environ", {"PYPROJ_GLOBAL_CONTEXT": "ON"}, clear=True)
def test_set_use_global_context__default_on():
with pytest.warns(FutureWarning):
set_use_global_context()
@patch.dict("os.environ", {"PYPROJ_GLOBAL_CONTEXT": "OFF"}, clear=True)
def test_set_use_global_context__on():
with pytest.warns(FutureWarning):
set_use_global_context(True)
def test_proj_debug_logging(capsys):
with proj_logging_env():
with pytest.warns(FutureWarning):
transformer = Transformer.from_proj("+init=epsg:4326", "+init=epsg:27700")
transformer.transform(100000, 100000)
captured = capsys.readouterr()
if os.environ.get("PROJ_DEBUG") == "3":
assert "PROJ_TRACE" in captured.err
assert "PROJ_DEBUG" in captured.err
elif os.environ.get("PROJ_DEBUG") == "2":
assert "PROJ_TRACE" not in captured.err
assert "PROJ_DEBUG" in captured.err
else:
assert "PROJ_ERROR" in captured.err
def test_proj_debug_logging__error(capsys):
with proj_logging_env(), pytest.raises(CRSError):
CRS("INVALID STRING")
captured = capsys.readouterr()
if os.environ.get("PROJ_DEBUG") == "3":
assert "PROJ_TRACE" in captured.err
assert "PROJ_DEBUG" in captured.err
assert "PROJ_ERROR" in captured.err
elif os.environ.get("PROJ_DEBUG") == "2":
assert "PROJ_TRACE" not in captured.err
assert "PROJ_DEBUG" in captured.err
assert "PROJ_ERROR" in captured.err
else:
assert captured.err == ""
assert captured.out == ""
|