Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Oct 25, 2023
1 parent f1570b5 commit 6144456
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 16 deletions.
18 changes: 8 additions & 10 deletions app/commons/esclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import traceback
from collections import deque
from time import time
from typing import List, Tuple, Set

import elasticsearch
import elasticsearch.helpers
Expand Down Expand Up @@ -198,25 +197,23 @@ def create_index_if_not_exists(self, index_name):

def _to_launch_test_item_list(
self,
launches: List[launch_objects.Launch]
) -> Tuple[Set[int], deque[Tuple[launch_objects.Launch, launch_objects.TestItem]]]:
launch_ids = set()
launches: list[launch_objects.Launch]
) -> deque[tuple[launch_objects.Launch, launch_objects.TestItem]]:
test_item_queue = deque()
for launch in launches:
test_items = launch.testItems
launch.testItems = []
launch_ids.add(launch.launchId)
for test_item in test_items:
for log in test_item.logs:
if str(log.clusterId) in launch.clusters:
log.clusterMessage = launch.clusters[str(log.clusterId)]
test_item_queue.append((launch, test_item))
return launch_ids, test_item_queue
return test_item_queue

def _to_index_bodies(
self,
project_with_prefix: str,
test_item_queue: deque[Tuple[launch_objects.Launch, launch_objects.TestItem]]
test_item_queue: deque[tuple[launch_objects.Launch, launch_objects.TestItem]]
) -> tuple[list[str], list[dict]]:
bodies = []
test_item_ids = []
Expand All @@ -238,8 +235,9 @@ def index_logs(self, launches):
logger.info("Indexing logs for %d launches", len(launches))
logger.info("ES Url %s", text_processing.remove_credentials_from_url(self.host))
t_start = time()
launch_ids = set(map(lambda launch_obj: launch_obj.launchId, launches))
project = str(next(map(lambda launch_obj: launch_obj.project, launches)))
launch_ids, test_item_queue = self._to_launch_test_item_list(launches)
test_item_queue = self._to_launch_test_item_list(launches)
del launches
if project is None:
return launch_objects.BulkResponse(took=0, errors=False)
Expand Down Expand Up @@ -574,7 +572,7 @@ def __time_range_query(
@utils.ignore_warnings
def get_launch_ids_by_start_time_range(
self, project: int, start_date: str, end_date: str
) -> List[str]:
) -> list[str]:
index_name = text_processing.unite_project_name(
str(project), self.app_config["esProjectIndexPrefix"]
)
Expand Down Expand Up @@ -602,7 +600,7 @@ def remove_by_launch_start_time_range(
@utils.ignore_warnings
def get_log_ids_by_log_time_range(
self, project: int, start_date: str, end_date: str
) -> List[str]:
) -> list[str]:
index_name = text_processing.unite_project_name(
str(project), self.app_config["esProjectIndexPrefix"]
)
Expand Down
6 changes: 6 additions & 0 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

from app.utils.utils import read_json_file

DEFAULT_ES_CONFIG = {'esHost': 'http://localhost:9200', 'esVerifyCerts': False, 'esUseSsl': False,
'esSslShowWarn': False, 'esCAcert': None, 'esClientCert': None, 'esClientKey': None,
'esUser': None, 'turnOffSslVerification': True, 'esProjectIndexPrefix': '', 'esChunkNumber': 1000}
DEFAULT_BOOST_LAUNCH = 8.0
DEFAULT_SEARCH_CONFIG = {'SimilarityWeightsFolder': '', 'BoostLaunch': DEFAULT_BOOST_LAUNCH}


def get_fixture(fixture_name, to_json=False):
return read_json_file("test_res/fixtures", fixture_name, to_json)
Expand Down
89 changes: 89 additions & 0 deletions test/unit/commons/test_esclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright 2023 EPAM Systems
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest import mock

import pytest

from app.commons import esclient
from app.commons.launch_objects import Launch, TestItem, Log
from test import DEFAULT_ES_CONFIG, DEFAULT_SEARCH_CONFIG

TEST_PROJECT_ID = 2


def create_test_es_client():
es_mock = mock.Mock()
es_mock.search.return_value = {'hits': {'hits': []}}
return esclient.EsClient(DEFAULT_ES_CONFIG, DEFAULT_SEARCH_CONFIG, es_mock)


