コード例 #1
0
def test_format_type_error():
    """
    It should raise a TypeError if the parameter passed to the method is not a dictionary.
    """
    csv_formatter = CsvFormatter({})
    test_cases = (
        1,
        'one',
        None,
        [],
    )
    for test_case in test_cases:
        try:
            csv_formatter.format(test_case)
            msg = 'The method must raise a TypeError if the supplied parameter is not a dictionary'
            assert False, format_msg(msg)
        except TypeError as e:
            if not e.args:
                msg = 'It is expected that the TypeError exception include a descriptive message, but none was provided'
                assert False, format_msg(msg)

            expected_message = 'The record parameter must be a dictionary'
            msg = 'Your message:  {:s} != Expected message {:s}'.format(
                e.args[0], expected_message)
            assert e.args[0] == expected_message, format_msg(msg)
コード例 #2
0
def test_format_with_all_exceptions():
    """
    It should return a tuple with a list of failed formats as the first element and the formatted record as the second
    if there are any exceptions raised during formatting.
    """
    format_map = {
        'column1': 'integer',
        'column2': 'thousands_integer',
        'column3': 'us_currency',
    }

    csv_formatter = CsvFormatter(format_map)
    record = {
        'column1': 'so',
        'column2': 'many',
        'column3': 'errors',
    }
    result = csv_formatter.format(record)
    msg = 'The result must be a tuple with a list of failed columns and the modified record'
    assert isinstance(result, tuple), format_msg(msg)

    msg = 'The list of columns does not match the expected list'
    assert result[0] == ['column1', 'column2', 'column3'], format_msg(msg)

    msg = 'The record should not have been modified'
    assert result[1] == record, format_msg(msg)
コード例 #3
0
def test_preserve_original():
    """
    It should not modify the source record.
    """
    format_map = {
        'column1': 'integer',
        'column2': 'thousands_integer',
        'column3': 'us_currency',
    }

    csv_formatter = CsvFormatter(format_map)
    record = {
        'column1': '012345',
        'column2': 'many',
        'column3': '1.23',
    }
    original_record = record.copy()
    expected_record = {
        'column1': '12345',
        'column2': 'many',
        'column3': '$1.23',
    }
    result = csv_formatter.format(record)
    msg = 'The format method must not modify the original record'
    assert record == original_record, format_msg(msg)
コード例 #4
0
def test_format_full_success_with_unmapped():
    """
    It should return an empty list of failures and a fully modified record when no exceptions.
    """
    format_map = {
        'column1': 'integer',
        'column2': 'thousands_integer',
        'column3': 'us_currency',
    }

    csv_formatter = CsvFormatter(format_map)
    record = {
        'column1': '012345',
        'column2': '0654321',
        'column3': '1.23',
        'column4': 'woo hoo!',
    }
    expected_record = {
        'column1': '12345',
        'column2': '654,321',
        'column3': '$1.23',
        'column4': 'woo hoo!',
    }
    result = csv_formatter.format(record)
    msg = 'The result must be a tuple with an empty list of failed columns and the modified record'
    assert isinstance(result, tuple), format_msg(msg)

    msg = 'The list of columns is empty'
    assert result[0] == [], format_msg(msg)

    msg = 'The formatted record does not match the expected formatting'
    assert result[1] == expected_record, format_msg(msg)
コード例 #5
0
def test_format_with_one_exception():
    """
    It should format the fields that it can.
    """
    format_map = {
        'column1': 'integer',
        'column2': 'thousands_integer',
        'column3': 'us_currency',
    }

    csv_formatter = CsvFormatter(format_map)
    record = {
        'column1': '012345',
        'column2': 'many',
        'column3': '1.23',
    }
    expected_record = {
        'column1': '12345',
        'column2': 'many',
        'column3': '$1.23',
    }
    result = csv_formatter.format(record)
    msg = 'The result must be a tuple with a list of failed columns and the modified record'
    assert isinstance(result, tuple), format_msg(msg)

    msg = 'The list of columns does not match the expected list'
    assert result[0] == ['column2'], format_msg(msg)

    msg = 'The formatted record does not match the expected formatting'
    assert result[1] == expected_record, format_msg(msg)
