Skip to content

Commit

Permalink
fix: tests and added query stracture test. (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-pticha authored Feb 11, 2024
1 parent 66a658e commit 768d1a5
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions tests/unit/test_report.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from pathlib import Path
from src.config.config import LAST_QUERY_ID, QUERIES_PATH_DEFAULT
from yaml import safe_load

query_dir = Path(__file__).parent.parent.parent / QUERIES_PATH_DEFAULT

RQ_PREFIX = "RQ-"


def test_report():
assert query_dir.exists(), f"Directory {query_dir} doesn't exist"
Expand All @@ -14,15 +17,33 @@ def test_report():
# get query ids from files in query_dir
query_ids = []
for query_file in query_files:
with open(query_file, "r") as f:
query_id = ""
while not query_id: # possible that first line is empty
line = f.readline()
if line.startswith("id: RQ-"):
query_id = line[4:].strip()
query_ids.append(query_id)

max_id_num = max([int(query_id[3:]) for query_id in query_ids])
with open(query_file, "r") as query:
parsed_query = safe_load(query)
if not parsed_query:
raise ValueError(f"{query_file} is not a valid query file")

query_id = parsed_query.get("id")
try:
int(query_id.split(RQ_PREFIX)[1])
except ValueError:
raise ValueError(f"Query {query_file} has invalid id")

query_info = parsed_query.get("info")

assert parsed_query["query"], f"Query in {query_file} is empty"
assert query_info["name"], f"Query in {query_file} has no name"
assert query_info["severity"], f"Query in {query_file} has no severity"
assert query_info[
"description"
], f"Query in {query_file} has no description"
assert query_info["tags"], f"Query in {query_file} has no tags"

query_ids.append(parsed_query.get("id"))

try:
max_id_num = max([int(query_id.split(RQ_PREFIX)[1]) for query_id in query_ids])
except ValueError:
raise ValueError(f"Added query has invalid id")

# sequence
assert set(query_ids) == set(
Expand Down

0 comments on commit 768d1a5

Please sign in to comment.