devtools/test/unittest_dbfill.py
changeset 0 b97547f5f1fa
child 1802 d628defebc17
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 # -*- coding: iso-8859-1 -*-
       
     2 """unit tests for database value generator"""
       
     3 
       
     4 import os.path as osp
       
     5 import re
       
     6 
       
     7 from logilab.common.testlib import TestCase, unittest_main
       
     8 
       
     9 from cubicweb.schema import Schema, EntitySchema
       
    10 from cubicweb.devtools.fill import ValueGenerator, make_tel
       
    11 from cubicweb.devtools import ApptestConfiguration
       
    12 
       
    13 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data')
       
    14 ISODATE_SRE = re.compile('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$')
       
    15 
       
    16 
       
    17 class MyValueGenerator(ValueGenerator):
       
    18 
       
    19     def generate_Bug_severity(self, index):
       
    20         return u'dangerous'
       
    21 
       
    22     def generate_Any_description(self, index, format=None):
       
    23         return u'yo'
       
    24 
       
    25 
       
    26 class ValueGeneratorTC(TestCase):
       
    27     """test case for ValueGenerator"""
       
    28 
       
    29     def _choice_func(self, etype, attrname):
       
    30         try:
       
    31             return getattr(self, '_available_%s_%s' % (etype, attrname))(etype, attrname)
       
    32         except AttributeError:
       
    33             return None
       
    34     
       
    35     def _available_Person_firstname(self, etype, attrname):
       
    36         return [f.strip() for f in file(osp.join(DATADIR, 'firstnames.txt'))]
       
    37 
       
    38 
       
    39     def setUp(self):
       
    40         config = ApptestConfiguration('data')
       
    41         config.bootstrap_cubes()
       
    42         schema = config.load_schema()
       
    43         e_schema = schema.entity_schema('Person')
       
    44         self.person_valgen = ValueGenerator(e_schema, self._choice_func)
       
    45         e_schema = schema.entity_schema('Bug')
       
    46         self.bug_valgen = MyValueGenerator(e_schema)
       
    47         self.config = config
       
    48 
       
    49     def _check_date(self, date):
       
    50         """checks that 'date' is well-formed"""
       
    51         year = date.year
       
    52         month = date.month
       
    53         day = date.day
       
    54         self.failUnless(day in range(1, 29), '%s not in [0;28]' % day) 
       
    55         self.failUnless(month in range(1, 13), '%s not in [1;12]' % month)
       
    56         self.failUnless(year in range(2000, 2005),
       
    57                         '%s not in [2000;2004]' % year)
       
    58         
       
    59 
       
    60     def test_string(self):
       
    61         """test string generation"""
       
    62         surname = self.person_valgen._generate_value('surname', 12)
       
    63         self.assertEquals(surname, u'é&surname12')
       
    64 
       
    65     def test_domain_value(self):
       
    66         """test value generation from a given domain value"""
       
    67         firstname = self.person_valgen._generate_value('firstname', 12)
       
    68         possible_choices = self._choice_func('Person', 'firstname')
       
    69         self.failUnless(firstname in possible_choices,
       
    70                         '%s not in %s' % (firstname, possible_choices))
       
    71 
       
    72     def test_choice(self):
       
    73         """test choice generation"""
       
    74         # Test for random index
       
    75         for index in range(5):
       
    76             sx_value = self.person_valgen._generate_value('civility', index)
       
    77             self.failUnless(sx_value in ('Mr', 'Mrs', 'Ms'))
       
    78 
       
    79     def test_integer(self):
       
    80         """test integer generation"""
       
    81         # Test for random index
       
    82         for index in range(5):
       
    83             cost_value = self.bug_valgen._generate_value('cost', index)
       
    84             self.failUnless(cost_value in range(index+1))
       
    85 
       
    86     def test_date(self):
       
    87         """test date generation"""
       
    88         # Test for random index
       
    89         for index in range(5):
       
    90             date_value = self.person_valgen._generate_value('birthday', index)
       
    91             self._check_date(date_value)
       
    92         
       
    93     def test_phone(self):
       
    94         """tests make_tel utility"""
       
    95         self.assertEquals(make_tel(22030405), '22 03 04 05')
       
    96 
       
    97 
       
    98     def test_customized_generation(self):
       
    99         self.assertEquals(self.bug_valgen._generate_value('severity', 12),
       
   100                           u'dangerous')
       
   101         self.assertEquals(self.bug_valgen._generate_value('description', 12),
       
   102                           u'yo')
       
   103         self.assertEquals(self.person_valgen._generate_value('description', 12),
       
   104                           u'yo')
       
   105                           
       
   106         
       
   107 
       
   108 class ConstraintInsertionTC(TestCase):
       
   109 
       
   110     def test_writeme(self):
       
   111         self.skip('Test automatic insertion / Schema Constraints')
       
   112     
       
   113 
       
   114 if __name__ == '__main__':
       
   115     unittest_main()