summaryrefslogtreecommitdiffstats
path: root/funcweb/funcweb/tests
diff options
context:
space:
mode:
Diffstat (limited to 'funcweb/funcweb/tests')
-rw-r--r--funcweb/funcweb/tests/test_widget_automation.py130
-rw-r--r--funcweb/funcweb/tests/test_widget_validation.py339
2 files changed, 469 insertions, 0 deletions
diff --git a/funcweb/funcweb/tests/test_widget_automation.py b/funcweb/funcweb/tests/test_widget_automation.py
new file mode 100644
index 0000000..d02de93
--- /dev/null
+++ b/funcweb/funcweb/tests/test_widget_automation.py
@@ -0,0 +1,130 @@
+import unittest
+import turbogears
+from turbogears import testutil
+
+from funcweb.widget_automation import WidgetListFactory,RemoteFormAutomation,RemoteFormFactory
+from funcweb.widget_validation import WidgetSchemaFactory
+
+class TestWidgetListFactory(unittest.TestCase):
+
+ def setUp(self):
+ self.widget_factory = WidgetListFactory(self.get_test_default_args(),minion="myminion",module="mymodule",method="my_method")
+
+
+ def tearDown(self):
+ pass
+
+ def test_default_args(self):
+ compare_with = self.get_test_default_args()
+ widget_list=self.widget_factory.get_widgetlist()
+
+ #print "The widget list is like :",widget_list
+
+ for argument_name,argument_options in compare_with.iteritems():
+ assert widget_list.has_key(argument_name) == True
+ #print "The argument name is :",argument_name
+ #because some of them dont have it like boolean
+ if argument_options.has_key('default'):
+ assert argument_options['default'] == getattr(widget_list[argument_name],'default')
+
+ if argument_options.has_key("description"):
+ assert argument_options['description']==getattr(widget_list[argument_name],'help_text')
+
+ if argument_options.has_key("options"):
+ assert argument_options['options'] == getattr(widget_list[argument_name],"options")
+
+ #that should be enough
+ def test_get_widgetlist_object(self):
+ compare_with = self.get_test_default_args()
+ widget_list_object = self.widget_factory.get_widgetlist_object()
+
+ #print widget_list_object
+
+ all_fields = [getattr(field,"name") for field in widget_list_object]
+ #print all_fields
+ for argument_name in compare_with.keys():
+ print argument_name
+ assert argument_name in all_fields
+ #print getattr(widget_list_object,argument_name)
+
+
+ def test_remote_form(self):
+ schema_factory = WidgetSchemaFactory(self.get_test_default_args())
+ schema_validator=schema_factory.get_ready_schema()
+ widget_list_object = self.widget_factory.get_widgetlist_object()
+ remote_form = RemoteFormAutomation(widget_list_object,schema_validator)
+ #print remote_form
+
+ def test_remote_form_factory(self):
+ from turbogears.view import load_engines
+ load_engines()
+
+ # WidgetsList object
+ widget_list_object = self.widget_factory.get_widgetlist_object()
+ #print widget_list_object
+ remote_form = RemoteFormFactory(widget_list_object).get_remote_form()
+
+ #it is a key,value dict
+ widget_list=self.widget_factory.get_widgetlist()
+ #print widget_list
+ all_fields = [getattr(field,"name") for field in remote_form.fields]
+ #print all_fields
+ #will check if the remote form object hass all the names in it
+ for argument_name in widget_list.items():
+ argument_name in all_fields
+
+
+ #print remote_form.render()
+
+ def get_test_default_args(self):
+ return {
+ 'string_default':{
+ 'type':'string',
+ 'default':'default string',
+ 'optional':False,
+ 'description':'default description'
+ },
+ 'int_default':{
+ 'type':'int',
+ 'default':'default int',
+ 'optional':False,
+ 'description':'default description'
+ },
+ #no sense to have default
+ 'boolean_default':{
+ 'type':'boolean',
+ 'optional':False,
+ 'description':'default description'
+ },
+ 'float_default':{
+ 'type':'float',
+ 'default':'default float',
+ 'optional':False,
+ 'description':'default description'
+
+ },
+ 'hash_default':{
+ 'type':'hash',
+ 'default':'default hash',
+ 'optional':False,
+ 'description':'default description'
+
+ },
+ 'list_default':{
+ 'type':'list',
+ 'default':'default list',
+ 'optional':False,
+ 'description':'default description'
+
+ },
+ #will be converted to dropdown
+ 'special_string':{
+ 'type':'string',
+ 'default':'myfirst',
+ 'options':['myfirst','mysecond','mythird'],
+ 'optional':False,
+ 'description':'default dropdown list'
+ }
+
+ }
+
diff --git a/funcweb/funcweb/tests/test_widget_validation.py b/funcweb/funcweb/tests/test_widget_validation.py
new file mode 100644
index 0000000..c800d41
--- /dev/null
+++ b/funcweb/funcweb/tests/test_widget_validation.py
@@ -0,0 +1,339 @@
+import unittest
+import turbogears
+from turbogears import testutil
+from funcweb.widget_validation import WidgetSchemaFactory,MinionIntValidator,MinionFloatValidator,MinionListValidator,MinionHashValidator
+from turbogears import validators
+
+class TestWidgetValidator(unittest.TestCase):
+
+ def test_string_validator(self):
+ wf = WidgetSchemaFactory(self.get_string_params())
+ schema_man=wf.get_ready_schema()
+
+ conversion_schema = {
+ 'max_length':'max',
+ 'min_length':'min',
+ 'validator':'regex'
+ }
+
+ #do better test here
+ for argument_name,arg_options in self.get_string_params().iteritems():
+ #print argument_name
+ assert hasattr(schema_man,argument_name)==True
+ #not very efficient but it si just a test :)
+ if argument_name != 'string_mix':
+ for arg,value in arg_options.iteritems():
+ #print getattr(schema_man,argument_name)
+ if conversion_schema.has_key(arg):
+ if hasattr(getattr(schema_man,argument_name),conversion_schema[arg]):
+ #print arg,value
+ #couldnt find a way to test it !??
+ if arg != 'validator':
+ assert getattr(getattr(schema_man,argument_name),conversion_schema[arg])==value
+ #print getattr(getattr(schema_man,argument_name),conversion_schema[arg])
+ else:
+ #just print it to see what is inside because the test will be very hardcoded otherwise
+ #print getattr(schema_man,argument_name)
+ continue
+ print "Happy tests !"
+
+ def test_int_validator(self):
+ wf = WidgetSchemaFactory(self.get_int_params())
+ schema_man=wf.get_ready_schema()
+
+ for argument_name,arg_options in self.get_int_params().iteritems():
+ #print argument_name
+ assert hasattr(schema_man,argument_name)==True
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+
+ #if the argument includes some range
+ if arg_options.has_key('range'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert getattr(getattr(schema_man,argument_name),'max') == arg_options['range'][1]
+ assert getattr(getattr(schema_man,argument_name),'min') == arg_options['range'][0]
+ if arg_options.has_key('min'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert getattr(getattr(schema_man,argument_name),'min') == arg_options['min']
+
+ if arg_options.has_key('max'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert getattr(getattr(schema_man,argument_name),'max') == arg_options['max']
+
+ if arg_options.has_key('optional'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert not getattr(getattr(schema_man,argument_name),'not_empty') == arg_options['optional']
+
+
+ print "Happy test!"
+
+ def test_float_validator(self):
+ wf = WidgetSchemaFactory(self.get_float_params())
+ schema_man=wf.get_ready_schema()
+
+ for argument_name,arg_options in self.get_float_params().iteritems():
+ #print argument_name
+ assert hasattr(schema_man,argument_name)==True
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+
+ if arg_options.has_key('min'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert getattr(getattr(schema_man,argument_name),'min') == arg_options['min']
+
+ if arg_options.has_key('max'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert getattr(getattr(schema_man,argument_name),'max') == arg_options['max']
+
+ if arg_options.has_key('optional'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert not getattr(getattr(schema_man,argument_name),'not_empty') == arg_options['optional']
+
+
+ print "Happy test!"
+
+ def test_bool_validator(self):
+ testing_data = self.get_bool_params()
+ wf = WidgetSchemaFactory(testing_data)
+ schema_man=wf.get_ready_schema()
+
+ for argument_name,arg_options in testing_data.iteritems():
+ #print argument_name
+ #should all the argument names really
+ assert hasattr(schema_man,argument_name)==True
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+
+ if arg_options.has_key('optional'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert not getattr(getattr(schema_man,argument_name),'not_empty') == arg_options['optional']
+
+ print "Happy test!"
+
+
+
+ def test_list_validator(self,the_type='list'):
+ if the_type == 'list':
+ testing_data = self.get_list_params()
+ else:
+ testing_data = self.get_hash_params()
+
+ wf = WidgetSchemaFactory(testing_data)
+ schema_man=wf.get_ready_schema()
+
+ for argument_name,arg_options in testing_data.iteritems():
+ #print argument_name
+ #should all the argument names really
+ assert hasattr(schema_man,argument_name)==True
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+
+ if arg_options.has_key('validator'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert getattr(getattr(schema_man,argument_name),'regex_string') == arg_options['validator']
+
+ if arg_options.has_key('optional'):
+ #print " ",argument_name," : ",getattr(schema_man,argument_name)
+ assert not getattr(getattr(schema_man,argument_name),'not_empty') == arg_options['optional']
+
+
+ print "Happy test!"
+
+
+ def test_hash_validator(self):
+ self.test_list_validator(the_type = 'hash')
+
+ def test_minion_int_validator(self):
+ mv=MinionIntValidator(max = 44,min=2)
+ self.assertRaises(validators.Invalid,mv.to_python,100)
+ self.assertRaises(validators.Invalid,mv.to_python,1)
+ self.assertRaises(validators.Invalid,mv.to_python,'some_string')
+ assert mv.to_python(21) == 21
+
+ #dont use the min
+ mv=MinionIntValidator(max = 44)
+ self.assertRaises(validators.Invalid,mv.to_python,100)
+ assert mv.to_python(1)==1
+ self.assertRaises(validators.Invalid,mv.to_python,'some_string')
+ assert mv.to_python(21) == 21
+
+ mv=MinionIntValidator(min=12)
+ self.assertRaises(validators.Invalid,mv.to_python,10)
+ assert mv.to_python(14)==14
+ self.assertRaises(validators.Invalid,mv.to_python,'some_string')
+ assert mv.to_python(21) == 21
+
+ mv=MinionIntValidator()
+ assert mv.to_python(14)==14
+ self.assertRaises(validators.Invalid,mv.to_python,'some_string')
+
+ def test_minion_float_validator(self):
+ mv=MinionFloatValidator(max = 44.0,min=2.0)
+ self.assertRaises(validators.Invalid,mv.to_python,100.0)
+ self.assertRaises(validators.Invalid,mv.to_python,1.0)
+ self.assertRaises(validators.Invalid,mv.to_python,'some_string')
+ assert mv.to_python(21.0) == 21.0
+
+ #dont use the min
+ mv=MinionFloatValidator(max = 44.0)
+ self.assertRaises(validators.Invalid,mv.to_python,100.0)
+ assert mv.to_python(1.0)==1.0
+ self.assertRaises(validators.Invalid,mv.to_python,'some_string')
+ assert mv.to_python(21.0) == 21.0
+
+ mv=MinionFloatValidator(min=12.0)
+ self.assertRaises(validators.Invalid,mv.to_python,10.0)
+ assert mv.to_python(14.0)==14.0
+ self.assertRaises(validators.Invalid,mv.to_python,'some_string')
+ assert mv.to_python(21.0) == 21.0
+
+ mv=MinionFloatValidator()
+ assert mv.to_python(14.0)==14.0
+ self.assertRaises(validators.Invalid,mv.to_python,'some_string')
+
+ def test_minion_list_validator(self):
+
+ #test default
+ mv = MinionListValidator()
+ assert mv.to_python(['fedora','debian','others']) == ['fedora','debian','others']
+ del mv
+
+ #test with regex
+ mv = MinionListValidator(regex_string='^[A-Z]+$',not_empty=True)
+ assert mv.to_python(['FEDORA','MACOSX']) == ['FEDORA','MACOSX']
+ self.assertRaises(validators.Invalid,mv.to_python,[])
+ self.assertRaises(validators.Invalid,mv.to_python,['hey_error'])
+ self.assertRaises(validators.Invalid,mv.to_python,{})
+ del mv
+
+
+ print "Happy testing !"
+
+
+ def test_minion_hash_validator(self):
+
+ #test default
+ mv = MinionHashValidator()
+ assert mv.to_python({'fedora':1,'debian':2,'others':3}) == {'fedora':1,'debian':2,'others':3}
+ del mv
+
+ #test with regex
+ mv = MinionHashValidator(regex_string='^[A-Z]+$',not_empty=True)
+ assert mv.to_python({'FEDORA':'FEDORA','MACOSX':'MACOSX'}) == {'FEDORA':'FEDORA','MACOSX':'MACOSX'}
+ self.assertRaises(validators.Invalid,mv.to_python,{})
+ self.assertRaises(validators.Invalid,mv.to_python,{'hey_error':12})
+ self.assertRaises(validators.Invalid,mv.to_python,"hwy")
+ del mv
+
+ print "Happy testing !"
+
+
+ def get_string_params(self):
+ return {
+ 'string_default':{
+ 'type':'string',
+ 'default':'default string',
+ 'description':'default description'
+ },
+ 'string_regex':{
+ 'type':'string',
+ 'default':'some',
+ 'validator':'^[a-z]*$'
+ }
+ ,
+ 'min_max_string':{
+ 'type':'string',
+ 'default':'myfirst',
+ 'optional':False,
+ 'description':'default dropdown list',
+ 'max_length':12,
+ 'min_length':5
+ },
+ 'string_mix':{
+ 'type':'string',
+ 'optional':False,
+ 'max_length':123,
+ 'min_length':12,
+ 'validator':'^[A-Z]+$'
+ }
+ }
+
+ def get_int_params(self):
+ return {
+ 'int_default':{
+ 'type':'int',
+ 'default':2,
+ 'description':'default integer'
+ },
+ 'min_max':{
+ 'type':'int',
+ 'default':12,
+ 'optional':False,
+ 'description':'default dropdown list',
+ 'max':12,
+ 'min':5
+ },
+ 'range_int':{
+ 'type':'int',
+ 'optional':False,
+ 'range':[1,55]
+ }
+ }
+
+ def get_float_params(self):
+ return {
+ 'float_default':{
+ 'type':'float',
+ 'default':2.0,
+ 'description':'default float'
+ },
+ 'min_max':{
+ 'type':'float',
+ 'default':11.0,
+ 'optional':False,
+ 'description':'default dropdown list',
+ 'max':12.0,
+ 'min':5.0
+ },
+ }
+
+ def get_list_params(self):
+ return {
+ 'list_default':{
+ 'type':'list',
+ 'default':'cooler',
+ 'description':'default list'
+ },
+ 'list_regex':{
+ 'type':'list',
+ 'default':'hey',
+ 'optional':False,
+ 'description':'default regex list',
+ 'validator':'^[A-Z]$'
+ },
+ }
+
+ def get_hash_params(self):
+ return {
+ 'hash_default':{
+ 'type':'hash',
+ 'default':{'hey':12},
+ 'description':'default hash'
+ },
+ 'hash_regex':{
+ 'type':'hash',
+ 'default':{'hey':12},
+ 'optional':False,
+ 'description':'default regex hash',
+ 'validator':'^[A-Z]$'
+ },
+ }
+
+ def get_bool_params(self):
+ return {
+ 'bool_default':{
+ 'type':'boolean',
+ 'default':{'hey':12},
+ 'description':'default hash',
+ 'optional':False,
+ },
+ }
+
+
+