Skip to content

Commit c710f47

Browse files
authored
Resolve warnings emitted by tests (#667)
For the vast majority of warnings, I have simply added a skip, since we cannot remove tests for deprecated APIs. See: https://docs.python.org/3.6/library/warnings.html#temporarily-suppressing-warnings Additionally, to eliminate warnings caused by invalid test classes, I renamed two classes to avoid using the “TestNAME” pattern, as such names are automatically recognized as test classes.
1 parent 76e3722 commit c710f47

File tree

6 files changed

+68
-46
lines changed

6 files changed

+68
-46
lines changed

splunklib/results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def read(self, n=None):
148148

149149

150150
@deprecation.deprecated(
151-
details="Use the JSONResultsReader function instead in conjuction with the 'output_mode' query param set to 'json'"
151+
details="Use the JSONResultsReader function instead in conjunction with the 'output_mode' query param set to 'json'"
152152
)
153153
class ResultsReader:
154154
"""This class returns dictionaries and Splunk messages from an XML results

tests/integration/test_job.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from splunklib.binding import _log_duration, HTTPError
3131

3232
import pytest
33+
import warnings
3334

3435

3536
class TestUtilities(testlib.SDKTestCase):
@@ -446,22 +447,25 @@ def test_results_reader(self):
446447
test_dir = Path(__file__).parent
447448
data_file = test_dir / "data" / "results.xml"
448449
with io.open(str(data_file), mode="br") as input:
449-
reader = results.ResultsReader(input)
450-
self.assertFalse(reader.is_preview)
451-
N_results = 0
452-
N_messages = 0
453-
for r in reader:
454-
from collections import OrderedDict
455-
456-
self.assertTrue(
457-
isinstance(r, OrderedDict) or isinstance(r, results.Message)
458-
)
459-
if isinstance(r, OrderedDict):
460-
N_results += 1
461-
elif isinstance(r, results.Message):
462-
N_messages += 1
463-
self.assertEqual(N_results, 4999)
464-
self.assertEqual(N_messages, 2)
450+
with warnings.catch_warnings():
451+
warnings.simplefilter("ignore", DeprecationWarning)
452+
453+
reader = results.ResultsReader(input)
454+
self.assertFalse(reader.is_preview)
455+
N_results = 0
456+
N_messages = 0
457+
for r in reader:
458+
from collections import OrderedDict
459+
460+
self.assertTrue(
461+
isinstance(r, OrderedDict) or isinstance(r, results.Message)
462+
)
463+
if isinstance(r, OrderedDict):
464+
N_results += 1
465+
elif isinstance(r, results.Message):
466+
N_messages += 1
467+
self.assertEqual(N_results, 4999)
468+
self.assertEqual(N_messages, 2)
465469

466470
def test_results_reader_with_streaming_results(self):
467471
# Run jobs.export("search index=_internal | stats count",
@@ -470,21 +474,24 @@ def test_results_reader_with_streaming_results(self):
470474
test_dir = Path(__file__).parent
471475
data_file = test_dir / "data" / "streaming_results.xml"
472476
with io.open(str(data_file), "br") as input:
473-
reader = results.ResultsReader(input)
474-
N_results = 0
475-
N_messages = 0
476-
for r in reader:
477-
from collections import OrderedDict
478-
479-
self.assertTrue(
480-
isinstance(r, OrderedDict) or isinstance(r, results.Message)
481-
)
482-
if isinstance(r, OrderedDict):
483-
N_results += 1
484-
elif isinstance(r, results.Message):
485-
N_messages += 1
486-
self.assertEqual(N_results, 3)
487-
self.assertEqual(N_messages, 3)
477+
with warnings.catch_warnings():
478+
warnings.simplefilter("ignore", DeprecationWarning)
479+
480+
reader = results.ResultsReader(input)
481+
N_results = 0
482+
N_messages = 0
483+
for r in reader:
484+
from collections import OrderedDict
485+
486+
self.assertTrue(
487+
isinstance(r, OrderedDict) or isinstance(r, results.Message)
488+
)
489+
if isinstance(r, OrderedDict):
490+
N_results += 1
491+
elif isinstance(r, results.Message):
492+
N_messages += 1
493+
self.assertEqual(N_results, 3)
494+
self.assertEqual(N_messages, 3)
488495

489496
def test_xmldtd_filter(self):
490497
s = results._XMLDTDFilter(

tests/integration/test_results.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from time import sleep
2121
from tests import testlib
2222
from splunklib import results
23+
import warnings
2324

2425

2526
class ResultsTestCase(testlib.SDKTestCase):
@@ -169,9 +170,11 @@ def test_read_raw_field_with_segmentation(self):
169170
self.assert_parsed_results_equals(xml_text, expected_results)
170171

171172
def assert_parsed_results_equals(self, xml_text, expected_results):
172-
results_reader = results.ResultsReader(BytesIO(xml_text.encode("utf-8")))
173-
actual_results = list(results_reader)
174-
self.assertEqual(expected_results, actual_results)
173+
with warnings.catch_warnings():
174+
warnings.simplefilter("ignore", DeprecationWarning)
175+
results_reader = results.ResultsReader(BytesIO(xml_text.encode("utf-8")))
176+
actual_results = list(results_reader)
177+
self.assertEqual(expected_results, actual_results)
175178

176179

177180
if __name__ == "__main__":

tests/unit/searchcommands/test_decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
@Configuration()
34-
class TestSearchCommand(SearchCommand):
34+
class SearchCommandForTests(SearchCommand):
3535
boolean = Option(
3636
doc="""
3737
**Syntax:** **boolean=***<value>*
@@ -399,7 +399,7 @@ def test_option(self):
399399
'show_configuration="f"',
400400
]
401401

