From d67b07c95666723ef4c50163d37b6eae64059050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Flores?= Date: Wed, 17 Jan 2024 15:09:46 +0000 Subject: [PATCH] Finished with query_builder.py tests --- resources/src/druid/query_builder.py | 6 ++--- resources/tests/test_query_builder.py | 33 ++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/resources/src/druid/query_builder.py b/resources/src/druid/query_builder.py index 4901efd..302c2ed 100644 --- a/resources/src/druid/query_builder.py +++ b/resources/src/druid/query_builder.py @@ -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): """ @@ -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): diff --git a/resources/tests/test_query_builder.py b/resources/tests/test_query_builder.py index 104f7b9..fe5ddec 100644 --- a/resources/tests/test_query_builder.py +++ b/resources/tests/test_query_builder.py @@ -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): @@ -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 = [ @@ -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 = {