Skip to content

Commit 64de6a4

Browse files
authored
Merge pull request #454 from splunk/py3-testcases-refractor
Py3 testcases refractor
2 parents 739a94a + 5c90746 commit 64de6a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+685
-963
lines changed

tests/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/modularinput/__init__.py

Whitespace-only changes.

tests/modularinput/modularinput_testlib.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
# under the License.
1616

1717
# Utility file for unit tests, import common functions and modules
18-
from __future__ import absolute_import
19-
try:
20-
import unittest2 as unittest
21-
except ImportError:
22-
import unittest
18+
import unittest
2319
import sys, os
2420
import io
2521

tests/modularinput/test_event.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17-
from __future__ import absolute_import
1817

1918
import sys
2019

2120
import pytest
2221

23-
from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
22+
from tests.modularinput.modularinput_testlib import xml_compare, data_open
2423
from splunklib.modularinput.event import Event, ET
2524
from splunklib.modularinput.event_writer import EventWriter
2625

@@ -99,7 +98,7 @@ def test_writing_events_on_event_writer(capsys):
9998
first_out_part = captured.out
10099

101100
with data_open("data/stream_with_one_event.xml") as data:
102-
found = ET.fromstring("%s</stream>" % first_out_part)
101+
found = ET.fromstring(f"{first_out_part}</stream>")
103102
expected = ET.parse(data).getroot()
104103

105104
assert xml_compare(expected, found)

tests/modularinput/test_input_definition.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17-
from __future__ import absolute_import
1817
from tests.modularinput.modularinput_testlib import unittest, data_open
1918
from splunklib.modularinput.input_definition import InputDefinition
2019

20+
2121
class InputDefinitionTestCase(unittest.TestCase):
2222

2323
def test_parse_inputdef_with_zero_inputs(self):
@@ -72,5 +72,6 @@ def test_attempt_to_parse_malformed_input_definition_will_throw_exception(self):
7272
with self.assertRaises(ValueError):
7373
found = InputDefinition.parse(data_open("data/conf_with_invalid_inputs.xml"))
7474

75+
7576
if __name__ == "__main__":
76-
unittest.main()
77+
unittest.main()

tests/modularinput/test_scheme.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515

16-
from __future__ import absolute_import
16+
import xml.etree.ElementTree as ET
1717
from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
1818
from splunklib.modularinput.scheme import Scheme
1919
from splunklib.modularinput.argument import Argument
2020

21-
try:
22-
import xml.etree.cElementTree as ET
23-
except ImportError:
24-
import xml.etree.ElementTree as ET
2521

2622
class SchemeTest(unittest.TestCase):
2723
def test_generate_xml_from_scheme_with_default_values(self):
@@ -40,7 +36,7 @@ def test_generate_xml_from_scheme(self):
4036
some arguments added matches what we expect."""
4137

4238
scheme = Scheme("abcd")
43-
scheme.description = u"쎼 and 쎶 and <&> für"
39+
scheme.description = "쎼 and 쎶 and <&> für"
4440
scheme.streaming_mode = Scheme.streaming_mode_simple
4541
scheme.use_external_validation = "false"
4642
scheme.use_single_instance = "true"
@@ -50,7 +46,7 @@ def test_generate_xml_from_scheme(self):
5046

5147
arg2 = Argument(
5248
name="arg2",
53-
description=u"쎼 and 쎶 and <&> für",
49+
description="쎼 and 쎶 and <&> für",
5450
validation="is_pos_int('some_name')",
5551
data_type=Argument.data_type_number,
5652
required_on_edit=True,
@@ -69,7 +65,7 @@ def test_generate_xml_from_scheme_with_arg_title(self):
6965
some arguments added matches what we expect. Also sets the title on an argument."""
7066

7167
scheme = Scheme("abcd")
72-
scheme.description = u"쎼 and 쎶 and <&> für"
68+
scheme.description = "쎼 and 쎶 and <&> für"
7369
scheme.streaming_mode = Scheme.streaming_mode_simple
7470
scheme.use_external_validation = "false"
7571
scheme.use_single_instance = "true"
@@ -79,7 +75,7 @@ def test_generate_xml_from_scheme_with_arg_title(self):
7975

