File: test_file.py

package info (click to toggle)
augur 24.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,312 kB
  • sloc: python: 14,253; sh: 227; makefile: 35
file content (117 lines) | stat: -rw-r--r-- 4,714 bytes parent folder | download | duplicates (2)
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
import pytest
import augur.io.file


class TestFile:
    def test_open_file_read_text(self, tmpdir):
        """Read a text file."""
        path = str(tmpdir / 'test.txt')
        with open(path, 'w') as f_write:
            f_write.write('foo\nbar\n')
        with augur.io.file.open_file(path) as f_read:
            assert f_read.read() == 'foo\nbar\n'

    def test_open_file_write_text(self, tmpdir):
        """Write a text file."""
        path = str(tmpdir / 'test.txt')
        with augur.io.file.open_file(path, 'w') as f_write:
            f_write.write('foo\nbar\n')
        with open(path) as f_read:
            assert f_read.read() == 'foo\nbar\n'

    def test_open_file_read_gzip(self, tmpdir):
        """Read a text file compressed with gzip."""
        import gzip
        path = str(tmpdir / 'test.txt.gz')
        with gzip.open(path, 'wt') as f_write:
            f_write.write('foo\nbar\n')
        with augur.io.file.open_file(path) as f_read:
            assert f_read.read() == 'foo\nbar\n'

    def test_open_file_write_gzip(self, tmpdir):
        """Write a text file compressed with gzip."""
        import gzip
        path = str(tmpdir / 'test.txt.gz')
        with augur.io.file.open_file(path, 'w') as f_write:
            f_write.write('foo\nbar\n')
        with gzip.open(path, 'rt') as f_read:
            assert f_read.read() == 'foo\nbar\n'

    def test_open_file_read_lzma(self, tmpdir):
        """Read a text file compressed with LZMA."""
        import lzma
        path = str(tmpdir / 'test.txt.xz')
        with lzma.open(path, 'wt') as f_write:
            f_write.write('foo\nbar\n')
        with augur.io.file.open_file(path) as f_read:
            assert f_read.read() == 'foo\nbar\n'

    def test_open_file_write_lzma(self, tmpdir):
        """Write a text file compressed with LZMA."""
        import lzma
        path = str(tmpdir / 'test.txt.xz')
        with augur.io.file.open_file(path, 'w') as f_write:
            f_write.write('foo\nbar\n')
        with lzma.open(path, 'rt') as f_read:
            assert f_read.read() == 'foo\nbar\n'

    def test_open_file_read_zstd(self, tmpdir):
        """Read a text file compressed with zstd."""
        import zstandard as zstd
        path = str(tmpdir / 'test.txt.zst')
        with zstd.open(path, 'wt') as f_write:
            f_write.write('foo\nbar\n')
        with augur.io.file.open_file(path) as f_read:
            assert f_read.read() == 'foo\nbar\n'

    def test_open_file_write_zstd(self, tmpdir):
        """Write a text file compressed with zstd."""
        import zstandard as zstd
        path = str(tmpdir / 'test.txt.zst')
        with augur.io.file.open_file(path, 'w') as f_write:
            f_write.write('foo\nbar\n')
        with zstd.open(path, 'rt') as f_read:
            assert f_read.read() == 'foo\nbar\n'

    def test_open_file_nested_read(self, tmpdir):
        """Open a text file as a path then buffer for reading."""
        path = str(tmpdir / 'test.txt')
        with open(path, 'wt') as f_write:
            f_write.write('foo\nbar\n')

        with augur.io.file.open_file(path) as f_read:
            with augur.io.file.open_file(f_read) as f_read:
                assert f_read.read() == 'foo\nbar\n'

    def test_open_file_nested_read_zstd(self, tmpdir):
        """Open a zstd-compressed text file as a path then buffer for reading."""
        import zstandard as zstd
        path = str(tmpdir / 'test.txt.zst')
        with zstd.open(path, 'wt') as f_write:
            f_write.write('foo\nbar\n')

        with augur.io.file.open_file(path) as f_read:
            with augur.io.file.open_file(f_read) as f_read:
                assert f_read.read() == 'foo\nbar\n'

    def test_open_file_nested_write(self, tmpdir):
        """Open a text file as a path then buffer for writing."""
        path = str(tmpdir / 'test.txt')
        with augur.io.file.open_file(path, 'w') as f_write:
            with augur.io.file.open_file(f_write, 'w') as f_write:
                f_write.write('foo\nbar\n')

    def test_open_file_nested_write_zstd(self, tmpdir):
        """Open a zstd-compressed text file as a path then buffer for writing."""
        path = str(tmpdir / 'test.txt.zst')
        with augur.io.file.open_file(path, 'w') as f_write:
            with augur.io.file.open_file(f_write, 'w') as f_write:
                f_write.write('foo\nbar\n')

    def test_open_file_read_error(self):
        """Try reading from an unsupported type."""
        path_or_buffer = len("bogus")

        with pytest.raises(TypeError, match="Type <class 'int'> is not supported."):
            with augur.io.file.open_file(path_or_buffer, 'r') as f:
                f.read()