Example #1
0
def test_convert_type(recwarn):
    si = SpreadsheetInput()
    assert si.convert_type('', 'somestring') == 'somestring'
    # If not type is specified, ints are kept as ints...
    assert si.convert_type('', 3) == 3

    # ... but all other ojbects are converted to strings
    class NotAString(object):
        def __str__(self):
            return 'string representation'
    assert NotAString() != 'string representation'
    assert si.convert_type('', NotAString()) == 'string representation'
    assert si.convert_type('string', NotAString()) == 'string representation'

    assert si.convert_type('string', 3) == '3'
    assert si.convert_type('number', '3') == Decimal('3')
    assert si.convert_type('number', '1.2') == Decimal('1.2')
    assert si.convert_type('integer', '3') == 3
    assert si.convert_type('integer', 3) == 3

    assert si.convert_type('boolean', 'TRUE') is True
    assert si.convert_type('boolean', 'True') is True
    assert si.convert_type('boolean', 1) is True
    assert si.convert_type('boolean', '1') is True
    assert si.convert_type('boolean', 'FALSE') is False
    assert si.convert_type('boolean', 'False') is False
    assert si.convert_type('boolean', 0) is False
    assert si.convert_type('boolean', '0') is False
    si.convert_type('boolean', 2)
    assert 'Unrecognised value for boolean: "2"' in text_type(recwarn.pop(UserWarning).message)
    si.convert_type('boolean', 'test')
    assert 'Unrecognised value for boolean: "test"' in text_type(recwarn.pop(UserWarning).message)

    si.convert_type('integer', 'test')
    assert 'Non-integer value "test"' in text_type(recwarn.pop(UserWarning).message)

    si.convert_type('number', 'test')
    assert 'Non-numeric value "test"' in text_type(recwarn.pop(UserWarning).message)

    assert si.convert_type('string', '') is None
    assert si.convert_type('number', '') is None
    assert si.convert_type('integer', '') is None
    assert si.convert_type('array', '') is None
    assert si.convert_type('boolean', '') is None
    assert si.convert_type('string', None) is None
    assert si.convert_type('number', None) is None
    assert si.convert_type('integer', None) is None
    assert si.convert_type('array', None) is None
    assert si.convert_type('boolean', None) is None

    assert si.convert_type('array', 'one') == ['one']
    assert si.convert_type('array', 'one;two') == ['one', 'two']
    assert si.convert_type('array', 'one,two;three,four') == [['one', 'two'], ['three', 'four']]

    with pytest.raises(ValueError) as e:
        si.convert_type('notatype', 'test')
    assert 'Unrecognised type: "notatype"' in text_type(e)

    assert si.convert_type('string', datetime.datetime(2015, 1, 1)) == '2015-01-01T00:00:00+00:00'
    assert si.convert_type('', datetime.datetime(2015, 1, 1)) == '2015-01-01T00:00:00+00:00'
    assert si.convert_type('string', datetime.datetime(2015, 1, 1, 13, 37, 59)) == '2015-01-01T13:37:59+00:00'
    assert si.convert_type('', datetime.datetime(2015, 1, 1, 13, 37, 59)) == '2015-01-01T13:37:59+00:00'

    si = SpreadsheetInput(timezone_name='Europe/London')
    assert si.convert_type('string', datetime.datetime(2015, 1, 1)) == '2015-01-01T00:00:00+00:00'
    assert si.convert_type('', datetime.datetime(2015, 1, 1)) == '2015-01-01T00:00:00+00:00'
    assert si.convert_type('string', datetime.datetime(2015, 1, 1, 13, 37, 59)) == '2015-01-01T13:37:59+00:00'
    assert si.convert_type('', datetime.datetime(2015, 1, 1, 13, 37, 59)) == '2015-01-01T13:37:59+00:00'
    assert si.convert_type('string', datetime.datetime(2015, 6, 1)) == '2015-06-01T00:00:00+01:00'
    assert si.convert_type('', datetime.datetime(2015, 6, 1)) == '2015-06-01T00:00:00+01:00'
    assert si.convert_type('string', datetime.datetime(2015, 6, 1, 13, 37, 59)) == '2015-06-01T13:37:59+01:00'
    assert si.convert_type('', datetime.datetime(2015, 6, 1, 13, 37, 59)) == '2015-06-01T13:37:59+01:00'
Example #2
0
def test_spreadsheetinput_base_fails():
    spreadsheet_input = SpreadsheetInput()
    with pytest.raises(NotImplementedError):
        spreadsheet_input.read_sheets()
    with pytest.raises(NotImplementedError):
        spreadsheet_input.get_sheet_lines('test')
def test_spreadsheetinput_base_fails():
    spreadsheet_input = SpreadsheetInput()
    with pytest.raises(NotImplementedError):
        spreadsheet_input.read_sheets()
    with pytest.raises(NotImplementedError):
        spreadsheet_input.get_sheet_lines("test")