def create_test_launch_one_item():
logs = [Log(logId=37135, logLevel=40000, message="Environment variable 'SAUCELABS_USER' does not exist.")]
test_items = [TestItem(testItemId=2190, uniqueId='auto:4cf9a1d86e663dd6e8a4a99dbd78e7ce', isAutoAnalyzed=False,
testCaseHash=-2120975783, testItemName='Example page test', logs=logs)]
return Launch(launchId=10, project=TEST_PROJECT_ID, launchName='Test Launch', launchNumber=7, testItems=test_items)


def create_test_launch_two_items():
launch = create_test_launch_one_item()
test_items = launch.testItems
logs = [Log(logId=37136, logLevel=40000, message="Environment variable 'SAUCELABS_USER' does not exist.")]
test_items.append(TestItem(testItemId=2191, uniqueId='auto:4cf9a1d86e663dd6e8a4a99dbd78e7cf', isAutoAnalyzed=False,
testCaseHash=-2120975784, testItemName='Example page test', logs=logs))
return launch


def create_test_launch_two_items_one_indexed_log():
launch = create_test_launch_one_item()
test_items = launch.testItems
logs = [Log(logId=37136, logLevel=30000, message="Environment variable 'SAUCELABS_USER' does not exist.")]
test_items.append(TestItem(testItemId=2191, uniqueId='auto:4cf9a1d86e663dd6e8a4a99dbd78e7cf', isAutoAnalyzed=False,
testCaseHash=-2120975784, testItemName='Example page test', logs=logs))
return launch


@pytest.mark.parametrize(
'launch, expected_launch',
[
(create_test_launch_one_item(), create_test_launch_one_item()),
(create_test_launch_two_items(), create_test_launch_two_items()),
]
)
def test_to_launch_test_item_list(launch, expected_launch):
es_client = create_test_es_client()
items = es_client._to_launch_test_item_list([launch])
assert len(items) == len(expected_launch.testItems)
i = 0
for actual_launch, item in items:
assert len(actual_launch.testItems) == 0
assert item.testItemId == expected_launch.testItems[i].testItemId
i += 1


@pytest.mark.parametrize(
'launch, expected_length',
[
(create_test_launch_one_item(), 1),
(create_test_launch_two_items(), 2),
(create_test_launch_two_items_one_indexed_log(), 1),
]
)
def test_to_index_bodies(launch, expected_length):
es_client = create_test_es_client()
test_item_ids, bodies = es_client._to_index_bodies(
str(TEST_PROJECT_ID),
es_client._to_launch_test_item_list([launch])
)
assert len(test_item_ids) == expected_length
assert len(bodies) == expected_length
8 changes: 2 additions & 6 deletions test/unit/service/test_analyzer_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

import pytest

from test import DEFAULT_ES_CONFIG, DEFAULT_SEARCH_CONFIG, DEFAULT_BOOST_LAUNCH
from app.service.analyzer_service import AnalyzerService

DEFAULT_ES_CONFIG = {'esHost': 'http://localhost:9200', 'esVerifyCerts': False, 'esUseSsl': False,
'esSslShowWarn': False, 'esCAcert': None, 'esClientCert': None, 'esClientKey': None,
'esUser': None, 'turnOffSslVerification': True}

DEFAULT_BOOST_LAUNCH = 8.0
DEFAULT_LAUNCH_NAME = 'Test Launch'
DEFAULT_LAUNCH_ID = 3

Expand Down Expand Up @@ -60,7 +57,6 @@ def test_add_constraints_for_launches_into_query(launch_number, launch_mode, exp
launch.launchNumber = launch_number
launch.launchName = DEFAULT_LAUNCH_NAME
launch.analyzerConfig.analyzerMode = launch_mode
analyzer = AnalyzerService(None, DEFAULT_ES_CONFIG, {'SimilarityWeightsFolder': '',
'BoostLaunch': DEFAULT_BOOST_LAUNCH})
analyzer = AnalyzerService(None, DEFAULT_ES_CONFIG, DEFAULT_SEARCH_CONFIG)
result = analyzer.add_constraints_for_launches_into_query(get_empty_bool_query(), launch)
assert result['query']['bool'] == expected_query

0 comments on commit 6144456

Please sign in to comment.