402-
command = TestSearchCommand()
402+
command = SearchCommandForTests()
403403
options = command.options
404404

405405
options.reset()

tests/unit/searchcommands/test_internals_v2.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import random
2121
import sys
22+
import warnings
2223

2324
import pytest
2425
from sys import float_info
@@ -185,10 +186,14 @@ def test_record_writer_with_random_data(self, save_recording=False):
185186
writer.write_metric(name, metric)
186187

187188
self.assertEqual(writer._chunk_count, 0)
188-
self.assertEqual(writer._record_count, 31)
189+
190+
with warnings.catch_warnings():
191+
warnings.simplefilter("ignore", PendingDeprecationWarning)
192+
self.assertEqual(writer._total_record_count, 0)
193+
self.assertEqual(writer._record_count, 31)
194+
189195
self.assertEqual(writer.pending_record_count, 31)
190196
self.assertGreater(writer._buffer.tell(), 0)
191-
self.assertEqual(writer._total_record_count, 0)
192197
self.assertEqual(writer.committed_record_count, 0)
193198
fieldnames.sort()
194199
writer._fieldnames.sort()
@@ -204,12 +209,15 @@ def test_record_writer_with_random_data(self, save_recording=False):
204209

205210
writer.flush(finished=True)
206211

212+
with warnings.catch_warnings():
213+
warnings.simplefilter("ignore", PendingDeprecationWarning)
214+
self.assertEqual(writer._record_count, 0)
215+
self.assertEqual(writer._total_record_count, 31)
216+
207217
self.assertEqual(writer._chunk_count, 1)
208-
self.assertEqual(writer._record_count, 0)
209218
self.assertEqual(writer.pending_record_count, 0)
210219
self.assertEqual(writer._buffer.tell(), 0)
211220
self.assertEqual(writer._buffer.getvalue(), "")
212-
self.assertEqual(writer._total_record_count, 31)
213221
self.assertEqual(writer.committed_record_count, 31)
214222

215223
self.assertRaises(AssertionError, writer.flush, finished=True, partial=True)

tests/unit/searchcommands/test_search_command.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import os
2222
import logging
23+
import warnings
2324

2425
from io import TextIOWrapper
2526

@@ -49,7 +50,7 @@ def build_command_input(getinfo_metadata, execute_metadata, execute_body):
4950

5051

5152
@Configuration()
52-
class TestCommand(SearchCommand):
53+
class CommandForTests(SearchCommand):
5354
required_option_1 = Option(require=True)
5455
required_option_2 = Option(require=True)
5556

@@ -160,7 +161,7 @@ def test_process_scpv2(self):
160161

161162
ifile = build_command_input(getinfo_metadata, execute_metadata, execute_body)
162163

163-
command = TestCommand()
164+
command = CommandForTests()
164165
result = BytesIO()
165166
argv = ["some-external-search-command.py"]
166167

@@ -184,8 +185,8 @@ def test_process_scpv2(self):
184185
self.assertEqual(command.required_option_2, "value_2")
185186

186187
expected = (
187-
"chunked 1.0,68,0\n"
188-
'{"inspector":{"messages":[["INFO","test command configuration: "]]}}'
188+
"chunked 1.0,79,0\n"
189+
'{"inspector":{"messages":[["INFO","commandfortests command configuration: "]]}}'
189190
"chunked 1.0,17,32\n"
190191
'{"finished":true}test,__mv_test\r\n'
191192
"data,\r\n"
@@ -206,7 +207,10 @@ def test_process_scpv2(self):
206207
self.assertEqual([], command.fieldnames)
207208

208209
command_metadata = command.metadata
209-
input_header = command.input_header
210+
211+
with warnings.catch_warnings():
212+
warnings.simplefilter("ignore", DeprecationWarning)
213+
input_header = command.input_header
210214

211215
self.assertIsNone(input_header["allowStream"])
212216
self.assertEqual(

0 commit comments

Comments
 (0)