def test_convert_type(recwarn):
    si = SpreadsheetInput()  # noqa
    assert convert_type("", "somestring") == "somestring"
    # If not type is specified, ints are kept as ints...
    assert convert_type("", 3) == 3

    # ... but all other ojbects are converted to strings
    class NotAString(object):
        def __str__(self):
            return "string representation"

    assert NotAString() != "string representation"
    assert convert_type("", NotAString()) == "string representation"
    assert convert_type("string", NotAString()) == "string representation"

    assert convert_type("string", 3) == "3"
    assert convert_type("number", "3") == Decimal("3")
    assert convert_type("number", "1.2") == Decimal("1.2")
    assert convert_type("integer", "3") == 3
    assert convert_type("integer", 3) == 3

    assert convert_type("boolean", "TRUE") is True
    assert convert_type("boolean", "True") is True
    assert convert_type("boolean", 1) is True
    assert convert_type("boolean", "1") is True
    assert convert_type("boolean", "FALSE") is False
    assert convert_type("boolean", "False") is False
    assert convert_type("boolean", 0) is False
    assert convert_type("boolean", "0") is False
    convert_type("boolean", 2)
    assert 'Unrecognised value for boolean: "2"' in str(
        recwarn.pop(UserWarning).message)
    convert_type("boolean", "test")
    assert 'Unrecognised value for boolean: "test"' in str(
        recwarn.pop(UserWarning).message)

    convert_type("integer", "test")
    assert 'Non-integer value "test"' in str(recwarn.pop(UserWarning).message)

    convert_type("number", "test")
    assert 'Non-numeric value "test"' in str(recwarn.pop(UserWarning).message)

    assert convert_type("string", "") is None
    assert convert_type("number", "") is None
    assert convert_type("integer", "") is None
    assert convert_type("array", "") is None
    assert convert_type("boolean", "") is None
    assert convert_type("string", None) is None
    assert convert_type("number", None) is None
    assert convert_type("integer", None) is None
    assert convert_type("array", None) is None
    assert convert_type("boolean", None) is None

    for type_string in [
            "array", "string_array", "array_array", "number_array"
    ]:
        assert convert_type(type_string, "one") == ["one"]
        assert convert_type(type_string, "one;two") == ["one", "two"]
        assert convert_type(type_string, "one,two;three,four") == [
            ["one", "two"],
            ["three", "four"],
        ]
    assert 'Non-numeric value "one"' in str(recwarn.pop(UserWarning).message)
    assert 'Non-numeric value "one;two"' in str(
        recwarn.pop(UserWarning).message)
    assert 'Non-numeric value "one,two;three,four"' in str(
        recwarn.pop(UserWarning).message)
    assert convert_type("number_array", "1") == [1]
    assert convert_type("number_array", "1;2") == [1, 2]
    assert convert_type("number_array", "1,2;3,4") == [[1, 2], [3, 4]]

    with pytest.raises(ValueError) as e:
        convert_type("notatype", "test")
    assert 'Unrecognised type: "notatype"' in str(e)

    assert (convert_type("string",
                         datetime.datetime(2015, 1,
                                           1)) == "2015-01-01T00:00:00+00:00")
    assert (convert_type("",
                         datetime.datetime(2015, 1,
                                           1)) == "2015-01-01T00:00:00+00:00")
    assert (convert_type("string",
                         datetime.datetime(2015, 1, 1, 13, 37,
                                           59)) == "2015-01-01T13:37:59+00:00")
    assert (convert_type("",
                         datetime.datetime(2015, 1, 1, 13, 37,
                                           59)) == "2015-01-01T13:37:59+00:00")

    timezone = pytz.timezone("Europe/London")
    assert (convert_type("string", datetime.datetime(2015, 1, 1),
                         timezone) == "2015-01-01T00:00:00+00:00")
    assert (convert_type("", datetime.datetime(2015, 1, 1),
                         timezone) == "2015-01-01T00:00:00+00:00")
    assert (convert_type("string", datetime.datetime(2015, 1, 1, 13, 37, 59),
                         timezone) == "2015-01-01T13:37:59+00:00")
    assert (convert_type("", datetime.datetime(2015, 1, 1, 13, 37, 59),
                         timezone) == "2015-01-01T13:37:59+00:00")
    assert (convert_type("string", datetime.datetime(2015, 6, 1),
                         timezone) == "2015-06-01T00:00:00+01:00")
    assert (convert_type("", datetime.datetime(2015, 6, 1),
                         timezone) == "2015-06-01T00:00:00+01:00")
    assert (convert_type("string", datetime.datetime(2015, 6, 1, 13, 37, 59),
                         timezone) == "2015-06-01T13:37:59+01:00")
    assert (convert_type("", datetime.datetime(2015, 6, 1, 13, 37, 59),
                         timezone) == "2015-06-01T13:37:59+01:00")

    assert len(recwarn) == 0
