Skip to content

Commit

Permalink
Finished with query_builder.py tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Rodríguez Flores committed Jan 17, 2024
1 parent 80f2661 commit d67b07c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
6 changes: 3 additions & 3 deletions resources/src/druid/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def load_json(self, path):
except json.JSONDecodeError:
error_msg=f"Could not decode{os.path.basename(path)} as a Json. Check the JSON format."
logger.logger.error(error_msg)
raise FileNotFoundError(error_msg)
raise ValueError(error_msg)

def granularity_to_seconds(self, granularity):
"""
Expand Down Expand Up @@ -106,11 +106,11 @@ def granularity_to_seconds(self, granularity):
return base_granularities[granularity]
try:
multiplier = base_granularities[granularity[-1]]
numbers = int(''.join(filter(str.isdigit, granularity)))
except Exception:
error_msg='Invalid granularity'
logger.logger.error(error_msg)
raise FileNotFoundError(error_msg)
numbers = int(''.join(filter(str.isdigit, granularity)))
raise ValueError(error_msg)
return numbers * multiplier

def modify_aggregations(self, query):
Expand Down
33 changes: 30 additions & 3 deletions resources/tests/test_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import unittest
import sys
import os
import json
import tempfile
from resources.src.druid import query_builder

class TestQueryBuilder(unittest.TestCase):
Expand All @@ -29,11 +31,19 @@ def setUp(self) -> None:
self.post_aggregations_file = os.path.join(os.getcwd(),"resources", "src", "druid", "data", "postAggregations.json")
self.builder = query_builder.QueryBuilder(self.aggregations_file, self.post_aggregations_file)

def test_invalid_files(self):
def test_nonexistent_files(self):
with self.assertRaises(FileNotFoundError):
query_builder.QueryBuilder("invalid.json", self.post_aggregations_file)
query_builder.QueryBuilder("nonexist.json", self.post_aggregations_file)
with self.assertRaises(FileNotFoundError):
query_builder.QueryBuilder(self.aggregations_file, "invalid.json")
query_builder.QueryBuilder(self.aggregations_file, "nonexist.json")

def test_nonjson_files(self):
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file_path = temp_file.name
with self.assertRaises(ValueError):
query_builder.QueryBuilder(temp_file_path, self.post_aggregations_file)
with self.assertRaises(ValueError):
query_builder.QueryBuilder(self.aggregations_file, temp_file_path)

def test_known_granularities_granularities_to_seconds(self):
test_cases = [
Expand All @@ -59,6 +69,23 @@ def test_invalid_input_granularities_to_seconds(self):
self.builder.granularity_to_seconds(None)
with self.assertRaises(ValueError):
self.builder.granularity_to_seconds("")
with self.assertRaises(ValueError):
self.builder.granularity_to_seconds("pttenm")
with self.assertRaises(ValueError):
self.builder.granularity_to_seconds("x")

def test_modify_granularity(self):
query1 = {
"granularity": {
"period": "pt5m"
}
}
query2 = {
"granularity": {
"period": "pt10m"
}
}
self.assertEqual(self.builder.modify_granularity(query1, "pt10m"), query2)

def test_modify_aggregations(self):
query = {
Expand Down

0 comments on commit d67b07c

Please sign in to comment.