Coverage for test/esxport/_validate_fields_test.py: 100%
36 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-22 17:59 +0530
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-22 17:59 +0530
1"""Field Validator test cases."""
2from contextlib import nullcontext
3from unittest.mock import Mock
5import pytest
6from typing_extensions import Self
8from src.esxport import EsXport
9from src.exceptions import FieldNotFoundError
12class TestValidateFields:
13 """Test that all expected fields exist in all indices."""
15 def test_all_expected_fields_exist_in_all_indices(self: Self, mocker: Mock, esxport_obj: EsXport) -> None:
16 """Test that all expected fields exist in all indices, me hearties!."""
17 # Mock the get_mapping method of ElasticsearchClient to return a mapping with all expected fields
18 mocker.patch.object(
19 esxport_obj.es_client,
20 "get_mapping",
21 return_value={
22 "index1": {
23 "mappings": {
24 "properties": ["field1", "field2", "field3"],
25 },
26 },
27 "index2": {
28 "mappings": {
29 "properties": ["field1", "field2", "field3"],
30 },
31 },
32 },
33 )
35 esxport_obj._validate_fields()
37 def test_all_expected_fields_exist_in_some_indices(self: Self, mocker: Mock, esxport_obj: EsXport) -> None:
38 """Ahoy!.Test that all expected fields exist in some indices, me mateys!."""
39 # Mock the get_mapping method of ElasticsearchClient to return a mapping with some expected fields
40 mocker.patch.object(
41 esxport_obj.es_client,
42 "get_mapping",
43 return_value={
44 "index1": {
45 "mappings": {
46 "properties": ["aaa", "bbb"],
47 },
48 },
49 "index2": {
50 "mappings": {
51 "properties": ["cccc", "dddd"],
52 },
53 },
54 },
55 )
57 with pytest.raises(FieldNotFoundError):
58 esxport_obj._validate_fields()
60 def test_all_expected_fields_exist_in_one_index(self: Self, mocker: Mock, esxport_obj: EsXport) -> None:
61 """Test that all expected fields exist in one index, me hearties!."""
62 # Mock the get_mapping method of ElasticsearchClient to return a mapping with all expected fields
63 mocker.patch.object(
64 esxport_obj.es_client,
65 "get_mapping",
66 return_value={
67 "index1": {
68 "mappings": {
69 "properties": ["field1", "field2", "field3"],
70 },
71 },
72 },
73 )
75 esxport_obj.opts.index_prefixes = ["index1"]
76 esxport_obj.opts.fields = ["field1", "field2", "field3"]
77 esxport_obj._validate_fields()
79 def test_sort_param_are_checked(self: Self, mocker: Mock, esxport_obj: EsXport) -> None:
80 """Test that all expected fields exist in one index, me hearties!."""
81 # Mock the get_mapping method of ElasticsearchClient to return a mapping with all expected fields
82 mocker.patch.object(
83 esxport_obj.es_client,
84 "get_mapping",
85 return_value={
86 "index1": {
87 "mappings": {
88 "properties": ["field1", "field2", "field3"],
89 },
90 },
91 },
92 )
94 esxport_obj.opts.index_prefixes = ["index1"]
95 esxport_obj.opts.sort = [{"abc": "desc"}, {"def": "desc"}]
97 with pytest.raises(FieldNotFoundError):
98 esxport_obj._validate_fields()
100 esxport_obj.opts.sort = [{"field1": "asc"}, {"field2": "desc"}]
102 esxport_obj._validate_fields()
104 def test_all_is_not_checked(self: Self, mocker: Mock, esxport_obj: EsXport) -> None:
105 """Test that _all if not checked."""
106 # Mock the get_mapping method of ElasticsearchClient to return a mapping with all expected fields
107 mocker.patch.object(
108 esxport_obj.es_client,
109 "get_mapping",
110 return_value={
111 "index1": {
112 "mappings": {
113 "properties": ["field1", "field2", "field3"],
114 },
115 },
116 },
117 )
119 esxport_obj.opts.index_prefixes = ["index1"]
120 esxport_obj.opts.fields = ["_all", "field2", "field3"]
122 with nullcontext():
123 esxport_obj._validate_fields()
125 esxport_obj.opts.fields = ["xyz", "field2", "field3"]
127 with pytest.raises(FieldNotFoundError):
128 esxport_obj._validate_fields()