Example #5
0
def test_convert_type(recwarn):
    si = SpreadsheetInput()
    assert convert_type('', 'somestring') == 'somestring'
    # If not type is specified, ints are kept as ints...
    assert convert_type('', 3) == 3

    # ... but all other ojbects are converted to strings
    class NotAString(object):
        def __str__(self):
            return 'string representation'

    assert NotAString() != 'string representation'
    assert convert_type('', NotAString()) == 'string representation'
    assert convert_type('string', NotAString()) == 'string representation'

    assert convert_type('string', 3) == '3'
    assert convert_type('number', '3') == Decimal('3')
    assert convert_type('number', '1.2') == Decimal('1.2')
    assert convert_type('integer', '3') == 3
    assert convert_type('integer', 3) == 3

    assert convert_type('boolean', 'TRUE') is True
    assert convert_type('boolean', 'True') is True
    assert convert_type('boolean', 1) is True
    assert convert_type('boolean', '1') is True
    assert convert_type('boolean', 'FALSE') is False
    assert convert_type('boolean', 'False') is False
    assert convert_type('boolean', 0) is False
    assert convert_type('boolean', '0') is False
    convert_type('boolean', 2)
    assert 'Unrecognised value for boolean: "2"' in str(
        recwarn.pop(UserWarning).message)
    convert_type('boolean', 'test')
    assert 'Unrecognised value for boolean: "test"' in str(
        recwarn.pop(UserWarning).message)

    convert_type('integer', 'test')
    assert 'Non-integer value "test"' in str(recwarn.pop(UserWarning).message)

    convert_type('number', 'test')
    assert 'Non-numeric value "test"' in str(recwarn.pop(UserWarning).message)

    assert convert_type('string', '') is None
    assert convert_type('number', '') is None
    assert convert_type('integer', '') is None
    assert convert_type('array', '') is None
    assert convert_type('boolean', '') is None
    assert convert_type('string', None) is None
    assert convert_type('number', None) is None
    assert convert_type('integer', None) is None
    assert convert_type('array', None) is None
    assert convert_type('boolean', None) is None

    for type_string in [
            'array', 'string_array', 'array_array', 'number_array'
    ]:
        assert convert_type(type_string, 'one') == ['one']
        assert convert_type(type_string, 'one;two') == ['one', 'two']
        assert convert_type(type_string,
                            'one,two;three,four') == [['one', 'two'],
                                                      ['three', 'four']]
    assert 'Non-numeric value "one"' in str(recwarn.pop(UserWarning).message)
    assert 'Non-numeric value "one;two"' in str(
        recwarn.pop(UserWarning).message)
    assert 'Non-numeric value "one,two;three,four"' in str(
        recwarn.pop(UserWarning).message)
    assert convert_type('number_array', '1') == [1]
    assert convert_type('number_array', '1;2') == [1, 2]
    assert convert_type('number_array', '1,2;3,4') == [[1, 2], [3, 4]]

    with pytest.raises(ValueError) as e:
        convert_type('notatype', 'test')
    assert 'Unrecognised type: "notatype"' in str(e)

    assert convert_type('string',
                        datetime.datetime(2015, 1,
                                          1)) == '2015-01-01T00:00:00+00:00'
    assert convert_type('',
                        datetime.datetime(2015, 1,
                                          1)) == '2015-01-01T00:00:00+00:00'
    assert convert_type('string',
                        datetime.datetime(2015, 1, 1, 13, 37,
                                          59)) == '2015-01-01T13:37:59+00:00'
    assert convert_type('',
                        datetime.datetime(2015, 1, 1, 13, 37,
                                          59)) == '2015-01-01T13:37:59+00:00'

    timezone = pytz.timezone('Europe/London')
    assert convert_type('string', datetime.datetime(2015, 1, 1),
                        timezone) == '2015-01-01T00:00:00+00:00'
    assert convert_type('', datetime.datetime(2015, 1, 1),
                        timezone) == '2015-01-01T00:00:00+00:00'
    assert convert_type('string', datetime.datetime(2015, 1, 1, 13, 37, 59),
                        timezone) == '2015-01-01T13:37:59+00:00'
    assert convert_type('', datetime.datetime(2015, 1, 1, 13, 37, 59),
                        timezone) == '2015-01-01T13:37:59+00:00'
    assert convert_type('string', datetime.datetime(2015, 6, 1),
                        timezone) == '2015-06-01T00:00:00+01:00'
    assert convert_type('', datetime.datetime(2015, 6, 1),
                        timezone) == '2015-06-01T00:00:00+01:00'
    assert convert_type('string', datetime.datetime(2015, 6, 1, 13, 37, 59),
                        timezone) == '2015-06-01T13:37:59+01:00'
    assert convert_type('', datetime.datetime(2015, 6, 1, 13, 37, 59),
                        timezone) == '2015-06-01T13:37:59+01:00'

    assert len(recwarn) == 0