Skip to content

Commit f8be860

Browse files
casenavefabiencasenavexroynard
authored
🚀 feat(tests) add time_series named 'a/b' in sample tests (#91)
* feat(tests) add scalar named 'a/b' in sample tests * add test for time_series with name containing / * (.gitignore) add venv/ dir * (sample.py) add check that names does not contain / * (sample.py) fix check_names * (sample.py) fix check_names for str argument * (sample.py) fix check_names tests --------- Co-authored-by: Fabien Casenave <[email protected]> Co-authored-by: Xavier Roynard <[email protected]>
1 parent 452b5ff commit f8be860

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

.gitignore

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ tests/post/*.yaml
55
tests/problem_definition/*.csv
66
examples/**/*.png
77
tests/problem_definition/split.csv
8+
# Heavy data
9+
*.cgns
10+
!notebooks/ex_rotor37_pv.cgns
811

912
# Byte-compiled / optimized / DLL files
1013
__pycache__/
@@ -13,6 +16,7 @@ __pycache__/
1316

1417
# Distribution / packaging
1518
.Python
19+
venv/
1620
build/
1721
develop-eggs/
1822
dist/
@@ -32,6 +36,7 @@ share/python-wheels/
3236
*.egg
3337
MANIFEST
3438
_version.py
39+
oryx-build-commands.txt
3540

3641
# Unit test / coverage reports
3742
htmlcov/
@@ -69,10 +74,6 @@ docs/_extra/
6974
docs/source/notebooks/*.png
7075
docs/source/notebooks/*.yaml
7176

72-
# Heavy data
73-
*.cgns
74-
!notebooks/ex_rotor37_pv.cgns
75-
7677
###############################################################################
7778
# .gitignore PATTERN FORMAT:
7879
# - A blank line matches no files, so it can serve as a separator for readability.
@@ -106,4 +107,4 @@ docs/source/notebooks/*.yaml
106107
# For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.
107108
# - A slash followed by two consecutive asterisks then a slash matches zero or more directories.
108109
# For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.
109-
# - Other consecutive asterisks are considered regular asterisks and will match according to the previous rules.
110+
# - Other consecutive asterisks are considered regular asterisks and will match according to the previous rules.

src/plaid/containers/sample.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@
5757
# %% Classes
5858

5959

60+
def _check_names(names: Union[str, list[str]]):
61+
"""Check that names do not contain invalid character ``/``.
62+
63+
Args:
64+
names (Union[str, list[str]]): The names to check.
65+
66+
Raises:
67+
ValueError: If any name contains the invalid character ``/``.
68+
"""
69+
if isinstance(names, str):
70+
names = [names]
71+
for name in names:
72+
if (name is not None) and ("/" in name):
73+
raise ValueError(
74+
f"feature_names containing `/` are not allowed, but {name=}, you should first replace any occurence of `/` with something else, for example: `name.replace('/','__')`"
75+
)
76+
77+
6078
def read_index(pyTree: list, dim: list[int]):
6179
"""Read Index Array or Index Range from CGNS.
6280
@@ -809,6 +827,8 @@ def init_base(
809827
Returns:
810828
CGNSNode: The created Base node.
811829
"""
830+
_check_names([base_name])
831+
812832
time = self.get_time_assignment(time)
813833

814834
if base_name is None:
@@ -951,6 +971,8 @@ def init_zone(
951971
Returns:
952972
CGLNode: The newly initialized zone node within the CGNS tree.
953973
"""
974+
_check_names([zone_name])
975+
954976
# init_tree will look for default time
955977
self.init_tree(time)
956978
# get_base will look for default base_name and time
@@ -1138,6 +1160,7 @@ def add_scalar(self, name: str, value: ScalarType) -> None:
11381160
name (str): The name of the scalar value.
11391161
value (ScalarType): The scalar value to add or update in the dictionary.
11401162
"""
1163+
_check_names([name])
11411164
if self._scalars is None:
11421165
self._scalars = {name: value}
11431166
else:
@@ -1211,6 +1234,7 @@ def add_time_series(
12111234
Raises:
12121235
TypeError: Raised if the length of `time_sequence` is not equal to the length of `values`.
12131236
"""
1237+
_check_names([name])
12141238
assert len(time_sequence) == len(values), (
12151239
"time sequence and values do not have the same size"
12161240
)
@@ -1573,6 +1597,7 @@ def add_field(
15731597
Raises:
15741598
KeyError: Raised if the specified zone does not exist in the given base.
15751599
"""
1600+
_check_names([name])
15761601
# init_tree will look for default time
15771602
self.init_tree(time)
15781603
# get_zone will look for default zone_name, base_name and time

tests/containers/test_sample.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from plaid.containers.sample import (
2020
Sample,
21+
_check_names,
2122
read_index,
2223
read_index_array,
2324
read_index_range,
@@ -115,6 +116,17 @@ def sample_with_tree_and_scalar_and_time_series(
115116
# %% Test
116117

117118

119+
def test_check_names():
120+
_check_names("test name")
121+
_check_names(["test name", "test_name_2"])
122+
with pytest.raises(ValueError):
123+
_check_names("test/name")
124+
with pytest.raises(ValueError):
125+
_check_names(["test/name"])
126+
with pytest.raises(ValueError):
127+
_check_names([r"test\/name"])
128+
129+
118130
def test_read_index(tree, physical_dim):
119131
read_index(tree, physical_dim)
120132

0 commit comments

Comments
 (0)