Package ajango :: Package tests :: Module runtests
[hide private]
[frames] | no frames]

Source Code for Module ajango.tests.runtests

  1  ########################################################################### 
  2  #                                                                         # 
  3  #  Copyright (C) 2016  Rafal Kobel <rafyco1@gmail.com>                    # 
  4  #                                                                         # 
  5  #  This program is free software: you can redistribute it and/or modify   # 
  6  #  it under the terms of the GNU General Public License as published by   # 
  7  #  the Free Software Foundation, either version 3 of the License, or      # 
  8  #  (at your option) any later version.                                    # 
  9  #                                                                         # 
 10  #  This program is distributed in the hope that it will be useful,        # 
 11  #  but WITHOUT ANY WARRANTY; without even the implied warranty of         # 
 12  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           # 
 13  #  GNU General Public License for more details.                           # 
 14  #                                                                         # 
 15  #  You should have received a copy of the GNU General Public License      # 
 16  #  along with this program.  If not, see <http://www.gnu.org/licenses/>.  # 
 17  #                                                                         # 
 18  ########################################################################### 
 19  """ Implementacja testow jednostowych. """ 
 20   
 21  import unittest 
 22  import importlib 
 23   
 24  from xml.dom.minidom              import parseString 
 25  from django.core.management.base  import CommandError 
 26  from ajango.database.validate     import validate_factory 
 27  from ajango.generator.application import Application 
 28  from ajango.core.factory          import FactoryBase 
 29   
 30   
 31  TEST_MODULES = [ 
 32      'ajango', 
 33      'ajango.conf', 
 34      'ajango.contrib', 
 35      'ajango.contrib.automatic', 
 36      'ajango.contrib.automatic.management', 
 37      'ajango.contrib.automatic.management.commands', 
 38      'ajango.contrib.automatic.management.commands.generateapps', 
 39      'ajango.contrib.automatic.management.commands.makeskeleton', 
 40      'ajango.contrib.automatic.apps', 
 41      'ajango.core', 
 42      'ajango.core.factory', 
 43      'ajango.core.XMLReader', 
 44      'ajango.core.generable', 
 45      'ajango.database', 
 46      'ajango.database.query', 
 47      'ajango.database.columns', 
 48      'ajango.database.columns.default', 
 49      'ajango.database.columns.button', 
 50      'ajango.database.data_base_manager', 
 51      'ajango.database.inputs', 
 52      'ajango.database.inputs.default', 
 53      'ajango.database.inputs.number', 
 54      'ajango.database.validate', 
 55      'ajango.database.validate.isempty', 
 56      'ajango.database.validate.regex', 
 57      'ajango.database.validate.gt', 
 58      'ajango.database.validate.lt', 
 59      'ajango.generator', 
 60      'ajango.generator.views', 
 61      'ajango.generator.views.editable', 
 62      'ajango.generator.views.empty', 
 63      'ajango.generator.views.input', 
 64      'ajango.generator.views.list', 
 65      'ajango.generator.views.raport', 
 66      'ajango.generator.views.container', 
 67      'ajango.generator.views.display', 
 68      'ajango.generator.application', 
 69      'ajango.generator.config_global', 
 70      'ajango.generator.project_manager', 
 71      'ajango.generator.renderer', 
 72      'ajango.generator.view_manager', 
 73      'ajango.generator.menu_manager', 
 74      'ajango.gui', 
 75      'ajango.gui.window', 
 76      'ajango.gui.controller', 
 77      'ajango.gui.model', 
 78      'ajango.site', 
 79      'ajango.site.site_menu', 
 80      'ajango.site.presentations', 
 81      'ajango.site.presentations.upper', 
 82      'ajango.site.sites', 
 83      'ajango.site.sites.display', 
 84      'ajango.site.sites.editable', 
 85      'ajango.site.sites.empty', 
 86      'ajango.site.sites.input', 
 87      'ajango.site.sites.list', 
 88      'ajango.site.sites.raport', 
 89      'ajango.site.sites.container', 
 90  ] 
 91   
 92  TEST_FACTORY = [ 
 93      ('ajango.database.columns', 'Column', 
 94          {'type' : 'default', 'label' : 'Wiek', 'tag' : 'age' }), 
 95      ('ajango.database.inputs', 'Input', \ 
 96          {'type': 'number', 'label': 'Wiek', 'tag': 'age', 'default': '18'}), 
 97      ('ajango.database.validate', 'Validate', {'type': 'isempty', 'param': '' }), 
 98      ('ajango.generator.views', 'View', \ 
 99          { 'xmldoc' : parseString("<view type=\"empty\" id=\"one\">" 
100             "</view>").documentElement}), 
101      ('ajango.site.presentations', 'Presentation', 'upper'), 
102      ('ajango.site.sites', 'Site', 'empty'), 
103  ] 
104   
105 -class TestAjango(unittest.TestCase):
106 """ Klasa testowa dla pakietu Ajango. """
107 - def __str__(self):
108 return "Testy jednostkowe pakietu Ajango."
109 - def testVerifyAll(self):
110 """ 111 Sprawdzenie czy wszystkie dostepne modele moga byc poprawnie wczytane. 112 """ 113 module_tested = "" 114 for mod in TEST_MODULES: 115 try: 116 module_tested = mod 117 importlib.import_module(mod) 118 except SyntaxError: 119 assert False, "Modules %r not validate" % module_tested 120 except ImportError: 121 assert False, "No module named %r" % module_tested 122 else: 123 assert True, "Module is read %s" % module_tested
124 - def testValidators(self):
125 """ 126 Sprawdzenie poprawnosci walidacji. 127 """ 128 data = [{ 'type' : "isempty", 129 'test' : "" , 130 'param' : None, 131 'is_valid' : False }, 132 { 'type' : "isempty", 133 'test' : " ", 134 'param' : None, 135 'is_valid' : True }, 136 { 'type' : "isempty", 137 'test' : "not empty string", 138 'param' : None, 139 'is_valid' : True }, 140 { 'type' : "regex", 141 'test' : "AAjfds", 142 'param' : "^[A]{2}", 143 'is_valid' : True }, 144 { 'type' : "regex", 145 'test' : "AAjfds", 146 'param' : "^[A-Z]{1}[a-z]+", 147 'is_valid' : False }, 148 { 'type' : "gt", 149 'test' : "5", 150 'param' : "1", 151 'is_valid' : True }, 152 { 'type' : "gt", 153 'test' : "5", 154 'param' : "5", 155 'is_valid' : False }, 156 { 'type' : "gt", 157 'test' : "5", 158 'param' : "10", 159 'is_valid' : False }, 160 { 'type' : "gt", 161 'test' : "one", 162 'param' : "5", 163 'is_valid' : False }, 164 { 'type' : "lt", 165 'test' : "5", 166 'param' : "10", 167 'is_valid' : True }, 168 { 'type' : "lt", 169 'test' : "5", 170 'param' : "5", 171 'is_valid' : False }, 172 { 'type' : "lt", 173 'test' : "5", 174 'param' : "1", 175 'is_valid' : False }, 176 { 'type' : "lt", 177 'test' : "ten", 178 'param' : "5", 179 'is_valid' : False } 180 ] 181 for test_case in data: 182 validator = validate_factory(test_case) 183 is_valid = test_case['is_valid'] 184 test_result = is_valid == validator.is_valid(test_case['test']) 185 assert test_result, "Validator %r not work" % test_case['type']
186 - def testApplicationMainView(self):
187 """ Sprawdzenie poprawnosci generowania aplikacji. """ 188 ob = Application(parseString("<application name=\"test\">" 189 "</application>").documentElement) 190 assert ob.get_main_view() == None, "Empty Application has view" 191 ob = Application(parseString("<application name=\"test\">" 192 "<view type=\"empty\" id=\"one\"></view>" 193 "</application>").documentElement) 194 assert ob.get_main_view().get_name() == "one", "Invalid Application id" 195 ob = Application(parseString("<application name=\"test\">" 196 "<view type=\"empty\" id=\"one\"></view>" 197 "<view type=\"empty\" id=\"two\"></view>" 198 "</application>").documentElement) 199 assert ob.get_main_view().get_name() == "one", "Invalid Application id" 200 ob = Application(parseString("<application name=\"test\">" 201 "<view type=\"empty\" id=\"one\"></view>" 202 "<view type=\"empty\" id=\"two\"" 203 " main=\"main\"></view>" 204 "</application>").documentElement) 205 assert ob.get_main_view().get_name() == "two", "Invalid Application id"
207 """ Testowanie wystepowania wyjatku dla generatora aplikacji. """ 208 def test_case(): 209 Application(parseString("<application name=\"test\">" 210 "<view type=\"empty\" id=\"one\"" 211 " main=\"main\"></view>" 212 "<view type=\"empty\" id=\"two\"" 213 " main=\"main\"></view>" 214 "</application>").documentElement)
215 self.assertRaises(CommandError, test_case)
216 - def testFactory(self):
217 """ Sprawdzenie poprawnosci implementacji fabryk. """ 218 def get_dictionary_from_param(param): 219 return {'lib' : param[0], 220 'factory' : '%sFactoryObject' % param[1], 221 'base' : '%sBase' % param[1], 222 'arg' : param[2]}
223 for elem in TEST_FACTORY: 224 # Testowanie fabryk. 225 test_data = get_dictionary_from_param(elem) 226 # Pobranie modulu dla konkretnej fabryki wraz z jej obiektem 227 try: 228 module = importlib.import_module(test_data['lib']) 229 factory = getattr(module, test_data['factory']) 230 base = getattr(module, test_data['base']) 231 except SyntaxError: 232 assert False, "No module named %r" % test_data['lib'] 233 except ImportError: 234 assert False, "No module named %r" % test_data['lib'] 235 assert issubclass(factory, FactoryBase), \ 236 "Factory %s must be child of FactoryBase" % \ 237 test_data['factory'] 238 factory_ob = factory(test_data['arg']) 239 objects = factory_ob._get_base_address() 240 for ob in objects.values(): 241 # Iteracja po klasach ktore powstaja z fabryki 242 assert ob in TEST_MODULES, \ 243 "Factory element doesn't exist: %r" % ob 244 try: 245 ob_module = importlib.import_module(ob) 246 ob_object = getattr(ob_module, factory_ob.class_name) 247 except SyntaxError: 248 assert False, "No module named %r" % ob 249 except ImportError: 250 assert False, "No module named %r" % ob 251 assert issubclass(ob_object, base), \ 252 "Object %s must be child of %s" % (ob, test_data['base']) 253 254 if __name__ == "__main__": 255 unittest.main() 256