Skip to content

Commit

Permalink
✅ Added Fields Test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilbadyal committed Sep 18, 2023
1 parent 3bd9343 commit 72becea
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from src.click_opt.cli_options import CliOptions
from src.esxport import EsXport


@pytest.fixture()
Expand Down Expand Up @@ -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)
101 changes: 101 additions & 0 deletions test/esxport/_validate_fields_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 72becea

Please sign in to comment.