Debian Codegraph @

test/test_autoparse/test_smart_open.py

raw
1 from autocommand.autoparse import smart_open
2
3
4 def test_smart_open_is_exported():
5 import autocommand
6
7 assert autocommand.smart_open is smart_open
8
9
10 def test_smart_open_path_read(tmpdir):
11 target = tmpdir.join('file.txt')
12 target.write("Hello")
13
14 with smart_open(str(target)) as file:
15 assert not file.closed
16 assert file.read() == "Hello"
17
18 assert file.closed
19
20
21 def test_smart_open_path_write(tmpdir):
22 target = tmpdir.join('file.txt').ensure(file=True)
23
24 with smart_open(str(target), 'w') as file:
25 assert not file.closed
26 file.write("Test Content") # Tests that the file is writable
27
28 assert file.closed
29 assert target.read() == "Test Content"
30
31
32 def test_smart_open_path_create(tmpdir):
33 target = tmpdir.join("file.txt")
34
35 with smart_open(str(target), 'w') as file:
36 file.write("Test Content")
37
38 assert target.read() == "Test Content"
39
40
41 def test_smart_open_file(tmpdir):
42 path = str(tmpdir.join('file.txt').ensure(file=True))
43
44 with open(path) as file:
45 with smart_open(file) as smart_file:
46 assert file is smart_file
47 assert not smart_file.closed
48 assert not smart_file.closed