8076
arg2 = Argument(
8177
name="arg2",
82-
description=u"쎼 and 쎶 and <&> für",
78+
description="쎼 and 쎶 and <&> für",
8379
validation="is_pos_int('some_name')",
8480
data_type=Argument.data_type_number,
8581
required_on_edit=True,
@@ -113,7 +109,7 @@ def test_generate_xml_from_argument(self):
113109

114110
argument = Argument(
115111
name="some_name",
116-
description=u"쎼 and 쎶 and <&> für",
112+
description="쎼 and 쎶 and <&> für",
117113
validation="is_pos_int('some_name')",
118114
data_type=Argument.data_type_boolean,
119115
required_on_edit="true",

tests/modularinput/test_script.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import sys
22

3+
import io
4+
import xml.etree.ElementTree as ET
35
from splunklib.client import Service
46
from splunklib.modularinput import Script, EventWriter, Scheme, Argument, Event
5-
import io
67

78
from splunklib.modularinput.utils import xml_compare
89
from tests.modularinput.modularinput_testlib import data_open
910

10-
try:
11-
import xml.etree.cElementTree as ET
12-
except ImportError:
13-
import xml.etree.ElementTree as ET
1411

1512
TEST_SCRIPT_PATH = "__IGNORED_SCRIPT_PATH__"
1613

@@ -51,7 +48,7 @@ def test_scheme_properly_generated_by_script(capsys):
5148
class NewScript(Script):
5249
def get_scheme(self):
5350
scheme = Scheme("abcd")
54-
scheme.description = u"\uC3BC and \uC3B6 and <&> f\u00FCr"
51+
scheme.description = "\uC3BC and \uC3B6 and <&> f\u00FCr"
5552
scheme.streaming_mode = scheme.streaming_mode_simple
5653
scheme.use_external_validation = False
5754
scheme.use_single_instance = True
@@ -60,7 +57,7 @@ def get_scheme(self):
6057
scheme.add_argument(arg1)
6158

6259
arg2 = Argument("arg2")
63-
arg2.description = u"\uC3BC and \uC3B6 and <&> f\u00FCr"
60+
arg2.description = "\uC3BC and \uC3B6 and <&> f\u00FCr"
6461
arg2.data_type = Argument.data_type_number
6562
arg2.required_on_create = True
6663
arg2.required_on_edit = True
@@ -208,7 +205,7 @@ def test_service_property(capsys):
208205
# Override abstract methods
209206
class NewScript(Script):
210207
def __init__(self):
211-
super(NewScript, self).__init__()
208+
super().__init__()
212209
self.authority_uri = None
213210

214211
def get_scheme(self):

tests/modularinput/test_validation_definition.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17-
from __future__ import absolute_import
17+
1818
from tests.modularinput.modularinput_testlib import unittest, data_open
1919
from splunklib.modularinput.validation_definition import ValidationDefinition
2020

21+
2122
class ValidationDefinitionTestCase(unittest.TestCase):
2223
def test_validation_definition_parse(self):
2324
"""Check that parsing produces expected result"""
@@ -42,5 +43,6 @@ def test_validation_definition_parse(self):
4243

4344
self.assertEqual(expected, found)
4445

46+
4547
if __name__ == "__main__":
46-
unittest.main()
48+
unittest.main()

tests/searchcommands/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
# License for the specific language governing permissions and limitations
1616
# under the License.
1717

18-
from __future__ import absolute_import, division, print_function, unicode_literals
19-
20-
from sys import version_info as python_version
21-
2218
from os import path
2319
import logging
2420

tests/searchcommands/chunked_data_stream.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from splunklib import six
88

99

10-
class Chunk(object):
10+
class Chunk:
1111
def __init__(self, version, meta, data):
1212
self.version = six.ensure_str(version)
1313
self.meta = json.loads(meta)
@@ -16,21 +16,21 @@ def __init__(self, version, meta, data):
1616
dialect=dialect)
1717

1818

19-
class ChunkedDataStreamIter(collections.Iterator):
19+
class ChunkedDataStreamIter(collections.abc.Iterator):
2020
def __init__(self, chunk_stream):
2121
self.chunk_stream = chunk_stream
2222

2323
def __next__(self):
24-
return self.next()
24+
return next(self)
2525

26-
def next(self):
26+
def __next__(self):
2727
try:
2828
return self.chunk_stream.read_chunk()
2929
except EOFError:
3030
raise StopIteration
3131

3232

33-
class ChunkedDataStream(collections.Iterable):
33+
class ChunkedDataStream(collections.abc.Iterable):
3434
def __iter__(self):
3535
return ChunkedDataStreamIter(self)
3636

@@ -91,7 +91,7 @@ def _build_data_csv(data):
9191

9292
headers = set()
9393
for datum in data:
94-
headers.update(datum.keys())
94+
headers.update(list(datum.keys()))
9595
writer = csv.DictWriter(csvout, headers,
9696
dialect=splunklib.searchcommands.internals.CsvDialect)
9797
writer.writeheader()

0 commit comments

Comments
 (0)