File: test_filepath.py

package info (click to toggle)
netcdf4-python 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,588 kB
  • sloc: python: 6,002; ansic: 854; makefile: 15; sh: 2
file content (38 lines) | stat: -rw-r--r-- 1,210 bytes parent folder | download
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
import os, sys, shutil
import tempfile
import unittest
import netCDF4
import pathlib


@unittest.skipIf(not netCDF4.__has_nc_inq_path__, "missing `nc_inq_path`")
class test_filepath(unittest.TestCase):
    def setUp(self):
        self.netcdf_file = pathlib.Path(__file__).parent / "netcdf_dummy_file.nc"
        self.nc = netCDF4.Dataset(self.netcdf_file)

    def tearDown(self):
        self.nc.close()

    def test_filepath(self):
        assert self.nc.filepath() == str(self.netcdf_file)

    def test_filepath_with_non_ascii_characters(self):
        # create nc-file in a filepath using a cp1252 string
        tmpdir = tempfile.mkdtemp()
        filepath = os.path.join(tmpdir,b'Pl\xc3\xb6n.nc'.decode('cp1252'))
        nc = netCDF4.Dataset(filepath,'w',encoding='cp1252')
        filepatho = nc.filepath(encoding='cp1252')
        assert filepath == filepatho
        assert filepath.encode('cp1252') == filepatho.encode('cp1252')
        nc.close()
        shutil.rmtree(tmpdir)

    def test_no_such_file_raises(self):
        fname = 'not_a_nc_file.nc'
        with self.assertRaisesRegex(OSError, fname):
            netCDF4.Dataset(fname, 'r')


if __name__ == '__main__':
    unittest.main()