|
1
|
|
|
# pylint: disable=missing-docstring,unused-variable,misplaced-comparison-constant,no-self-use |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import pytest |
|
4
|
|
|
from expecter import expect |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
from yorm.utilities import attr |
|
7
|
|
|
from yorm.types.standard import Integer, String, Float |
|
8
|
|
|
from yorm.types.extended import (NullableString, Markdown, |
|
9
|
|
|
AttributeDictionary, SortedList) |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
# CLASSES ###################################################################### |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
@attr(var1=Integer, var2=String) |
|
16
|
|
|
class SampleAttributeDictionary(AttributeDictionary): |
|
17
|
|
|
|
|
18
|
|
|
"""Sample dictionary container with initialization.""" |
|
19
|
|
|
|
|
20
|
|
|
def __init__(self, var1, var2, var3): |
|
21
|
|
|
super().__init__() |
|
22
|
|
|
# pylint: disable=duplicate-code |
|
23
|
|
|
self.var1 = var1 |
|
24
|
|
|
self.var2 = var2 |
|
25
|
|
|
self.var3 = var3 |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
@attr(all=Float) |
|
29
|
|
|
class SampleSortedList(SortedList): |
|
30
|
|
|
|
|
31
|
|
|
"""Sample sorted list container.""" |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class UnknownSortedList(SortedList): |
|
35
|
|
|
|
|
36
|
|
|
"""Sample list container.""" |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
# TESTS ######################################################################## |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def describe_nullable_string(): |
|
43
|
|
|
|
|
44
|
|
|
def describe_to_value(): |
|
45
|
|
|
|
|
46
|
|
|
def it_allows_none(): |
|
47
|
|
|
expect(NullableString.to_value(None)).is_none() |
|
48
|
|
|
|
|
49
|
|
|
def describe_to_data(): |
|
50
|
|
|
|
|
51
|
|
|
def it_allows_none(): |
|
52
|
|
|
expect(NullableString.to_data(None)).is_none() |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
# TODO: make these tests look like `test_types_standard.py` |
|
56
|
|
|
class TestMarkdown: |
|
57
|
|
|
|
|
58
|
|
|
"""Unit tests for the `Markdown` converter.""" |
|
59
|
|
|
|
|
60
|
|
|
obj = "This is **the** sentence." |
|
61
|
|
|
|
|
62
|
|
|
data_value = [ |
|
63
|
|
|
(obj, obj), |
|
64
|
|
|
(None, ""), |
|
65
|
|
|
(['a', 'b', 'c'], "a, b, c"), |
|
66
|
|
|
("This is\na sentence.", "This is a sentence."), |
|
67
|
|
|
("Sentence one.\nSentence two.", "Sentence one. Sentence two."), |
|
68
|
|
|
] |
|
69
|
|
|
|
|
70
|
|
|
value_data = [ |
|
71
|
|
|
(obj, obj + '\n'), |
|
72
|
|
|
("Sentence one. Sentence two.", "Sentence one.\nSentence two.\n"), |
|
73
|
|
|
("", ""), |
|
74
|
|
|
(" \t ", ""), |
|
75
|
|
|
] |
|
76
|
|
|
|
|
77
|
|
|
@pytest.mark.parametrize("data,value", data_value) |
|
78
|
|
|
def test_to_value(self, data, value): |
|
79
|
|
|
"""Verify input data is converted to values.""" |
|
80
|
|
|
assert value == Markdown.to_value(data) |
|
81
|
|
|
|
|
82
|
|
|
@pytest.mark.parametrize("value,data", value_data) |
|
83
|
|
|
def test_to_data(self, value, data): |
|
84
|
|
|
"""Verify values are converted to output data.""" |
|
85
|
|
|
assert data == Markdown.to_data(value) |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
class TestAttributeDictionary: |
|
89
|
|
|
|
|
90
|
|
|
"""Unit tests for the `AttributeDictionary` container.""" |
|
91
|
|
|
|
|
92
|
|
|
def test_not_implemented(self): |
|
93
|
|
|
"""Verify `AttributeDictionary` cannot be used directly.""" |
|
94
|
|
|
with pytest.raises(NotImplementedError): |
|
95
|
|
|
AttributeDictionary.to_value(None) |
|
96
|
|
|
with pytest.raises(NotImplementedError): |
|
97
|
|
|
AttributeDictionary.to_data(None) |
|
98
|
|
|
|
|
99
|
|
|
def test_attribute_access(self): |
|
100
|
|
|
"""Verify `AttributeDictionary` keys are available as attributes.""" |
|
101
|
|
|
obj = SampleAttributeDictionary(1, 2, 3.0) |
|
102
|
|
|
value = {'var1': 1, 'var2': '2'} |
|
103
|
|
|
value2 = obj.to_value(obj) |
|
104
|
|
|
assert value == value2 |
|
105
|
|
|
assert 1 == value2.var1 |
|
106
|
|
|
assert '2' == value2.var2 |
|
107
|
|
|
assert not hasattr(value2, 'var3') # lost in conversion |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
class TestSortedList: |
|
111
|
|
|
|
|
112
|
|
|
"""Unit tests for the `SortedList` container.""" |
|
113
|
|
|
|
|
114
|
|
|
def test_not_implemented(self): |
|
115
|
|
|
"""Verify `SortedList` cannot be used directly.""" |
|
116
|
|
|
with pytest.raises(NotImplementedError): |
|
117
|
|
|
SortedList.to_value(None) |
|
118
|
|
|
with pytest.raises(NotImplementedError): |
|
119
|
|
|
SortedList.to_data(None) |
|
120
|
|
|
with pytest.raises(NotImplementedError): |
|
121
|
|
|
UnknownSortedList.to_value(None) |
|
122
|
|
|
with pytest.raises(NotImplementedError): |
|
123
|
|
|
UnknownSortedList.to_data(None) |
|
124
|
|
|
|
|
125
|
|
|
def test_sorted_result(self): |
|
126
|
|
|
"""Verify `SortedList` sorts the resulting data.""" |
|
127
|
|
|
obj = SampleSortedList([4, 2, 0, 1, 3]) |
|
128
|
|
|
data = [0.0, 1.0, 2.0, 3.0, 4.0] |
|
129
|
|
|
data2 = obj.to_data(obj) |
|
130
|
|
|
assert data == data2 |
|
131
|
|
|
|