test_files.py 882 Bytes
Newer Older
Matteo's avatar
Matteo committed
1
import tempfile
Matteo's avatar
update    
Matteo committed
2
import pytest
Matteo's avatar
Matteo committed
3
4
5
from mpai_cae_arp import files


Matteo's avatar
update    
Matteo committed
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
def test_open_file():
    with tempfile.NamedTemporaryFile() as tmp_file:

        tmp_file.write(b'{"test": "test"}')
        tmp_file.seek(0)

        my_file = files.File(path=tmp_file.name,
                             filetype=files.FileType.JSON).open(files.FileAction.READ)

        assert my_file.read() == '{"test": "test"}'


def test_failing_read():
    with pytest.raises(FileNotFoundError):
        files.File("test", files.FileType.JSON).open(files.FileAction.READ)


def test_get_content():
    with tempfile.NamedTemporaryFile() as tmp_file:

        tmp_file.write(b'{"test": "test"}')
        tmp_file.seek(0)

        my_file_content = files.File(path=tmp_file.name,
                                     filetype=files.FileType.JSON).get_content()

        assert my_file_content == {"test": "test"}