コード例 #6
0
def test_fmt_thousands_integer():
    """
    It should return a value formatted as an integer with commas grouping thousands.
    """
    csv_formatter = CsvFormatter({})
    expected_result = '12,345'
    result = csv_formatter._fmt_thousands_integer('12345')
    msg = 'The _fmt_thousands_integer method must return a formatted string, not None'
    assert result is not None, format_msg(msg)
    msg = 'The expected result was not produced:  Your result: {:s} != Expected result: {:s}'.format(
        result, expected_result)
    assert result == expected_result, format_msg(msg)
コード例 #7
0
def test_fmt_default_success():
    """
    It should return the value it was provided.
    """
    csv_formatter = CsvFormatter({})
    msg = 'The _fmt_default method should return its input'
    test_cases = (
        '123',
        'onetwothree',
    )
    for test_case in test_cases:
        assert csv_formatter._fmt_default(test_case) == test_case, format_msg(
            msg)
コード例 #8
0
def test_fmt_thousands_us_currency_success():
    """
    It should return a value formatted as us currency - with commas
    """
    csv_formatter = CsvFormatter({})
    expected_result = '$12,345.00'
    test_cases = ('12345', '12345.000')
    for test_case in test_cases:
        result = csv_formatter._fmt_thousands_us_currency(test_case)
        msg = 'The _fmt_thousands_us_currency method must return a formatted string, not None'
        assert result is not None, format_msg(msg)
        msg = 'The expected result was not produced:  Your result: {:s} != Expected result: {:s}'.format(
            result, expected_result)
        assert result == expected_result, format_msg(msg)
コード例 #9
0
def test_has_format_method():
    """
    It should have an __exec__ method
    """
    csv_formatter = CsvFormatter({})
    msg = 'The class must have a method named format'
    assert hasattr(csv_formatter, 'format'), format_msg(msg)
コード例 #10
0
def test_check_method_exists():
    """
    It should have a method named _verify_map.
    """
    csv_formatter = CsvFormatter({})
    msg = 'It must have a method named _verify_map.  Please refer to the tasks and requirements.'
    assert hasattr(csv_formatter, '_verify_map'), format_msg(msg)
コード例 #11
0
def test_has_fmt_thousands_integer():
    """
    It should have a method _fmt_thousands_integer
    """
    csv_formatter = CsvFormatter({})
    msg = 'The class must have a method named _fmt_thousands_integer'
    assert hasattr(csv_formatter, '_fmt_thousands_integer'), format_msg(msg)
コード例 #12
0
def test_has_fmt_us_currency():
    """
    It should have a method named _fmt_us_currency
    """
    csv_formatter = CsvFormatter({})
    msg = 'The class must have a method named _fmt_us_currency'
    assert hasattr(csv_formatter, '_fmt_us_currency'), format_msg(msg)
コード例 #13
0
def test_has_fmt_default():
    """
    It should have a method named _fmt_default.
    """
    csv_formatter = CsvFormatter({})
    msg = 'The class must have a method named _fmt_default to implement the default format specifier'
    assert hasattr(csv_formatter, '_fmt_default'), format_msg(msg)
コード例 #14
0
def test_init_type_error():
    """
    It should override the __init__ method and validate the format_map.
    """
    for bad_type in (1, '', [], None):
        try:
            csv_formatter = CsvFormatter(bad_type)
            msg = 'The __init__ method must raise a TypeError if the parameter is not a dictionary instance'
            assert False, format_msg(msg)
        except TypeError as e:
            if not e.args:
                msg = 'It is expected that a message be provided with the TypeError exception, but one was not provided'
                assert False, format_msg(msg)
            if e.args[0] == 'object() takes no parameters':
                msg = 'It is expected that you will override the __init__ method with a method of 1 parameter.  Please see the task instructions'
                assert False, format_msg(msg)
            if e.args[
                    0] == '__init__() takes 1 positional argument but 2 were given':
                msg = 'There is a parameter error.  The __init__ method needs two parameters:  the object reference "self" and the format_map, but only one parameter has been specified.'
                assert False, format_msg(msg)

            msg = 'The exception message is expected to be:  "The format_map parameter provided to instantiate the class must be a dictionary"'
            assert e.args[
                0] == 'The format_map parameter provided to instantiate the class must be a dictionary', format_msg(
                    msg)
