Skip to content

Py3 testcases refractor #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion tests/__init__.py

This file was deleted.

Empty file removed tests/modularinput/__init__.py
Empty file.
6 changes: 1 addition & 5 deletions tests/modularinput/modularinput_testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
# under the License.

# Utility file for unit tests, import common functions and modules
from __future__ import absolute_import
try:
import unittest2 as unittest
except ImportError:
import unittest
import unittest
import sys, os
import io

Expand Down
5 changes: 2 additions & 3 deletions tests/modularinput/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import

import sys

import pytest

from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
from tests.modularinput.modularinput_testlib import xml_compare, data_open
from splunklib.modularinput.event import Event, ET
from splunklib.modularinput.event_writer import EventWriter

Expand Down Expand Up @@ -99,7 +98,7 @@ def test_writing_events_on_event_writer(capsys):
first_out_part = captured.out

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

assert xml_compare(expected, found)
Expand Down
5 changes: 3 additions & 2 deletions tests/modularinput/test_input_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import
from tests.modularinput.modularinput_testlib import unittest, data_open
from splunklib.modularinput.input_definition import InputDefinition


class InputDefinitionTestCase(unittest.TestCase):

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


if __name__ == "__main__":
unittest.main()
unittest.main()
16 changes: 6 additions & 10 deletions tests/modularinput/test_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import
import xml.etree.ElementTree as ET
from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
from splunklib.modularinput.scheme import Scheme
from splunklib.modularinput.argument import Argument

try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET

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

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

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

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

