Changeset 3047

Show
Ignore:
Timestamp:
04/10/07 12:23:42 (1 year ago)
Author:
nate
Message:

Moved all of the relevant dTextBox stuff over to dTextBoxMixin.py. Added a few more tests on dTextBox while I was at it. Everything is still working ok as far as I can tell.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/NateBranch/dabo/ui/uiwx/dTextBox.py

    r3021 r3047  
    1414    dabo.ui.loadUI("wx") 
    1515 
    16 import dDataControlMixin as dc
     16import dTextBoxMixin as tb
    1717from dabo.dLocalize import _ 
    1818import dabo.dEvents as dEvents 
     
    2020 
    2121 
    22 class dTextBox(dcm.dDataControlMixin, wx.TextCtrl): 
     22class dTextBox(tbm.dTextBoxMixin, wx.TextCtrl): 
    2323    """Creates a text box for editing one line of string data.""" 
    2424    def __init__(self, parent, properties=None, attProperties=None, *args, **kwargs): 
    2525        self._baseClass = dTextBox 
    26  
    27         self._dregex = {} 
    28         self._lastDataType = unicode 
    29         self._forceCase = None 
    30         self._inForceCase = False 
    31         self._textLength = None 
    32         self._inTextLength = False 
    33         self._flushOnLostFocus = True  ## see dabo.ui.dDataControlMixinBase::flushValue() 
    34  
     26         
    3527        preClass = wx.PreTextCtrl 
    36         dcm.dDataControlMixin.__init__(self, preClass, parent, properties, attProperties,  
     28        tbm.dTextBoxMixin.__init__(self, preClass, parent, properties, attProperties,  
    3729                *args, **kwargs) 
    38  
    39         # Keep passwords, etc., from being written to disk 
    40         if self.PasswordEntry: 
    41             self.IsSecret = True 
    42  
    43      
    44     def _initEvents(self): 
    45         super(dTextBox, self)._initEvents() 
    46         self.Bind(wx.EVT_TEXT, self._onWxHit) 
    47          
    48          
    49     def flushValue(self): 
    50         # Call the wx SetValue() directly to reset the string value displayed to the user. 
    51         # This resets the value to the string representation as Python shows it. Also, we 
    52         # must save and restore the InsertionPosition because wxGtk at least resets it to 
    53         # 0 upon SetValue(). 
    54         insPos = self.InsertionPosition 
    55         self.SetValue(self.getStringValue(self.Value)) 
    56         self.InsertionPosition = insPos 
    57          
    58         # Now that the dabo Value is set properly, the default behavior that flushes  
    59         # the value to the bizobj can be called: 
    60         super(dTextBox, self).flushValue() 
    6130     
    6231     
    63     def selectAll(self): 
    64         """Each subclass must define their own selectAll method. This will  
    65         be called if SelectOnEntry is True when the control gets focus. 
    66         """ 
    67         self.SetSelection(-1, -1) 
    68          
    69          
    70     def getBlankValue(self): 
    71         return "" 
    72  
    73      
    74     def convertStringValueToDataType(self, strVal, dataType): 
    75         """Given a string value and a type, return an appropriate value of that type. 
    76  
    77         If the value can't be converted, a ValueError will be raised. 
    78         """      
    79         if dataType == bool: 
    80             # Bools can't convert from string representations, because a zero- 
    81             # length denotes False, and anything else denotes True. 
    82             if strVal == "True": 
    83                 retVal = True 
    84             else: 
    85                 retVal = False 
    86  
    87         elif dataType in (datetime.date, datetime.datetime, datetime.time): 
    88             # We expect the string to be in ISO 8601 format. 
    89             if dataType == datetime.date: 
    90                 retVal = self._getDateFromString(strVal) 
    91             elif dataType == datetime.datetime: 
    92                 retVal = self._getDateTimeFromString(strVal) 
    93             elif dataType == datetime.time: 
    94                 retVal = self._getTimeFromString(strVal) 
    95                  
    96             if retVal is None: 
    97                 raise ValueError, "String not in ISO 8601 format." 
    98                  
    99         elif str(dataType) == "<type 'DateTime'>": 
    100             # mx DateTime type. MySQLdb will use this if mx is installed. 
    101             try: 
    102                 import mx.DateTime 
    103                 retVal = mx.DateTime.DateTimeFrom(str(strVal)) 
    104             except ImportError: 
    105                 raise ValueError, "Can't import mx.DateTime" 
    106          
    107         elif str(dataType) == "<type 'DateTimeDelta'>": 
    108             # mx TimeDelta type. MySQLdb will use this for Time columns if mx is installed. 
    109             try: 
    110                 import mx.DateTime 
    111                 retVal = mx.DateTime.TimeFrom(str(strVal)) 
    112             except ImportError: 
    113                 raise ValueError, "Can't import mx.DateTime" 
    114          
    115         elif (decimal is not None and dataType == decimal.Decimal): 
    116             try: 
    117                 _oldVal = self._oldVal 
    118             except: 
    119                 _oldVal = None 
    120  
    121             try: 
    122                 if type(_oldVal) == decimal.Decimal: 
    123                     # Enforce the precision as previously set programatically 
    124                     retVal = decimal.DefaultContext.quantize(decimal.Decimal(strVal), _oldVal) 
    125                 else: 
    126                     retVal = decimal.Decimal(strVal) 
    127             except: 
    128                 raise ValueError, "Can't convert to decimal." 
    129  
    130         else: 
    131             # Other types can convert directly. 
    132             if dataType == str: 
    133                 dataType = unicode 
    134             try: 
    135                 retVal = dataType(strVal) 
    136             except: 
    137                 # The Python object couldn't convert it. Our validator, once  
    138                 # implemented, won't let the user get this far. Just keep the  
    139                 # old value. 
    140                 raise ValueError, "Can't convert." 
    141         return retVal 
    142  
    143  
    144     def getStringValue(self, value): 
    145         """Given a value of any data type, return a string rendition. 
    146          
    147         Used internally by _setValue and flushValue, but also exposed to subclasses 
    148         in case they need specialized behavior. The value returned from this  
    149         function will be what is displayed in the UI textbox. 
    150         """ 
    151         if isinstance(value, basestring): 
    152             # keep it unicode instead of converting to str 
    153             strVal = value 
    154         elif isinstance(value, datetime.datetime): 
    155             # Use the ISO 8601 datetime string format (with a " " separator 
    156             # instead of "T")  
    157             strVal = value.isoformat(" ") 
    158         elif isinstance(value, datetime.date): 
    159             # Use the ISO 8601 date string format so we can convert 
    160             # back from a known quantity later... 
    161             strVal = value.isoformat() 
    162         elif isinstance(value, datetime.time): 
    163             # Use the ISO 8601 time string format 
    164             strVal = value.isoformat() 
    165         elif value is None: 
    166             strVal = self.Application.NoneDisplay 
    167         else: 
    168             # convert all other data types to string: 
    169             strVal = str(value)   # (floats look like 25.55) 
    170             #strVal = repr(value) # (floats look like 25.55000000000001) 
    171         return strVal 
    172  
    173          
    174     def _getDateFromString(self, strVal): 
    175         """Given a string in an accepted date format, return a  
    176         datetime.date object, or None. 
    177         """ 
    178         formats = ["ISO8601"] 
    179         if not self.StrictDateEntry: 
    180             # Add some less strict date-entry formats: 
    181             formats.append("YYYYMMDD") 
    182             formats.append("YYMMDD") 
    183             formats.append("MMDD") 
    184             # (define more formats in dabo.lib.dates._getDateRegex, and enter  
    185             # them above in more explicit -> less explicit order.) 
    186         return dabo.lib.dates.getDateFromString(strVal, formats) 
    187  
    188  
    189     def _getDateTimeFromString(self, strVal): 
    190         """Given a string in ISO 8601 datetime format, return a  
    191         datetime.datetime object. 
    192         """ 
    193         formats = ["ISO8601"] 
    194         if not self.StrictDateEntry: 
    195             # Add some less strict date-entry formats: 
    196             formats.append("YYYYMMDDHHMMSS") 
    197             formats.append("YYMMDDHHMMSS") 
    198             formats.append("YYYYMMDD") 
    199             formats.append("YYMMDD") 
    200             # (define more formats in dabo.lib.dates._getDateTimeRegex, and enter  
    201             # them above in more explicit -> less explicit order.) 
    202         return dabo.lib.dates.getDateTimeFromString(strVal, formats) 
    203  
    204  
    205     def _getTimeFromString(self, strVal): 
    206         """Given a string in ISO 8601 time format, return a  
    207         datetime.time object. 
    208         """ 
    209         formats = ["ISO8601"] 
    210         return dabo.lib.dates.getTimeFromString(strVal, formats) 
    211  
    212  
    213     def __onKeyChar(self, evt): 
    214         """This handles KeyChar events when ForceCase is set to a non-empty value.""" 
    215         keyChar = evt.keyChar 
    216         if keyChar is not None and (keyChar.isalnum()  
    217                 or keyChar in """,./<>?;':"[]\\{}|`~!@#$%%^&*()-_=+"""): 
    218             dabo.ui.callAfter(self.__forceCase) 
    219             dabo.ui.callAfter(self.__textLength) 
    220      
    221     def __textLength(self): 
    222         """If the TextLength property is set, checks the current value of the control 
    223         and truncates it if too long""" 
    224         if not isinstance(self.Value, basestring): 
    225             #Don't bother if it isn't a string type 
    226             return 
    227         length = self.TextLength 
    228         if not length: 
    229             return 
    230          
    231         insPos = self.InsertionPosition 
    232          
    233         self._inTextLength = True 
    234         if len(self.Value) > length: 
    235             self.Value = self.Value[:length] 
    236             if insPos > length: 
    237                 self.InsertionPosition = length 
    238             else: 
    239                 self.InsertionPosition = insPos 
    240             self.refresh() 
    241         self._inTextLength = False 
    242      
    243     def __forceCase(self): 
    244         """If the ForceCase property is set, casts the current value of the control 
    245         to the specified case. 
    246         """ 
    247         if not isinstance(self.Value, basestring): 
    248             # Don't bother if it isn't a string type 
    249             return 
    250         case = self.ForceCase 
    251         if not case: 
    252             return 
    253         insPos = self.InsertionPosition 
    254         selLen = self.SelectionLength 
    255         changed = False 
    256         self._inForceCase = True 
    257         if case == "upper": 
    258             self.Value = self.Value.upper() 
    259             changed = True 
    260         elif case == "lower": 
    261             self.Value = self.Value.lower() 
    262             changed = True 
    263         elif case == "title": 
    264             self.Value = self.Value.title() 
    265             changed = True 
    266         if changed: 
    267             #self.SelectionStart = selStart 
    268             self.InsertionPosition = insPos 
    269             self.SelectionLength = selLen 
    270             self.refresh() 
    271         self._inForceCase = False 
    272  
    273  
    274     # property get/set functions 
    275     def _getAlignment(self): 
    276         if self._hasWindowStyleFlag(wx.TE_RIGHT): 
    277             return "Right" 
    278         elif self._hasWindowStyleFlag(wx.TE_CENTRE): 
    279             return "Center" 
    280         else: 
    281             return "Left" 
    282  
    283     def _setAlignment(self, val): 
    284         # Note: alignment doesn't seem to work, at least on GTK2 
    285         self._delWindowStyleFlag(wx.TE_LEFT) 
    286         self._delWindowStyleFlag(wx.TE_CENTRE) 
    287         self._delWindowStyleFlag(wx.TE_RIGHT) 
    288         val = val[0].lower() 
    289         if val == "l": 
    290             self._addWindowStyleFlag(wx.TE_LEFT) 
    291         elif val == "c": 
    292             self._addWindowStyleFlag(wx.TE_CENTRE) 
    293         elif val == "r": 
    294             self._addWindowStyleFlag(wx.TE_RIGHT) 
    295         else: 
    296             raise ValueError, "The only possible values are 'Left', 'Center', and 'Right'" 
    297  
    298  
    299     def _getForceCase(self): 
    300         return self._forceCase 
    301  
    302     def _setForceCase(self, val): 
    303         if self._constructed(): 
    304             if val is None: 
    305                 valKey = None 
    306             else: 
    307                 valKey = val[0].upper() 
    308             self._forceCase = {"U": "upper", "L": "lower", "T": "title", None: None, 
    309                     "None": None}.get(valKey) 
    310             self.__forceCase() 
    311             self.unbindEvent(dEvents.KeyChar, self.__onKeyChar) 
    312             if self._forceCase or self._textLength: 
    313                 self.bindEvent(dEvents.KeyChar, self.__onKeyChar) 
    314         else: 
    315             self._properties["ForceCase"] = val 
    316  
    317  
    318     def _getInsertionPosition(self): 
    319         return self.GetInsertionPoint() 
    320  
    321     def _setInsertionPosition(self, val): 
    322         self.SetInsertionPoint(val) 
    323  
    324  
    325     def _getPasswordEntry(self): 
    326         return self._hasWindowStyleFlag(wx.TE_PASSWORD) 
    327  
    328     def _setPasswordEntry(self, val): 
    329         self._delWindowStyleFlag(wx.TE_PASSWORD) 
    330         if val: 
    331             self._addWindowStyleFlag(wx.TE_PASSWORD) 
    332             self.IsSecret = True 
    333  
    334                  
    335     def _getReadOnly(self): 
    336         return not self.IsEditable() 
    337          
    338     def _setReadOnly(self, val): 
    339         if self._constructed(): 
    340             self.SetEditable(not bool(val)) 
    341         else: 
    342             self._properties["ReadOnly"] = val 
    343  
    344  
    345     def _getSelectedText(self): 
    346         return self.GetStringSelection() 
    347  
    348  
    349     def _getSelectionEnd(self): 
    350         return self.GetSelection()[1] 
    351  
    352     def _setSelectionEnd(self, val): 
    353         start, end = self.GetSelection() 
    354         self.SetSelection(start, val) 
    355         self.refresh() 
    356  
    357  
    358     def _getSelectionLength(self): 
    359         start, end = self.GetSelection() 
    360         return end - start 
    361  
    362     def _setSelectionLength(self, val): 
    363         start = self.GetSelection()[0] 
    364         self.SetSelection(start, start + val) 
    365         self.refresh() 
    366  
    367  
    368     def _getSelectionStart(self): 
    369         return self.GetSelection()[0] 
    370  
    371     def _setSelectionStart(self, val): 
    372         start, end = self.GetSelection() 
    373         self.SetSelection(val, end) 
    374         self.refresh() 
    375  
    376  
    377     def _getSelectOnEntry(self): 
    378         try: 
    379             return self._SelectOnEntry 
    380         except AttributeError: 
    381             return False 
    382              
    383     def _setSelectOnEntry(self, val): 
    384         self._SelectOnEntry = bool(val) 
    385  
    386          
    387     def _getStrictDateEntry(self): 
    388         try: 
    389             v = self._strictDateEntry 
    390         except AttributeError: 
    391             v = self._strictDateEntry = False 
    392         return v 
    393  
    394     def _setStrictDateEntry(self, val): 
    395         self._strictDateEntry = bool(val) 
    396  
    397  
    398     def _getTextLength(self): 
    399         return self._textLength 
    400  
    401     def _setTextLength(self, val): 
    402         if val == None: 
    403             self._textLength = None 
    404         else: 
    405             val = int(val) 
    406             if val < 1: 
    407                 raise ValueError, 'TextLength must be a positve Integer' 
    408             self._textLength = val 
    409         self.__textLength() 
    410          
    411         self.unbindEvent(dEvents.KeyChar, self.__onKeyChar) 
    412         if self._forceCase or self._textLength: 
    413             self.bindEvent(dEvents.KeyChar, self.__onKeyChar) 
    414  
    415  
    416     def _getValue(self): 
    417         # Return the value as reported by wx, but convert it to the data type as 
    418         # reported by self._value. 
    419         try: 
    420             _value = self._value 
    421         except AttributeError: 
    422             _value = self._value = unicode("") 
    423         dataType = type(_value) 
    424          
    425         # Get the string value as reported by wx, which is the up-to-date  
    426         # string value of the control: 
    427         strVal = self.GetValue() 
    428  
    429         # Convert the current string value of the control, as entered by the  
    430         # user, into the proper data type. 
    431         skipConversion = False 
    432         if _value is None: 
    433             if strVal == self.Application.NoneDisplay: 
    434                 # Keep the value None 
    435                 convertedVal = None 
    436                 skipConversion = True 
    437             else: 
    438                 # User changed the None value to something else, convert to the last 
    439                 # known real datatype. 
    440                 dataType = self._lastDataType 
    441  
    442         if not skipConversion: 
    443             try: 
    444                 convertedVal = self.convertStringValueToDataType(strVal, dataType) 
    445             except ValueError: 
    446                 # It couldn't convert; return the previous value. 
    447                 convertedVal = self._value 
    448         return convertedVal 
    449      
    450     def _setValue(self, val): 
    451         if self._constructed(): 
    452             # Must convert all to string for sending to wx, but our internal  
    453             # _value will always retain the correct type. 
    454          
    455             # Todo: set up validators based on the type of data we are editing, 
    456             # so the user can't, for example, enter a letter "p" in a textbox 
    457             # that is currently showing numeric data. 
    458  
    459             if self._inForceCase: 
    460                 # Value is changing internally. Don't update the oldval 
    461                 # setting or change the type; just set the value. 
    462                 self.SetValue(val) 
    463                 return 
    464             else: 
    465                 dabo.ui.callAfter(self.__forceCase) 
    466              
    467             strVal = self.getStringValue(val) 
    468             _oldVal = self._oldVal = self.Value 
    469                  
    470             # save the actual value for return by _getValue: 
    471             self._value = val 
    472  
    473             if val is not None: 
    474                 # Save the type of the value, so that in the case of actual None 
    475                 # assignments, we know the datatype to expect. 
    476                 self._lastDataType = type(val) 
    477  
    478             # Update the display no matter what: 
    479             self.SetValue(strVal) 
    480          
    481             if type(_oldVal) != type(val) or _oldVal != val: 
    482                 self._afterValueChanged() 
    483         else: 
    484             self._properties["Value"] = val 
    485          
    486         self.__textLength() 
    487  
    488          
    489     # Property definitions: 
    490     Alignment = property(_getAlignment, _setAlignment, None, 
    491             _("""Specifies the alignment of the text. (str) 
    492                Left (default) 
    493                Center 
    494                Right""")) 
    495  
    496     ForceCase = property(_getForceCase, _setForceCase, None, 
    497             _("""Determines if we change the case of entered text. Possible values are: 
    498                 None, "" (empty string): No changes made (default) 
    499                 "Upper": FORCE TO UPPER CASE 
    500                 "Lower": force to lower case 
    501                 "Title": Force To Title Case 
    502             These can be abbreviated to "u", "l" or "t"  (str)""")) 
    503      
    504     InsertionPosition = property(_getInsertionPosition, _setInsertionPosition, None, 
    505             _("Position of the insertion point within the control  (int)")) 
    506      
    507     PasswordEntry = property(_getPasswordEntry, _setPasswordEntry, None, 
    508             _("Specifies whether plain-text or asterisks are echoed. (bool)")) 
    509  
    510     ReadOnly = property(_getReadOnly, _setReadOnly, None,  
    511             _("Specifies whether or not the text can be edited. (bool)")) 
    512      
    513     SelectedText = property(_getSelectedText, None, None, 
    514             _("Currently selected text. Returns the empty string if nothing is selected  (str)"))    
    515      
    516     SelectionEnd = property(_getSelectionEnd, _setSelectionEnd, None, 
    517             _("""Position of the end of the selected text. If no text is 
    518             selected, returns the Position of the insertion cursor.  (int)""")) 
    519      
    520     SelectionLength = property(_getSelectionLength, _setSelectionLength, None, 
    521             _("Length of the selected text, or 0 if nothing is selected.  (int)")) 
    522      
    523     SelectionStart = property(_getSelectionStart, _setSelectionStart, None, 
    524             _("""Position of the beginning of the selected text. If no text is 
    525             selected, returns the Position of the insertion cursor.  (int)""")) 
    526      
    527     SelectOnEntry = property(_getSelectOnEntry, _setSelectOnEntry, None,  
    528             _("Specifies whether all text gets selected upon receiving focus. (bool)")) 
    529  
    530     StrictDateEntry = property(_getStrictDateEntry, _setStrictDateEntry, None, 
    531             _("""Specifies whether date values must be entered in strict ISO8601 format. Default=False. 
    532  
    533             If not strict, dates can be accepted in YYYYMMDD, YYMMDD, and MMDD format, 
    534             which will be coerced into sensible date values automatically.""")) 
    535      
    536     TextLength = property(_getTextLength, _setTextLength, None, 
    537             _("""The maximum length the entered text can be. (int)""")) 
    538  
    539     Value = property(_getValue, _setValue, None, 
    540             _("Specifies the current state of the control (the value of the field). (varies)")) 
    541  
    542  
    543     # Dynamic property declarations 
    544     DynamicAlignment = makeDynamicProperty(Alignment) 
    545     DynamicInsertionPosition = makeDynamicProperty(InsertionPosition) 
    546     DynamicPasswordEntry = makeDynamicProperty(PasswordEntry) 
    547     DynamicReadOnly = makeDynamicProperty(ReadOnly) 
    548     DynamicSelectionEnd = makeDynamicProperty(SelectionEnd) 
    549     DynamicSelectionLength = makeDynamicProperty(SelectionLength) 
    550     DynamicSelectionStart = makeDynamicProperty(SelectionStart) 
    551     DynamicSelectOnEntry = makeDynamicProperty(SelectOnEntry) 
    552     DynamicStrictDateEntry = makeDynamicProperty(StrictDateEntry) 
    553     DynamicValue = makeDynamicProperty(Value) 
    554  
    55532 
    55633 
  • branches/NateBranch/tests/unitTests/ui/UIwx/Test_dTextBox.py

    r2898 r3047  
    1 """Test Case for the dTextbox class. 
     1"""Test Case for the dTextbox class.  This file will test the code found in dTextBoxMixin. 
     2    We need to test the dTextBoxMixin through this because it is the only way to initialize 
     3    the object and test it in the proper GUI setting. 
    24 
    35To import this file into the test suite run: 
     
    1315from mock import Mock 
    1416dabo.ui.loadUI('wx') 
     17 
     18import datetime 
    1519 
    1620#We want the dApp and mainForm to persist through the settings for speed sake 
     
    2226testForm = dabo.ui.dForm() 
    2327 
    24 #Set up a movab
     28#Set up a base class that handle the set up and tear down of a semi-persistant test fixtur
    2529class BaseTestdTextBox(unittest.TestCase): 
    2630    """This class will set up a the fresh part of the picture which is the dTextBox 
     
    3539     
    3640    def setProperty(self, propertyInfo): 
    37         """setProperty(self, (object.property, val))""" 
     41        """setProperty(self, ('object.property', val)).""" 
    3842        exec("%s = %s" % propertyInfo) 
    3943 
    4044 
     45#Begin public Method Tests 
     46class TestConvertStringValueToDataType(BaseTestdTextBox): 
     47    """ 
     48    TestList: 
     49        For dataType == bool: 
     50            - When dataType == bool, Passing a value of 'True' for strVal causes convertSringValueToDataType to return a boolean True 
     51            - When dataType == bool, Passing any value other than 'True' for strVal causes convertStringValueToDataType to return a boolean False 
     52        For dataType == datetime.date: 
     53            - When dataType == datetime.date, Passing a string value in ISO 8601 format causes convertStringValueToDataType to return a datetime.date object with the correct date. 
     54            - When dataType == datetime.date, Passing a string value not in ISO 8601 format causes convertStringValueToDataType to raise a ValueError. 
     55            - NEED TO ADD DOMAIN CHECKING TESTS!!!! 
     56        For dataType == datetime.date: 
     57            - When dataType == datetime.datetime, Passing a string value in ISO 8601 format causes convertStringValueToDataType to return a datetime.datetime object with the correct date and time. 
     58            - When dataType == datetime.datetime, Passing a string value not in ISO 8601 format causes convertStringValueToDataType to raise a ValueError. 
     59            - NEED TO ADD DOMAIN CHECKING TESTS!!!! 
     60        For dataType == datetime.date: 
     61            - When dataType == datetime.time, Passing a string value in ISO 8601 format causes convertStringValueToDataType to return a datetime.date object with the correct time. 
     62            - When dataType == datetime.time, Passing a string value not in ISO 8601 format causes convertStringValueToDataType to raise a ValueError. 
     63            - NEED TO ADD DOMAIN CHECKING TESTS!!!! 
     64    """ 
     65     
     66    def callfunction(self, parameterTuple): 
     67        self.testTextBox.convertStringValueToDataType(parameterTuple[0], parameterTuple[1]) 
     68     
     69     
     70    #tests for bool dataType 
     71    def testGetTrueValue(self): 
     72        """When dataType == bool, Passing a value of 'True' for strVal causes convertSringValueToDataType to return a boolean True""" 
     73        value = self.testTextBox.convertStringValueToDataType("True", bool) 
     74        isBool = isinstance(value, bool) 
     75        self.assertEqual(isBool, True) 
     76        self.assertEqual(value, True) 
     77     
     78    def testGetFalseValue(self): 
     79        """When dataType == bool, Passing any value other than 'True' for strVal causes convertStringValueToDataType to return a boolean False""" 
     80        testValues = ["true", "False", "just a string", "", "June 14th, 2004"] 
     81        for parameter in testValues: 
     82            value = self.testTextBox.convertStringValueToDataType(parameter, bool) 
     83            isBool = isinstance(value, bool) 
     84            self.assertEqual(isBool, True) 
     85            self.assertEqual(value, False) 
     86     
     87     
     88    #tests for datetime.date dataType 
     89    def testGetValidDateValue(self): 
     90        """When dataType == datetime.date, Passing a string value in ISO 8601 format causes convertStringValueToDataType to return a datetime.date object with the correct date.""" 
     91        value = self.testTextBox.convertStringValueToDataType("2007-04-10", datetime.date) 
     92        isDate = isinstance(value, datetime.date) 
     93        self.assertEqual(isDate, True) 
     94        self.assertEqual(value, datetime.date(2007, 4, 10)) 
     95     
     96    def testBadDateInput(self): 
     97        """When dataType == datetime.date, Passing a string value not in ISO 8601 format causes convertStringValueToDataType to raise a ValueError.""" 
     98        testValues = ["4/10/07", "2007-4-10", "2007-04-1", "Not a date", "2007-04-10T12:12:12"] 
     99        for parameter in testValues: 
     100            self.assertRaises(ValueError, self.callfunction, (parameter, datetime.date)) 
     101     
     102     
     103    #tests for datetime.datetime dataType 
     104    def testGetValidDatetimeValue(self): 
     105        """When dataType == datetime.datetime, Passing a string value in ISO 8601 format causes convertStringValueToDataType to return a datetime.datetime object with the correct date and time.""" 
     106        value = self.testTextBox.convertStringValueToDataType("2007-04-10T12:12:12", datetime.datetime) 
     107        isDatetime = isinstance(value, datetime.datetime) 
     108        self.assertEqual(isDatetime, True) 
     109        self.assertEqual(value, datetime.datetime(2007, 4, 10, 12, 12, 12)) 
     110     
     111    def testBadDatetimeInput(self): 
     112        """When dataType == datetime.datetime, Passing a string value not in ISO 8601 format causes convertStringValueToDataType to raise a ValueError.""" 
     113        testValues = ["4/10/07", "2007-4-10", "2007-04-1", "Not a date", "2007-04-10T1:12:12.32", 
     114            "2007-04-10T1:12:12", "2007-04-10T1:12:12", "2007-04-10T12:1:12", "2007-04-10T12:12:1"] 
     115        for parameter in testValues: 
     116            self.assertRaises(ValueError, self.callfunction, (parameter, datetime.datetime)) 
     117     
     118     
     119    #tests for datetime.time dataType 
     120    def testGetValidTimeValue(self): 
     121        """When dataType == datetime.time, Passing a string value in ISO 8601 format causes convertStringValueToDataType to return a datetime.time object with the correct time.""" 
     122        value = self.testTextBox.convertStringValueToDataType("08:13:24", datetime.time) 
     123        isTime = isinstance(value, datetime.time) 
     124        self.assertEqual(isTime, True) 
     125        self.assertEqual(value, datetime.time(8, 13, 24)) 
     126     
     127    def testBadTimeInput(self): 
     128        """When dataType == datetime.time, Passing a string value not in ISO 8601 format causes convertStringValueToDataType to raise a ValueError.""" 
     129        testValues = ["Not a time", "2007-04-10T01:12:12", "1:12:12", "02", "02:04", 
     130            "T01:12:12", "12:1:12", "12:12:1"] 
     131        for parameter in testValues: 
     132            self.assertRaises(ValueError, self.callfunction, (parameter, datetime.time)) 
     133 
     134 
     135 
     136class TestGetBlankValue(BaseTestdTextBox): 
     137    """ 
     138    TestList: 
     139        - call getBlankValue and assert that the returned value is a string of length 0. 
     140    """ 
     141     
     142    def testGetBlankValue(self): 
     143        """call getBlankValue and assert that the returned value is a string of length 0.""" 
     144        value = self.testTextBox.getBlankValue() 
     145        self.assertEqual(value, "") 
     146 
     147 
     148#Begin property tests 
    41149class TestTextLengthProperty(BaseTestdTextBox): 
    42150    """ 
  • branches/NateBranch/tests/unitTests/ui/UIwx/__init__.py

    r2898 r3047  
    1414import Test_dTextBox 
    1515suiteList.append(unittest.TestLoader().loadTestsFromModule(Test_dTextBox)) 
     16#import Test_dSearchBox 
     17#suiteList.append(unittest.TestLoader().loadTestsFromModule(Test_dSearchBox)) 
    1618 
    1719#setup a suite and return it