コード例 #15
0
def test_known_values():
    """
    It should not raise an exception if there are not any invalid format specifiers.
    """
    csv_formatter = CsvFormatter({})
    try:
        format_map = {
            'column1': 'default',
            'column2': 'us_currency',
            'column3': 'thousands_us_currency',
            'column4': 'integer',
            'column5': 'thousands_integer',
        }
        result = csv_formatter._verify_map(format_map)
        msg = 'It is expected that the _verify_map method return None on success'
        assert result is None, format_msg(msg)
    except ValueError as e:
        msg = 'An unexpected ValueError was raised.  This likely indicates that one or more valid format specifiers was not accounted for.  See the tasks and requirements for the list of valid specifiers.'
        assert False, format_msg(msg)
コード例 #16
0
def test_instantiate_success():
    """
    It should set format_map on self as part of the instantiate of the class instance.
    """
    format_map = {'column': 'default'}
    csv_formatter = CsvFormatter(format_map)
    msg = 'The instance is expected to have an attribute named "format_map." Verify that this attribute is being assigned.'
    assert hasattr(csv_formatter, 'format_map'), format_msg(msg)
    msg = 'The format_map attribute of the instance must match the value of the supplied parameter.  Verify that the correct value is being assigned.'
    assert format_map == csv_formatter.format_map, format_msg(msg)
コード例 #17
0
def test_override_init_method():
    """
    It should override the __init__ method.
    """
    try:
        CsvFormatter(1)
    except TypeError as e:
        msg = 'It is expected that you will override the __init__ method with a method of 1 parameter.  Please see the task instructions'
        assert e.args and e.args[
            0] != 'object() takes no parameters', format_msg(msg)
コード例 #18
0
def test_fmt_thousands_integer_value_error():
    """
    It should raise a ValueError if the value does not represent a valid integer.
    """
    csv_formatter = CsvFormatter({})
    for test_case in ('bad', None):
        try:
            csv_formatter._fmt_thousands_integer(test_case)
            msg = 'The _fmt_thousands_integer must raise an exception when the value is not a representation of an integer'
            assert False, format_msg(msg)
        except ValueError as e:
            if not e.args:
                msg = 'It is expected that the ValueError exception include a descriptive message, but none was provided'
                assert False, format_msg(msg)

            expected_message = 'The value "{:s}" is not valid for the thousands_integer formatter'.format(
                str(test_case))
            msg = 'The exception message did not match the expected message:  Your message: {:s} != Expected message {:s}'.format(
                e.args[0], expected_message)
            assert expected_message == e.args[0], format_msg(msg)
コード例 #19
0
def test_only_one_each():
    """
    It should only list an unknown format specifier once per invalid specifier.
    """
    csv_formatter = CsvFormatter({})
    try:
        csv_formatter._verify_map({
            'column1': 'unknown1',
            'column2': 'unknown1',
            'column3': 'an_unknown'
        })
        msg = 'The _verify_map method must raise an exception if there are invalid format specifiers'
        assert False, format_msg(msg)
    except ValueError as e:
        if not e.args:
            msg = 'It is expected that the ValueError exception include a descriptive message, but none was provided'
            assert False, format_msg(msg)

        expected_message = 'Invalid format specifier(s) in map:  an_unknown, unknown1'
        msg = 'Incorrect message returned from the exception:  "{:s}."  The expected message is:  "{:s}."  Please see tasks and requirements'.format(
            e.args[0], expected_message)
        assert e.args[0] == expected_message, format_msg(msg)
コード例 #20
0
def test_init_validates_map():
    """
    It should validate the map during initialization.
    """
    bad_map = {'column1': 'unknown1', 'column2': 'unknown2'}
    try:
        csv_formatter = CsvFormatter(bad_map)
        msg = 'An expected ValueError exception was not raised.  The __init__ method should validate the format_map with the _verify_map method'
        assert False, format_msg(msg)
    except ValueError as e:
        if not e.args:
            msg = 'It is expected that the ValueError exception include a descriptive message, but none was provided'
            assert False, format_msg(msg)

        expected_message = 'Invalid format specifier(s) in map:  unknown1, unknown2'
        msg = 'The ValueError message does not match the expected message:  Your message {:s} != Expected message {:s}'.format(
            e.args[0], expected_message)
        assert e.args[0] == expected_message, format_msg(msg)