arg2 = Argument(
name="arg2",
description=u"쎼 and 쎶 and <&> für",
description="쎼 and 쎶 and <&> für",
validation="is_pos_int('some_name')",
data_type=Argument.data_type_number,
required_on_edit=True,
Expand Down Expand Up @@ -113,7 +109,7 @@ def test_generate_xml_from_argument(self):

argument = Argument(
name="some_name",
description=u"쎼 and 쎶 and <&> für",
description="쎼 and 쎶 and <&> für",
validation="is_pos_int('some_name')",
data_type=Argument.data_type_boolean,
required_on_edit="true",
Expand Down
13 changes: 5 additions & 8 deletions tests/modularinput/test_script.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import sys

import io
import xml.etree.ElementTree as ET
from splunklib.client import Service
from splunklib.modularinput import Script, EventWriter, Scheme, Argument, Event
import io

from splunklib.modularinput.utils import xml_compare
from tests.modularinput.modularinput_testlib import data_open

try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET

TEST_SCRIPT_PATH = "__IGNORED_SCRIPT_PATH__"

Expand Down Expand Up @@ -51,7 +48,7 @@ def test_scheme_properly_generated_by_script(capsys):
class NewScript(Script):
def get_scheme(self):
scheme = Scheme("abcd")
scheme.description = u"\uC3BC and \uC3B6 and <&> f\u00FCr"
scheme.description = "\uC3BC and \uC3B6 and <&> f\u00FCr"
scheme.streaming_mode = scheme.streaming_mode_simple
scheme.use_external_validation = False
scheme.use_single_instance = True
Expand All @@ -60,7 +57,7 @@ def get_scheme(self):
scheme.add_argument(arg1)

arg2 = Argument("arg2")
arg2.description = u"\uC3BC and \uC3B6 and <&> f\u00FCr"
arg2.description = "\uC3BC and \uC3B6 and <&> f\u00FCr"
arg2.data_type = Argument.data_type_number
arg2.required_on_create = True
arg2.required_on_edit = True
Expand Down Expand Up @@ -208,7 +205,7 @@ def test_service_property(capsys):
# Override abstract methods
class NewScript(Script):
def __init__(self):
super(NewScript, self).__init__()
super().__init__()
self.authority_uri = None

def get_scheme(self):
Expand Down
6 changes: 4 additions & 2 deletions tests/modularinput/test_validation_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import

from tests.modularinput.modularinput_testlib import unittest, data_open
from splunklib.modularinput.validation_definition import ValidationDefinition


class ValidationDefinitionTestCase(unittest.TestCase):
def test_validation_definition_parse(self):
"""Check that parsing produces expected result"""
Expand All @@ -42,5 +43,6 @@ def test_validation_definition_parse(self):

self.assertEqual(expected, found)


if __name__ == "__main__":
unittest.main()
unittest.main()
4 changes: 0 additions & 4 deletions tests/searchcommands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import, division, print_function, unicode_literals

from sys import version_info as python_version

from os import path
import logging

Expand Down
12 changes: 6 additions & 6 deletions tests/searchcommands/chunked_data_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from splunklib import six


class Chunk(object):
class Chunk:
def __init__(self, version, meta, data):
self.version = six.ensure_str(version)
self.meta = json.loads(meta)
Expand All @@ -16,21 +16,21 @@ def __init__(self, version, meta, data):
dialect=dialect)


class ChunkedDataStreamIter(collections.Iterator):
class ChunkedDataStreamIter(collections.abc.Iterator):
def __init__(self, chunk_stream):
self.chunk_stream = chunk_stream

def __next__(self):
return self.next()
return next(self)

def next(self):
def __next__(self):
try:
return self.chunk_stream.read_chunk()
except EOFError:
raise StopIteration


class ChunkedDataStream(collections.Iterable):
class ChunkedDataStream(collections.abc.Iterable):
def __iter__(self):
return ChunkedDataStreamIter(self)

Expand Down Expand Up @@ -91,7 +91,7 @@ def _build_data_csv(data):

headers = set()
for datum in data:
headers.update(datum.keys())
headers.update(list(datum.keys()))
writer = csv.DictWriter(csvout, headers,
dialect=splunklib.searchcommands.internals.CsvDialect)
writer.writeheader()
Expand Down
32 changes: 12 additions & 20 deletions tests/searchcommands/test_builtin_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import absolute_import, division, print_function, unicode_literals

from splunklib.six.moves import cStringIO as StringIO
try:
from unittest2 import main, TestCase
except ImportError:
from unittest import main, TestCase

import os
import sys
import logging

from unittest import main, TestCase
import pytest
from splunklib.six.moves import cStringIO as StringIO


from splunklib.searchcommands import environment
from splunklib.searchcommands.decorators import Configuration
Expand Down Expand Up @@ -117,18 +113,18 @@ def test_logging_configuration(self):
except ValueError:
pass
except BaseException as e:
self.fail('Expected ValueError, but {} was raised'.format(type(e)))
self.fail(f'Expected ValueError, but {type(e)} was raised')
else:
self.fail('Expected ValueError, but logging_configuration={}'.format(command.logging_configuration))
self.fail(f'Expected ValueError, but logging_configuration={command.logging_configuration}')

try:
command.logging_configuration = os.path.join(package_directory, 'non-existent.logging.conf')
except ValueError:
pass
except BaseException as e:
self.fail('Expected ValueError, but {} was raised'.format(type(e)))
self.fail(f'Expected ValueError, but {type(e)} was raised')
else:
self.fail('Expected ValueError, but logging_configuration={}'.format(command.logging_configuration))
self.fail(f'Expected ValueError, but logging_configuration={command.logging_configuration}')

def test_logging_level(self):

Expand All @@ -146,7 +142,7 @@ def test_logging_level(self):
self.assertEqual(warning, command.logging_level)

for level in level_names():
if type(level) is int:
if isinstance(level, int):
command.logging_level = level
level_name = logging.getLevelName(level)
self.assertEqual(command.logging_level, warning if level_name == notset else level_name)
Expand All @@ -171,9 +167,9 @@ def test_logging_level(self):
except ValueError:
pass
except BaseException as e:
self.fail('Expected ValueError, but {} was raised'.format(type(e)))
self.fail(f'Expected ValueError, but {type(e)} was raised')
else:
self.fail('Expected ValueError, but logging_level={}'.format(command.logging_level))
self.fail(f'Expected ValueError, but logging_level={command.logging_level}')

self.assertEqual(command.logging_level, current_value)

Expand Down Expand Up @@ -211,13 +207,9 @@ def _test_boolean_option(self, option):
except ValueError:
pass
except BaseException as error:
self.fail('Expected ValueError when setting {}={}, but {} was raised'.format(
option.name, repr(value), type(error)))
self.fail(f'Expected ValueError when setting {option.name}={repr(value)}, but {type(error)} was raised')
else:
self.fail('Expected ValueError, but {}={} was accepted.'.format(
option.name, repr(option.fget(command))))

return
self.fail(f'Expected ValueError, but {option.name}={repr(option.fget(command))} was accepted.')


if __name__ == "__main__":
Expand Down
Loading