|
| 1 | +""" |
| 2 | +This module comprises test cases for verifying |
| 3 | +the functionality of parser classes in FiSE. |
| 4 | +""" |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from fise.common import tools, constants |
| 11 | +from fise.query.parsers import FileQueryParser |
| 12 | +from fise.shared import Field, SearchQuery |
| 13 | + |
| 14 | + |
| 15 | +class TestFileQueryParser: |
| 16 | + """Tests the FileQueryParser class""" |
| 17 | + |
| 18 | + file_fields = [Field(field) for field in constants.FILE_FIELDS] |
| 19 | + |
| 20 | + file_search_query_test_params = [ |
| 21 | + "* FROM .", |
| 22 | + "name, path, parent FROM ABSOLUTE '.'", |
| 23 | + "access_time, modify_time from RELATIVE .", |
| 24 | + r"* FROM ABSOLUTE . WHERE type = '.txt' AND name LIKE '^report-[0-9]*\.txt$'", |
| 25 | + "name, path, access_time FROM . WHERE atime >= '2023-04-04' OR ctime >= '2023-12-04'", |
| 26 | + "* FROM '.' WHERE atime >= '2024-02-20'", |
| 27 | + ] |
| 28 | + |
| 29 | + # The following list comprises results for the file search query test comprising sub-lists, |
| 30 | + # each with a length of 2. The first element of each sub-list signifies whether the path is |
| 31 | + # absolute (True) or relative (False) whereas the second element is a list comprising names |
| 32 | + # of the search fields. |
| 33 | + file_search_query_test_results = [ |
| 34 | + [False, list(constants.FILE_FIELDS)], |
| 35 | + [True, ["name", "path", "parent"]], |
| 36 | + [False, ["access_time", "modify_time"]], |
| 37 | + [True, list(constants.FILE_FIELDS)], |
| 38 | + [False, ["name", "path", "access_time"]], |
| 39 | + [False, list(constants.FILE_FIELDS)], |
| 40 | + ] |
| 41 | + |
| 42 | + @pytest.mark.parametrize( |
| 43 | + ("subquery", "results"), |
| 44 | + zip(file_search_query_test_params, file_search_query_test_results), |
| 45 | + ) |
| 46 | + def test_file_search_query_parser(self, subquery, results): |
| 47 | + """ |
| 48 | + Tests the file query parser with search queries. |
| 49 | + """ |
| 50 | + |
| 51 | + query: list[str] = tools.parse_query(subquery) |
| 52 | + |
| 53 | + parser = FileQueryParser(query, "search") |
| 54 | + search_query: SearchQuery = parser.parse_query() |
| 55 | + |
| 56 | + path: Path = Path(".") |
| 57 | + columns: list[str] = results[1] |
| 58 | + |
| 59 | + if results[0]: |
| 60 | + path = path.resolve() |
| 61 | + |
| 62 | + assert search_query.path == path |
| 63 | + assert [field.field for field in search_query.fields] == columns |
| 64 | + assert search_query.columns == columns |
0 commit comments