Debian Codegraph @

tests/test_core.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010, 2011, 2012 Sebastian Wiesner <[email protected]>
3
4 # This library is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU Lesser General Public License as published by the
6 # Free Software Foundation; either version 2.1 of the License, or (at your
7 # option) any later version.
8
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 # for more details.
13
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18 # isort: STDLIB
19 import random
20 import syslog
21
22 # isort: FIRSTPARTY
23 from tests._constants import _UDEV_TEST
24 from tests.utils import is_unicode_string
25
26 # isort: LOCAL
27 from pyudev import udev_version
28
29 try:
30 # isort: STDLIB
31 from unittest import mock
32 except ImportError:
33 # isort: THIRDPARTY
34 import mock
35
36
37 def test_udev_version():
38 assert isinstance(udev_version(), int)
39 # just to make sure, that udev versioning works. pyudev itself should be
40 # compatible with earlier versions of pyudev. However, 150 is currently
41 # the earliest udev release, I'm testing against (using Ubuntu 10.04)
42 assert udev_version() > 150
43
44
45 class TestContext:
46 def test_sys_path(self, context):
47 assert is_unicode_string(context.sys_path)
48 assert context.sys_path == "/sys"
49
50 def test_device_path(self, context):
51 assert is_unicode_string(context.device_path)
52 assert context.device_path == "/dev"
53
54 @_UDEV_TEST(167, "test_run_path")
55 def test_run_path(self, context):
56 assert is_unicode_string(context.run_path)
57 assert context.run_path == "/run/udev"
58
59 def test_log_priority_get(self, context):
60 assert isinstance(context.log_priority, int)
61 assert syslog.LOG_EMERG <= context.log_priority <= syslog.LOG_DEBUG
62
63 def test_log_priority_get_mock(self, context):
64 spec = lambda c: None
65 funcname = "udev_get_log_priority"
66 with mock.patch.object(context._libudev, funcname, autospec=spec) as func:
67 func.return_value = mock.sentinel.log_priority
68 assert context.log_priority is mock.sentinel.log_priority
69 func.assert_called_once_with(context)
70
71 def test_log_priority_set_mock(self, context):
72 spec = lambda c, p: None
73 funcname = "udev_set_log_priority"
74 with mock.patch.object(context._libudev, funcname, autospec=spec) as func:
75 context.log_priority = mock.sentinel.log_priority
76 func.assert_called_once_with(context, mock.sentinel.log_priority)
77
78 def test_log_priority_roundtrip(self, context):
79 # FIXME: This adds UDEV_LOG properties?!
80 old_priority = context.log_priority
81 available_levels = [
82 l
83 for l in range(syslog.LOG_EMERG, syslog.LOG_DEBUG + 1)
84 if l != old_priority
85 ]
86 new_priority = random.choice(available_levels)
87 assert new_priority != old_priority
88 try:
89 context.log_priority = new_priority
90 assert context.log_priority == new_priority
91 finally:
92 context.log_priority = old_priority