diff --git a/requirements.txt b/requirements.txt index af2f050..a649b51 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ pytest==7.4.2 pytest-cov==4.1.0 pytest-emoji==0.2.0 pytest-md==0.2.0 +pytest-mock==3.11.1 pytest-xdist==3.3.1 tqdm==4.66.1 typing-extensions==4.8.0 diff --git a/test/conftest.py b/test/conftest.py index 500bb5b..6cbac30 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -7,6 +7,7 @@ import pytest from src.click_opt.cli_options import CliOptions +from src.esxport import EsXport @pytest.fixture() @@ -48,3 +49,9 @@ def mock_es_client() -> Mock: }, } return mock_client + + +@pytest.fixture() +def esxport_obj(cli_options: CliOptions, mock_es_client: Mock) -> EsXport: + """Mocked EsXport class.""" + return EsXport(cli_options, mock_es_client) diff --git a/test/esxport/_validate_fields_test.py b/test/esxport/_validate_fields_test.py new file mode 100644 index 0000000..7704ff9 --- /dev/null +++ b/test/esxport/_validate_fields_test.py @@ -0,0 +1,101 @@ +"""Field Validator test cases.""" +from unittest.mock import Mock + +import pytest +from typing_extensions import Self + +from src.esxport import EsXport +from src.exceptions import FieldNotFoundError + + +class TestValidateFields: + """Test that all expected fields exist in all indices.""" + + def test_all_expected_fields_exist_in_all_indices(self: Self, mocker: Mock, esxport_obj: EsXport) -> None: + """Test that all expected fields exist in all indices, me hearties!.""" + # Mock the get_mapping method of ElasticsearchClient to return a mapping with all expected fields + mocker.patch.object( + esxport_obj.es_client, + "get_mapping", + return_value={ + "index1": { + "mappings": { + "properties": ["field1", "field2", "field3"], + }, + }, + "index2": { + "mappings": { + "properties": ["field1", "field2", "field3"], + }, + }, + }, + ) + + esxport_obj._validate_fields() + + def test_all_expected_fields_exist_in_some_indices(self: Self, mocker: Mock, esxport_obj: EsXport) -> None: + """Ahoy!.Test that all expected fields exist in some indices, me mateys!.""" + # Mock the get_mapping method of ElasticsearchClient to return a mapping with some expected fields + mocker.patch.object( + esxport_obj.es_client, + "get_mapping", + return_value={ + "index1": { + "mappings": { + "properties": ["aaa", "bbb"], + }, + }, + "index2": { + "mappings": { + "properties": ["cccc", "dddd"], + }, + }, + }, + ) + + with pytest.raises(FieldNotFoundError): + esxport_obj._validate_fields() + + def test_all_expected_fields_exist_in_one_index(self: Self, mocker: Mock, esxport_obj: EsXport) -> None: + """Test that all expected fields exist in one index, me hearties!.""" + # Mock the get_mapping method of ElasticsearchClient to return a mapping with all expected fields + mocker.patch.object( + esxport_obj.es_client, + "get_mapping", + return_value={ + "index1": { + "mappings": { + "properties": ["field1", "field2", "field3"], + }, + }, + }, + ) + + esxport_obj.opts.index_prefixes = ["index1"] + esxport_obj.opts.fields = ["field1", "field2", "field3"] + esxport_obj._validate_fields() + + def test_sort_param_are_checked(self: Self, mocker: Mock, esxport_obj: EsXport) -> None: + """Test that all expected fields exist in one index, me hearties!.""" + # Mock the get_mapping method of ElasticsearchClient to return a mapping with all expected fields + mocker.patch.object( + esxport_obj.es_client, + "get_mapping", + return_value={ + "index1": { + "mappings": { + "properties": ["field1", "field2", "field3"], + }, + }, + }, + ) + + esxport_obj.opts.index_prefixes = ["index1"] + esxport_obj.opts.sort = [{"abc": "desc"}, {"def": "desc"}] + + with pytest.raises(FieldNotFoundError): + esxport_obj._validate_fields() + + esxport_obj.opts.sort = [{"field1": "asc"}, {"field2": "desc"}] + + esxport_obj._validate_fields()