Skip to content

Commit 8410add

Browse files
Remove redundant functions and optimize code
1 parent c54a9d6 commit 8410add

File tree

3 files changed

+98
-72
lines changed

3 files changed

+98
-72
lines changed

tests/test_query/test_delete_query.py

Lines changed: 90 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pytest
1616
import pandas as pd
1717

18+
import utils
1819
import reset_tests
1920
from fise.query import QueryHandler
2021

@@ -25,22 +26,12 @@
2526
TEST_RECORDS_FILE = Path(__file__).parent / "test_delete_query.hdf"
2627

2728

28-
def read_hdf(file: Path, path: str) -> pd.Series:
29-
"""
30-
Reads the test records stored at the specified
31-
path from the specified HDF5 file.
32-
"""
33-
34-
with pd.HDFStore(str(file)) as store:
35-
return store[path]
36-
37-
3829
# Pandas series comprising path of all the filses and directories
3930
# present within the `file_dir` directory within the test directory.
4031
FILE_DIR_TEST_DIRECTORY_LISTINGS = pd.concat(
4132
[
42-
read_hdf(TEST_DIRECTORY_LISTINGS_FILE, "/file_dir/dirs"),
43-
read_hdf(TEST_DIRECTORY_LISTINGS_FILE, "/file_dir/files"),
33+
utils.read_hdf(TEST_DIRECTORY_LISTINGS_FILE, "/file_dir/dirs"),
34+
utils.read_hdf(TEST_DIRECTORY_LISTINGS_FILE, "/file_dir/files"),
4435
],
4536
ignore_index=True,
4637
)
@@ -55,7 +46,7 @@ def verify_delete_query(path: str) -> None:
5546

5647
# File and directories to be exempted during verification as
5748
# they are meant to be removed during the delete operation.
58-
records: pd.Series = read_hdf(TEST_RECORDS_FILE, path)
49+
records: pd.Series = utils.read_hdf(TEST_RECORDS_FILE, path)
5950

6051
for i in FILE_DIR_TEST_DIRECTORY_LISTINGS:
6152
if (FILE_DIR_TEST_DIRECTORY / i).exists() or i in records.values:
@@ -90,45 +81,75 @@ class TestFileDeleteQuery:
9081

9182
basic_query_syntax_test_params = [
9283
(1, f"DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'project'}'"),
93-
(2, f"DELETE[TYPE FILE] FROM '{FILE_DIR_TEST_DIRECTORY / 'media'}' WHERE type = '.mp3'"),
94-
(3, rf"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'project'}' WHERE name LIKE '.*\.py'"),
84+
(
85+
2,
86+
f"DELETE[TYPE FILE] FROM '{FILE_DIR_TEST_DIRECTORY / 'media'}' WHERE type = '.mp3'",
87+
),
88+
(
89+
3,
90+
rf"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'project'}' WHERE name LIKE '.*\.py'",
91+
),
9592
]
9693

