Exemple #1
0
 def testWithinRange(self):
     fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10",
                                             _anyFormat)
     self.assertEquals(fieldFormat.validated("1"), 1)
     self.assertEquals(fieldFormat.validated("7"), 7)
     self.assertEquals(fieldFormat.validated("10"), 10)
     fieldFormat = fields.IntegerFieldFormat("x", False, None, "123",
                                             _anyFormat)
     self.assertEquals(fieldFormat.validated("123"), 123)
Exemple #2
0
 def testFailsOnInvalidExample(self):
     fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10",
                                             _anyFormat)
     try:
         fieldFormat.example = "11"
     except fields.FieldValueError:
         # Ignore expected error.
         pass
Exemple #3
0
 def testAsIcdRow(self):
     fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10",
                                             _anyFormat)
     length = fieldFormat.length
     items = length.items
     self.assertEquals(items, None)
     self.assertEquals(fieldFormat.asIcdRow(),
                       ["x", "", "", "", "Integer", "1:10"])
Exemple #4
0
    def asFieldFormat(self):
        isAllowedToBeEmpty = (self.emptyCount > 0)
        lengthText = ""
        if self.minLength == self.maxLength:
            lengthText = unicode(self.minLength)
        else:
            if self.minLength:
                lengthText += unicode(self.minLength)
            lengthText += ":%d" % self.maxLength

        # TODO: Detect date format.
        # TODO: Collect range for Integer and Decimal fields.
        if self._isAlwaysNumber[_tools.NUMBER_INTEGER]:
            result = fields.IntegerFieldFormat(self.name, isAllowedToBeEmpty, u"", u"", self.dataFormat)
        elif self._isAlwaysNumber[_tools.NUMBER_DECIMAL_COMMA] or self._isAlwaysNumber[_tools.NUMBER_DECIMAL_POINT]:
            result = fields.DecimalFieldFormat(self.name, isAllowedToBeEmpty, u"", u"", self.dataFormat)
        else:
            result = fields.TextFieldFormat(self.name, isAllowedToBeEmpty, lengthText, u"", self.dataFormat)
        result.example = self.example
        return result
Exemple #5
0
 def testCanProcessValidExample(self):
     fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10",
                                             _anyFormat)
     fieldFormat.example = "3"
     self.assertEqual(fieldFormat.example, "3")
Exemple #6
0
 def testNoNumber(self):
     fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10",
                                             _anyFormat)
     self.assertRaises(fields.FieldValueError, fieldFormat.validated, "abc")
Exemple #7
0
 def testTooSmall(self):
     fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10",
                                             _anyFormat)
     self.assertRaises(fields.FieldValueError, fieldFormat.validated, "0")