|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +from pytzdata import set_directory, tz_path, TimezoneNotFound |
| 6 | + |
| 7 | + |
| 8 | +fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'tz') |
| 9 | + |
| 10 | + |
| 11 | +def setup_module(module): |
| 12 | + if 'PYTZDATA_TZDATADIR' in os.environ: |
| 13 | + del os.environ['PYTZDATA_TZDATADIR'] |
| 14 | + |
| 15 | + set_directory() |
| 16 | + |
| 17 | + |
| 18 | +def teardown_module(module): |
| 19 | + if 'PYTZDATA_TZDATADIR' in os.environ: |
| 20 | + del os.environ['PYTZDATA_TZDATADIR'] |
| 21 | + |
| 22 | + set_directory() |
| 23 | + |
| 24 | + |
| 25 | +def test_set_directory(): |
| 26 | + set_directory(fixtures_path) |
| 27 | + |
| 28 | + assert tz_path('Europe/Paris') == os.path.join(fixtures_path, 'Europe/Paris') |
| 29 | + |
| 30 | + with pytest.raises(TimezoneNotFound): |
| 31 | + tz_path('America/New_York') |
| 32 | + |
| 33 | + here = os.path.realpath(os.path.dirname(__file__)) |
| 34 | + filepath = os.path.realpath( |
| 35 | + os.path.join(here, '..', 'pytzdata', 'zoneinfo', 'America', 'New_York') |
| 36 | + ) |
| 37 | + |
| 38 | + set_directory() |
| 39 | + |
| 40 | + assert tz_path('America/New_York') == filepath |
| 41 | + |
| 42 | + |
| 43 | +def test_env_variable(): |
| 44 | + os.environ['PYTZDATA_TZDATADIR'] = fixtures_path |
| 45 | + set_directory() |
| 46 | + |
| 47 | + assert tz_path('Europe/Paris') == os.path.join(fixtures_path, 'Europe/Paris') |
| 48 | + |
| 49 | + with pytest.raises(TimezoneNotFound): |
| 50 | + tz_path('America/New_York') |
| 51 | + |
| 52 | + del os.environ['PYTZDATA_TZDATADIR'] |
| 53 | + |
| 54 | + here = os.path.realpath(os.path.dirname(__file__)) |
| 55 | + filepath = os.path.realpath( |
| 56 | + os.path.join(here, '..', 'pytzdata', 'zoneinfo', 'America', 'New_York') |
| 57 | + ) |
| 58 | + |
| 59 | + set_directory() |
| 60 | + |
| 61 | + assert tz_path('America/New_York') == filepath |
0 commit comments