1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: UTF-8 -*- |
3
|
|
|
|
4
|
|
|
# Isomer - The distributed application framework |
5
|
|
|
# ============================================== |
6
|
|
|
# Copyright (C) 2011-2020 Heiko 'riot' Weinen <[email protected]> and others. |
7
|
|
|
# |
8
|
|
|
# This program is free software: you can redistribute it and/or modify |
9
|
|
|
# it under the terms of the GNU Affero General Public License as published by |
10
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
# (at your option) any later version. |
12
|
|
|
# |
13
|
|
|
# This program is distributed in the hope that it will be useful, |
14
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
# GNU Affero General Public License for more details. |
17
|
|
|
# |
18
|
|
|
# You should have received a copy of the GNU Affero General Public License |
19
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
|
21
|
|
|
""" |
22
|
|
|
Isomer - Backend |
23
|
|
|
|
24
|
|
|
Test Isomer Tools |
25
|
|
|
=============== |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
""" |
30
|
|
|
|
31
|
|
|
# import os |
32
|
|
|
import re |
33
|
|
|
|
34
|
|
|
import pytest |
35
|
|
|
import dateutil.parser |
36
|
|
|
from tempfile import NamedTemporaryFile |
37
|
|
|
from copy import copy |
38
|
|
|
# from datetime import datetime |
39
|
|
|
|
40
|
|
|
import isomer.tool.templates |
41
|
|
|
from isomer.misc.std import std_table, std_uuid, std_now, std_color, std_hash, \ |
42
|
|
|
std_salt, std_datetime, std_human_uid |
43
|
|
|
from isomer.misc import path, nested_map_find, nested_map_update |
44
|
|
|
from collections import namedtuple |
45
|
|
|
|
46
|
|
|
template = """Hello {{placeholder}}!""" |
47
|
|
|
|
48
|
|
|
content = { |
49
|
|
|
'placeholder': 'Isomer dev' |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
nested_map = { |
53
|
|
|
"app": { |
54
|
|
|
"Garden": { |
55
|
|
|
"Flowers": { |
56
|
|
|
"Red flower": "Rose", |
57
|
|
|
"White Flower": "Jasmine", |
58
|
|
|
"Yellow Flower": "Marigold" |
59
|
|
|
} |
60
|
|
|
}, |
61
|
|
|
"Fruits": { |
62
|
|
|
"Yellow fruit": "Mango", |
63
|
|
|
"Green fruit": "Guava", |
64
|
|
|
"White Flower": "groovy" |
65
|
|
|
}, |
66
|
|
|
"Trees": { |
67
|
|
|
"label": { |
68
|
|
|
"Yellow fruit": "Pumpkin", |
69
|
|
|
"White Flower": "Bogan" |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
key = 'app.Garden.Flowers.White Flower'.split('.') |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
def test_uuid(): |
79
|
|
|
uuid = std_uuid() |
80
|
|
|
|
81
|
|
|
assert isinstance(uuid, str) |
82
|
|
|
assert re.match(r'(\w{8}(-\w{4}){3}-\w{12}?)', uuid) |
83
|
|
|
|
84
|
|
|
def test_std_salt(): |
85
|
|
|
test_salt = std_salt() |
86
|
|
|
# b'$2b$12$CFbqPr4m0sE5OTvVVxTyWO' |
87
|
|
|
|
88
|
|
|
assert isinstance(test_salt, bytes) is True |
89
|
|
|
assert test_salt[0:7].decode('ascii') == '$2b$12$' |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def test_std_now(): |
93
|
|
|
now = std_now() |
94
|
|
|
|
95
|
|
|
assert isinstance(now, str) |
96
|
|
|
|
97
|
|
|
try: |
98
|
|
|
result = dateutil.parser.parse(now) |
99
|
|
|
except ValueError: |
100
|
|
|
pytest.fail('std_now produces non parsable datetime strings') |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
def test_std_table(): |
104
|
|
|
Row = namedtuple("Row", ['A', 'B']) |
105
|
|
|
rows = [ |
106
|
|
|
Row("1", "2") |
107
|
|
|
] |
108
|
|
|
|
109
|
|
|
table = std_table(rows) |
110
|
|
|
|
111
|
|
|
rows.append(Row("345", "6")) |
112
|
|
|
table = std_table(rows) |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
def test_format_template(): |
116
|
|
|
result = isomer.tool.templates.format_template(template, content) |
117
|
|
|
|
118
|
|
|
assert result == 'Hello Isomer dev!' |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
def test_format_template_file(): |
122
|
|
|
with NamedTemporaryFile(prefix='isomer-test', |
123
|
|
|
suffix='tpl', |
124
|
|
|
delete=True) as f: |
125
|
|
|
f.write(template.encode('utf-8')) |
126
|
|
|
f.flush() |
127
|
|
|
result = isomer.tool.templates.format_template_file(f.name, content) |
128
|
|
|
|
129
|
|
|
assert result == 'Hello Isomer dev!' |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
def test_write_template_file(): |
133
|
|
|
with NamedTemporaryFile(prefix='isomer-test', |
134
|
|
|
suffix='tpl', |
135
|
|
|
delete=True) as f: |
136
|
|
|
f.write(template.encode('utf-8')) |
137
|
|
|
f.flush() |
138
|
|
|
|
139
|
|
|
target = f.name + '_filled' |
140
|
|
|
isomer.tool.templates.write_template_file(f.name, target, content) |
141
|
|
|
|
142
|
|
|
with open(target, 'r') as tf: |
143
|
|
|
result = tf.readline() |
144
|
|
|
|
145
|
|
|
assert result == 'Hello Isomer dev!' |
146
|
|
|
|
147
|
|
|
print(target) |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
def test_path_tools(): |
151
|
|
|
path.set_instance('TESTING', 'MAUVE', '/tmp/isomer-test') |
152
|
|
|
|
153
|
|
|
assert path.INSTANCE == 'TESTING' |
154
|
|
|
assert path.ENVIRONMENT == 'MAUVE' |
155
|
|
|
|
156
|
|
|
assert 'cache' in path.locations |
157
|
|
|
assert 'local' in path.locations |
158
|
|
|
assert 'lib' in path.locations |
159
|
|
|
|
160
|
|
|
assert path.get_path('cache', |
161
|
|
|
'') == '/tmp/isomer-test/var/cache/isomer/TESTING/MAUVE' |
162
|
|
|
assert path.get_path('local', |
163
|
|
|
'foo') == '/tmp/isomer-test/var/local/isomer/TESTING/MAUVE/foo' |
164
|
|
|
assert path.get_path('lib', |
165
|
|
|
'bar/qux') == '/tmp/isomer-test/var/lib/isomer/TESTING/MAUVE/bar/qux' |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
def test_nested_map_find(): |
169
|
|
|
"""Tests if nested dictionaries can be traversed""" |
170
|
|
|
|
171
|
|
|
assert nested_map_find(nested_map, key) == 'Jasmine' |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
def test_nested_map_update(): |
175
|
|
|
"""Tests if nested dictionaries can be updated""" |
176
|
|
|
|
177
|
|
|
assert nested_map_find( |
178
|
|
|
nested_map_update(nested_map, 'Tulip', key), |
179
|
|
|
key) == 'Tulip' |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
def test_nested_map_delete(): |
183
|
|
|
"""Tests if nested dictionaries items can be deleted""" |
184
|
|
|
|
185
|
|
|
deletable_map = copy(nested_map) |
186
|
|
|
|
187
|
|
|
with pytest.raises(KeyError): |
188
|
|
|
nested_map_find(nested_map_update(deletable_map, None, key), key) |
189
|
|
|
|