File: test_from_file.py

package info (click to toggle)
python-box 7.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 572 kB
  • sloc: python: 3,471; makefile: 4
file content (49 lines) | stat: -rw-r--r-- 2,547 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pathlib import Path
from test.common import test_root

import pytest

from box import Box, BoxError, BoxList, box_from_file, box_from_string


class TestFromFile:
    def test_from_all(self):
        assert isinstance(box_from_file(Path(test_root, "data", "json_file.json")), Box)
        assert isinstance(box_from_file(Path(test_root, "data", "toml_file.tml")), Box)
        assert isinstance(box_from_file(Path(test_root, "data", "yaml_file.yaml")), Box)
        assert isinstance(box_from_file(Path(test_root, "data", "json_file.json"), file_type="json"), Box)
        assert isinstance(box_from_file(Path(test_root, "data", "toml_file.tml"), file_type="toml"), Box)
        assert isinstance(box_from_file(Path(test_root, "data", "yaml_file.yaml"), file_type="yaml"), Box)
        assert isinstance(box_from_file(Path(test_root, "data", "json_list.json")), BoxList)
        assert isinstance(box_from_file(Path(test_root, "data", "yaml_list.yaml")), BoxList)
        assert isinstance(box_from_file(Path(test_root, "data", "msgpack_file.msgpack")), Box)
        assert isinstance(box_from_file(Path(test_root, "data", "msgpack_list.msgpack")), BoxList)
        assert isinstance(box_from_file(Path(test_root, "data", "csv_file.csv")), BoxList)

    def test_bad_file(self):
        with pytest.raises(BoxError):
            box_from_file(Path(test_root, "data", "bad_file.txt"), file_type="json")
        with pytest.raises(BoxError):
            box_from_file(Path(test_root, "data", "bad_file.txt"), file_type="toml")
        with pytest.raises(BoxError):
            box_from_file(Path(test_root, "data", "bad_file.txt"), file_type="yaml")
        with pytest.raises(BoxError):
            box_from_file(Path(test_root, "data", "bad_file.txt"), file_type="msgpack")
        with pytest.raises(BoxError):
            box_from_file(Path(test_root, "data", "bad_file.txt"), file_type="unknown")
        with pytest.raises(BoxError):
            box_from_file(Path(test_root, "data", "bad_file.txt"))
        with pytest.raises(BoxError):
            box_from_file("does not exist")

    def test_from_string_all(self):
        with open(Path(test_root, "data", "json_file.json"), "r") as f:
            box_from_string(f.read())

        with open(Path(test_root, "data", "toml_file.tml"), "r") as f:
            box_from_string(f.read(), string_type="toml")

        with open(Path(test_root, "data", "yaml_file.yaml"), "r") as f:
            box_from_string(f.read(), string_type="yaml")