Package Camelot :: Package camelot :: Package test :: Module test_field_attributes |
[frames] | no frames]

# Source Code for Module Camelot.camelot.test.test_field_attributes

      1  #  ============================================================================

      2  #

      3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.

      4  #  www.conceptive.be / project-camelot@conceptive.be

      5  #

      6  #  This file is part of the Camelot Library.

      7  #

      8  #  This file may be used under the terms of the GNU General Public

      9  #  License version 2.0 as published by the Free Software Foundation

     10  #  and appearing in the file LICENSE.GPL included in the packaging of

     11  #  this file.  Please review the following information to ensure GNU

     12  #  General Public Licensing requirements will be met:

     13  #  http://www.trolltech.com/products/qt/opensource.html

     14  #

     15  #  If you are unsure which license is appropriate for your use, please

     16  #  review the following information:

     17  #  http://www.trolltech.com/products/qt/licensing.html or contact

     18  #  project-camelot@conceptive.be.

     19  #

     20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE

     21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

     22  #

     23  #  For use of this library in commercial applications, please contact

     24  #  project-camelot@conceptive.be

     25  #

     26  #  ============================================================================

     27

     28  """test module for the 'camelot/view/field_attributes.py' module"""
     29

     30  import unittest
     31  from datetime import datetime
     32  from camelot.core import constants
     33  from camelot.view.utils import (ParsingError,

     34                                  bool_from_string,

     35                                  date_from_string,

     36                                  time_from_string,

     37                                  datetime_from_string,

     38                                  int_from_string,

     39                                  float_from_string)
     40

     41

     42 -class FromStringTestCase(unittest.TestCase):
     43

     44 -    def test_bool_from_string(self):
     45          self.assertRaises(ParsingError, bool_from_string, None)
     46          self.assertRaises(ParsingError, bool_from_string, 'soup')
     47          self.assertEqual(False, bool_from_string('false'))
     48          self.assertEqual(False, bool_from_string('False'))
     49          self.assertEqual(True, bool_from_string('true'))
     50          self.assertEqual(True, bool_from_string('True'))
     51

     52 -    def test_date_from_string(self):
     53          self.assertRaises(ParsingError, date_from_string, None)
     54

     55          fmt = constants.strftime_date_format
     56          d_1 = datetime.strptime('19-11-2009', fmt).date()
     57          d_2 = date_from_string('19-11-2009', fmt)
     58

     59          self.assertEqual(d_1, d_2)
     60          self.assertRaises(ParsingError, date_from_string, '2009', fmt)
     61          self.assertRaises(ParsingError, date_from_string, '11-19-2009', fmt)
     62          self.assertRaises(ParsingError, date_from_string, '11-19-09', fmt)
     63          self.assertRaises(ParsingError, date_from_string, '11/09/2009', fmt)
     64

     65 -    def test_time_from_string(self):
     66          self.assertRaises(ParsingError, time_from_string, None)
     67

     68          fmt = constants.strftime_time_format
     69          t_1 = datetime.strptime('11:48', fmt).time()
     70          t_2 = time_from_string('11:48', fmt)
     71

     72          self.assertEqual(t_1, t_2)
     73          self.assertRaises(ParsingError, time_from_string, 'am', fmt)
     74          self.assertRaises(ParsingError, time_from_string, '11:48 am', fmt)
     75          self.assertRaises(ParsingError, date_from_string, '11:48 AM', fmt)
     76

     77 -    def test_datetime_from_string(self):
     78          self.assertRaises(ParsingError, datetime_from_string, None)
     79

     80          fmt = constants.strftime_datetime_format
     81          dt_1 = datetime.strptime('19-11-2009 11:48', fmt)
     82          dt_2 = datetime_from_string('19-11-2009 11:48', fmt)
     83

     84          self.assertEqual(dt_1, dt_2)
     85          self.assertRaises(ParsingError,

     86                            datetime_from_string,

     87                            '19-11-2009, 11:48',

     88                            fmt)
     89          self.assertRaises(ParsingError,

     90                            datetime_from_string,

     91                            '11:48',

     92                            fmt)
     93          self.assertRaises(ParsingError,

     94                            datetime_from_string,

     95                            '19-11-2009',

     96                            fmt)
     97

     98 -    def test_int_from_string(self):
     99          self.assertRaises(ParsingError, int_from_string, None)
    100          self.assertRaises(ParsingError, int_from_string, 'sausage')
    101          self.assertEqual(102, int_from_string('102'))
    102          self.assertEqual(0, int_from_string(''))
    103          self.assertRaises(ParsingError, int_from_string, '105.4')
    104

    105 -    def test_float_from_string(self):
    106          self.assertRaises(ParsingError, float_from_string, None)
    107          self.assertRaises(ParsingError, float_from_string, 'casserole')
    108          self.assertEqual(0.0, float_from_string(''))
    109          self.assertEqual(0.1, float_from_string('0.1'))
    110          self.assertEqual(5105.5, float_from_string('5105.5'))
    111

    112

    113  if __name__ == '__main__':
    114      unittest.main()
    115