9794
recursive_command_test_params = [
98-
(1, f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE name = 'Q4.txt'"),
95+
(
96+
1,
97+
f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE name = 'Q4.txt'",
98+
),
9999
(2, f"RECURSIVE DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'docs'}'"),
100100
]
101101

102102
path_types_test_params = [
103-
(1, f"R DELETE FROM ABSOLUTE '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE name = 'Q2.txt'"),
103+
(
104+
1,
105+
f"R DELETE FROM ABSOLUTE '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE name = 'Q2.txt'",
106+
),
104107
(2, f"DELETE FROM ABSOLUTE '{FILE_DIR_TEST_DIRECTORY / 'project'}'"),
105-
(3, f"DELETE FROM RELATIVE '{FILE_DIR_TEST_DIRECTORY / 'media'}' WHERE type = '.mp4'"),
108+
(
109+
3,
110+
f"DELETE FROM RELATIVE '{FILE_DIR_TEST_DIRECTORY / 'media'}' WHERE type = '.mp4'",
111+
),
106112
]
107113

108114
mixed_case_query_test_params = [
109115
(1, f"DeLeTE FRoM '{FILE_DIR_TEST_DIRECTORY / 'project'}'"),
110-
(2, f"DelETE[TYPE FiLE] FrOM '{FILE_DIR_TEST_DIRECTORY / 'media'}' Where TypE = '.mp3'"),
111-
(3, rf"r dELETe FrOM '{FILE_DIR_TEST_DIRECTORY / 'project'}' wHERe NaMe liKe '.*\.py'"),
116+
(
117+
2,
118+
f"DelETE[TYPE FiLE] FrOM '{FILE_DIR_TEST_DIRECTORY / 'media'}' Where TypE = '.mp3'",
119+
),
120+
(
121+
3,
122+
rf"r dELETe FrOM '{FILE_DIR_TEST_DIRECTORY / 'project'}' wHERe NaMe liKe '.*\.py'",
123+
),
112124
]
113125

114126
query_conditions_test_params = [
115127
(1, f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE name = 'Q1.txt'"),
116-
(2, rf"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE type='.txt' AND name LIKE '^IN.*\.txt$'"),
117-
(3, f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE type IN ('.mp4', '.avi') OR type='.mp3'"),
128+
(
129+
2,
130+
rf"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE type='.txt' AND name LIKE '^IN.*\.txt$'",
131+
),
132+
(
133+
3,
134+
f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE type IN ('.mp4', '.avi') OR type='.mp3'",
135+
),
118136
]
119137

120138
nested_conditions_test_params = [
121139
(
122-
1, f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE "
123-
"size[b] = 0 AND (filetype = '.txt' OR type = '.mp3')"
140+
1,
141+
f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE "
142+
"size[b] = 0 AND (filetype = '.txt' OR type = '.mp3')",
124143
),
125144
(
126-
2, f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'project'}' WHERE size[b]"
127-
"= 0 AND (type = None OR (type IN ('.txt', '.py'))) AND name != 'LICENSE'"
145+
2,
146+
f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'project'}' WHERE size[b]"
147+
"= 0 AND (type = None OR (type IN ('.txt', '.py'))) AND name != 'LICENSE'",
128148
),
129149
(
130-
3, f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE"
131-
" type = '.txt' AND (((name = 'Q1.txt' OR name = 'Q3.txt')))"
150+
3,
151+
f"R DELETE FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE"
152+
" type = '.txt' AND (((name = 'Q1.txt' OR name = 'Q3.txt')))",
132153
),
133154
]
134155

@@ -172,14 +193,26 @@ class TestDirDeleteQuery:
172193

173194
basic_query_syntax_test_params = [
174195
(1, f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'docs'}'"),
175-
(2, f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE name LIKE '^report.*$'"),
176-
(3, f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE name IN ('project', 'media')"),
196+
(
197+
2,
198+
f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE name LIKE '^report.*$'",
199+
),
200+
(
201+
3,
202+
f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE name IN ('project', 'media')",
203+
),
177204
]
178205

179206
recursive_command_test_params = [
180207
(1, f"R DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'project'}'"),
181-
(2, f"R DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE name='report-2023'"),
182-
(3, f"R DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE name IN ('media', 'orders')"),
208+
(
209+
2,
210+
f"R DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE name='report-2023'",
211+
),
212+
(
213+
3,
214+
f"R DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE name IN ('media', 'orders')",
215+
),
183216
]
184217

185218
path_types_test_params = [
@@ -190,36 +223,45 @@ class TestDirDeleteQuery:
190223

191224
mixed_case_query_test_params = [
192225
(1, f"DeLEtE[TYPe DIR] froM '{FILE_DIR_TEST_DIRECTORY / 'docs'}'"),
193-
(2, f"DElEtE[tYPE DiR] From '{FILE_DIR_TEST_DIRECTORY / 'reports'}' Where NamE LikE '^report.*$'"),
194-
(3, f"Delete[Type diR] FroM '{FILE_DIR_TEST_DIRECTORY}' WheRE NAMe In ('project', 'media')"),
226+
(
227+
2,
228+
f"DElEtE[tYPE DiR] From '{FILE_DIR_TEST_DIRECTORY / 'reports'}' Where NamE LikE '^report.*$'",
229+
),
230+
(
231+
3,
232+
f"Delete[Type diR] FroM '{FILE_DIR_TEST_DIRECTORY}' WheRE NAMe In ('project', 'media')",
233+
),
195234
]
196235

197236
query_conditions_test_params = [
237+
(1, f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE name = 'media'"),
198238
(
199-
1, f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE name = 'media'"
239+
2,
240+
f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE "
241+
"name IN ('orders', 'media') AND parent LIKE '^.*file_dir[/]?$'",
200242
),
201243
(
202-
2, f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE "
203-
"name IN ('orders', 'media') AND parent LIKE '^.*file_dir[/]?$'"
244+
3,
245+
f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE"
246+
" name = 'report-2021' OR name = 'report-2022' OR name = 'report-2023'",
204247
),
205-
(
206-
3, f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE"
207-
" name = 'report-2021' OR name = 'report-2022' OR name = 'report-2023'"
208-
)
209248
]
210249

211250
nested_conditions_test_params = [
212251
(
213-
1, f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE parent"
214-
" LIKE '^.*file_dir[/]?$' AND (name = 'orders' OR name = 'media')"
252+
1,
253+
f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE parent"
254+
" LIKE '^.*file_dir[/]?$' AND (name = 'orders' OR name = 'media')",
215255
),
216256
(
217-
2, f"R DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE"
218-
" ((name IN ('src'))) AND path LIKE '^.*file_dir/src[/]?$'"
257+
2,
258+
f"R DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY}' WHERE"
259+
" ((name IN ('src'))) AND path LIKE '^.*file_dir/src[/]?$'",
219260
),
220261
(
221-
3, f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE path LIKE "
222-
"'^.*report-20(23|24)[/]?$' AND ((name = 'report-2023') OR ((name = 'report-2024')))"
262+
3,
263+
f"DELETE[TYPE DIR] FROM '{FILE_DIR_TEST_DIRECTORY / 'reports'}' WHERE path LIKE "
264+
"'^.*report-20(23|24)[/]?$' AND ((name = 'report-2023') OR ((name = 'report-2024')))",
223265
),
224266
]
225267

@@ -242,7 +284,7 @@ def test_path_types(self, index: int, query: str) -> None:
242284
def test_query_conditions(self, index: int, query: str) -> None:
243285
"""Tests directory delete query conditions."""
244286
examine_delete_query(query, f"/dir/conditions/test{index}")
245-
287+
246288
@pytest.mark.parametrize(("index", "query"), nested_conditions_test_params)
247289
def test_nested_query_conditions(self, index: int, query: str) -> None:
248290
"""Tests nested directory delete query conditions."""

tests/test_query/test_handlers/test_operators.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import pytest
3434
import pandas as pd
3535

36+
import utils
3637
import reset_tests
3738
from fise.common import constants
3839
from fise.shared import File, Directory, Field, DataLine
@@ -51,20 +52,12 @@
5152
TEST_DIRECTORY_LISTINGS_FILE = TEST_DIRECTORY.parent / "test_directory.hdf"
5253
TEST_RECORDS_FILE = Path(__file__).parent / "test_operators.hdf"
5354

54-
55-
def read_hdf(file: Path, path: str) -> pd.Series | pd.DataFrame:
56-
"""Reads records stored at the path mentioned from specified HDF5 file."""
57-
58-
with pd.HDFStore(str(file)) as store:
59-
return store[path]
60-
61-
6255
# Pandas series comprising path of all the files and directories
6356
# present within the `file_dir` directory within test directory.
6457
FILE_DIR_TEST_DIRECTORY_LISTINGS = pd.concat(
6558
[
66-
read_hdf(TEST_DIRECTORY_LISTINGS_FILE, "/file_dir/dirs"),
67-
read_hdf(TEST_DIRECTORY_LISTINGS_FILE, "/file_dir/files"),
59+
utils.read_hdf(TEST_DIRECTORY_LISTINGS_FILE, "/file_dir/dirs"),
60+
utils.read_hdf(TEST_DIRECTORY_LISTINGS_FILE, "/file_dir/files"),
6861
],
6962
ignore_index=True,
7063
)
@@ -79,7 +72,7 @@ def verify_search_operation(path: str, data: pd.DataFrame) -> None:
7972

8073
assert isinstance(data, pd.DataFrame)
8174

82-
results: pd.DataFrame = read_hdf(TEST_RECORDS_FILE, path)
75+
results: pd.DataFrame = utils.read_hdf(TEST_RECORDS_FILE, path)
8376

8477
data_set: set[tuple[Any]] = set(tuple(row) for row in data.values)
8578
results_set: set[tuple[Any]] = set(tuple(row) for row in results.values)
@@ -96,7 +89,7 @@ def verify_delete_operation(path: str) -> None:
9689

9790
# File and directories to be exempted during verification as
9891
# they are meant to be removed during the delete operation.
99-
records: pd.Series = read_hdf(TEST_RECORDS_FILE, path)
92+
records: pd.Series = utils.read_hdf(TEST_RECORDS_FILE, path)
10093

10194
for i in FILE_DIR_TEST_DIRECTORY_LISTINGS:
10295
if (FILE_DIR_TEST_DIRECTORY / i).exists() or i in records.values:

tests/test_query/test_search_query.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import pytest
1616
import pandas as pd
1717

18+
import utils
19+
1820
from fise.common import constants
1921
from fise.query import QueryHandler
2022

@@ -23,25 +25,14 @@
2325
TEST_RECORDS_FILE = Path(__file__).parent / "test_search_query.hdf"
2426

2527

26-
def read_hdf(path: str) -> pd.DataFrame:
27-
"""
28-
Reads the test records stored at the specified
29-
path from the `test_search_query.hdf` file.
30-
"""
31-
global TEST_RECORDS_FILE
32-
33-
with pd.HDFStore(str(TEST_RECORDS_FILE)) as store:
34-
return store[path]
35-
36-
3728
def verify_search_query(path: str, data: pd.DataFrame) -> None:
3829
"""
3930
Verifies the pandas dataframe extracted from the search operation with the
4031
records stored at the specified path in the `test_operators.hdf` file.
4132
"""
4233
global TEST_RECORDS_FILE
4334

44-
results: pd.DataFrame = read_hdf(path)
35+
results: pd.DataFrame = utils.read_hdf(TEST_RECORDS_FILE, path)
4536

4637
data_set: set[tuple[Any]] = set(tuple(row) for row in data.values)
4738
results_set: set[tuple[Any]] = set(tuple(row) for row in results.values)

0 commit comments

Comments
 (0)