File: test_File.py

package info (click to toggle)
python-biopython 1.64%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 44,416 kB
  • ctags: 12,472
  • sloc: python: 153,759; xml: 67,286; ansic: 9,003; sql: 1,488; makefile: 144; sh: 59
file content (94 lines) | stat: -rw-r--r-- 2,590 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
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
# Copyright 1999 by Jeffrey Chang.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

from __future__ import print_function

import os.path
import unittest
import shutil
from Bio._py3k import StringIO
import tempfile

from Bio import File


data = """This
is
a multi-line
file"""


### UndoHandle

h = File.UndoHandle(StringIO(data))

print(h.readline())   # 'This'
print(h.peekline())   # 'is'
print(h.readline())   # 'is'
h.saveline("saved")
print(h.peekline())   # 'saved'
h.saveline("another")
print(h.readline())   # 'another'
print(h.readline())   # 'saved'

# Test readlines after saveline
h.saveline("saved again")
lines = h.readlines()
print(repr(lines[0]))   # 'saved again'
print(repr(lines[1]))   # 'a multi-line'
print(repr(lines[2]))   # 'file'

# should be empty now
print(repr(h.readline()))       # ''

h.saveline("save after empty")
print(h.readline())             # 'save after empty'
print(repr(h.readline()))       # ''

# test read method
h = File.UndoHandle(StringIO("some text"))
h.saveline("more text")
print(h.read())                 # 'more textsome text'


class AsHandleTestCase(unittest.TestCase):

    def setUp(self):
        # Create a directory to work in
        self.temp_dir = tempfile.mkdtemp(prefix='biopython-test')

    def tearDown(self):
        shutil.rmtree(self.temp_dir)

    def _path(self, *args):
        return os.path.join(self.temp_dir, *args)

    def test_handle(self):
        "Test as_handle with a file-like object argument"
        p = self._path('test_file.fasta')
        with open(p, 'wb') as fp:
            with File.as_handle(fp) as handle:
                self.assertEqual(fp, handle, "as_handle should "
                        "return argument when given a file-like object")
                self.assertFalse(handle.closed)

            self.assertFalse(handle.closed,
                    "Exiting as_handle given a file-like object should not "
                    "close the file")

    def test_path(self):
        "Test as_handle with a path argument"
        p = self._path('test_file.fasta')
        mode = 'wb'
        with File.as_handle(p, mode=mode) as handle:
            self.assertEqual(p, handle.name)
            self.assertEqual(mode, handle.mode)
            self.assertFalse(handle.closed)
        self.assertTrue(handle.closed)

    def test_stringio(self):
        s = StringIO()
        with File.as_handle(s) as handle:
            self.assertEqual(s, handle)