Added Application HolidayViewer
This commit is contained in:
116
tests/helpers.py
116
tests/helpers.py
@@ -2,6 +2,7 @@ import dataclasses
|
||||
import json
|
||||
import re
|
||||
from collections import OrderedDict
|
||||
from typing import Any
|
||||
|
||||
import numpy
|
||||
import pandas as pd
|
||||
@@ -43,6 +44,54 @@ class Contains:
|
||||
s: str
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class JsonViewerNode:
|
||||
is_expanded: bool | None
|
||||
key: str
|
||||
value: Any
|
||||
debug_key: Any = None
|
||||
debug_folding: Any = None
|
||||
|
||||
def find(self, path):
|
||||
"""
|
||||
Finds and returns a node in a hierarchical structure based on a dot-delimited path.
|
||||
|
||||
The method uses a recursive helper function to navigate through a tree-like
|
||||
hierarchical node structure. Each node in the structure is assumed to potentially
|
||||
have a "children" attribute, which is iterated to find matching keys in the path.
|
||||
If, at any point, a node does not have the expected structure or the key is not
|
||||
found within the children, the method will return None.
|
||||
|
||||
:param path: A dot-delimited string representing the hierarchical path to
|
||||
the desired node (e.g., "root.child.subchild").
|
||||
:return: The node in the hierarchy that matches the specified path or None
|
||||
if no such node exists.
|
||||
"""
|
||||
|
||||
def _find(node, path_parts):
|
||||
if len(path_parts) == 0:
|
||||
return node
|
||||
|
||||
element = node.value # to deal with ft element
|
||||
|
||||
if not hasattr(element, "children"):
|
||||
return None
|
||||
|
||||
to_find = path_parts[0]
|
||||
|
||||
for child in element.children:
|
||||
child_node = extract_jsonviewer_node(child)
|
||||
if child_node is not None and child_node.key == to_find:
|
||||
return _find(child_node, path_parts[1:])
|
||||
|
||||
return None
|
||||
|
||||
path_parts = path.split(".")
|
||||
return _find(self, path_parts)
|
||||
|
||||
def text_value(self):
|
||||
return str(self.value.children[0])
|
||||
|
||||
Empty = EmptyElement()
|
||||
|
||||
|
||||
@@ -424,6 +473,37 @@ def matches(actual, expected, path=""):
|
||||
return True
|
||||
|
||||
|
||||
def contains(lst, element, recursive=False):
|
||||
"""
|
||||
Check if any item in the list matches the given element pattern
|
||||
using the existing matches() function.
|
||||
|
||||
Args:
|
||||
lst: List of elements to search through
|
||||
element: Element pattern to match against
|
||||
recursive: If True, also search in children of each element
|
||||
|
||||
Returns:
|
||||
bool: True if a match is found, False otherwise
|
||||
"""
|
||||
if not lst:
|
||||
return False
|
||||
|
||||
for item in lst:
|
||||
try:
|
||||
if matches(item, element):
|
||||
return True
|
||||
except AssertionError:
|
||||
pass
|
||||
|
||||
# If recursive is True, check children too
|
||||
if recursive and hasattr(item, "children") and item.children:
|
||||
if contains(item.children, element, recursive=True):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def get_selected(return_elements):
|
||||
assert isinstance(return_elements, list), "result must be a list"
|
||||
for element in return_elements:
|
||||
@@ -616,6 +696,42 @@ def extract_popup_content(element, filter_input=True) -> OrderedDict:
|
||||
return res
|
||||
|
||||
|
||||
def extract_jsonviewer_node(element):
|
||||
# This structure of the Jsonview Node is
|
||||
# 3 children
|
||||
# 1st : Span(NotStr(name="expanded|collapse")) or None
|
||||
# 2nd : Span("key : ") or None (None is possible only for the root node)
|
||||
# 3rd : Span(value)
|
||||
|
||||
if not hasattr(element, "children") or len(element.children) != 3:
|
||||
return None
|
||||
|
||||
debug_folding = element.children[0]
|
||||
debug_key = element.children[1]
|
||||
value = element.children[2]
|
||||
|
||||
if contains([debug_folding], span_icon("expanded")):
|
||||
is_expanded = True
|
||||
elif contains([debug_folding], span_icon("collapsed")):
|
||||
is_expanded = False
|
||||
else:
|
||||
is_expanded = None
|
||||
|
||||
if debug_key is not None:
|
||||
assert hasattr(debug_key, "tag") and debug_key.tag == "span", "debug_key must be a span"
|
||||
key = debug_key.children[0].split(" : ")[0]
|
||||
else:
|
||||
key = None
|
||||
|
||||
return JsonViewerNode(
|
||||
is_expanded,
|
||||
key,
|
||||
value,
|
||||
debug_key,
|
||||
debug_folding
|
||||
)
|
||||
|
||||
|
||||
def to_array(dataframe: pd.DataFrame) -> list:
|
||||
return [[val for val in row] for _, row in dataframe.iterrows()]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user