Beispiel #1
0
    def test_strict(self):
        column = BoolColumn(strict=True)

        for valid in (True, False):
            column._from_excel(FakeCell(valid))

        for invalid in ("string", ):
            with self.assertRaises(UnableToParseBool, msg=str(invalid)):
                column._from_excel(FakeCell(invalid))
Beispiel #2
0
    def test_rounding_required(self):
        column = IntColumn(round_value=False)

        for value in ("1.3", 1.3, 0.99999999, "-23.6"):
            with self.assertRaises(RoundingRequired):
                column._to_excel(value)

            with self.assertRaises(RoundingRequired):
                column._from_excel(FakeCell(value))

        # Does not raise
        for value in (1, 1.0, -0.0, None):
            column._to_excel(value)
            column._from_excel(FakeCell(value))
Beispiel #3
0
    def test_unable_to_parse_datetime(self):
        for value in ("2017-09-28", -1, -1000.0, "adsf", False, object()):
            with self.assertRaises(UnableToParseDatetime, msg=value):
                self.column._from_excel(FakeCell(value))

            with self.assertRaises(UnableToParseDatetime, msg=value):
                self.column._to_excel(value)
Beispiel #4
0
    def test_illegal_choice(self):
        for value in (1, "string"):
            with self.assertRaises(IllegalChoice):
                self.column._from_excel(FakeCell(value))

            with self.assertRaises(IllegalChoice):
                self.column._to_excel(value)
Beispiel #5
0
    def test_unable_to_parse_float(self):
        for value in ("String", object()):
            with self.assertRaises(UnableToParseFloat, msg=value):
                self.column._to_excel(value)

        for value in ("String", ):
            with self.assertRaises(UnableToParseFloat, msg=value):
                self.column._from_excel(FakeCell(value))
Beispiel #6
0
    def to_excel(self, value, row_type=None):
        if value not in self.to_excel_map:
            if self.default is not None:
                value = self.default

            if value not in self.to_excel_map:
                raise IllegalChoice(FakeCell(value), tuple(self.to_excel_map.keys()))

        return self.to_excel_map[value]
Beispiel #7
0
 def to_excel(self, value, row_type=None):
     try:
         f = float(value)
         i = round(f, 0)
         if i != f and not self.round_value:
             raise RoundingRequired(FakeCell(value=value))
         return int(i)
     except (ValueError, TypeError):
         raise UnableToParseInt(value=value)
Beispiel #8
0
 def assertFromExcel(self, excel, internal, column=None):
     column = column or self.column
     result = column._from_excel(FakeCell(excel))
     self.assertEqual(
         result,
         internal,
         msg=
         "Excel value '%s' (%s) was converted to '%s' (%s) instead of '%s' (%s)."
         % (excel, type(excel).__name__, result, type(result).__name__,
            internal, type(internal).__name__))
Beispiel #9
0
    def test_blank_not_allowed(self):
        column = TableColumn(allow_blank=False)

        for value in ("", None):
            with self.assertRaises(BlankNotAllowed,
                                   msg="'%s' (%s)" %
                                   (value, type(value).__name__)):
                column._to_excel(value)

        for value in ("", None, "'"):
            with self.assertRaises(BlankNotAllowed,
                                   msg="'%s' (%s)" %
                                   (value, type(value).__name__)):
                column._from_excel(FakeCell(value))
Beispiel #10
0
    def test_string_to_long(self):
        column = CharColumn(max_length=5)
        column._from_excel(FakeCell("12345"))

        with self.assertRaises(StringToLong):
            column._from_excel(FakeCell("123456"))