Skip to content

Commit

Permalink
Added output checking in test_print_insts_as_table()
Browse files Browse the repository at this point in the history
Details:

* test_print_insts_as_table() was using the simplified_test_function
  decorator which does not allow additional fixture arguments such
  as capsys for capturing stdout. As a result, thi testcase did
  not check the output of the tested print function.
  Attempts were made to extend the simplified_test_function decorator
  in order to support additional arguments, but they were all
  uncuccessful.
  This change removes the use of the simplified_test_function decorator
  from  test_print_insts_as_table() and passes the parametrization
  directly, so the capsys fixture could be added.
  Checking of the output was added, and the expected output in the
  testcases needed to be adjusted.

* In order to have reliable order of keys in references, the
  _scalar_value_tomof() function has been changed to invoke
  to_wbem_uri() with the new format 'sorted' introduced in pywbem 1.0.0.
  If that format is not supported, pywbem >=0.13,<1.0 raises ValueError,
  and in that case, format 'standard' is used.
  Pywbem <0.13 ignores format 'sorted' and returns the standard format.

Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
  • Loading branch information
andy-maier committed Jun 15, 2020
1 parent f39ee84 commit 2a82a2e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 35 deletions.
6 changes: 5 additions & 1 deletion pywbemtools/pywbemcli/_cimvalueformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ def _scalar_value_tomof(value, type, indent, maxline, line_pos=0, end_space=0,
val = six.text_type(value)
return _mofstr(val, indent, maxline, line_pos, end_space, avoid_splits)
elif type == 'reference':
val = value.to_wbem_uri()
try:
# requires pywbem >= 1.0.0
val = value.to_wbem_uri(format='sorted')
except ValueError:
val = value.to_wbem_uri(format='standard')
return _mofstr(val, indent, maxline, line_pos, end_space, avoid_splits)
elif isinstance(value, (CIMFloat, CIMInt, int, _Longint)):
val = six.text_type(value)
Expand Down
92 changes: 58 additions & 34 deletions tests/unit/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from datetime import datetime
import unittest
from packaging.version import parse as parse_version
import click
from mock import patch
import pytest
Expand All @@ -34,7 +35,7 @@

from pywbem import CIMClass, CIMProperty, CIMQualifier, CIMInstance, \
CIMQualifierDeclaration, CIMInstanceName, Uint8, Uint32, Uint64, Sint32, \
CIMDateTime, CIMClassName
CIMDateTime, CIMClassName, __version__

from tests.unit.pytest_extensions import simplified_test_function

Expand All @@ -58,6 +59,10 @@
FAIL = False # Any test currently FAILING or not tested yet
SKIP = False # mark tests that are to be skipped.

_PYWBEM_VERSION = parse_version(__version__)
# pywbem 1.0.0 beta or final (but not dev)
PYWBEM_1_0_0_NON_DEV = _PYWBEM_VERSION.release >= (1, 0, 0) and \
_PYWBEM_VERSION.dev is None

TESTCASES_ISCLASSNAME = [
# TESTCASES for is_classname
Expand Down Expand Up @@ -2681,7 +2686,7 @@ def test_format_insts_as_rows(testcase, args, kwargs, exp_rtn):
# * kwargs: Keyword arguments for the test function:
# * args: CIMInstance(s) object to be tested, col_width field, ouput_fmt
# * kwargs: Dict of input args for tocimxml().
# * exp_tbl: Expected Table to be output.
# * exp_out: Expected output on stdout.
# * exp_exc_types: Expected exception type(s), or None.
# * exp_warn_types: Expected warning type(s), or None.
# * condition: Boolean condition for testcase to run, or 'pdb' for debugger
Expand All @@ -2692,27 +2697,33 @@ def test_format_insts_as_rows(testcase, args, kwargs, exp_rtn):
dict(
args=([simple_instance()], None, 'simple'),
kwargs=dict(),
exp_tbl=[
'Pbt Pbf Pint32 Pint64 Pdt '
'Pstr1\n'
'----- ----- -------- -------- --------------------- '
'-------------\n'
'true false 99 9999 "20140922104920.5247"'
' "Test String"\n'
' "89+000"\n'
],
exp_stdout="""\
Instances: CIM_Foo
Pbt Pbf Pint32 Pint64 Pdt Pstr1
----- ----- -------- -------- ----------------------- -------------
true false 99 9999 "20140922104920.524789" "Test String"
"+000"
""",
),
None, None, True, ),

None, None, True
),
(
"Verify print of simple instance to table with col limit",
dict(
args=([simple_instance2()], 80, 'simple'),
kwargs=dict(),
exp_tbl=[
["true", "false", "99", '"Test String"']],
exp_stdout="""\
Instances: CIM_Foo
Pbt Pbf Puint32 Psint32 Pint64 Pdt Pstr1
----- ----- ---------- ----------- -------- --------- --------
true false 4294967295 -2147483648 9999 "2014092" "Test "
"2104920" "String"
".524789"
"+000"
""",
),
None, None, True, ),
None, None, True
),
(
"Verify print of instance with reference property",
dict(
Expand All @@ -2727,38 +2738,51 @@ def test_format_insts_as_rows(testcase, args, kwargs, exp_rtn):
k2=32)))])],
80, 'simple'),
kwargs=dict(),
exp_tbl=[
["/:REF_CLN.k2=32,k1=\"v1\""]],
exp_stdout="""\
Instances: CIM_Foo
P
---------------------------
"/:REF_CLN.k1=\\"v1\\",k2=32"
""",
),
None, None, True, ),
None, None, PYWBEM_1_0_0_NON_DEV,
),
]


@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_PRINT_INSTANCE_AS_TABLE)
@simplified_test_function
def test_print_insts_as_table(testcase, args, kwargs, exp_tbl):
def test_print_insts_as_table(
desc, kwargs, exp_exc_types, exp_warn_types, condition, capsys):
"""
Test the output of the print_insts_as_table function. This primarily
tests for overall format and the ability of the function to output to
stdout. The previous test tests the row formatting and handling of
multiple instances.
"""
# TODO fix simplified_test_function so we can use so we capture output.
# capsys in a builtin fixture that must be passed to this function.
# Currently the simplified_test_function does not allow any other
# parameters so we cannot use pytest capsys
mock_echo_func = 'pywbemtools.pywbemcli.click.echo'
with patch(mock_echo_func):
# The code to be tested
_print_instances_as_table(*args, **kwargs)
# stdout, stderr = capsys.readouterr()
# Ensure that exceptions raised in the remainder of this function
# are not mistaken as expected exceptions
assert testcase.exp_exc_types is None
if not condition:
pytest.skip("Testcase condition not satisfied")

# This logic only supports successful testcases without warnings
assert exp_exc_types is None
assert exp_warn_types is None

# assertexp_tbl, stdout, testcase.desc)
args = kwargs['args']
kwargs_ = kwargs['kwargs']
exp_stdout = kwargs['exp_stdout']

# The code to be tested
_print_instances_as_table(*args, **kwargs_)

stdout, stderr = capsys.readouterr()
assert exp_stdout == stdout, \
"Unexpected output in test case: {}\n" \
"Actual:\n" \
"{}\n" \
"Expected:\n" \
"{}\n" \
"End\n".format(desc, stdout, exp_stdout)


# TODO Test compare and failure in compare_obj
Expand Down

0 comments on commit 2a82a2e

Please sign in to comment.