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)
def testFailsOnInvalidExample(self): fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10", _anyFormat) try: fieldFormat.example = "11" except fields.FieldValueError: # Ignore expected error. pass
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"])
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
def testCanProcessValidExample(self): fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10", _anyFormat) fieldFormat.example = "3" self.assertEqual(fieldFormat.example, "3")
def testNoNumber(self): fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10", _anyFormat) self.assertRaises(fields.FieldValueError, fieldFormat.validated, "abc")
def testTooSmall(self): fieldFormat = fields.IntegerFieldFormat("x", False, None, "1:10", _anyFormat) self.assertRaises(fields.FieldValueError, fieldFormat.validated, "0")