File: test_info.py

package info (click to toggle)
python-fs 2.4.16-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,944 kB
  • sloc: python: 13,048; makefile: 226; sh: 3
file content (132 lines) | stat: -rw-r--r-- 4,581 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from __future__ import unicode_literals

import unittest
from datetime import datetime

from fs.enums import ResourceType
from fs.info import Info
from fs.permissions import Permissions
from fs.time import datetime_to_epoch

try:
    from datetime import timezone
except ImportError:
    from fs._tzcompat import timezone  # type: ignore


class TestInfo(unittest.TestCase):
    def test_empty(self):
        """Test missing info."""
        info = Info({"basic": {}, "details": {}, "access": {}, "link": {}})

        self.assertIsNone(info.name)
        self.assertIsNone(info.is_dir)
        self.assertEqual(info.type, ResourceType.unknown)
        self.assertIsNone(info.accessed)
        self.assertIsNone(info.modified)
        self.assertIsNone(info.created)
        self.assertIsNone(info.metadata_changed)
        self.assertIsNone(info.accessed)
        self.assertIsNone(info.permissions)
        self.assertIsNone(info.user)
        self.assertIsNone(info.group)
        self.assertIsNone(info.target)
        self.assertFalse(info.is_link)

    def test_access(self):
        info = Info(
            {
                "access": {
                    "uid": 10,
                    "gid": 12,
                    "user": "will",
                    "group": "devs",
                    "permissions": ["u_r"],
                }
            }
        )
        self.assertIsInstance(info.permissions, Permissions)
        self.assertEqual(info.permissions, Permissions(user="r"))
        self.assertEqual(info.user, "will")
        self.assertEqual(info.group, "devs")
        self.assertEqual(info.uid, 10)
        self.assertEqual(info.gid, 12)

    def test_link(self):
        info = Info({"link": {"target": "foo"}})
        self.assertTrue(info.is_link)
        self.assertEqual(info.target, "foo")

    def test_basic(self):
        # Check simple file
        info = Info({"basic": {"name": "bar.py", "is_dir": False}})
        self.assertEqual(info.name, "bar.py")
        self.assertIsInstance(info.is_dir, bool)
        self.assertFalse(info.is_dir)
        self.assertEqual(repr(info), "<file 'bar.py'>")
        self.assertEqual(info.suffix, ".py")

        # Check dir
        info = Info({"basic": {"name": "foo", "is_dir": True}})
        self.assertTrue(info.is_dir)
        self.assertEqual(repr(info), "<dir 'foo'>")
        self.assertEqual(info.suffix, "")

    def test_details(self):
        dates = [
            datetime(2016, 7, 5, tzinfo=timezone.utc),
            datetime(2016, 7, 6, tzinfo=timezone.utc),
            datetime(2016, 7, 7, tzinfo=timezone.utc),
            datetime(2016, 7, 8, tzinfo=timezone.utc),
        ]
        epochs = [datetime_to_epoch(d) for d in dates]

        info = Info(
            {
                "details": {
                    "accessed": epochs[0],
                    "modified": epochs[1],
                    "created": epochs[2],
                    "metadata_changed": epochs[3],
                    "type": int(ResourceType.file),
                }
            }
        )
        self.assertEqual(info.accessed, dates[0])
        self.assertEqual(info.modified, dates[1])
        self.assertEqual(info.created, dates[2])
        self.assertEqual(info.metadata_changed, dates[3])
        self.assertIsInstance(info.type, ResourceType)
        self.assertEqual(info.type, ResourceType.file)
        self.assertEqual(info.type, 2)

    def test_has_namespace(self):
        info = Info({"basic": {}, "details": {}})
        self.assertTrue(info.has_namespace("basic"))
        self.assertTrue(info.has_namespace("details"))
        self.assertFalse(info.has_namespace("access"))

    def test_copy(self):
        info = Info({"basic": {"name": "bar", "is_dir": False}})
        info_copy = info.copy()
        self.assertEqual(info.raw, info_copy.raw)

    def test_get(self):
        info = Info({"baz": {}})
        self.assertIsNone(info.get("foo", "bar"))
        self.assertIsNone(info.get("baz", "bar"))

    def test_suffix(self):
        info = Info({"basic": {"name": "foo.tar.gz"}})
        self.assertEqual(info.suffix, ".gz")
        self.assertEqual(info.suffixes, [".tar", ".gz"])
        self.assertEqual(info.stem, "foo")
        info = Info({"basic": {"name": "foo"}})
        self.assertEqual(info.suffix, "")
        self.assertEqual(info.suffixes, [])
        self.assertEqual(info.stem, "foo")

        info = Info({"basic": {"name": ".foo"}})
        self.assertEqual(info.suffix, "")
        self.assertEqual(info.suffixes, [])
        self.assertEqual(info.stem, ".foo")