Terarea  2
The automation project
Loading...
Searching...
No Matches
test_actions_variables.py
Go to the documentation of this file.
1"""_summary_
2 File in charge of testing the variables class
3"""
4import os
5import sys
6import base64
7
8import pytest
9
10sys.path.append(os.path.join("..", os.getcwd()))
11sys.path.append(os.getcwd())
12
13try:
14 import constants as TCONST
15except ImportError as e:
16 raise ImportError("Failed to import the unit test constants module") from e
17
18try:
19 from src.lib.actions.variables import Variables, ScopeError, VariableNotFoundError
20except ImportError as e:
21 raise ImportError("Failed to import the src module") from e
22
23
24ERROR = TCONST.ERROR
25SUCCESS = TCONST.SUCCESS
26DEBUG = TCONST.DEBUG
27
28SCOPE = "test_scope"
29
31 success=SUCCESS,
32 error=ERROR,
33 debug=DEBUG
34)
35
36
37def test_create_scope() -> None:
38 """_summary_
39 Function in charge of testing the one that will create scopes in the variable class.
40 """
41 VI.variables = {}
42 assert VI.create_scope(SCOPE) == SUCCESS
43 assert SCOPE in VI.variables
44 assert VI.create_scope(SCOPE) == ERROR
45
46
47def test_add_variable() -> None:
48 """_summary_
49 Function in charge of testing the add_variable function.
50 """
51 VI.variables = {}
52 assert VI.add_variable("test1", 1, int, scope=SCOPE) == SUCCESS
53 with pytest.raises(TypeError):
54 VI.add_variable("test2", 1, str, scope=SCOPE)
55 assert VI.add_variable("test3", "1", str, scope=SCOPE) == SUCCESS
56 assert SCOPE in VI.variables
57 assert VI.variables[SCOPE]["test1"] == {"data": 1, "type": int}
58 assert "test2" not in VI.variables[SCOPE]
59 assert VI.variables[SCOPE]["test3"] == {"data": "1", "type": str}
60
61
63 """_summary_
64 Function in charge of testing the update_variable function.
65 """
66 node1 = {"data": 1, "type": int}
67 node2 = {"data": "1", "type": str}
68 node3 = {"data": "1", "type": str}
69 VI.variables = {
70 SCOPE: {
71 "test1": node1,
72 "test2": node2,
73 "test3": node3
74 }
75 }
76 with pytest.raises(ScopeError):
77 VI.update_variable("not_present", 1, int)
78 with pytest.raises(VariableNotFoundError):
79 VI.update_variable("not_present", 1, int, scope=SCOPE)
80 with pytest.raises(ScopeError):
81 VI.update_variable("test1", 1, int)
82 with pytest.raises(ScopeError):
83 VI.update_variable("test2", 1, str)
84 with pytest.raises(ScopeError):
85 VI.update_variable("test3", "1", str)
86 assert VI.update_variable("test1", 1, int, scope=SCOPE) == SUCCESS
87 with pytest.raises(TypeError):
88 VI.update_variable("test2", 1, str, scope=SCOPE)
89 assert VI.update_variable("test3", "1", str, scope=SCOPE) == SUCCESS
90 assert SCOPE in VI.variables
91 assert "not_present" not in VI.variables[SCOPE]
92 assert VI.variables[SCOPE]["test1"] == node1
93 assert VI.variables[SCOPE]["test2"] == node2
94 assert VI.variables[SCOPE]["test3"] == node3
95
96
98 """_summary_
99 Function in charge of testing the insert_or_update function.
100 """
101 VI.variables = {
102 SCOPE: {
103 "test4": {"data": "1", "type": str},
104 "test5": {"data": "1", "type": str}
105 }
106 }
107 assert VI.insert_or_update("test1", 1, int) == SUCCESS
108 with pytest.raises(TypeError):
109 VI.insert_or_update("test2", 1, str)
110 assert VI.insert_or_update("test3", "1", str) == SUCCESS
111 assert VI.insert_or_update("test4", "1", str) == SUCCESS
112 assert VI.insert_or_update("test5", 1.0, float) == SUCCESS
113 with pytest.raises(TypeError):
114 VI.insert_or_update("test5", "1", int)
115 assert VI.insert_or_update("test1", 1, int, scope=SCOPE) == SUCCESS
116 with pytest.raises(TypeError):
117 VI.insert_or_update("test2", 1, str, scope=SCOPE)
118 assert VI.insert_or_update("test3", "1", str, scope=SCOPE) == SUCCESS
119 assert VI.insert_or_update("test4", "1", str, scope=SCOPE) == SUCCESS
120 with pytest.raises(TypeError):
121 VI.insert_or_update("test5", "1", int, scope=SCOPE)
122 assert SCOPE in VI.variables
123 assert VI.variables[SCOPE]["test1"] == {"data": 1, "type": int}
124 assert "test2" not in VI.variables[SCOPE]
125 assert VI.variables[SCOPE]["test3"] == {"data": "1", "type": str}
126 assert VI.variables[SCOPE]["test4"] == {"data": "1", "type": str}
127 assert VI.variables[SCOPE]["test5"] == {"data": "1", "type": str}
128 assert "default_scope" in VI.variables
129 assert VI.variables["default_scope"]["test1"] == {"data": 1, "type": int}
130 assert "test2" not in VI.variables["default_scope"]
131 assert VI.variables["default_scope"]["test3"] == {"data": "1", "type": str}
132 assert VI.variables["default_scope"]["test4"] == {"data": "1", "type": str}
133 assert VI.variables["default_scope"]["test5"] == {
134 "data": 1.0, "type": float
135 }
136
137
138def test_has_variable() -> None:
139 """_summary_
140 Function in charge of testing the has_variable function.
141 """
142 VI.variables = {
143 SCOPE: {
144 "test": {"data": "1", "type": str}
145 },
146 "default_scope": {
147 "test": {"data": "1", "type": str}
148 }
149 }
150 assert VI.has_variable("test") is True
151 assert VI.has_variable("test2") is False
152 assert VI.has_variable("test", scope=SCOPE) is True
153 assert VI.has_variable("test2", scope=SCOPE) is False
154 with pytest.raises(ScopeError):
155 VI.has_variable("test", scope="not_a_scope")
156 with pytest.raises(ScopeError):
157 VI.has_variable("test2", scope="not_a_scope")
158
159
160def test_get_variable() -> None:
161 """_summary_
162 Function in charge of testing the get_variable function.
163 """
164 VI.variables = {
165 SCOPE: {
166 "test": {"data": "1", "type": str}
167 }
168 }
169 assert VI.get_variable("test", scope=SCOPE) == "1"
170 with pytest.raises(ScopeError):
171 VI.get_variable("test")
172 with pytest.raises(ScopeError):
173 VI.get_variable("not_a_variable")
174 with pytest.raises(ValueError):
175 VI.get_variable("not_a_variable", scope=SCOPE)
176
177
178def test_get_variables() -> None:
179 """_summary_
180 Function in charge of testing the get_variables function.
181 """
182 node = {
183 "test": {"data": "1", "type": str},
184 "test2": {"data": 1, "type": int},
185 "test3": {"data": 1.0, "type": float}
186 }
187 VI.variables[SCOPE] = node.copy()
188 assert VI.get_variables(scope=SCOPE) == node
189 assert VI.get_variables("*") == {SCOPE: node}
190 with pytest.raises(ScopeError):
191 VI.get_variables()
192
193
195 """_summary_
196 Function in charge of testing the get_variable_type function.
197 """
198 VI.variables = {
199 SCOPE: {
200 "test": {"data": "1", "type": str}
201 }
202 }
203 with pytest.raises(ScopeError):
204 VI.get_variable_type("test")
205 with pytest.raises(ScopeError):
206 VI.get_variable_type("test2")
207 assert VI.get_variable_type("test", scope=SCOPE) == str
208 with pytest.raises(ValueError):
209 VI.get_variable_type("test2", scope=SCOPE)
210
211
213 """_summary_
214 Function in charge of testing the remove_variable function.
215 """
216 VI.variables = {
217 SCOPE: {
218 "test": {"data": "1", "type": str}
219 }
220 }
221 assert SCOPE in VI.variables
222 with pytest.raises(ScopeError):
223 VI.remove_variable("test")
224 with pytest.raises(ScopeError):
225 VI.remove_variable("test")
226 with pytest.raises(ScopeError):
227 VI.remove_variable("test2")
228 assert VI.remove_variable("test", scope=SCOPE) == SUCCESS
229 with pytest.raises(VariableNotFoundError):
230 VI.remove_variable("test", scope=SCOPE)
231 with pytest.raises(VariableNotFoundError):
232 VI.remove_variable("test2", scope=SCOPE)
233 assert not VI.variables[SCOPE]
234
235
237 """_summary_
238 Function in charge of testing the remove_variable function with different types.
239 """
240 VI.variables = {
241 SCOPE: {
242 "test": {"data": "1", "type": str},
243 "test2": {"data": 1, "type": int},
244 "test3": {"data": 1.0, "type": float}
245 }
246 }
247 assert SCOPE in VI.variables
248 assert VI.remove_variable("test2", scope=SCOPE) == SUCCESS
249 assert VI.remove_variable("test", scope=SCOPE) == SUCCESS
250 with pytest.raises(VariableNotFoundError):
251 VI.remove_variable("test4", scope=SCOPE)
252 assert "test3" in VI.variables[SCOPE]
253
254
256 """_summary_
257 Function in charge of testing the clear_variables function with the "default_scope" variable.
258 """
259 VI.variables = {
260 SCOPE: {
261 "test": {"data": "1", "type": str},
262 "test2": {"data": 1, "type": int},
263 "test3": {"data": 1.0, "type": float}
264 },
265 "default_scope": {
266 "test": {"data": "1", "type": str},
267 "test2": {"data": 1, "type": int},
268 "test3": {"data": 1.0, "type": float}
269 }
270 }
271 assert VI.clear_variables() == SUCCESS
272 with pytest.raises(ScopeError):
273 VI.clear_variables(scope="not_a_scope")
274 assert "default_scope" in VI.variables
275 assert SCOPE in VI.variables
276 assert not VI.variables["default_scope"]
277 assert len(VI.variables[SCOPE]) > 0
278
279
281 """_summary_
282 Function in charge of testing the clear_variables function with the SCOPE variable.
283 """
284 VI.variables = {
285 SCOPE: {
286 "test": {"data": "1", "type": str},
287 "test2": {"data": 1, "type": int},
288 "test3": {"data": 1.0, "type": float}
289 },
290 "default_scope": {
291 "test": {"data": "1", "type": str},
292 "test2": {"data": 1, "type": int},
293 "test3": {"data": 1.0, "type": float}
294 }
295 }
296 assert VI.clear_variables(scope=SCOPE) == SUCCESS
297 with pytest.raises(ScopeError):
298 VI.clear_variables(scope="not_a_scope")
299 assert SCOPE in VI.variables
300 assert "default_scope" in VI.variables
301 assert not VI.variables[SCOPE]
302 assert len(VI.variables["default_scope"]) > 0
303
304
306 """_summary_
307 Function in charge of testing the clear_variables function with the * argument.
308 """
309 VI.variables = {
310 SCOPE: {
311 "test": {"data": "1", "type": str},
312 "test2": {"data": 1, "type": int},
313 "test3": {"data": 1.0, "type": float}
314 },
315 "default_scope": {
316 "test": {"data": "1", "type": str},
317 "test2": {"data": 1, "type": int},
318 "test3": {"data": 1.0, "type": float}
319 }
320 }
321 assert VI.clear_variables(scope="*") == SUCCESS
322 assert "default_scope" in VI.variables
323 assert SCOPE in VI.variables
324 assert not VI.variables[SCOPE]
325 assert not VI.variables["default_scope"]
326
327
328def test_clear_scopes() -> None:
329 """_summary_
330 Function in charge of testing the clear_scopes function.
331 """
332 VI.variables = {
333 SCOPE: {
334 "test": {"data": "1", "type": str},
335 "test2": {"data": 1, "type": int},
336 "test3": {"data": 1.0, "type": float}
337 },
338 "default_scope": {
339 "test": {"data": "1", "type": str},
340 "test2": {"data": 1, "type": int},
341 "test3": {"data": 1.0, "type": float}
342 }
343 }
344 assert VI.clear_scopes() == SUCCESS
345 assert not VI.variables
346 assert "default_scope" not in VI.variables
347 assert SCOPE not in VI.variables
348
349
351 """_summary_
352 Function in charge of testing the clear_scope_contents function.
353 """
354 VI.variables = {
355 SCOPE: {
356 "test": {"data": "1", "type": str},
357 "test2": {"data": 1, "type": int},
358 "test3": {"data": 1.0, "type": float}
359 },
360 "default_scope": {
361 "test": {"data": "1", "type": str},
362 "test2": {"data": 1, "type": int},
363 "test3": {"data": 1.0, "type": float}
364 }
365 }
366 assert VI.clear_scope_contents() == SUCCESS
367 assert "default_scope" in VI.variables
368 assert SCOPE in VI.variables
369 assert not VI.variables[SCOPE]
370 assert not VI.variables["default_scope"]
371
372
373def test_remove_scope() -> None:
374 """_summary_
375 Function in charge of testing the remove_scope function.
376 """
377 VI.variables = {
378 SCOPE: {
379 "test": {"data": "1", "type": str},
380 "test2": {"data": 1, "type": int},
381 "test3": {"data": 1.0, "type": float}
382 },
383 "default_scope": {
384 "test": {"data": "1", "type": str},
385 "test2": {"data": 1, "type": int},
386 "test3": {"data": 1.0, "type": float}
387 }
388 }
389 assert VI.remove_scope(SCOPE) == SUCCESS
390 assert "default_scope" in VI.variables
391 assert SCOPE not in VI.variables
392 assert VI.clear_variables("default_scope") == SUCCESS
393 assert not VI.variables["default_scope"]
394 assert VI.remove_scope("default_scope") == SUCCESS
395 assert not VI.variables
396 assert "default_scope" not in VI.variables
397 assert SCOPE not in VI.variables
398 with pytest.raises(ScopeError):
399 VI.remove_scope("default_scope")
400
401
403 """_summary_
404 Function in charge of testing the sanitize_for_json function.
405 """
406 def dummy() -> str:
407 return "dummy"
408
409 class SampleEmptyClass:
410 pass
411
412 class SampleFuncClass:
413 def __init__(self):
414 pass
415
416 class SampleMultyFuncClass:
417 def __init__(self):
418 pass
419
420 def dummy(self) -> str:
421 return "dummy"
422
423 scope_list = [
424 SCOPE,
425 "default_scope",
426 "not_a_scope",
427 "functions",
428 "classes",
429 "classes_initialised",
430 "file_handles",
431 "complex_numbers",
432 "example_set",
433 "example_bytes",
434 "example_generator"
435 ]
436
437 scopes = {
438 scope_list[0]: {
439 "test": {"data": "1", "type": str},
440 "test2": {"data": 1, "type": int},
441 "test3": {"data": 1.0, "type": float}
442 },
443 scope_list[1]: {
444 "test": {"data": "1", "type": str},
445 "test2": {"data": 1, "type": int},
446 "test3": {"data": 1.0, "type": float}
447 },
448 scope_list[2]: {
449 "test": {"data": "1", "type": str},
450 "test2": {"data": 1, "type": int},
451 "test3": {"data": 1.0, "type": float}
452 },
453 scope_list[3]: {
454 "test": lambda x: x,
455 "test2": lambda x: x,
456 "test3": lambda x: x,
457 "test4": dummy
458 },
459 scope_list[4]: {
460 "test": SampleEmptyClass,
461 "test2": SampleFuncClass,
462 "test3": SampleMultyFuncClass
463 },
464 scope_list[5]: {
465 "test": SampleEmptyClass(),
466 "test2": SampleFuncClass(),
467 "test3": SampleMultyFuncClass()
468 },
469 scope_list[6]: {
470 "test": open("test.tmp.txt", "w", encoding='utf-8'),
471 "test2": open("test2.tmp.txt", "w", encoding='utf-8'),
472 "test3": open("test3.tmp.txt", "w", encoding='utf-8')
473 },
474 scope_list[7]: {
475 "test": complex(1, 1),
476 "test2": complex(1, 1),
477 "test3": complex(1, 1)
478 },
479 scope_list[8]: {
480 "test": {1, 2, 3},
481 "test2": {1, 2, 3},
482 "test3": {1, 2, 3}
483 },
484 scope_list[9]: {
485 "test": b"test",
486 "test2": b"test",
487 "test3": b"test"
488 },
489 scope_list[10]: {
490 "test": (x for x in range(10)),
491 "test2": (x for x in range(10)),
492 "test3": (x for x in range(10))
493 }
494 }
495 VI.variables = scopes.copy()
496 scopes_cleaned = scopes.copy()
497 node = scope_list[1]
498 for i in scopes_cleaned[node]:
499 for b in scopes_cleaned[node][i]:
500 if b != "data":
501 scopes_cleaned[node][i][b] = str(
502 scopes_cleaned[node][i][b])
503 node = scope_list[2]
504 for i in scopes_cleaned[node]:
505 for b in scopes_cleaned[node][i]:
506 if b != "data":
507 scopes_cleaned[node][i][b] = str(
508 scopes_cleaned[node][i][b]
509 )
510 node = scope_list[3]
511 for i in scopes_cleaned[node]:
512 scopes_cleaned[node][i] = str(
513 scopes_cleaned[node][i]
514 )
515 node = scope_list[4]
516 for i in scopes_cleaned[node]:
517 scopes_cleaned[node][i] = str(
518 scopes_cleaned[node][i]
519 )
520 node = scope_list[5]
521 for i in scopes_cleaned[node]:
522 scopes_cleaned[node][i] = str(
523 scopes_cleaned[node][i]
524 )
525 node = scope_list[6]
526 for i in scopes_cleaned[node]:
527 scopes_cleaned[node][i] = str(
528 scopes_cleaned[node][i]
529 )
530 node = scope_list[7]
531 for i in scopes_cleaned[node]:
532 scopes_cleaned[node][i] = str(
533 scopes_cleaned[node][i]
534 )
535 node = scope_list[8]
536 for i in scopes_cleaned[node]:
537 scopes_cleaned[node][i] = list(
538 scopes_cleaned[node][i]
539 )
540 node = scope_list[9]
541 for i in scopes_cleaned[node]:
542 scopes_cleaned[node][i] = base64.b64encode(
543 scopes_cleaned[node][i]
544 ).decode("utf-8")
545 node = scope_list[10]
546 for i in scopes_cleaned[node]:
547 scopes_cleaned[node][i] = str(
548 scopes_cleaned[node][i]
549 )
550
551 assert VI.sanitize_for_json(scopes) == scopes_cleaned
552 with pytest.raises(ScopeError):
553 VI.sanitize_for_json(data_or_scope="ThisIsNotAScope", use_scope=True)
554 for i in scope_list:
555 assert VI.sanitize_for_json(
556 data_or_scope=i, use_scope=True
557 ) == scopes_cleaned[i]