Coverage for test/esxport/_prepare_search_query_test.py: 97%

69 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-22 17:59 +0530

1"""Export testing.""" 

2from __future__ import annotations 

3 

4import string 

5from random import choice, randint 

6from typing import TYPE_CHECKING, Any 

7from unittest.mock import patch 

8 

9import pytest 

10 

11from src.exceptions import IndexNotFoundError 

12from src.strings import index_not_found, output_fields, sorting_by, using_indexes 

13 

14if TYPE_CHECKING: 

15 from typing_extensions import Self 

16 

17 from src.esxport import EsXport 

18 

19 

20@patch("src.esxport.EsXport._validate_fields") 

21class TestSearchQuery: 

22 """Tests that a search query with valid input parameters is successful.""" 

23 

24 @staticmethod 

25 def random_string(str_len: int = 20) -> str: 

26 """Generates a random string.""" 

27 characters = string.ascii_letters + string.digits 

28 return "".join(choice(characters) for _ in range(str_len)) 

29 

30 @staticmethod 

31 def random_number(upper: int = 100, lower: int = 10000) -> int: 

32 """Generates a random number.""" 

33 return randint(upper, lower) 

34 

35 def test_index(self: Self, _: Any, esxport_obj: EsXport) -> None: 

36 """Arr, matey!. 

37 

38 Let's test if our search query be successful, with valid input parameters!. 

39 """ 

40 random_strings = [self.random_string(10) for _ in range(5)] 

41 esxport_obj.opts.index_prefixes = random_strings 

42 indexes = ",".join(random_strings) 

43 

44 esxport_obj._prepare_search_query() 

45 assert esxport_obj.search_args["index"] == indexes 

46 

47 def test_all_index(self: Self, _: Any, esxport_obj: EsXport) -> None: 

48 """Arr, matey!. 

49 

50 Let's test if our search query be successful, with valid input parameters!. 

51 """ 

52 esxport_obj.opts.index_prefixes = ["_all", "invalid_index"] 

53 esxport_obj._check_indexes() 

54 assert esxport_obj.opts.index_prefixes == ["_all"] 

55 

56 def test_invalid_index( 

57 self: Self, 

58 _: Any, 

59 esxport_obj: EsXport, 

60 ) -> None: 

61 """Arr, matey!. 

62 

63 Let's test if our search query be successful, with valid input parameters!. 

64 """ 

65 esxport_obj.opts.index_prefixes = ["invalid_index"] 

66 

67 with patch.object(esxport_obj.es_client, "indices_exists", return_value=False): 

68 with pytest.raises(IndexNotFoundError) as exc_info: 

69 esxport_obj._check_indexes() 

70 

71 msg = index_not_found.format("invalid_index", esxport_obj.opts.url) 

72 assert str(exc_info.value) == msg 

73 

74 def test_size( 

75 self: Self, 

76 _: Any, 

77 esxport_obj: EsXport, 

78 ) -> None: 

79 """Arr, matey!. 

80 

81 Let's test if our search query be successful, with valid input parameters!. 

82 """ 

83 page_size = randint(100, 9999) 

84 esxport_obj.opts.scroll_size = page_size 

85 

86 esxport_obj._prepare_search_query() 

87 assert esxport_obj.search_args["size"] == page_size 

88 

89 def test_query(self: Self, _: Any, esxport_obj: EsXport) -> None: 

90 """Arr, matey!. 

91 

92 Let's test if our search query be successful, with valid input parameters!. 

93 """ 

94 expected_query: dict[str, Any] = {"query": {"match_all": {}}} 

95 esxport_obj.opts.query = expected_query 

96 

97 esxport_obj._prepare_search_query() 

98 assert esxport_obj.search_args["body"] == expected_query 

99 

100 def test_terminate_after(self: Self, _: Any, esxport_obj: EsXport) -> None: 

101 """Arr, matey!. 

102 

103 Let's test if our search query be successful, with valid input parameters!. 

104 """ 

105 random_max = self.random_number() 

106 esxport_obj.opts.max_results = random_max 

107 esxport_obj._prepare_search_query() 

108 assert esxport_obj.search_args["terminate_after"] == random_max 

109 

110 def test_sort( 

111 self: Self, 

112 _: Any, 

113 esxport_obj: EsXport, 

114 ) -> None: 

115 """Arr, matey!. 

116 

117 Let's test if our search query be successful, with valid input parameters!. 

118 """ 

119 random_sort = [{self.random_string(): "asc"}, {self.random_string(): "desc"}] 

120 esxport_obj.opts.sort = random_sort 

121 

122 esxport_obj._prepare_search_query() 

123 assert esxport_obj.search_args["sort"] == random_sort 

124 

125 def test_debug_option( 

126 self: Self, 

127 _: Any, 

128 esxport_obj: EsXport, 

129 caplog: pytest.LogCaptureFixture, 

130 ) -> None: 

131 """Arr, matey!. 

132 

133 Let's test if our search query be successful, with valid input parameters!. 

134 """ 

135 esxport_obj.opts.debug = True 

136 

137 esxport_obj._prepare_search_query() 

138 assert caplog.records[0].msg == using_indexes.format(indexes={", ".join(esxport_obj.opts.index_prefixes)}) 

139 assert caplog.records[1].msg.startswith("Using query") 

140 assert caplog.records[2].msg == output_fields.format(fields={", ".join(esxport_obj.opts.fields)}) 

141 assert caplog.records[3].msg == sorting_by.format(sort=esxport_obj.opts.sort) 

142 

143 def test_custom_output_fields(self: Self, _: Any, esxport_obj: EsXport) -> None: 

144 """Test if selection only some fields for the output works.""" 

145 random_strings = [self.random_string(10) for _ in range(5)] 

146 esxport_obj.opts.fields = random_strings 

147 esxport_obj._prepare_search_query() 

148 assert esxport_obj.search_args["_source_includes"] == ",".join(random_strings)