Skip to content

Commit 9d357b0

Browse files
committed
feat: more tests
1 parent 3c3b5d2 commit 9d357b0

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

tagstudio/src/core/query_lang/ast.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,24 @@ def visit(self, node: AST) -> T:
103103
return self.visit_property(node)
104104
elif isinstance(node, Not):
105105
return self.visit_not(node)
106-
raise Exception(f"Unknown Node Type of {node}")
106+
raise Exception(f"Unknown Node Type of {node}") # pragma: nocover
107107

108108
@abstractmethod
109109
def visit_and_list(self, node: ANDList) -> T:
110-
raise NotImplementedError()
110+
raise NotImplementedError() # pragma: nocover
111111

112112
@abstractmethod
113113
def visit_or_list(self, node: ORList) -> T:
114-
raise NotImplementedError()
114+
raise NotImplementedError() # pragma: nocover
115115

116116
@abstractmethod
117117
def visit_constraint(self, node: Constraint) -> T:
118-
raise NotImplementedError()
118+
raise NotImplementedError() # pragma: nocover
119119

120120
@abstractmethod
121121
def visit_property(self, node: Property) -> T:
122-
raise NotImplementedError()
122+
raise NotImplementedError() # pragma: nocover
123123

124124
@abstractmethod
125125
def visit_not(self, node: Not) -> T:
126-
raise NotImplementedError()
126+
raise NotImplementedError() # pragma: nocover

tagstudio/src/core/query_lang/tokenizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def EOF() -> "Token": # noqa: N802
4141
return Token.from_type(TokenType.EOF)
4242

4343
def __str__(self) -> str:
44-
return f"Token({self.type}, {self.value}, {self.start}, {self.end})"
44+
return f"Token({self.type}, {self.value}, {self.start}, {self.end})" # pragma: nocover
4545

4646
def __repr__(self) -> str:
47-
return self.__str__()
47+
return self.__str__() # pragma: nocover
4848

4949

5050
class Tokenizer:

tagstudio/src/core/query_lang/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def __init__(self, start: int, end: int, msg: str = "Syntax Error") -> None:
99
self.msg = msg
1010

1111
def __str__(self) -> str:
12-
return f"Syntax Error {self.start}->{self.end}: {self.msg}"
12+
return f"Syntax Error {self.start}->{self.end}: {self.msg}" # pragma: nocover
1313

1414
def __repr__(self) -> str:
15-
return self.__str__()
15+
return self.__str__() # pragma: nocover

tagstudio/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def library(request):
120120
@pytest.fixture
121121
def search_library() -> Library:
122122
lib = Library()
123-
lib.open_library(pathlib.Path("./tagstudio/tests/fixtures/search_library"))
123+
lib.open_library(pathlib.Path(CWD / "fixtures" / "search_library"))
124124
return lib
125125

126126

tagstudio/tests/test_search.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from src.core.library.alchemy.enums import FilterState
33
from src.core.library.alchemy.library import Library
4+
from src.core.query_lang.util import ParsingError
45

56

67
def verify_count(lib: Library, query: str, count: int):
@@ -19,6 +20,7 @@ def verify_count(lib: Library, query: str, count: int):
1920
("special:untagged", 1),
2021
("filetype:png", 23),
2122
("filetype:jpg", 6),
23+
("filetype:'jpg'", 6),
2224
("tag_id:1011", 5),
2325
("tag_id:1038", 11),
2426
("doesnt exist", 0),
@@ -72,3 +74,51 @@ def test_and(search_library: Library, query: str, count: int):
7274
)
7375
def test_or(search_library: Library, query: str, count: int):
7476
verify_count(search_library, query, count)
77+
78+
79+
@pytest.mark.parametrize(
80+
["query", "count"],
81+
[
82+
("not unexistant", 29),
83+
("not path:*", 0),
84+
("not not path:*", 29),
85+
("not special:untagged", 28),
86+
("not filetype:png", 6),
87+
("not filetype:jpg", 23),
88+
("not tag_id:1011", 24),
89+
("not tag_id:1038", 18),
90+
("not green", 24),
91+
("tag:favorite", 0),
92+
("not circle", 18),
93+
("not tag:square", 18),
94+
("circle and not square", 6),
95+
("not circle and square", 6),
96+
("special:untagged or not filetype:jpg", 24),
97+
("not square or green", 20),
98+
],
99+
)
100+
def test_not(search_library: Library, query: str, count: int):
101+
verify_count(search_library, query, count)
102+
103+
104+
@pytest.mark.parametrize(
105+
["query", "count"],
106+
[
107+
("(tag_id:1041)", 11),
108+
("(((tag_id:1041)))", 11),
109+
("not (not tag_id:1041)", 11),
110+
("((circle) and (not square))", 6),
111+
("(not ((square) OR (green)))", 15),
112+
("filetype:png and (tag:square or green)", 12),
113+
],
114+
)
115+
def test_parentheses(search_library: Library, query: str, count: int):
116+
verify_count(search_library, query, count)
117+
118+
119+
@pytest.mark.parametrize(
120+
"invalid_query", ["asd AND", "asd AND AND", "tag:(", "(asd", "asd[]", "asd]", ":", "tag: :"]
121+
)
122+
def test_syntax(search_library: Library, invalid_query: str):
123+
with pytest.raises(ParsingError) as e_info: # noqa: F841
124+
search_library.search_library(FilterState.from_search_query(invalid_query))

0 commit comments

Comments
 (0)