| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
Tests methods belonging to Attributes class. |
| 19 |
|
| 20 |
.. moduleauthor:: mulhern <[email protected]> |
| 21 |
""" |
| 22 |
|
| 23 |
|
| 24 |
import os |
| 25 |
import stat |
| 26 |
|
| 27 |
|
| 28 |
import pytest |
| 29 |
from hypothesis import given, settings, strategies |
| 30 |
|
| 31 |
|
| 32 |
from pyudev import Devices |
| 33 |
|
| 34 |
from ..utils import is_unicode_string |
| 35 |
from ._device_tests import _CONTEXT_STRATEGY, _DEVICE_DATA, _DEVICES, _UDEV_TEST |
| 36 |
|
| 37 |
|
| 38 |
class TestAttributes: |
| 39 |
""" |
| 40 |
Test ``Attributes`` class methods. |
| 41 |
""" |
| 42 |
|
| 43 |
@given(_CONTEXT_STRATEGY, strategies.sampled_from(_DEVICE_DATA)) |
| 44 |
@settings(max_examples=5) |
| 45 |
def test_getitem(self, a_context, device_datum): |
| 46 |
""" |
| 47 |
Test that attribute value exists and is instance of bytes. |
| 48 |
""" |
| 49 |
device = Devices.from_path(a_context, device_datum.device_path) |
| 50 |
for key in device_datum.attributes.keys(): |
| 51 |
value = device.attributes.get(key) |
| 52 |
if value is not None: |
| 53 |
assert isinstance(value, bytes) |
| 54 |
|
| 55 |
@given(strategies.sampled_from(_DEVICES)) |
| 56 |
@settings(max_examples=5) |
| 57 |
def test_getitem_nonexisting(self, a_device): |
| 58 |
""" |
| 59 |
Test behavior when corresponding value is non-existant. |
| 60 |
""" |
| 61 |
not_key = "a non-existing attribute" |
| 62 |
assert a_device.attributes.get(not_key) is None |
| 63 |
with pytest.raises(KeyError): |
| 64 |
a_device.attributes.asstring(not_key) |
| 65 |
with pytest.raises(KeyError): |
| 66 |
a_device.attributes.asint(not_key) |
| 67 |
with pytest.raises(KeyError): |
| 68 |
a_device.attributes.asbool(not_key) |
| 69 |
|
| 70 |
@given(strategies.sampled_from(_DEVICES)) |
| 71 |
@settings(max_examples=5) |
| 72 |
def test_non_iterable(self, a_device): |
| 73 |
""" |
| 74 |
Test that the attributes object can not be iterated over. |
| 75 |
""" |
| 76 |
|
| 77 |
with pytest.raises(TypeError): |
| 78 |
"key" in a_device.attributes |
| 79 |
with pytest.raises(TypeError): |
| 80 |
a_device.attributes["key"] |
| 81 |
|
| 82 |
@given(_CONTEXT_STRATEGY, strategies.sampled_from(_DEVICE_DATA)) |
| 83 |
@settings(max_examples=5) |
| 84 |
def test_asstring(self, a_context, device_datum): |
| 85 |
""" |
| 86 |
Test that attribute exists for actual device and is unicode. |
| 87 |
""" |
| 88 |
device = Devices.from_path(a_context, device_datum.device_path) |
| 89 |
for key in device_datum.attributes.keys(): |
| 90 |
try: |
| 91 |
assert is_unicode_string(device.attributes.asstring(key)) |
| 92 |
except KeyError: |
| 93 |
pass |
| 94 |
|
| 95 |
@given(_CONTEXT_STRATEGY, strategies.sampled_from(_DEVICE_DATA)) |
| 96 |
@settings(max_examples=10) |
| 97 |
def test_asint(self, a_context, device_datum): |
| 98 |
""" |
| 99 |
Test that integer result is an int or ValueError raised. |
| 100 |
""" |
| 101 |
device = Devices.from_path(a_context, device_datum.device_path) |
| 102 |
for key, value in device_datum.attributes.items(): |
| 103 |
try: |
| 104 |
value = int(value) |
| 105 |
except ValueError: |
| 106 |
try: |
| 107 |
device.attributes.asint(key) |
| 108 |
except KeyError: |
| 109 |
pass |
| 110 |
except ValueError: |
| 111 |
pass |
| 112 |
|
| 113 |
@given(_CONTEXT_STRATEGY, strategies.sampled_from(_DEVICE_DATA)) |
| 114 |
@settings(max_examples=5) |
| 115 |
def test_asbool(self, a_context, device_datum): |
| 116 |
""" |
| 117 |
Test that bool result is a bool or ValueError raised. |
| 118 |
""" |
| 119 |
device = Devices.from_path(a_context, device_datum.device_path) |
| 120 |
for key, value in device_datum.attributes.items(): |
| 121 |
if value in ("0", "1"): |
| 122 |
try: |
| 123 |
assert device.attributes.asbool(key) in (False, True) |
| 124 |
except KeyError: |
| 125 |
pass |
| 126 |
else: |
| 127 |
try: |
| 128 |
device.attributes.asbool(key) |
| 129 |
except KeyError: |
| 130 |
pass |
| 131 |
except ValueError: |
| 132 |
pass |
| 133 |
|
| 134 |
@given(_CONTEXT_STRATEGY, strategies.sampled_from(_DEVICE_DATA)) |
| 135 |
@settings(max_examples=5) |
| 136 |
def test_unsetitem(self, a_context, device_datum): |
| 137 |
""" |
| 138 |
Test that attribute value can be unset. |
| 139 |
""" |
| 140 |
device = Devices.from_path(a_context, device_datum.device_path) |
| 141 |
for key in device_datum.attributes.keys(): |
| 142 |
device.attributes.unset(key) |